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

428 lines
14 KiB

<script lang="ts">
import { PinInput, type PinInputSize, type PinInputType } from '$uix/eidos/components/pin-input';
import { compileMorfo } from '$uix/morfo';
import { pinInputMorfo } from '@/uix/morfo/components/pin-input';
type Tab = 'live' | 'api' | 'morfo' | 'sema' | 'recipe' | 'a11y';
let tab = $state<Tab>('live');
const sizes: PinInputSize[] = ['sm', 'md', 'lg', 'xl'];
const types: PinInputType[] = ['numeric', 'alphanumeric', 'alphabetic', 'custom'];
let value = $state('');
let length = $state(6);
let type = $state<PinInputType>('numeric');
let size = $state<PinInputSize>('md');
let mask = $state(false);
let disabled = $state(false);
let invalid = $state(false);
let placeholder = $state('•');
let lastComplete = $state<string | null>(null);
function handleComplete(v: string) {
lastComplete = v;
}
type TraceEntry = { event: string; family: string; intent?: string; at: number };
let trace = $state<TraceEntry[]>([]);
let stageRef = $state<HTMLElement | null>(null);
$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 compiled = compileMorfo(pinInputMorfo);
const partsList = $derived([...compiled.parts.byKebab.values()]);
const events = $derived([...compiled.actions.byName.values()]);
function fmtTime(at: number): string {
const d = new Date(at);
return `${String(d.getSeconds()).padStart(2, '0')}.${String(d.getMilliseconds()).padStart(3, '0')}`;
}
const somaSnippet = $derived(
[
"<script lang='ts'>",
" import * as PinInput from '$soma/components/pin-input';",
' let value = $state(\'\');',
'</' + 'script>',
'',
'<PinInput.Provider',
' bind:value',
length !== 6 && ` length={${length}}`,
type !== 'numeric' && ` type="${type}"`,
mask && ' mask',
disabled && ' disabled',
invalid && ' invalid',
'>',
' {#snippet children({ cells })}',
' {#each cells as cell, i}',
' <PinInput.Cell {cell} index={i} />',
' {/each}',
' {/snippet}',
'</PinInput.Provider>'
]
.filter(Boolean)
.join('\n')
);
const eidosSnippet = $derived(
[
"<script lang='ts'>",
" import { PinInput } from '$uix/eidos/components/pin-input';",
' let value = $state(\'\');',
'</' + 'script>',
'',
'<PinInput',
' bind:value',
length !== 6 && ` length={${length}}`,
type !== 'numeric' && ` type="${type}"`,
size !== 'md' && ` size="${size}"`,
mask && ' mask',
disabled && ' disabled',
invalid && ' invalid',
' onComplete={(v) => console.log(v)}',
'>',
' {#snippet children({ cells })}',
' {#each cells as cell, i}',
' <PinInput.Cell {cell} index={i} />',
' {/each}',
' {/snippet}',
'</PinInput>'
]
.filter(Boolean)
.join('\n')
);
</script>
<div data-uix-canvas-inner>
<header>
<div data-uix-eyebrow>Form · PinInput</div>
<h1 data-uix-page-title>PinInput</h1>
<p data-uix-page-lede>
Multi-cell OTP / verification code input. Single hidden
<code>&lt;input&gt;</code> captures every keystroke + paste so
autofill (<code>autocomplete="one-time-code"</code>), IME and
distribute-on-paste all work naturally.
<span data-uix-layer-badge="soma">soma</span> owns the capture
pipeline; eidos paints the bordered cells with the focus ring
tracking the caret via <code>data-active</code>.
</p>
<div data-uix-page-meta>
<span data-uix-meta-pill>
<span data-uix-meta-key>length</span>{length}
</span>
<span data-uix-meta-pill>
<span data-uix-meta-key>type</span>{type}
</span>
<span data-uix-meta-pill>
<span data-uix-meta-key>value</span>{value || '—'}
</span>
{#if lastComplete}
<span data-uix-meta-pill>
<span data-uix-meta-key>completed</span>{lastComplete}
</span>
{/if}
</div>
</header>
<div data-uix-stage>
<div data-uix-stage-area bind:this={stageRef}>
<PinInput
bind:value
{length}
{type}
{size}
{mask}
{disabled}
{invalid}
{placeholder}
onComplete={handleComplete}
>
{#snippet children({ cells })}
{#each cells as cell, i (i)}
<PinInput.Cell {cell} index={i} />
{/each}
{/snippet}
</PinInput>
</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>
{#if trace[0]}
<span style="color: var(--uix-text-faint)">·</span>
<span data-uix-stage-trace-key>last</span>
<span>{trace[0].event} ({trace[0].family}) @{fmtTime(trace[0].at)}</span>
{/if}
</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>2</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>
</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>
<div data-uix-subsection-head>
<span data-uix-layer-badge="soma">soma</span> props · shape + alphabet
</div>
<div data-uix-controls>
<label data-uix-control>
<span data-uix-control-label>length</span>
<input type="range" min="3" max="10" step="1" bind:value={length} />
<span data-uix-control-value>{length}</span>
</label>
<label data-uix-control>
<span data-uix-control-label>type</span>
<span data-uix-chips role="radiogroup">
{#each types as opt}
<button data-uix-chip data-active={type === opt} onclick={() => (type = opt)}>{opt}</button>
{/each}
</span>
</label>
<label data-uix-control>
<span data-uix-control-label>placeholder</span>
<input type="text" bind:value={placeholder} maxlength="1" style="inline-size: 4rem;" />
</label>
<label data-uix-control data-control-kind="switch">
<input type="checkbox" bind:checked={mask} /> mask (•)
</label>
<label data-uix-control data-control-kind="switch">
<input type="checkbox" bind:checked={disabled} /> disabled
</label>
<label data-uix-control data-control-kind="switch">
<input type="checkbox" bind:checked={invalid} /> invalid
</label>
</div>
<div data-uix-subsection-head>
<span data-uix-layer-badge="eidos">eidos</span> props · cell shape
</div>
<div data-uix-controls>
<label data-uix-control>
<span data-uix-control-label>size</span>
<span data-uix-chips role="radiogroup">
{#each sizes as opt}
<button data-uix-chip data-active={size === opt} onclick={() => (size = opt)}>{opt}</button>
{/each}
</span>
</label>
</div>
<div data-uix-code>
<div data-uix-code-head>
<span data-uix-layer-badge="soma">soma</span>
<span>headless · hidden capture input</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 · sized bordered cells</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>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Name</th><th>Type</th><th>Default</th><th>Notes</th></tr></thead>
<tbody>
<tr>
<td class="name">value <span data-uix-tag data-kind="soma">soma</span></td>
<td class="type">string</td>
<td class="default">''</td>
<td>Bindable code value.</td>
</tr>
<tr>
<td class="name">length <span data-uix-tag data-kind="soma">soma</span></td>
<td class="type">number</td>
<td class="default">6</td>
<td>Number of cells.</td>
</tr>
<tr>
<td class="name">type <span data-uix-tag data-kind="soma">soma</span></td>
<td class="type">'numeric' | 'alphanumeric' | 'alphabetic' | 'custom'</td>
<td class="default">'numeric'</td>
<td>Alphabet preset → drives pattern + inputmode + autocomplete defaults.</td>
</tr>
<tr>
<td class="name">pattern <span data-uix-tag data-kind="soma">soma</span></td>
<td class="type">string | RegExp</td>
<td class="default empty">—</td>
<td>Custom accept regex (overrides <code>type</code>).</td>
</tr>
<tr>
<td class="name">mask <span data-uix-tag data-kind="soma">soma</span></td>
<td class="type">boolean</td>
<td class="default">false</td>
<td>Show <code>•</code> instead of the real char.</td>
</tr>
<tr>
<td class="name">placeholder <span data-uix-tag data-kind="soma">soma</span></td>
<td class="type">string</td>
<td class="default">''</td>
<td>Glyph shown in empty cells.</td>
</tr>
<tr>
<td class="name">onComplete <span data-uix-tag data-kind="soma">soma</span></td>
<td class="type">(value: string) =&gt; void</td>
<td class="default empty">—</td>
<td>Fired once when <code>value.length === length</code>.</td>
</tr>
<tr>
<td class="name">size <span data-uix-tag data-kind="eidos">eidos</span></td>
<td class="type">'sm' | 'md' | 'lg' | 'xl'</td>
<td class="default">'md'</td>
<td>Cell dimensions + glyph size.</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>Kebab</th><th>Role</th><th>Marker</th></tr></thead>
<tbody>
{#each partsList as part}
{@const partAny = part as unknown as Record<string, unknown>}
<tr>
<td class="name">{partAny.kebab}</td>
<td>{partAny.role ?? '—'}</td>
<td><code>{partAny.marker ?? '—'}</code></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>
PinInput emits {events.length} events. <code>onComplete</code>
fires when the value reaches the target length.
</p>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Name</th><th>Family</th><th>Verb</th><th>Sequence</th></tr></thead>
<tbody>
{#each events as action}
{@const sem = action.semantic}
<tr>
<td class="name">{action.name}</td>
<td class="type">{sem.family}</td>
<td>{sem.verb ?? '—'}</td>
<td>{sem.sequence ?? 'pre'}</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>
<code>pin-input.css</code> styles the bordered cell. The
focus ring follows the caret via <code>data-active</code> on
the cell — the only actually-focused element is the hidden
input, but the visible cell receives the synthetic ring.
</p>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Selector</th><th>Purpose</th></tr></thead>
<tbody>
<tr><td class="name"><code>[data-pin-input]</code></td><td>Inline flex row.</td></tr>
<tr><td class="name"><code>[data-pin-input-cell]</code></td><td>Square bordered cell.</td></tr>
<tr><td class="name"><code>[data-pin-input-cell][data-active]</code></td><td>Focus ring + primary border.</td></tr>
<tr><td class="name"><code>[data-pin-input-cell][data-filled]</code></td><td>Subtle bg lift on filled cells.</td></tr>
<tr><td class="name"><code>[data-pin-input][data-invalid] [data-pin-input-cell]</code></td><td>Threat-toned border.</td></tr>
<tr><td class="name"><code>[data-pin-input][data-size='lg']</code></td><td>Cell + glyph scale token swap.</td></tr>
</tbody>
</table>
</div>
</section>
{/if}
{#if tab === 'a11y'}
<section data-uix-section>
<h2 data-uix-section-title>A11y</h2>
<p data-uix-section-desc>
No formal ARIA pattern for OTP inputs. PinInput follows the
de-facto convention: a single hidden
<code>&lt;input&gt;</code> with
<code>autocomplete="one-time-code"</code> (when
<code>type="numeric"</code>) so iOS / Android surface the
SMS-autofill banner.
</p>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Key</th><th>Effect</th></tr></thead>
<tbody>
<tr><td>Any character</td><td>Insert at caret; advance to next cell.</td></tr>
<tr><td><kbd>Backspace</kbd></td><td>Clear current cell; if empty, clear + focus previous.</td></tr>
<tr><td><kbd>Delete</kbd></td><td>Clear current cell; stay.</td></tr>
<tr><td><kbd>ArrowLeft</kbd> / <kbd>ArrowRight</kbd></td><td>Move caret one cell.</td></tr>
<tr><td><kbd>Home</kbd> / <kbd>End</kbd></td><td>Caret to first / last cell.</td></tr>
<tr><td>Paste</td><td>Distribute across cells (via <code>pasteTransformer</code> if set).</td></tr>
</tbody>
</table>
</div>
</section>
{/if}
</div>

Powered by TurnKey Linux.