You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte-kit-vice/web/routes/uix/components/avatar/+page.svelte

538 lines
18 KiB

<script lang="ts">
import {
Avatar,
type AvatarSize,
type AvatarVariant,
type AvatarColor,
type AvatarRadius
} from '$uix/eidos/components/avatar';
import { compileMorfo } from '$uix/morfo';
import { avatarMorfo } from '@/uix/morfo/components/avatar';
type Tab = 'live' | 'api' | 'morfo' | 'sema' | 'recipe' | 'a11y';
type TraceEntry = { event: string; family: string; intent?: string; at: number };
let tab = $state<Tab>('live');
let trace = $state<TraceEntry[]>([]);
let stageRef = $state<HTMLElement | null>(null);
// ── Live state ────────────────────────────────────────────────────────
let size = $state<AvatarSize>('md');
let variant = $state<AvatarVariant>('soft');
let color = $state<AvatarColor>('neutral');
let radius = $state<AvatarRadius>('full');
let src = $state('https://i.pravatar.cc/200?u=vicen-avatar');
let alt = $state('User portrait');
let fallback = $state('JD');
let breakImage = $state(false);
const effectiveSrc = $derived(breakImage ? 'https://broken.invalid/x.png' : src);
// ── Compiled morfo ────────────────────────────────────────────────────
const compiled = compileMorfo(avatarMorfo);
const partsList = $derived([...compiled.parts.byKebab.values()]);
const events = $derived([...compiled.actions.byName.values()]);
$effect(() => {
const el = stageRef;
if (!el) return;
const obs = new MutationObserver((mutations) => {
for (const m of mutations) {
if (m.attributeName !== 'data-event') continue;
const target = m.target as Element;
const ev = target.getAttribute('data-event');
if (!ev) continue;
trace = [
{
event: ev,
family: target.getAttribute('data-event-family') ?? '—',
intent: target.getAttribute('data-event-intent') ?? undefined,
at: Date.now()
},
...trace
].slice(0, 6);
}
});
obs.observe(el, { attributes: true, subtree: true, attributeFilter: ['data-event'] });
return () => obs.disconnect();
});
// ── Code snippets ─────────────────────────────────────────────────────
const somaSnippet = $derived(
[
'<!-- Avatar is eidos-native — no soma layer. -->',
'<!-- Equivalent semantic markup (not real soma): -->',
'',
'<span',
' data-avatar',
' data-image-loaded={loaded}',
' data-image-error={error}',
'>',
' <img src="…" alt="…" />',
' <span>JD</span>',
'</span>'
].join('\n')
);
const eidosSnippet = $derived(
[
"<script lang='ts'>",
" import { Avatar } from '$uix/eidos/components/avatar';",
'</' + 'script>',
'',
'<Avatar',
size !== 'md' && ` size="${size}"`,
variant !== 'soft' && ` variant="${variant}"`,
color !== 'neutral' && ` color="${color}"`,
radius !== 'full' && ` radius="${radius}"`,
'>',
` <Avatar.Image src="${src}" alt="${alt}" />`,
` <Avatar.Fallback>${fallback}</Avatar.Fallback>`,
'</Avatar>'
]
.filter(Boolean)
.join('\n')
);
</script>
<div data-uix-canvas-inner>
<header>
<div data-uix-eyebrow>Display · Avatar</div>
<h1 data-uix-page-title>Avatar</h1>
<p data-uix-page-lede>
User portrait shell with image-load fallback. Three parts —
<code>Avatar</code> (provider), <code>Image</code>,
<code>Fallback</code>. Eidos-native: no soma backing, the load / error state lives in
<code>Avatar.Image</code>
and toggles
<code>data-image-loaded</code> / <code>data-image-error</code> on the provider so the recipe can
swap layers without consumer plumbing.
</p>
<div data-uix-page-meta>
<span data-uix-meta-pill>
<span data-uix-meta-key>parts</span>{compiled.parts.order.length}
</span>
<span data-uix-meta-pill>
<span data-uix-meta-key>events</span>0
</span>
<span data-uix-meta-pill>
<span data-uix-meta-key>variants</span>3
</span>
<span data-uix-meta-pill>
<span data-uix-meta-key>sizes</span>6
</span>
<span data-uix-meta-pill>
<span data-uix-meta-key>scope</span>eidos
</span>
</div>
</header>
<!-- Live preview always rendered -->
<div data-uix-stage>
<div data-uix-stage-area bind:this={stageRef}>
<Avatar {size} {variant} {color} {radius}>
<Avatar.Image src={effectiveSrc} {alt} />
<Avatar.Fallback>{fallback}</Avatar.Fallback>
</Avatar>
</div>
<div data-uix-stage-trace>
<span data-uix-stage-trace-key>trace</span>
<span>{trace.length === 0 ? 'no semantic events' : `${trace.length} event(s)`}</span>
<span style="color: var(--uix-text-faint)">·</span>
<span data-uix-stage-trace-key>state</span>
<span>{breakImage ? 'fallback (image broken)' : 'image'}</span>
<span style="margin-inline-start: auto;">
<span data-uix-stage-trace-key>variant</span>
{variant} · {color}
</span>
</div>
</div>
<div data-uix-tabs role="tablist">
<button data-uix-tab data-active={tab === 'live'} onclick={() => (tab = 'live')}>Live</button>
<button data-uix-tab data-active={tab === 'api'} onclick={() => (tab = 'api')}>
API <span data-uix-tab-count>7</span>
</button>
<button data-uix-tab data-active={tab === 'morfo'} onclick={() => (tab = 'morfo')}>
<span data-uix-layer-badge="morfo">morfo</span>
<span data-uix-tab-count>{partsList.length}p · 0e</span>
</button>
<button data-uix-tab data-active={tab === 'sema'} onclick={() => (tab = 'sema')}>
<span data-uix-layer-badge="sema">sema</span>
<span data-uix-tab-count>{events.length}</span>
</button>
<button data-uix-tab data-active={tab === 'recipe'} onclick={() => (tab = 'recipe')}
>Recipe</button
>
<button data-uix-tab data-active={tab === 'a11y'} onclick={() => (tab = 'a11y')}>A11y</button>
</div>
{#if tab === 'live'}
<section data-uix-section>
<h2 data-uix-section-title>Controls</h2>
<p data-uix-section-desc>
Avatar is eidos-native — no <span data-uix-layer-badge="soma">soma</span> split. Provider props
drive the recipe; demo content controls the image / fallback strings.
</p>
<!-- ── Eidos props ─────────────────────────────────────────────── -->
<div data-uix-subsection-head>
<span data-uix-layer-badge="eidos">eidos</span> props · visual treatment
</div>
<div data-uix-controls>
<label data-uix-control>
<span data-uix-control-label>size</span>
<span data-uix-chips role="radiogroup">
{#each ['xs', 'sm', 'md', 'lg', 'xl', 'xxl'] as s}
<button
data-uix-chip
data-active={size === s}
onclick={() => (size = s as AvatarSize)}>{s}</button
>
{/each}
</span>
</label>
<label data-uix-control>
<span data-uix-control-label>variant</span>
<span data-uix-chips role="radiogroup">
{#each ['solid', 'soft', 'outline'] as v}
<button
data-uix-chip
data-active={variant === v}
onclick={() => (variant = v as AvatarVariant)}>{v}</button
>
{/each}
</span>
</label>
<label data-uix-control>
<span data-uix-control-label>color</span>
<span data-uix-chips role="radiogroup">
{#each ['primary', 'secondary', 'neutral', 'affirm', 'fulfill', 'risk', 'threat', 'loss'] as c}
<button
data-uix-chip
data-active={color === c}
onclick={() => (color = c as AvatarColor)}>{c}</button
>
{/each}
</span>
</label>
<label data-uix-control>
<span data-uix-control-label>radius</span>
<span data-uix-chips role="radiogroup">
{#each ['full', 'md', 'sm', 'none'] as r}
<button
data-uix-chip
data-active={radius === r}
onclick={() => (radius = r as AvatarRadius)}>{r}</button
>
{/each}
</span>
</label>
</div>
<!-- ── Demo content ────────────────────────────────────────────── -->
<div data-uix-subsection-head>
Demo content <span style="font-weight: normal; color: var(--uix-text-muted);"
>· image / fallback / break-image switch</span
>
</div>
<div data-uix-controls>
<label data-uix-control>
<span data-uix-control-label>src</span>
<input type="text" bind:value={src} disabled={breakImage} />
</label>
<label data-uix-control>
<span data-uix-control-label>alt</span>
<input type="text" bind:value={alt} />
</label>
<label data-uix-control>
<span data-uix-control-label>fallback (initials)</span>
<input type="text" bind:value={fallback} maxlength="3" />
</label>
<label data-uix-control>
<span data-uix-control-label
>break image <span data-uix-control-hint>force fallback</span></span
>
<span data-uix-switch>
<input type="checkbox" bind:checked={breakImage} />
<span data-uix-switch-label>{breakImage ? 'on' : 'off'}</span>
</span>
</label>
</div>
<!-- ── Code snippets per layer ─────────────────────────────────── -->
<div data-uix-code>
<div data-uix-code-head>
<span data-uix-layer-badge="soma">soma</span>
<span>n/a · avatar is eidos-native — equivalent markup shown</span>
<span data-uix-code-lang>svelte</span>
</div>
<pre><code>{somaSnippet}</code></pre>
</div>
<div data-uix-code style="margin-top: var(--uix-space-3);">
<div data-uix-code-head>
<span data-uix-layer-badge="eidos">eidos</span>
<span>visual · size, variant, color, radius</span>
<span data-uix-code-lang>svelte</span>
</div>
<pre><code>{eidosSnippet}</code></pre>
</div>
</section>
{/if}
{#if tab === 'api'}
<section data-uix-section>
<h2 data-uix-section-title>API reference</h2>
<p data-uix-section-desc>All public props on every part.</p>
<div data-uix-subsection-head>Avatar (provider)</div>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Prop</th><th>Type</th><th>Default</th><th>Description</th></tr></thead>
<tbody>
<tr
><td class="name">size</td><td class="type"
>'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl'</td
><td class="default">'md'</td><td
>Visual size token. Scales the box, font, and border.</td
></tr
>
<tr
><td class="name">variant</td><td class="type">'solid' | 'soft' | 'outline'</td><td
class="default">'soft'</td
><td>Surface treatment.</td></tr
>
<tr
><td class="name">color</td><td class="type">AvatarColor</td><td class="default"
>'neutral'</td
><td
>Doctrinal palette key. Affirm/fulfill/risk/threat/loss are admissible — avatar is a
non-evaluative shell, but color carries semantic when used for status badges.</td
></tr
>
<tr
><td class="name">radius</td><td class="type">'full' | 'md' | 'sm' | 'none'</td><td
class="default">'full'</td
><td>Border-radius token.</td></tr
>
</tbody>
</table>
</div>
<div data-uix-subsection-head>Avatar.Image</div>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Prop</th><th>Type</th><th>Default</th><th>Description</th></tr></thead>
<tbody>
<tr
><td class="name">src</td><td class="type">string | null</td><td class="default empty"
>—</td
><td>Image URL. Null hides the image and shows the fallback.</td></tr
>
<tr
><td class="name">alt</td><td class="type">string</td><td class="default">''</td><td
>Accessible name. Required when the avatar is meaningful (not purely decorative).</td
></tr
>
</tbody>
</table>
</div>
<div data-uix-subsection-head>Avatar.Fallback</div>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Prop</th><th>Type</th><th>Default</th><th>Description</th></tr></thead>
<tbody>
<tr
><td class="name">children</td><td class="type">Snippet</td><td class="default empty"
>—</td
><td
>Initials, glyph, or any inline content rendered while the image is loading or has
errored.</td
></tr
>
</tbody>
</table>
</div>
</section>
{/if}
{#if tab === 'morfo'}
<section data-uix-section>
<h2 data-uix-section-title>
<span data-uix-layer-badge="morfo">morfo</span>
· declarative contract
</h2>
<p data-uix-section-desc>
Eidos-scoped morfo: declares only the data-attrs the recipe targets. No events. Source: <code
>src/uix/morfo/components/avatar.ts</code
>.
</p>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Field</th><th>Value</th></tr></thead>
<tbody>
<tr><td class="name">name</td><td class="type">"{avatarMorfo.name}"</td></tr>
<tr><td class="name">kebab</td><td class="type">"{avatarMorfo.kebab}"</td></tr>
<tr
><td class="name">scope</td><td class="type"
>[{avatarMorfo.scope.map((s) => `"${s}"`).join(', ')}]</td
></tr
>
<tr
><td class="name">parts.length</td><td class="default">{avatarMorfo.parts.length}</td
></tr
>
<tr><td class="name">events.length</td><td class="default">0</td></tr>
</tbody>
</table>
</div>
<div data-uix-subsection-head>Parts</div>
<div data-uix-table-wrap>
<table data-uix-table>
<thead
><tr
><th>Part</th><th>Marker</th><th>Element</th><th>Role</th><th>Archetype</th><th
>States</th
><th>Optional</th></tr
></thead
>
<tbody>
{#each partsList as part}
<tr>
<td class="name">{part.kebab}</td>
<td><code data-uix-part-marker>[{part.marker}]</code></td>
<td class="type">&lt;{part.defaultElement}&gt;</td>
<td class="type">{part.role ?? '—'}</td>
<td class="default">{part.archetype ?? '—'}</td>
<td class="default">{part.states.length ? part.states.join(' | ') : '—'}</td>
<td class="default">{part.optional ? 'yes' : 'no'}</td>
</tr>
{/each}
</tbody>
</table>
</div>
{#each avatarMorfo.parts as rawPart}
{@const partAny = rawPart as unknown as { kebab: string }}
{@const dataAttrs = compiled.contracts.dataAttrsByPart.get(partAny.kebab) ?? []}
{#if dataAttrs.length}
<div data-uix-subsection-head>{partAny.kebab}</div>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>data-attr</th><th>Values</th><th>Source kind</th></tr></thead>
<tbody>
{#each dataAttrs as attr}
<tr>
<td class="name">{attr.attr}</td>
<td class="type">{attr.values ? attr.values.join(' | ') : '—'}</td>
<td class="default"
>{'value' in attr && attr.value
? (attr as { value: { kind: string } }).value.kind
: '—'}</td
>
</tr>
{/each}
</tbody>
</table>
</div>
{/if}
{/each}
</section>
{/if}
{#if tab === 'sema'}
<section data-uix-section>
<h2 data-uix-section-title>
<span data-uix-layer-badge="sema">sema</span> · events
</h2>
<p data-uix-section-desc>
This morfo declares no semantic events. If the component grows user-triggered perceptual
feedback, the event must be declared in Morfo first and surfaced here.
</p>
<div data-uix-empty-state>No Sema events declared.</div>
</section>
{/if}
{#if tab === 'recipe'}
<section data-uix-section>
<h2 data-uix-section-title>Eidos recipe</h2>
<p data-uix-section-desc>
Selectors at <code>src/uix/eidos/components/avatar/avatar.css</code>. Tokens bare-prefixed (<code
>--avatar-*</code
>).
</p>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Selector</th><th>Source</th></tr></thead>
<tbody>
<tr
><td class="name">[data-avatar][data-size]</td><td
><span data-uix-tag data-kind="soma">morfo</span></td
></tr
>
<tr
><td class="name">[data-avatar][data-variant][data-color]</td><td
><span data-uix-tag data-kind="soma">morfo</span></td
></tr
>
<tr
><td class="name">[data-avatar][data-radius]</td><td
><span data-uix-tag data-kind="soma">morfo</span></td
></tr
>
<tr
><td class="name">[data-avatar-image]</td><td
><span data-uix-tag data-kind="soma">morfo</span></td
></tr
>
<tr
><td class="name">[data-avatar-fallback]</td><td
><span data-uix-tag data-kind="soma">morfo</span></td
></tr
>
<tr
><td class="name">[data-avatar][data-image-loaded] [data-avatar-fallback]</td><td
><span data-uix-tag data-kind="eidos">eidos</span></td
></tr
>
</tbody>
</table>
</div>
</section>
{/if}
{#if tab === 'a11y'}
<section data-uix-section>
<h2 data-uix-section-title>Accessibility</h2>
<p data-uix-section-desc>
Avatar is non-interactive. The accessible name comes from
<code>Image.alt</code>; fallback content is decorative when the image loads, semantic
otherwise.
</p>
<div data-uix-subsection-head>Naming</div>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Scenario</th><th>Source</th></tr></thead>
<tbody>
<tr><td>Image loads</td><td class="type">Image.alt</td></tr>
<tr
><td>Image errors / no src</td><td class="type"
>Fallback content (initials announced)</td
></tr
>
<tr
><td>Decorative (avatar next to a name label)</td><td class="type"
>alt="" — name comes from the visible label</td
></tr
>
</tbody>
</table>
</div>
</section>
{/if}
</div>

Powered by TurnKey Linux.