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/field/+page.svelte

569 lines
19 KiB

<script lang="ts">
import {
Field,
type FieldColor,
type FieldOrientation,
type FieldSize,
type FieldVariant
} from '$uix/eidos/components/field';
import { compileMorfo } from '$uix/morfo';
import { fieldMorfo } from '@/uix/morfo/components/field';
type Tab = 'live' | 'api' | 'morfo' | 'sema' | 'recipe' | 'a11y';
type FieldState = 'idle' | 'error' | 'disabled' | 'readonly' | 'loading';
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);
let value = $state('ana@example.com');
let name = $state('email');
let required = $state(true);
let invalid = $state(false);
let disabled = $state(false);
let readonly = $state(false);
let dirty = $state(true);
let touched = $state(false);
let stateOverride = $state<FieldState>('idle');
let size = $state<FieldSize>('md');
let variant = $state<FieldVariant>('surface');
let orientation = $state<FieldOrientation>('vertical');
let color = $state<FieldColor>('primary');
const compiled = compileMorfo(fieldMorfo);
const partsList = $derived([...compiled.parts.byKebab.values()]);
const events = $derived([...compiled.actions.byName.values()]);
const orientations: FieldOrientation[] = ['vertical', 'horizontal', 'responsive'];
const colors: FieldColor[] = [
'primary',
'secondary',
'neutral',
'affirm',
'fulfill',
'risk',
'threat',
'loss'
];
const effectiveState = $derived(
stateOverride !== 'idle'
? stateOverride
: disabled
? 'disabled'
: readonly
? 'readonly'
: invalid
? 'error'
: 'idle'
);
$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();
});
const somaSnippet = $derived(
[
"<script lang='ts'>",
" import * as Field from '$soma/components/field';",
` let value = $state('${value}');`,
'</' + 'script>',
'',
`<Field.Provider name="${name}" required invalid={${invalid}} dirty={${dirty}}>`,
' <Field.Label>Email <Field.RequiredIndicator /></Field.Label>',
' <Field.Control>',
' <Field.Prefix>@</Field.Prefix>',
' <Field.Input type="email" bind:value />',
' <Field.Suffix>.com</Field.Suffix>',
' </Field.Control>',
' <Field.HelperText>Use your work email.</Field.HelperText>',
' <Field.ErrorText>Enter a valid email.</Field.ErrorText>',
'</Field.Provider>'
]
.filter(Boolean)
.join('\n')
);
const eidosSnippet = $derived(
[
"<script lang='ts'>",
" import { Field } from '$uix/eidos/components/field';",
` let value = $state('${value}');`,
'</' + 'script>',
'',
`<Field name="${name}" size="${size}" variant="${variant}"`,
orientation !== 'vertical' && ` orientation="${orientation}"`,
color !== 'primary' && ` color="${color}"`,
` required invalid={${invalid}}>`,
' <Field.Label>Email <Field.RequiredIndicator /></Field.Label>',
' <Field.Control>',
' <Field.Prefix>@</Field.Prefix>',
' <Field.Input type="email" bind:value />',
' <Field.Suffix>.com</Field.Suffix>',
' </Field.Control>',
' <Field.HelperText>Use your work email.</Field.HelperText>',
' <Field.ErrorText>Enter a valid email.</Field.ErrorText>',
'</Field>'
]
.filter(Boolean)
.join('\n')
);
</script>
<div data-uix-canvas-inner>
<header>
<div data-uix-eyebrow>Forms · Field</div>
<h1 data-uix-page-title>Field</h1>
<p data-uix-page-lede>
Form field composition primitive. Soma owns label/input wiring, field state and ARIA; Eidos
adds size, variant, orientation, intent color and recipe styling.
</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>{events.length}</span>
<span data-uix-meta-pill><span data-uix-meta-key>states</span>5</span>
<span data-uix-meta-pill><span data-uix-meta-key>variants</span>2</span>
<span data-uix-meta-pill><span data-uix-meta-key>layout</span>{orientation}</span>
</div>
</header>
<div data-uix-stage>
<div data-uix-stage-area bind:this={stageRef}>
<div style="inline-size: min(24rem, 100%);">
<Field
{name}
{required}
{invalid}
{disabled}
{readonly}
{dirty}
{touched}
state={stateOverride}
{size}
{variant}
{orientation}
{color}
>
<Field.Label>Email <Field.RequiredIndicator /></Field.Label>
<Field.Control>
<Field.Prefix>@</Field.Prefix>
<Field.Input type="email" bind:value />
<Field.Suffix>.com</Field.Suffix>
</Field.Control>
<Field.HelperText>Use your work email.</Field.HelperText>
<Field.ErrorText>Enter a valid email.</Field.ErrorText>
</Field>
</div>
</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 data-uix-stage-trace-event>{effectiveState}</span>
<span style="color: var(--uix-text-faint)">·</span>
<span data-uix-stage-trace-key>value</span>
<span>{value || 'empty'}</span>
<span style="margin-inline-start: auto;">
<span data-uix-stage-trace-key>size</span>
{size} · {orientation}
</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>10</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 · {events.length}e</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>Composition examples</h2>
<p data-uix-section-desc>
Auto-rendered label text, optional indicator and required indicator side by side.
</p>
<div data-uix-example-grid style="display: grid; gap: var(--uix-space-4); margin-block: var(--uix-space-3);">
<Field name="given-name" required>
<Field.Label>First name <Field.RequiredIndicator /></Field.Label>
<Field.Control>
<Field.Input type="text" />
</Field.Control>
<Field.HelperText>Required — required indicator shows.</Field.HelperText>
</Field>
<Field name="middle-name">
<Field.Label>Middle name <Field.OptionalIndicator /></Field.Label>
<Field.Control>
<Field.Input type="text" />
</Field.Control>
<Field.HelperText>Optional — optional indicator shows its default text.</Field.HelperText>
</Field>
<Field name="nickname">
<Field.Label />
<Field.Control>
<Field.Input type="text" />
</Field.Control>
<Field.HelperText>Empty &lt;Field.Label /&gt; falls back to localized texts.label.</Field.HelperText>
</Field>
</div>
<h2 data-uix-section-title>Controls</h2>
<p data-uix-section-desc>
<span data-uix-layer-badge="soma">soma</span> owns state and ARIA wiring.
<span data-uix-layer-badge="eidos">eidos</span> owns layout and visual treatment.
</p>
<div data-uix-subsection-head>
<span data-uix-layer-badge="soma">soma</span> props · field state
</div>
<div data-uix-controls>
<label data-uix-control>
<span data-uix-control-label>value</span>
<input type="email" bind:value />
</label>
<label data-uix-control>
<span data-uix-control-label>name</span>
<input type="text" bind:value={name} />
</label>
<label data-uix-control>
<span data-uix-control-label>state override</span>
<span data-uix-chips role="radiogroup">
{#each ['idle', 'error', 'disabled', 'readonly', 'loading'] as s}
<button
data-uix-chip
data-active={stateOverride === s}
onclick={() => (stateOverride = s as FieldState)}>{s}</button
>
{/each}
</span>
</label>
{#each ['required', 'invalid', 'disabled', 'readonly', 'dirty', 'touched'] as flag}
<label data-uix-control>
<span data-uix-control-label>{flag}</span>
<span data-uix-switch>
{#if flag === 'required'}
<input type="checkbox" bind:checked={required} />
<span data-uix-switch-label>{required ? 'on' : 'off'}</span>
{:else if flag === 'invalid'}
<input type="checkbox" bind:checked={invalid} />
<span data-uix-switch-label>{invalid ? 'on' : 'off'}</span>
{:else if flag === 'disabled'}
<input type="checkbox" bind:checked={disabled} />
<span data-uix-switch-label>{disabled ? 'on' : 'off'}</span>
{:else if flag === 'readonly'}
<input type="checkbox" bind:checked={readonly} />
<span data-uix-switch-label>{readonly ? 'on' : 'off'}</span>
{:else if flag === 'dirty'}
<input type="checkbox" bind:checked={dirty} />
<span data-uix-switch-label>{dirty ? 'on' : 'off'}</span>
{:else}
<input type="checkbox" bind:checked={touched} />
<span data-uix-switch-label>{touched ? 'on' : 'off'}</span>
{/if}
</span>
</label>
{/each}
</div>
<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'] as s}
<button data-uix-chip data-active={size === s} onclick={() => (size = s as FieldSize)}
>{s}</button
>
{/each}
</span>
</label>
<label data-uix-control>
<span data-uix-control-label>variant</span>
<span data-uix-chips role="radiogroup">
{#each ['surface', 'outline', 'ghost'] as v}
<button
data-uix-chip
data-active={variant === v}
onclick={() => (variant = v as FieldVariant)}>{v}</button
>
{/each}
</span>
</label>
<label data-uix-control>
<span data-uix-control-label>orientation</span>
<span data-uix-chips role="radiogroup">
{#each orientations as item}
<button
data-uix-chip
data-active={orientation === item}
onclick={() => (orientation = item)}>{item}</button
>
{/each}
</span>
</label>
<label data-uix-control>
<span data-uix-control-label>color</span>
<span data-uix-chips role="radiogroup">
{#each colors as item}
<button data-uix-chip data-active={color === item} onclick={() => (color = item)}>
{item}
</button>
{/each}
</span>
</label>
</div>
<div data-uix-code>
<div data-uix-code-head>
<span data-uix-layer-badge="soma">soma</span>
<span>headless · field state</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, orientation and intent color</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 comparison</h2>
<div data-uix-table-wrap>
<table data-uix-table>
<thead>
<tr>
<th>Feature</th><th>Air</th><th>Radix Form</th><th>Ark Field</th><th>React Aria</th><th>shadcn Field</th><th>UIX</th><th>Decision</th>
</tr>
</thead>
<tbody>
<tr><td class="name">Label/control association</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>Soma wires ids and ARIA.</td></tr>
<tr><td class="name">Helper/error association</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>✅</td><td>Covered with HelperText/ErrorText.</td></tr>
<tr><td class="name">Form state propagation</td><td>⚠️</td><td>✅</td><td>✅</td><td>✅</td><td>⚠️</td><td>✅</td><td>Soma merges explicit props and form state.</td></tr>
<tr><td class="name">Prefix/suffix control shell</td><td>✅</td><td>❌</td><td>⚠️</td><td>⚠️</td><td>⚠️</td><td>✅</td><td>Covered through Field.Control, Prefix, Suffix.</td></tr>
<tr><td class="name">Responsive orientation</td><td>❌</td><td>❌</td><td>⚠️</td><td>⚠️</td><td>✅</td><td>✅</td><td>Eidos owns vertical/horizontal/responsive layout.</td></tr>
</tbody>
</table>
</div>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Prop</th><th>Owner</th><th>Default</th><th>Notes</th></tr></thead>
<tbody>
<tr
><td class="name">name / label</td><td>soma</td><td class="default">—</td><td
>Form registration and summaries</td
></tr
>
<tr
><td class="name">required / invalid</td><td>soma</td><td class="default">false</td
><td>State and ARIA flags</td></tr
>
<tr
><td class="name">disabled / readonly</td><td>soma</td><td class="default">false</td
><td>Locks field interaction</td></tr
>
<tr
><td class="name">dirty / touched</td><td>soma</td><td class="default"
>form-derived</td
><td>May be overridden</td></tr
>
<tr
><td class="name">state</td><td>soma</td><td class="default">idle</td><td
>Explicit state override</td
></tr
>
<tr
><td class="name">Input.value</td><td>soma</td><td class="default">''</td><td
>Bindable input value</td
></tr
>
<tr
><td class="name">size</td><td>eidos</td><td class="default">md</td><td
>xs | sm | md | lg</td
></tr
>
<tr
><td class="name">variant</td><td>eidos</td><td class="default">outline</td><td
>outline | ghost</td
></tr
>
<tr><td class="name">orientation</td><td>eidos</td><td class="default">vertical</td><td>vertical | horizontal | responsive.</td></tr>
<tr><td class="name">color</td><td>eidos</td><td class="default">primary</td><td>Intent palette for focus and required indicator.</td></tr>
</tbody>
</table>
</div>
</section>
{/if}
{#if tab === 'morfo'}
<section data-uix-section>
<h2 data-uix-section-title>Morfo contract</h2>
<div data-uix-table-wrap>
<table data-uix-table>
<thead
><tr><th>Part</th><th>Marker</th><th>Element</th><th>Role</th><th>States</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.states.length ? part.states.join(' | ') : '—'}</td>
</tr>
{/each}
</tbody>
</table>
</div>
</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 live in <code>src/uix/eidos/components/field/field.css</code> and consume field
state plus Eidos <code>data-size</code>, <code>data-variant</code>,
<code>data-orientation</code> and <code>data-color</code>.
</p>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Selector</th><th>Owner</th><th>Purpose</th></tr></thead>
<tbody>
<tr>
<td class="name"><code>[data-field]</code></td>
<td><span data-uix-tag data-kind="morfo">morfo</span></td>
<td>Root field shell and state projection.</td>
</tr>
<tr>
<td class="name"
><code>[data-field][data-size][data-variant][data-color]</code></td
>
<td><span data-uix-tag data-kind="eidos">eidos</span></td>
<td>Visual scale, control treatment and intent palette.</td>
</tr>
<tr>
<td class="name"
><code>[data-field][data-invalid][data-disabled][data-readonly]</code></td
>
<td><span data-uix-tag data-kind="morfo">morfo</span></td>
<td>State styling from field validity and locks.</td>
</tr>
<tr>
<td class="name"
><code>[data-field-label]</code> / <code>[data-field-required-indicator]</code></td
>
<td><span data-uix-tag data-kind="morfo">morfo</span></td>
<td>Label and required affordance.</td>
</tr>
<tr>
<td class="name"><code>[data-field-control]</code></td>
<td><span data-uix-tag data-kind="morfo">morfo</span></td>
<td>Input container that receives focus and invalid state.</td>
</tr>
<tr>
<td class="name"
><code>[data-field-prefix]</code> / <code>[data-field-suffix]</code></td
>
<td><span data-uix-tag data-kind="morfo">morfo</span></td>
<td>Inline adornments around the native control.</td>
</tr>
<tr>
<td class="name"
><code>[data-field-helper-text]</code> / <code>[data-field-error-text]</code></td
>
<td><span data-uix-tag data-kind="morfo">morfo</span></td>
<td>Assistive copy and validation message surfaces.</td>
</tr>
</tbody>
</table>
</div>
</section>
{/if}
{#if tab === 'a11y'}
<section data-uix-section>
<h2 data-uix-section-title>Accessibility</h2>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Concern</th><th>Contract</th></tr></thead>
<tbody>
<tr><td class="name">Label</td><td>Label wires to Input when both parts exist.</td></tr>
<tr
><td class="name">Description</td><td
>Input points at helper text through <code>aria-describedby</code>.</td
></tr
>
<tr
><td class="name">Error</td><td>Error text uses polite live-region semantics.</td></tr
>
<tr
><td class="name">Required</td><td
>Required indicator is decorative; Input receives ARIA required.</td
></tr
>
</tbody>
</table>
</div>
</section>
{/if}
</div>

Powered by TurnKey Linux.