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

408 lines
15 KiB

<script lang="ts">
import { Highlight, type HighlightColor } from '$uix/eidos/components/highlight';
import { compileMorfo } from '$uix/morfo';
import { highlightMorfo } from '@/uix/morfo/components/highlight';
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);
let color = $state<HighlightColor>('fulfill');
let ignoreCase = $state(true);
let text = $state(
'The quick brown fox jumps over the lazy dog. Some FOXES are quicker than others.'
);
let queryRaw = $state('fox, quick');
const colors: HighlightColor[] = [
'primary',
'secondary',
'neutral',
'affirm',
'fulfill',
'risk',
'threat',
'loss'
];
const queryArray = $derived(
queryRaw
.split(',')
.map((s) => s.trim())
.filter(Boolean)
);
const compiled = compileMorfo(highlightMorfo);
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();
});
const somaSnippet = $derived(
[
'<!-- Highlight is eidos-native — no soma layer. -->',
'<!-- Equivalent markup (per match): -->',
'',
'<span data-highlight',
` data-color="${color}"`,
'>',
' …text…',
` <mark data-mark data-color="${color}">match</mark>`,
' …more text…',
'</span>'
].join('\n')
);
const eidosSnippet = $derived(
[
"<script lang='ts'>",
" import { Highlight } from '$uix/eidos/components/highlight';",
'</' + 'script>',
'',
'<Highlight',
' text={' +
JSON.stringify(text.length > 60 ? text.slice(0, 60) + '…' : text) +
'}',
queryArray.length === 1
? ` query=${JSON.stringify(queryArray[0])}`
: ` query={${JSON.stringify(queryArray)}}`,
color !== 'fulfill' && ` color="${color}"`,
!ignoreCase && ' ignoreCase={false}',
'/>'
]
.filter(Boolean)
.join('\n')
);
</script>
<div data-uix-canvas-inner>
<header>
<div data-uix-eyebrow>Typography · Highlight</div>
<h1 data-uix-page-title>Highlight</h1>
<p data-uix-page-lede>
Substring highlighter primitive. Wraps a <code>{`<span>`}</code> shell around the input text
and replaces matches of <code>query</code> (string or array) with <code>{`<mark>`}</code>
elements that inherit the same recipe as the standalone Mark primitive. Composes inside any
inline-flow primitive. Single part. Eidos-native: no soma backing, no semantic events.
</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>colors</span>8
</span>
<span data-uix-meta-pill>
<span data-uix-meta-key>multi-term</span>yes
</span>
<span data-uix-meta-pill>
<span data-uix-meta-key>scope</span>eidos
</span>
</div>
</header>
<div data-uix-stage>
<div data-uix-stage-area bind:this={stageRef}>
<p
style="max-inline-size: 38rem; padding: var(--space-3); border: 1px dashed var(--color-neutral-border); border-radius: var(--radius-md); font: inherit;"
>
<Highlight {text} query={queryArray} {color} {ignoreCase} />
</p>
</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>color</span>
<span>{color}</span>
<span style="margin-inline-start: auto;">
<span data-uix-stage-trace-key>terms</span> {queryArray.length} ·
<span data-uix-stage-trace-key>ignoreCase</span> {ignoreCase ? 'on' : 'off'}
</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>5</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>
Highlight is eidos-native — no <span data-uix-layer-badge="soma">soma</span> split.
Multiple terms via comma-separated input. Terms are regex-escaped automatically.
</p>
<div data-uix-subsection-head>
<span data-uix-layer-badge="eidos">eidos</span> props · substring match
</div>
<div data-uix-controls>
<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>ignoreCase</span>
<span data-uix-switch>
<input type="checkbox" bind:checked={ignoreCase} />
<span data-uix-switch-label>{ignoreCase ? 'on' : 'off'}</span>
</span>
</label>
<label data-uix-control style="grid-column: span 2;">
<span data-uix-control-label
>query <span data-uix-control-hint>comma-separated terms</span></span
>
<input type="text" bind:value={queryRaw} style="inline-size: 100%; font: inherit;" />
</label>
<label data-uix-control style="grid-column: span 2;">
<span data-uix-control-label>text</span>
<textarea bind:value={text} rows="3" style="inline-size: 100%; font: inherit;"></textarea>
</label>
</div>
<div data-uix-code>
<div data-uix-code-head>
<span data-uix-layer-badge="soma">soma</span>
<span>n/a · highlight 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 · text + query + intent</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>
Highlight scans <code>text</code> for matches of <code>query</code> (string or array of
strings, regex-escaped) and wraps each match in <code>{`<mark data-mark>`}</code>.
</p>
<div data-uix-subsection-head>Props</div>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Prop</th><th>Type</th><th>Default</th><th>Notes</th></tr></thead>
<tbody>
<tr><td class="name">text</td><td class="type">string</td><td class="default">—</td><td>Required. The full text to render.</td></tr>
<tr><td class="name">query</td><td class="type">string | string[]</td><td class="default">—</td><td>Substring(s) to highlight.</td></tr>
<tr><td class="name">ignoreCase</td><td class="type">boolean</td><td class="default">true</td><td>Case-insensitive match.</td></tr>
<tr><td class="name">color</td><td class="type">'primary' | 'secondary' | 'neutral' | 'affirm' | 'fulfill' | 'risk' | 'threat' | 'loss'</td><td class="default">'fulfill'</td><td>Intent palette for the highlight tint.</td></tr>
<tr><td class="name">as</td><td class="type">string</td><td class="default">'span'</td><td>Override outer shell tag.</td></tr>
</tbody>
</table>
</div>
<div data-uix-subsection-head>Composability examples</div>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Use case</th><th>Snippet</th></tr></thead>
<tbody>
<tr>
<td class="name">Single-term search result</td>
<td>
<code>{`<Highlight text={row.title} query={search} />`}</code>
</td>
</tr>
<tr>
<td class="name">Multi-term filter</td>
<td>
<code>{`<Highlight text={message} query={['error', 'failed']} color="threat" />`}</code>
</td>
</tr>
<tr>
<td class="name">Case-sensitive code search</td>
<td>
<code>{`<Highlight text={src} query="TODO" ignoreCase={false} />`}</code>
</td>
</tr>
<tr>
<td class="name">Inside a Heading</td>
<td>
<code>{`<Heading><Highlight text={title} query={search} /></Heading>`}</code>
</td>
</tr>
<tr>
<td class="name">List of results</td>
<td>
<code
>{`{#each results as r}<li><Highlight text={r.label} query={search} /></li>{/each}`}</code
>
</td>
</tr>
</tbody>
</table>
</div>
<div data-uix-subsection-head>Reference comparison</div>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Library</th><th>Closest equivalent</th><th>Difference</th></tr></thead>
<tbody>
<tr><td class="name">radix-themes</td><td>—</td><td>Radix has no substring highlighter; consumer composes manually.</td></tr>
<tr><td class="name">chakra-ui</td><td><code>&lt;Highlight&gt;</code></td><td>Chakra uses colorScheme; UIX restricts to intent palette.</td></tr>
<tr><td class="name">mantine</td><td><code>&lt;Highlight&gt;</code></td><td>Mantine supports regex via <code>highlight={"{ regex: true }"}</code>; UIX reserves for v2.</td></tr>
<tr><td class="name">react-highlight-words</td><td>library</td><td>Bundles highlighting + regex + custom render; UIX is intentionally minimal.</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>Field</th><th>Value</th></tr></thead>
<tbody>
<tr><td class="name">name</td><td>{highlightMorfo.name}</td></tr>
<tr><td class="name">kebab</td><td><code>{highlightMorfo.kebab}</code></td></tr>
<tr><td class="name">scope</td><td>{highlightMorfo.scope.join(', ')}</td></tr>
<tr><td class="name">parts</td><td>{partsList.length}</td></tr>
<tr><td class="name">events</td><td>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>kebab</th><th>marker</th><th>element</th><th>archetype</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.archetype}</td>
<td class="default">{part.optional ? 'yes' : 'no'}</td>
</tr>
{/each}
</tbody>
</table>
</div>
<p data-uix-section-desc style="margin-top: var(--uix-space-4);">
The Provider stamps <code>data-highlight</code> on the outer shell. Inner matches reuse
the Mark recipe via the embedded <code>data-mark</code> attr, sharing
<code>data-color</code>.
</p>
</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>
Highlight declares no semantic events. The primitive is a formatter: given
<code>text</code> + <code>query</code> it produces a tree of segments and decorates the
matches. No commit, no emerge. Animating the appearance of matched text belongs to a
containing list / popover.
</p>
</section>
{/if}
{#if tab === 'recipe'}
<section data-uix-section>
<h2 data-uix-section-title>Eidos recipe</h2>
<p data-uix-section-desc>
Recipe lives in <code>src/uix/eidos/components/highlight/highlight.css</code>. The outer
shell is structural (<code>font: inherit</code>); inner Mark elements pick up the
palette from the standalone Mark recipe.
</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-highlight]</code></td><td><span data-uix-tag data-kind="morfo">morfo</span></td><td>Outer shell. Inherits ambient typography.</td></tr>
<tr><td class="name"><code>[data-highlight] [data-mark]</code></td><td><span data-uix-tag data-kind="eidos">eidos</span></td><td>Reuses the Mark recipe via shared <code>data-mark</code> attr.</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">Role</td><td>The shell is a generic <code>{`<span>`}</code>; inner matches are <code>{`<mark>`}</code>.</td></tr>
<tr><td class="name">Full text in DOM</td><td>Highlight doesn't truncate or hide content. AT read the full text including the marked segments.</td></tr>
<tr><td class="name">Multi-term ordering</td><td>Matches are found left-to-right in <code>text</code> order. Reading order follows the source.</td></tr>
<tr><td class="name">Color contrast</td><td>Inherits the Mark recipe's contract — each intent's bg + text meets WCAG AA.</td></tr>
</tbody>
</table>
</div>
</section>
{/if}
</div>

Powered by TurnKey Linux.