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

623 lines
22 KiB

<script lang="ts">
import {
ToggleGroup,
type ToggleGroupSize,
type ToggleGroupVariant,
type ToggleGroupColor,
type ToggleGroupType
} from '$uix/eidos/components/toggle-group';
import type { Orientation } from '$soma/types';
import { compileMorfo } from '$uix/morfo';
import { toggleGroupMorfo } from '@/uix/morfo/components/toggle-group';
import { getActiveUix } from '$active-uix';
import { page } from '$app/state';
const uix = getActiveUix();
const params = page.url.searchParams;
type Tab = 'live' | 'api' | 'morfo' | 'sema' | 'recipe' | 'a11y';
let tab = $state<Tab>('live');
// ── Live state ──────────────────────────────────────────────────
const types: ToggleGroupType[] = ['single', 'multiple'];
const variants: ToggleGroupVariant[] = ['solid', 'outline', 'ghost'];
const colors: ToggleGroupColor[] = [
'primary',
'neutral',
'affirm',
'risk',
'threat',
'absent',
'unknown'
];
const sizes: ToggleGroupSize[] = ['xs', 'sm', 'md', 'lg', 'xl'];
const orientations: Orientation[] = ['horizontal', 'vertical'];
let type = $state<ToggleGroupType>('single');
let value = $state<string[]>(['bold']);
let variant = $state<ToggleGroupVariant>('outline');
let color = $state<ToggleGroupColor>('neutral');
let size = $state<ToggleGroupSize>('md');
let orientation = $state<Orientation>('horizontal');
let attached = $state(true);
let block = $state(false);
let disabled = $state(params.get('perm.disabled') === 'true');
let loop = $state(true);
let iconOnly = $state(false);
let italicDisabled = $state(false);
let ariaLabel = $state('Text format');
const items = $derived([
{ value: 'bold', label: 'Bold', glyph: 'B' },
{ value: 'italic', label: 'Italic', glyph: 'I', disabled: italicDisabled },
{ value: 'underline', label: 'Underline', glyph: 'U' },
{ value: 'strike', label: 'Strikethrough', glyph: 'S' }
]);
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(toggleGroupMorfo);
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')}`;
}
// ── Snippets ────────────────────────────────────────────────────
const somaSnippet = $derived(
[
"<script lang='ts'>",
" import * as ToggleGroup from '$soma/components/toggle-group';",
' let value = $state<string[]>([\'bold\']);',
'</' + 'script>',
'',
'<ToggleGroup.Provider',
type !== 'single' && ` type="${type}"`,
' bind:value',
disabled && ' disabled',
!loop && ' loop={false}',
orientation !== 'horizontal' && ` orientation="${orientation}"`,
` aria-label="${ariaLabel}"`,
'>',
...items.map(
(it) =>
` <ToggleGroup.Item value="${it.value}"${it.disabled ? ' disabled' : ''}>${it.glyph}</ToggleGroup.Item>`
),
'</ToggleGroup.Provider>'
]
.filter(Boolean)
.join('\n')
);
const eidosSnippet = $derived(
[
"<script lang='ts'>",
" import { ToggleGroup } from '$uix/eidos/components/toggle-group';",
' let value = $state<string[]>([\'bold\']);',
'</' + 'script>',
'',
'<ToggleGroup',
type !== 'single' && ` type="${type}"`,
' bind:value',
variant !== 'solid' && ` variant="${variant}"`,
color !== 'neutral' && ` color="${color}"`,
size !== 'md' && ` size="${size}"`,
orientation !== 'horizontal' && ` orientation="${orientation}"`,
attached && ' attached',
block && ' block',
disabled && ' disabled',
!loop && ' loop={false}',
` aria-label="${ariaLabel}"`,
'>',
...items.map(
(it) =>
` <ToggleGroup.Item value="${it.value}"${it.disabled ? ' disabled' : ''}${iconOnly ? ' iconOnly' : ''}>${it.glyph}</ToggleGroup.Item>`
),
'</ToggleGroup>'
]
.filter(Boolean)
.join('\n')
);
</script>
<div data-uix-canvas-inner>
<header>
<div data-uix-eyebrow>Form · ToggleGroup</div>
<h1 data-uix-page-title>ToggleGroup</h1>
<p data-uix-page-lede>
Multiple toggle buttons sharing one selection model.
<code>type="single"</code> behaves radio-like (deselectable);
<code>type="multiple"</code> behaves checkbox-like. Headless
<span data-uix-layer-badge="soma">soma</span>
owns selection state, keyboard navigation, and the
<code>commit-toggle</code> perceptual event; Eidos paints chrome —
<code>variant</code>, <code>color</code>, <code>size</code>,
<code>attached</code> (segmented control affordance) and
<code>block</code> (stretch to fill).
</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>type</span>{type}
</span>
<span data-uix-meta-pill>
<span data-uix-meta-key>selected</span>{value.length}
</span>
</div>
</header>
<div data-uix-stage>
<div data-uix-stage-area bind:this={stageRef}>
<ToggleGroup
{type}
bind:value
{variant}
{color}
{size}
{orientation}
{attached}
{block}
{disabled}
{loop}
aria-label={ariaLabel}
>
{#each items as item (item.value)}
<ToggleGroup.Item value={item.value} disabled={item.disabled} {iconOnly}>
{item.glyph}
</ToggleGroup.Item>
{/each}
</ToggleGroup>
</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}
<span style="margin-inline-start: auto;">
<span data-uix-stage-trace-key>value</span>
[{value.map((v) => `"${v}"`).join(', ')}]
</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>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>
<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>
Knobs split by layer — <span data-uix-layer-badge="soma">soma</span> owns
selection/keyboard/disabled, <span data-uix-layer-badge="eidos">eidos</span> owns chrome
(variant/color/size/attached/block/iconOnly).
</p>
<div data-uix-subsection-head>
<span data-uix-layer-badge="soma">soma</span> props · selection + a11y
</div>
<div data-uix-controls>
<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>orientation</span>
<span data-uix-chips role="radiogroup">
{#each orientations as opt}
<button data-uix-chip data-active={orientation === opt} onclick={() => (orientation = opt)}>{opt}</button>
{/each}
</span>
</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={loop} /> loop arrow keys
</label>
<label data-uix-control data-control-kind="switch">
<input type="checkbox" bind:checked={italicDisabled} /> disable "Italic" item
</label>
<label data-uix-control>
<span data-uix-control-label>aria-label</span>
<input type="text" bind:value={ariaLabel} />
</label>
</div>
<div data-uix-subsection-head>
<span data-uix-layer-badge="eidos">eidos</span> props · chrome
</div>
<div data-uix-controls>
<label data-uix-control>
<span data-uix-control-label>variant</span>
<span data-uix-chips role="radiogroup">
{#each variants as opt}
<button data-uix-chip data-active={variant === opt} onclick={() => (variant = opt)}>{opt}</button>
{/each}
</span>
</label>
<label data-uix-control>
<span data-uix-control-label>color</span>
<span data-uix-chips role="radiogroup">
{#each colors as opt}
<button data-uix-chip data-active={color === opt} onclick={() => (color = opt)}>{opt}</button>
{/each}
</span>
</label>
<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>
<label data-uix-control data-control-kind="switch">
<input type="checkbox" bind:checked={attached} /> attached (segmented)
</label>
<label data-uix-control data-control-kind="switch">
<input type="checkbox" bind:checked={block} /> block (stretch)
</label>
<label data-uix-control data-control-kind="switch">
<input type="checkbox" bind:checked={iconOnly} /> iconOnly (square)
</label>
</div>
<div data-uix-code>
<div data-uix-code-head>
<span data-uix-layer-badge="soma">soma</span>
<span>headless · selection + keyboard</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 · variant + size + attached + block</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>
Two parts — <code>ToggleGroup</code> (provider/container) and
<code>ToggleGroup.Item</code> (button). Soma drives selection,
eidos drives visual treatment.
</p>
<div data-uix-subsection-head>ToggleGroup (root)</div>
<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">type <span data-uix-tag data-kind="soma">soma</span></td>
<td class="type">'single' | 'multiple'</td>
<td class="default">'single'</td>
<td>Selection mode.</td>
</tr>
<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 array of selected item values.</td>
</tr>
<tr>
<td class="name">disabled <span data-uix-tag data-kind="soma">soma</span></td>
<td class="type">boolean</td>
<td class="default">false</td>
<td>Disables all items.</td>
</tr>
<tr>
<td class="name">orientation <span data-uix-tag data-kind="soma">soma</span></td>
<td class="type">'horizontal' | 'vertical'</td>
<td class="default">'horizontal'</td>
<td>Arrow-key direction.</td>
</tr>
<tr>
<td class="name">loop <span data-uix-tag data-kind="soma">soma</span></td>
<td class="type">boolean</td>
<td class="default">true</td>
<td>Wrap arrow-key navigation at boundaries.</td>
</tr>
<tr>
<td class="name">aria-label <span data-uix-tag data-kind="soma">soma</span></td>
<td class="type">string</td>
<td class="default empty">—</td>
<td>
<strong>Required</strong> unless <code>aria-labelledby</code> is set.
</td>
</tr>
<tr>
<td class="name">variant <span data-uix-tag data-kind="eidos">eidos</span></td>
<td class="type">'solid' | 'outline' | 'ghost'</td>
<td class="default">'solid'</td>
<td>Visual treatment. Mirrors Toggle.</td>
</tr>
<tr>
<td class="name">color <span data-uix-tag data-kind="eidos">eidos</span></td>
<td class="type">primary | neutral | affirm | risk | threat | absent | unknown</td>
<td class="default">'neutral'</td>
<td>Propagates to items via <code>data-color</code>.</td>
</tr>
<tr>
<td class="name">size <span data-uix-tag data-kind="eidos">eidos</span></td>
<td class="type">'xs' | 'sm' | 'md' | 'lg' | 'xl'</td>
<td class="default">'md'</td>
<td>T-shirt scale, responsive.</td>
</tr>
<tr>
<td class="name">attached <span data-uix-tag data-kind="eidos">eidos</span></td>
<td class="type">boolean</td>
<td class="default">false</td>
<td>Segmented control — shared borders.</td>
</tr>
<tr>
<td class="name">block <span data-uix-tag data-kind="eidos">eidos</span></td>
<td class="type">boolean</td>
<td class="default">false</td>
<td>Stretch items to fill the container.</td>
</tr>
</tbody>
</table>
</div>
<div data-uix-subsection-head>ToggleGroup.Item</div>
<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 empty">—</td>
<td><strong>Required</strong>. Item identifier.</td>
</tr>
<tr>
<td class="name">disabled <span data-uix-tag data-kind="soma">soma</span></td>
<td class="type">boolean</td>
<td class="default">false</td>
<td>Disables this item only.</td>
</tr>
<tr>
<td class="name">iconOnly <span data-uix-tag data-kind="eidos">eidos</span></td>
<td class="type">boolean</td>
<td class="default">false</td>
<td>Square aspect, label sr-only.</td>
</tr>
</tbody>
</table>
</div>
</section>
{/if}
{#if tab === 'morfo'}
<section data-uix-section>
<h2 data-uix-section-title>Morfo contract</h2>
<p data-uix-section-desc>
Compiled from <code>src/uix/morfo/components/toggle-group.ts</code>.
</p>
<div data-uix-subsection-head>Parts ({partsList.length})</div>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Kebab</th><th>Role</th><th>Marker</th><th>States</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>
<td>
{#if Array.isArray(partAny.states) && partAny.states.length}
{(partAny.states as string[]).join(', ')}
{:else}—{/if}
</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>
One event: <code>commit-toggle</code> from the
<code>commit</code> family. Each item press fires it with
sequence <em>post</em> — the perceptual signal lands after
the state has flipped. Intent is per-item via
<code>fromProp:intent</code>.
</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>Intent</th><th>Play</th></tr></thead>
<tbody>
{#each events as action}
{@const sem = action.semantic}
{@const intentDecl = 'intent' in sem ? sem.intent : undefined}
{@const effectiveIntent =
typeof intentDecl === 'string'
? intentDecl
: intentDecl && typeof intentDecl === 'object' && 'default' in intentDecl
? (intentDecl as { default: string }).default
: undefined}
<tr>
<td class="name">{action.name}</td>
<td class="type">{sem.family}</td>
<td>{sem.verb ?? '—'}</td>
<td>{sem.sequence ?? 'pre'}</td>
<td class="default">{effectiveIntent ?? '—'}</td>
<td>
<button
data-uix-play
onclick={() => {
const target = stageRef?.querySelector(
'[data-toggle-group-item]'
) as HTMLElement | null;
if (!target) return;
void uix.events?.emit({
name: action.name,
family: sem.family,
target,
...(effectiveIntent
? { intent: effectiveIntent as 'neutral' | 'affirm' | 'risk' | 'threat' }
: {})
});
}}>▶ 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>
Recipe at <code>src/uix/eidos/components/toggle-group/toggle-group.css</code>.
Items consume the Toggle palette tokens so visuals stay
coherent across <code>&lt;Toggle&gt;</code> and
<code>&lt;ToggleGroup.Item&gt;</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-toggle-group]</code></td>
<td><span data-uix-tag data-kind="morfo">morfo</span></td>
<td>Flex container, orientation-aware direction.</td>
</tr>
<tr>
<td class="name"><code>[data-toggle-group][data-attached]</code></td>
<td><span data-uix-tag data-kind="eidos">eidos</span></td>
<td>Collapsed gap + shared borders (segmented control).</td>
</tr>
<tr>
<td class="name"><code>[data-toggle-group][data-block]</code></td>
<td><span data-uix-tag data-kind="eidos">eidos</span></td>
<td>Items <code>flex: 1</code>; group <code>inline-size: 100%</code>.</td>
</tr>
<tr>
<td class="name"><code>[data-toggle-group-item]</code></td>
<td><span data-uix-tag data-kind="morfo">morfo</span></td>
<td>Button — inherits Toggle's height/padding/radius/font tokens.</td>
</tr>
<tr>
<td class="name"><code>[data-toggle-group-item][data-state='on']</code></td>
<td><span data-uix-tag data-kind="morfo">morfo</span></td>
<td>Pressed visual — switches to <code>--_toggle-on-*</code> palette slice.</td>
</tr>
<tr>
<td class="name"
><code>[data-toggle-group][data-variant='outline'] [data-toggle-group-item]</code></td
>
<td><span data-uix-tag data-kind="eidos">eidos</span></td>
<td>Cascades the variant slice from root to items.</td>
</tr>
</tbody>
</table>
</div>
</section>
{/if}
{#if tab === 'a11y'}
<section data-uix-section>
<h2 data-uix-section-title>A11y contract</h2>
<p data-uix-section-desc>
Group exposes <code>role="group"</code> with the required
accessible name (warns in dev when missing). Items are
<code>&lt;button&gt;</code> with
<code>aria-pressed</code> and arrow-key navigation.
Pattern: WAI-ARIA <a
href="https://www.w3.org/WAI/ARIA/apg/patterns/button/"
target="_blank"
rel="noopener">Button</a
>.
</p>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Key</th><th>Effect</th></tr></thead>
<tbody>
<tr><td><kbd>ArrowRight</kbd> / <kbd>ArrowDown</kbd></td><td>Move focus to next item.</td></tr>
<tr><td><kbd>ArrowLeft</kbd> / <kbd>ArrowUp</kbd></td><td>Move focus to previous item.</td></tr>
<tr><td><kbd>Home</kbd></td><td>Move focus to first item.</td></tr>
<tr><td><kbd>End</kbd></td><td>Move focus to last item.</td></tr>
<tr><td><kbd>Space</kbd> / <kbd>Enter</kbd></td><td>Toggle the focused item.</td></tr>
<tr><td><kbd>Tab</kbd></td><td>Move focus out of the group.</td></tr>
</tbody>
</table>
</div>
</section>
{/if}
</div>

Powered by TurnKey Linux.