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

584 lines
21 KiB

<script lang="ts">
import {
TimeField,
type TimeFieldColor,
type TimeFieldSize,
type TimeFieldVariant,
type InputSnippetProps
} from '$uix/eidos/components/time-field';
import { compileMorfo } from '$uix/morfo';
import { timeFieldMorfo } from '@/uix/morfo/components/time-field';
import { getActiveUix } from '$active-uix';
import {
Time,
ZonedDateTime,
type TimeValue,
type TimeGranularity,
type HourCycle,
type EditableTimeSegmentPart
} from '$libs/days';
type Tab = 'live' | 'api' | 'morfo' | 'sema' | 'recipe' | 'a11y';
type TraceEntry = { event: string; family: string; intent?: string; at: number };
type Direction = 'ltr' | 'rtl';
const uix = getActiveUix();
const startTime: TimeValue = new Time(9, 30, 0);
const endTime: TimeValue = new Time(17, 45, 30);
let tab = $state<Tab>('live');
let trace = $state<TraceEntry[]>([]);
let stageRef = $state<HTMLElement | null>(null);
let value = $state<TimeValue | undefined>(startTime);
let placeholder = $state<TimeValue>(startTime);
let name = $state('start');
let required = $state(true);
let disabled = $state(false);
let readonly = $state(false);
let bounds = $state(true);
let lockSeconds = $state(false);
let granularity = $state<TimeGranularity>('minute');
let hourCycle = $state<HourCycle>(24);
let hideTimeZone = $state(false);
let locale = $state('en-US');
let dir = $state<Direction>('ltr');
let size = $state<TimeFieldSize>('md');
let variant = $state<TimeFieldVariant>('surface');
let color = $state<TimeFieldColor>('primary');
const compiled = compileMorfo(timeFieldMorfo);
const partsList = $derived([...compiled.parts.byKebab.values()]);
const events = $derived([...compiled.actions.byName.values()]);
const readonlySegments = $derived<EditableTimeSegmentPart[]>(lockSeconds ? ['second'] : []);
const valueLabel = $derived(value?.toString() ?? 'empty');
const minValue: TimeValue = new Time(8, 0, 0);
const maxValue: TimeValue = new Time(20, 0, 0);
function sameValue(a: TimeValue | undefined, b: TimeValue): boolean {
return a?.toString() === b.toString();
}
function setDemoValue(kind: 'start' | 'end' | 'empty'): void {
if (kind === 'empty') {
value = undefined;
return;
}
value = kind === 'start' ? startTime : endTime;
}
function fmtTime(at: number): string {
return new Intl.DateTimeFormat(undefined, {
minute: '2-digit',
second: '2-digit',
fractionalSecondDigits: 2
}).format(at);
}
$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 TimeField from '$soma/components/time-field';",
" import { Time, type TimeValue } from '$libs/days';",
'',
' const placeholder = new Time(9, 30, 0);',
' let value = $state<TimeValue | undefined>(placeholder);',
'</' + 'script>',
'',
'<TimeField.Provider',
' bind:value',
' {placeholder}',
required && ' required',
disabled && ' disabled',
readonly && ' readonly',
bounds && ' minValue={new Time(8, 0, 0)}',
bounds && ' maxValue={new Time(20, 0, 0)}',
granularity !== 'minute' && ` granularity="${granularity}"`,
hourCycle !== 24 && ` hourCycle={${hourCycle}}`,
locale !== 'en-US' && ` locale="${locale}"`,
dir !== 'ltr' && ` dir="${dir}"`,
lockSeconds && " readonlySegments={['second']}",
'>',
' <TimeField.Label>Start</TimeField.Label>',
` <TimeField.Input name="${name}" aria-label="Start time">`,
' {#snippet children({ segments })}',
' {#each segments as { part, value }, index (`${part}-${index}`)}',
' <TimeField.Segment {part}>{value}</TimeField.Segment>',
' {/each}',
' {/snippet}',
' </TimeField.Input>',
'</TimeField.Provider>'
]
.filter(Boolean)
.join('\n')
);
const eidosSnippet = $derived(
[
"<script lang='ts'>",
" import { TimeField } from '$uix/eidos/components/time-field';",
" import { Time, type TimeValue } from '$libs/days';",
'',
' const placeholder = new Time(9, 30, 0);',
' let value = $state<TimeValue | undefined>(placeholder);',
'</' + 'script>',
'',
'<TimeField',
' bind:value',
' {placeholder}',
size !== 'md' && ` size="${size}"`,
variant !== 'surface' && ` variant="${variant}"`,
color !== 'primary' && ` color="${color}"`,
required && ' required',
disabled && ' disabled',
readonly && ' readonly',
bounds && ' minValue={new Time(8, 0, 0)}',
bounds && ' maxValue={new Time(20, 0, 0)}',
granularity !== 'minute' && ` granularity="${granularity}"`,
hourCycle !== 24 && ` hourCycle={${hourCycle}}`,
locale !== 'en-US' && ` locale="${locale}"`,
dir !== 'ltr' && ` dir="${dir}"`,
lockSeconds && " readonlySegments={['second']}",
'>',
' <TimeField.Label>Start</TimeField.Label>',
` <TimeField.Input name="${name}" aria-label="Start time">`,
' {#snippet children({ segments })}',
' {#each segments as { part, value }, index (`${part}-${index}`)}',
' <TimeField.Segment {part}>{value}</TimeField.Segment>',
' {/each}',
' {/snippet}',
' </TimeField.Input>',
'</TimeField>'
]
.filter(Boolean)
.join('\n')
);
</script>
<div data-uix-canvas-inner>
<header>
<div data-uix-eyebrow>Forms · TimeField</div>
<h1 data-uix-page-title>TimeField</h1>
<p data-uix-page-lede>
Segmented time input. Soma owns hour/minute/second editing, ARIA spinbuttons, validation
and form submission; Eidos adds size, variant, 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>variants</span>3</span>
<span data-uix-meta-pill><span data-uix-meta-key>sizes</span>5</span>
</div>
</header>
<div data-uix-stage>
<div data-uix-stage-area bind:this={stageRef}>
<div style="inline-size: min(28rem, 100%);">
<TimeField
bind:value
bind:placeholder
{required}
{disabled}
{readonly}
{readonlySegments}
{granularity}
{locale}
{dir}
{hourCycle}
{hideTimeZone}
{size}
{variant}
{color}
minValue={bounds ? minValue : undefined}
maxValue={bounds ? maxValue : undefined}
>
<TimeField.Label>Start</TimeField.Label>
<TimeField.Input {name} aria-label="Start time">
{#snippet children({ segments }: InputSnippetProps)}
{#each segments as { part, value }, index (`${part}-${index}`)}
<TimeField.Segment {part}>{value}</TimeField.Segment>
{/each}
{/snippet}
</TimeField.Input>
</TimeField>
</div>
</div>
<div data-uix-stage-trace>
<span data-uix-stage-trace-key>trace</span>
{#if trace.length === 0}
<span>edit or play an event to see sema stamps</span>
{:else}
{#each trace.slice(0, 3) as entry}
<span
><span data-uix-stage-trace-event>{entry.event}</span> · {entry.family}{entry.intent
? ' · ' + entry.intent
: ''}</span
>
<span style="color: var(--uix-text-faint)">{fmtTime(entry.at)}</span>
{/each}
{/if}
<span style="margin-inline-start: auto;">
<span data-uix-stage-trace-key>value</span>
{valueLabel}
</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</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>Controls</h2>
<p data-uix-section-desc>
<span data-uix-layer-badge="soma">soma</span> owns value, segment behavior and
validation. <span data-uix-layer-badge="eidos">eidos</span> owns only the visual
treatment.
</p>
<div data-uix-subsection-head>
<span data-uix-layer-badge="soma">soma</span> props · segmented value
</div>
<div data-uix-controls>
<label data-uix-control>
<span data-uix-control-label>value</span>
<span data-uix-chips role="group">
<button
data-uix-chip
data-active={sameValue(value, startTime)}
onclick={() => setDemoValue('start')}>start</button
>
<button
data-uix-chip
data-active={sameValue(value, endTime)}
onclick={() => setDemoValue('end')}>end</button
>
<button
data-uix-chip
data-active={value === undefined}
onclick={() => setDemoValue('empty')}>empty</button
>
</span>
</label>
<label data-uix-control>
<span data-uix-control-label>granularity</span>
<span data-uix-chips role="radiogroup">
{#each ['hour', 'minute', 'second'] as v}
<button
data-uix-chip
data-active={granularity === v}
onclick={() => (granularity = v as TimeGranularity)}>{v}</button
>
{/each}
</span>
</label>
<label data-uix-control>
<span data-uix-control-label>hour cycle</span>
<span data-uix-chips role="radiogroup">
{#each [12, 24] as v}
<button
data-uix-chip
data-active={hourCycle === v}
onclick={() => (hourCycle = v as HourCycle)}>{v}</button
>
{/each}
</span>
</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>locale</span>
<select bind:value={locale}>
<option value="en-US">en-US</option>
<option value="es-ES">es-ES</option>
<option value="fr-FR">fr-FR</option>
</select>
</label>
<label data-uix-control>
<span data-uix-control-label>direction</span>
<span data-uix-chips role="radiogroup">
{#each ['ltr', 'rtl'] as v}
<button
data-uix-chip
data-active={dir === v}
onclick={() => (dir = v as Direction)}>{v}</button
>
{/each}
</span>
</label>
<label data-uix-control>
<span data-uix-control-label>states</span>
<span data-uix-switch>
<input type="checkbox" bind:checked={required} />
<span data-uix-switch-label>required</span>
</span>
<span data-uix-switch>
<input type="checkbox" bind:checked={disabled} />
<span data-uix-switch-label>disabled</span>
</span>
<span data-uix-switch>
<input type="checkbox" bind:checked={readonly} />
<span data-uix-switch-label>readonly</span>
</span>
</label>
<label data-uix-control>
<span data-uix-control-label>constraints</span>
<span data-uix-switch>
<input type="checkbox" bind:checked={bounds} />
<span data-uix-switch-label>min 08:00 · max 20:00</span>
</span>
<span data-uix-switch>
<input type="checkbox" bind:checked={lockSeconds} />
<span data-uix-switch-label>seconds readonly</span>
</span>
</label>
</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 v}
<button
data-uix-chip
data-active={size === v}
onclick={() => (size = v as TimeFieldSize)}>{v}</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 TimeFieldVariant)}>{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 v}
<button
data-uix-chip
data-active={color === v}
onclick={() => (color = v as TimeFieldColor)}>{v}</button
>
{/each}
</span>
</label>
</div>
<div data-uix-code>
<div data-uix-code-head>
<span data-uix-layer-badge="soma">soma</span>
<span>headless · segment editing + ARIA</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 · root + attached parts</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>React Aria</th><th>Ark</th><th>Bits</th><th>UIX</th><th>Decision</th></tr>
</thead>
<tbody>
<tr><td class="name">Segmented input</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>Soma owns editable segments.</td></tr>
<tr><td class="name">Granularity (hour/min/sec)</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>Drives which segments render.</td></tr>
<tr><td class="name">12 / 24 hour cycle</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>12h adds dayPeriod (AM/PM) segment.</td></tr>
<tr><td class="name">Validation</td><td>yes</td><td>yes</td><td>yes</td><td>yes</td><td>Custom validate + min/max.</td></tr>
<tr><td class="name">Readonly segments</td><td>no</td><td>no</td><td>yes</td><td>yes</td><td>Per-segment lock without disabling input.</td></tr>
<tr><td class="name">Field integration</td><td>form context</td><td>field</td><td>manual</td><td>yes</td><td>Soma integrates with Field context.</td></tr>
<tr><td class="name">Visual variants</td><td>CSS</td><td>CSS</td><td>CSS</td><td>yes</td><td>Eidos adds canonical control variants.</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">value / placeholder</td><td>soma</td><td class="default">—</td><td>Bindable TimeValue.</td></tr>
<tr><td class="name">minValue / maxValue</td><td>soma</td><td class="default">—</td><td>Validation bounds.</td></tr>
<tr><td class="name">granularity</td><td>soma</td><td class="default">minute</td><td><code>hour</code> | <code>minute</code> | <code>second</code>.</td></tr>
<tr><td class="name">hourCycle</td><td>soma</td><td class="default">locale</td><td>12h adds dayPeriod segment; 24h hides it.</td></tr>
<tr><td class="name">hideTimeZone</td><td>soma</td><td class="default">false</td><td>Only affects ZonedDateTime values.</td></tr>
<tr><td class="name">readonlySegments</td><td>soma</td><td class="default">[]</td><td>Lock individual segments (e.g. seconds).</td></tr>
<tr><td class="name">locale / dir</td><td>soma</td><td class="default">services</td><td>Formatting + keyboard direction.</td></tr>
<tr><td class="name">size / variant / color</td><td>eidos</td><td class="default">md / surface / primary</td><td>Visual-only data attrs.</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>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.optional ? 'yes' : 'no'}</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 + perceptual signature
</h2>
<p data-uix-section-desc>
TimeField only declares commit events. Segment editing remains headless until the value is set.
</p>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Name</th><th>Family</th><th>Verb</th><th>Sequence</th><th>Target</th><th>Play</th></tr></thead>
<tbody>
{#each events as action}
<tr>
<td class="name">{action.name}</td>
<td class="type">{action.semantic.family}</td>
<td>{action.semantic.verb ?? '—'}</td>
<td>{action.semantic.sequence ?? 'pre'}</td>
<td>{action.target}</td>
<td>
<button
data-uix-play
onclick={() => {
const target = stageRef?.querySelector(
'[data-time-field]'
) as HTMLElement | null;
if (!target) return;
void uix.events?.emit({
name: action.name,
family: action.semantic.family,
target
});
}}>▶ play</button
>
</td>
</tr>
{/each}
</tbody>
</table>
</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/time-field/time-field.css</code> and
consume Soma segment state plus Eidos <code>data-size</code>,
<code>data-variant</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-time-field]</code></td><td><span data-uix-tag data-kind="morfo">morfo</span></td><td>Root time-field contract and state.</td></tr>
<tr><td class="name"><code>[data-time-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-time-field-input]</code></td><td><span data-uix-tag data-kind="morfo">morfo</span></td><td>Segment group container.</td></tr>
<tr><td class="name"><code>[data-time-field-segment][data-placeholder][data-readonly]</code></td><td><span data-uix-tag data-kind="morfo">morfo</span></td><td>Individual segment state.</td></tr>
<tr><td class="name"><code>[data-time-field][data-event]</code></td><td><span data-uix-tag data-kind="eidos">eidos</span></td><td>Transient perceptual event styling from Sema projection.</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">Group</td><td>Input exposes <code>role="group"</code> and an accessible name.</td></tr>
<tr><td class="name">Segments</td><td>Editable segments expose <code>role="spinbutton"</code> with min/max/current value.</td></tr>
<tr><td class="name">Keyboard</td><td>Arrow keys, Home/End, Backspace, digits and A/P (for dayPeriod) handled by Soma.</td></tr>
<tr><td class="name">Announcer</td><td>Live region announces segment changes via <code>aria-valuetext</code>.</td></tr>
<tr><td class="name">Form</td><td>Hidden input submits the ISO time string when <code>name</code> is set.</td></tr>
</tbody>
</table>
</div>
</section>
{/if}
</div>

Powered by TurnKey Linux.