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

606 lines
22 KiB

<script lang="ts">
import {
FileUpload,
type FileUploadColor,
type FileUploadSize,
type FileUploadVariant,
type FileRejection
} from '$uix/eidos/components/file-upload';
import { compileMorfo } from '$uix/morfo';
import { fileUploadMorfo } from '@/uix/morfo/components/file-upload';
import type { Intent } from '$uix/intent';
import { getActiveUix } from '$active-uix';
import { page } from '$app/state';
type Tab = 'live' | 'api' | 'morfo' | 'sema' | 'recipe' | 'a11y';
const uix = getActiveUix();
const params = page.url.searchParams;
let tab = $state<Tab>('live');
let files = $state<File[]>([
createDemoFile('brief.pdf', 'application/pdf', 240_000),
createDemoFile('cover.png', 'image/png', 92_000)
]);
let accept = $state('image/*,.pdf');
let maxFiles = $state(4);
let maxSize = $state(1_000_000);
let multiple = $state(true);
let allowDrop = $state(true);
let preventDocumentDrop = $state(false);
let disabled = $state(params.get('perm.disabled') === 'true');
let required = $state(params.get('perm.required') === 'true');
let size = $state<FileUploadSize>('md');
let variant = $state<FileUploadVariant>('surface');
let color = $state<FileUploadColor>('primary');
let rejected = $state<FileRejection[]>([]);
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(fileUploadMorfo);
const partsList = $derived([...compiled.parts.byKebab.values()]);
const events = $derived([...compiled.actions.byName.values()]);
function createDemoFile(name: string, type: string, size: number): File {
if (typeof File !== 'undefined') {
// For image types we ship a valid 1x1 PNG so the preview renders
// instead of showing the user-agent broken-image icon. atob is
// browser-only so this lives inside the runtime check.
const transparentPng = Uint8Array.from(
atob(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII='
),
(c) => c.charCodeAt(0)
);
const bytes = type.startsWith('image/')
? new Uint8Array([...transparentPng, ...new Uint8Array(Math.max(0, size - transparentPng.length))])
: new Uint8Array(size);
return new File([bytes], name, { type, lastModified: Date.now() });
}
return {
name,
type,
size,
lastModified: Date.now(),
webkitRelativePath: '',
arrayBuffer: async () => new ArrayBuffer(size),
slice: () => new Blob(),
stream: () => new ReadableStream(),
text: async () => ''
} as File;
}
function fmtTime(at: number): string {
const d = new Date(at);
return `${String(d.getSeconds()).padStart(2, '0')}.${String(d.getMilliseconds()).padStart(3, '0')}`;
}
function intentLiteral(value: unknown): Intent | undefined {
return typeof value === 'string' ? (value as Intent) : undefined;
}
function onReject(next: FileRejection[]): void {
rejected = next;
}
function progressFor(index: number): number {
return index === 0 ? 100 : 64;
}
function previewLabel(file: File): string {
if (file.type.startsWith('image/')) return 'IMG';
if (file.type.includes('pdf')) return 'PDF';
return 'FILE';
}
const somaSnippet = $derived(
[
"<script lang='ts'>",
" import * as FileUpload from '$soma/components/file-upload';",
' let files = $state<File[]>([]);',
'</' + 'script>',
'',
'<FileUpload.Provider bind:files accept="image/*,.pdf" maxFiles={4}>',
' <FileUpload.Label>Project files</FileUpload.Label>',
' <FileUpload.Dropzone>',
' <FileUpload.HiddenInput />',
' <FileUpload.Trigger>Select files</FileUpload.Trigger>',
' </FileUpload.Dropzone>',
' <FileUpload.FileList>',
' {#each files as file, index}',
' <FileUpload.Item {file} {index}>',
' <FileUpload.ItemName />',
' <FileUpload.ItemSize />',
' <FileUpload.ItemRemove>Remove</FileUpload.ItemRemove>',
' </FileUpload.Item>',
' {/each}',
' </FileUpload.FileList>',
'</FileUpload.Provider>'
].join('\n')
);
const eidosSnippet = $derived(
[
"<script lang='ts'>",
" import { FileUpload } from '$uix/eidos/components/file-upload';",
' let files = $state<File[]>([]);',
'</' + 'script>',
'',
'<FileUpload',
' bind:files',
' accept="image/*,.pdf"',
' maxFiles={4}',
size !== 'md' && ` size="${size}"`,
variant !== 'surface' && ` variant="${variant}"`,
color !== 'primary' && ` color="${color}"`,
'>',
' <FileUpload.Label>Project files</FileUpload.Label>',
' <FileUpload.Dropzone>',
' <FileUpload.HiddenInput />',
' <FileUpload.Trigger>Select files</FileUpload.Trigger>',
' </FileUpload.Dropzone>',
' <FileUpload.FileList>',
' {#each files as file, index}',
' <FileUpload.Item {file} {index}>',
' <FileUpload.ItemPreview />',
' <FileUpload.ItemName />',
' <FileUpload.ItemSize />',
' <FileUpload.ItemRemove>Remove</FileUpload.ItemRemove>',
' </FileUpload.Item>',
' {/each}',
' </FileUpload.FileList>',
'</FileUpload>'
]
.filter(Boolean)
.join('\n')
);
</script>
<svelte:head>
<meta name="perm-axes" content="disabled,required" />
</svelte:head>
<div data-uix-canvas-inner>
<header>
<div data-uix-eyebrow>Forms · File upload</div>
<h1 data-uix-page-title>FileUpload</h1>
<p data-uix-page-lede>
Compound file picker with dropzone, hidden native input, validation, accepted file list,
removal, preview slots and progress rows.
</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>files</span>{files.length}</span>
<span data-uix-meta-pill><span data-uix-meta-key>accept</span>{accept}</span>
</div>
</header>
<div data-uix-stage>
<div
data-uix-stage-area
bind:this={stageRef}
style="display: flex; flex-direction: column; gap: 1rem; align-items: stretch; min-inline-size: min(100%, 42rem);"
>
<FileUpload
bind:files
{accept}
{maxFiles}
{maxSize}
{multiple}
{allowDrop}
{preventDocumentDrop}
{disabled}
{required}
{size}
{variant}
{color}
onReject={onReject}
>
<FileUpload.Label>Project files</FileUpload.Label>
<FileUpload.Dropzone>
<FileUpload.HiddenInput />
<div style="display: grid; gap: .4rem;">
<strong>Drop files here</strong>
<span style="color: var(--uix-text-muted);">Images and PDF, up to 1 MB each.</span>
</div>
<FileUpload.Trigger>Select files</FileUpload.Trigger>
</FileUpload.Dropzone>
<FileUpload.FileList>
{#each files as file, index (file.name + file.size + index)}
<FileUpload.Item {file} {index}>
<FileUpload.ItemPreview>
{#snippet children({ url, file })}
{#if file.type.startsWith('image/')}
<img src={url} alt="" />
{:else}
<span style="font-size: .7rem; font-weight: 700;">{previewLabel(file)}</span>
{/if}
{/snippet}
</FileUpload.ItemPreview>
<div style="display: grid; gap: .25rem; min-inline-size: 0;">
<FileUpload.ItemName />
<FileUpload.ItemSize />
</div>
<FileUpload.ItemRemove
data-perm-step={!disabled && index === 0 ? '0' : undefined}
data-perm-label={!disabled && index === 0 ? 'remove first file' : undefined}
>
Remove
</FileUpload.ItemRemove>
<FileUpload.ItemProgress value={progressFor(index)} />
</FileUpload.Item>
{/each}
</FileUpload.FileList>
<FileUpload.ClearTrigger
data-perm-step={!disabled && files.length > 0 ? '1' : undefined}
data-perm-label={!disabled && files.length > 0 ? 'clear files' : undefined}
>
Clear all
</FileUpload.ClearTrigger>
</FileUpload>
</div>
<div data-uix-stage-trace>
<span data-uix-stage-trace-key>trace</span>
{#if trace.length === 0}
<span>remove or clear files to see Sema events</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>rejected</span>
{rejected.length ? rejected[0].reasons.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>12</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>
<span data-uix-layer-badge="soma">soma</span> owns picker, drop, paste, validation and
file mutations. <span data-uix-layer-badge="eidos">eidos</span> owns visual treatment.
</p>
<div data-uix-subsection-head>
<span data-uix-layer-badge="soma">soma</span> props · upload model
</div>
<div data-uix-controls>
<label data-uix-control>
<span data-uix-control-label>accept</span>
<input bind:value={accept} />
</label>
<label data-uix-control>
<span data-uix-control-label>maxFiles</span>
<input type="number" min="1" max="8" bind:value={maxFiles} />
</label>
<label data-uix-control>
<span data-uix-control-label>maxSize</span>
<input type="number" min="1000" step="1000" bind:value={maxSize} />
</label>
{#each ['multiple', 'allowDrop', 'preventDocumentDrop', 'disabled', 'required'] as key}
<label data-uix-control>
<span data-uix-control-label>{key}</span>
<span data-uix-switch>
{#if key === 'multiple'}
<input type="checkbox" bind:checked={multiple} />
<span data-uix-switch-label>{multiple ? 'on' : 'off'}</span>
{:else if key === 'allowDrop'}
<input type="checkbox" bind:checked={allowDrop} />
<span data-uix-switch-label>{allowDrop ? 'on' : 'off'}</span>
{:else if key === 'preventDocumentDrop'}
<input type="checkbox" bind:checked={preventDocumentDrop} />
<span data-uix-switch-label>{preventDocumentDrop ? 'on' : 'off'}</span>
{:else if key === 'disabled'}
<input type="checkbox" bind:checked={disabled} />
<span data-uix-switch-label>{disabled ? 'on' : 'off'}</span>
{:else}
<input type="checkbox" bind:checked={required} />
<span data-uix-switch-label>{required ? '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 ['sm', 'md', 'lg'] as s}
<button data-uix-chip data-active={size === s} onclick={() => (size = s as FileUploadSize)}>
{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 FileUploadVariant)}
>
{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 c}
<button data-uix-chip data-active={color === c} onclick={() => (color = c as FileUploadColor)}>
{c}
</button>
{/each}
</span>
</label>
</div>
<div data-uix-code>
<div data-uix-code-head>
<span data-uix-layer-badge="soma">soma</span>
<span>headless · file 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 · upload recipe</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 surface</h2>
<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">files</td><td>soma</td><td class="default">[]</td><td>Accepted files, bindable.</td></tr>
<tr><td class="name">accept</td><td>soma</td><td class="default">undefined</td><td>Native accept string plus validation guard.</td></tr>
<tr><td class="name">maxFiles / multiple</td><td>soma</td><td class="default">undefined / true</td><td>Caps accepted file count.</td></tr>
<tr><td class="name">maxSize / minSize</td><td>soma</td><td class="default">undefined</td><td>Per-file byte limits.</td></tr>
<tr><td class="name">validate</td><td>soma</td><td class="default">undefined</td><td>Custom rejection hook.</td></tr>
<tr><td class="name">onReject</td><td>soma</td><td class="default">noop</td><td>Receives rejected files and reasons.</td></tr>
<tr><td class="name">allowDrop / preventDocumentDrop</td><td>soma</td><td class="default">true / true</td><td>Dropzone and document-drop behavior.</td></tr>
<tr><td class="name">disabled / required</td><td>soma</td><td class="default">false</td><td>Interaction and form state.</td></tr>
<tr><td class="name">size / variant / color</td><td>eidos</td><td class="default">md / surface / primary</td><td>Visual-only treatment.</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>
FileUpload declares dropzone, native input, accepted-list rows and clear/remove actions.
Picker opening is contact; add/remove/clear are commits; validation failure is a risk signal.
</p>
<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>
<div data-uix-subsection-head>Keyboard</div>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Part</th><th>Key</th><th>Action</th></tr></thead>
<tbody>
{#each fileUploadMorfo.parts as part}
{@const partAny = part as unknown as {
kebab: string;
keyboard?: ReadonlyArray<{ key: string; action: string }>;
}}
{#each partAny.keyboard ?? [] as key}
<tr><td class="name">{partAny.kebab}</td><td>{key.key}</td><td>{key.action}</td></tr>
{/each}
{/each}
</tbody>
</table>
</div>
{#each fileUploadMorfo.parts as rawPart}
{@const partAny = rawPart as unknown as {
kebab: string;
aria?: ReadonlyArray<{ attr: string; value: { kind: string }; severity?: string }>;
}}
{@const dataAttrs = compiled.contracts.dataAttrsByPart.get(partAny.kebab) ?? []}
{@const ariaAttrs = partAny.aria ?? []}
{#if dataAttrs.length || ariaAttrs.length}
<div data-uix-subsection-head>{partAny.kebab}</div>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Attribute</th><th>Values</th><th>Source</th></tr></thead>
<tbody>
{#each dataAttrs as attr}
<tr>
<td class="name">{attr.attr}</td>
<td class="type">{attr.values ? attr.values.join(' | ') : '-'}</td>
<td class="default">
{'value' in attr && attr.value
? (attr as { value: { kind: string } }).value.kind
: '-'}
</td>
</tr>
{/each}
{#each ariaAttrs as a}
<tr><td class="name">{a.attr}</td><td class="type">-</td><td>{a.value.kind}</td></tr>
{/each}
</tbody>
</table>
</div>
{/if}
{/each}
</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>
Drop hover remains structural state. Only picker contact and accepted/rejected/mutated file
actions emit perception.
</p>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Name</th><th>Family</th><th>Verb</th><th>Intent</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.intent ?? '-'}</td>
<td>{action.target}</td>
<td>
<button
data-uix-play
onclick={() => {
const selector =
action.target === 'trigger'
? '[data-file-upload-trigger]'
: action.target === 'item'
? '[data-file-upload-item]'
: '[data-file-upload]';
const target = stageRef?.querySelector(selector) as HTMLElement | null;
if (!target) return;
void uix.events?.emit({
name: action.name,
family: action.semantic.family,
intent: intentLiteral(action.semantic.intent),
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/file-upload/file-upload.css</code>.
The recipe consumes disabled/dragging/empty state from Soma plus Eidos visual attrs.
</p>
<div data-uix-table-wrap>
<table data-uix-table>
<thead><tr><th>Selector</th><th>Source</th></tr></thead>
<tbody>
<tr><td class="name">[data-file-upload]</td><td><span data-uix-tag data-kind="soma">morfo</span></td></tr>
<tr><td class="name">[data-file-upload-dropzone][data-dragging]</td><td><span data-uix-tag data-kind="soma">morfo</span></td></tr>
<tr><td class="name">[data-file-upload-item]</td><td><span data-uix-tag data-kind="soma">morfo</span></td></tr>
<tr><td class="name">[data-file-upload-item-progress][data-value]</td><td><span data-uix-tag data-kind="soma">morfo</span></td></tr>
<tr><td class="name">[data-file-upload][data-size]</td><td><span data-uix-tag data-kind="eidos">eidos</span></td></tr>
<tr><td class="name">[data-file-upload][data-variant]</td><td><span data-uix-tag data-kind="eidos">eidos</span></td></tr>
<tr><td class="name">[data-file-upload][data-color]</td><td><span data-uix-tag data-kind="eidos">eidos</span></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">Native input</td><td>HiddenInput remains the real form control with native accept/multiple/name/required attributes.</td></tr>
<tr><td class="name">Dropzone</td><td>Dropzone is keyboard reachable and opens the picker on Enter or Space.</td></tr>
<tr><td class="name">List</td><td>FileList is labelled and rows are list items with file metadata.</td></tr>
<tr><td class="name">Removal</td><td>ItemRemove announces the generic remove label plus the current file name.</td></tr>
<tr><td class="name">Validation</td><td>Rejected files are reported through onReject; rejected-list rendering is not a Soma contract yet.</td></tr>
</tbody>
</table>
</div>
</section>
{/if}
</div>

Powered by TurnKey Linux.