Choose a file or drop it on the target. The component manages the selection and the caller handles sending the file.
File upload example
Example.jsx
import { FileUpload } from "./ui/file-upload";
import "./example.css";
export default function Example() {
return (
<FileUpload
label="Attach proof notes"
accept=".txt,.md,.pdf"
multiple
maxSizeBytes={2 * 1024 * 1024}
hint="Choose review notes or a marked-up PDF, up to 2 MB per file."
/>
);
}
example.css
@media screen and (prefers-reduced-motion: no-preference) {
#example button:is(.v-primary, .v-secondary):enabled {
transition: transform 120ms ease-out;
}
#example button:is(.v-primary, .v-secondary):enabled:active {
transform: scale(0.98);
}
}
@media (prefers-reduced-motion: reduce) {
#example button:is(.v-primary, .v-secondary):enabled:active {
transition: none;
transform: none;
}
}
* {
box-sizing: border-box;
}
:root {
--example-seed: var(--aster-primary);
--example-secondary-text: var(--aster-on-surface-subtle);
}
body {
--aster-primary: color-mix(in srgb, var(--example-seed) 84%, var(--aster-on-surface));
--aster-on-surface-subtle: color-mix(in srgb, var(--example-secondary-text) 84%, var(--aster-on-surface));
--aster-input-border: color-mix(in srgb, var(--aster-on-surface) 55%, var(--aster-surface));
margin: 0;
padding: clamp(0.8rem, 4vw, 1.5rem);
color: var(--aster-on-surface);
background: var(--aster-surface-muted);
font: 1rem/1.5 var(--aster-font-sans);
}
#example {
display: grid;
gap: 0.8rem;
min-width: 0;
align-content: start;
}
#example > button,
.sample > button {
justify-self: start;
}
.sample,
.tile {
background: var(--aster-surface);
border: 1px solid var(--aster-border);
border-radius: var(--aster-radius-lg);
}
.sample {
display: grid;
gap: 0.8rem;
padding: clamp(0.85rem, 3vw, 1.35rem);
box-shadow: var(--aster-shadow-xs);
min-width: 0;
}
.tile {
padding: 1rem;
min-width: 0;
}
h2,
h3,
p,
dl,
ul {
margin-block: 0;
}
h2,
h3 {
letter-spacing: -0.025em;
line-height: 1.2;
}
h2 {
font-size: 1.25rem;
}
h3 {
font-size: 1rem;
}
.sample > header,
.identity > div,
.receipt {
display: grid;
gap: 0.25rem;
}
.sample > header p,
.tile p,
small {
color: var(--aster-on-surface-muted);
}
small {
font-size: 0.82rem;
}
.split,
.actions,
.identity {
display: flex;
align-items: center;
gap: 0.65rem;
flex-wrap: wrap;
}
.split {
justify-content: space-between;
padding-block: 0.4rem;
border-bottom: 1px solid var(--aster-border);
}
.split:last-child {
border-bottom: 0;
}
.metric {
font-size: 2rem;
letter-spacing: -0.06em;
}
.facts {
display: grid;
grid-template-columns: max-content 1fr;
gap: 0.45rem 1rem;
}
.facts dt {
color: var(--aster-on-surface-muted);
}
.facts dd {
margin: 0;
font-weight: 600;
}
.type-sample {
font:
2rem/1.2 Georgia,
serif;
}
.listing {
padding-inline-start: 1.25rem;
}
.ink-strip {
height: 1.2rem;
background: var(--aster-primary);
border-radius: var(--aster-radius-sm);
}
.ink-proof {
display: grid;
gap: 0.15rem;
border-left: 0.6rem solid;
padding-left: 0.8rem;
}
.ink-proof strong {
font-size: 1.25rem;
letter-spacing: 0.06em;
}
.glyph {
display: inline-grid;
place-items: center;
width: 2rem;
height: 2rem;
border-radius: var(--aster-radius-sm);
color: var(--aster-on-primary-subtle);
background: var(--aster-primary-subtle);
}
.cover-box {
width: min(100%, 18rem);
}
.cover {
display: flex;
min-height: 7.5rem;
height: 100%;
padding: 1rem 1.25rem;
flex-direction: column;
justify-content: space-between;
gap: 1.6rem;
color: #fff;
background: repeating-linear-gradient(150deg, transparent 0 8px, #fff2 8px 9px), #204a48;
}
.cover strong {
font:
italic 2.45rem/1 Georgia,
serif;
}
.cover span {
font: 0.78rem var(--aster-font-mono);
}
.cover.grove {
background-color: #3d4b27;
}
.cover.dune {
background-color: #7c4428;
}
button:not([class]),
.entry {
font: inherit;
font-size: 0.875rem;
padding: 0.45rem 0.8rem;
border: 1px solid var(--aster-border-strong);
border-radius: var(--aster-radius-control);
background: var(--aster-surface);
color: var(--aster-on-surface);
}
button:not([class]) {
cursor: pointer;
font-weight: 600;
}
button:not([class]):hover:enabled {
background: var(--aster-primary-subtle);
}
button:disabled {
cursor: default;
}
:focus-visible {
outline: 2px solid var(--aster-primary);
outline-offset: 3px;
}
output:not(:empty) {
display: block;
color: var(--aster-on-primary-subtle);
font-weight: 600;
font-size: 0.9rem;
}
Site-owned component source and scoped styles
src/ui/file-upload
import './theme.css';
import { FileText, UploadCloud, X } from '@lucide/icons';
import { scope, type Ctx } from '@fierst/aster';
import { Icon } from './icon';
const fuScope = scope`
:scope {
display: grid;
gap: 0.65rem;
font-family: var(--aster-font-sans);
}
.fu-label { font-size: 0.84rem; font-weight: 500; color: var(--aster-on-surface); }
.dropzone {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 0.45rem;
padding: 1.5rem 1rem;
border-radius: var(--aster-radius-lg);
border: 1.5px dashed var(--aster-border-strong);
background: var(--aster-surface);
color: var(--aster-on-surface-muted);
text-align: center;
cursor: pointer;
transition:
border-color var(--aster-dur-fast),
background-color var(--aster-dur-fast);
}
.dropzone:hover,
.dropzone.is-dragover {
border-color: var(--aster-primary);
background: var(--aster-primary-subtle);
}
.dz-icon {
width: 38px;
height: 38px;
border-radius: var(--aster-radius-md);
border: 1px solid var(--aster-border);
background: var(--aster-surface);
color: var(--aster-on-surface);
display: inline-grid;
place-items: center;
box-shadow: var(--aster-shadow-xs);
}
.dz-title { font-size: 0.875rem; font-weight: 500; color: var(--aster-on-surface); }
.dz-sub { font-size: 0.78rem; color: var(--aster-on-surface-muted); }
.native-file { position: absolute; width: 1px; height: 1px; clip-path: inset(50%); overflow: hidden; }
.dropzone:focus-within { outline: 2px solid var(--aster-primary); outline-offset: 2px; }
.fu-error { font-size: 0.78rem; color: var(--aster-danger); font-weight: 500; }
.file-list { display: grid; gap: 0.4rem; }
.file-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.65rem;
padding: 0.5rem 0.75rem;
border-radius: var(--aster-radius-md);
border: 1px solid var(--aster-border);
background: var(--aster-surface);
font-size: 0.82rem;
}
.file-meta { display: flex; align-items: center; gap: 0.5rem; min-width: 0; }
.file-name { font-weight: 500; color: var(--aster-on-surface); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-size { color: var(--aster-on-surface-subtle); font-family: var(--aster-font-mono); font-size: 0.74rem; flex-shrink: 0; }
.file-rm {
border: none;
background: transparent;
color: var(--aster-on-surface-muted);
cursor: pointer;
padding: 0.2rem;
border-radius: var(--aster-radius-xs);
display: inline-grid;
place-items: center;
}
.file-rm:hover { background: var(--aster-surface-muted); color: var(--aster-on-surface); }
`;
function formatBytes(n: number): string {
if (n < 1024) return `${n} B`;
if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)} KB`;
return `${(n / (1024 * 1024)).toFixed(1)} MB`;
}
let fuSeq = 0;
export function* FileUpload(
props: {
label?: string;
accept?: string;
multiple?: boolean;
maxSizeBytes?: number;
hint?: string;
onChange?: (files: File[]) => void;
class?: string;
},
ctx: Ctx,
) {
const uid = `aster-fu-${++fuSeq}`;
let files: File[] = [];
let dragOver = false;
let errorMsg = '';
for (props of ctx) {
const ingest = (incoming: FileList | null) => {
if (!incoming) return;
errorMsg = '';
const accepted: File[] = [];
const types = props.accept
?.toLowerCase()
.split(',')
.map((t) => t.trim())
.filter(Boolean);
for (const f of Array.from(incoming)) {
if (
types?.length &&
!types.some((type) =>
type.startsWith('.')
? f.name.toLowerCase().endsWith(type)
: type.endsWith('/*')
? f.type.toLowerCase().startsWith(type.slice(0, -1))
: f.type.toLowerCase() === type,
)
) {
errorMsg = f.name + ' is not an accepted file type.';
continue;
}
if (props.maxSizeBytes !== undefined && f.size > props.maxSizeBytes) {
errorMsg = `"${f.name}" exceeds max size of ${formatBytes(props.maxSizeBytes)}.`;
continue;
}
accepted.push(f);
}
if (!accepted.length) return;
files = props.multiple ? [...files, ...accepted] : accepted.slice(0, 1);
props.onChange?.(files);
};
const removeAt = (idx: number) => {
files = files.filter((_, i) => i !== idx);
props.onChange?.(files);
};
yield (
<div class={[fuScope, props.class].filter(Boolean).join(' ')}>
{props.label ? (
<label class="fu-label" for={uid}>
{props.label}
</label>
) : null}
<label
for={uid}
class="dropzone"
class:is-dragover={dragOver}
on:dragover={(e: Event) => {
e.preventDefault();
dragOver = true;
}}
on:dragleave={() => (dragOver = false)}
on:drop={(e: Event) => {
e.preventDefault();
dragOver = false;
ingest((e as DragEvent).dataTransfer?.files ?? null);
}}
>
<span class="dz-icon" aria-hidden="true">
<Icon icon={UploadCloud} size={18} />
</span>
<span class="dz-title">Click to upload or drag and drop</span>
<span class="dz-sub">
{props.hint ??
`${props.accept ?? 'Any file'}${props.maxSizeBytes ? ` · Max ${formatBytes(props.maxSizeBytes)}` : ''}`}
</span>
<input
id={uid}
type="file"
class="native-file"
accept={props.accept}
multiple={props.multiple}
onChange={(e: Event) => {
const input = e.currentTarget as HTMLInputElement;
ingest(input.files);
input.value = '';
}}
/>
</label>
{errorMsg ? (
<span class="fu-error" role="alert">
{errorMsg}
</span>
) : null}
{files.length > 0 ? (
<div class="file-list">
{files.map((f, idx) => (
<div key={`${f.name}-${idx}`} class="file-row">
<div class="file-meta">
<Icon icon={FileText} size={15} />
<span class="file-name">{f.name}</span>
<span class="file-size">{formatBytes(f.size)}</span>
</div>
<button
type="button"
class="file-rm"
aria-label={`Remove ${f.name}`}
onClick={() => removeAt(idx)}
>
<Icon icon={X} size={14} />
</button>
</div>
))}
</div>
) : null}
</div>
);
}
}
Install
sh
npx @fierst/ui@1.0.2 add file-uploadRegistry dependencies: theme, icon. Package dependencies: @lucide/icons.
Exports from ./ui/file-upload: FileUpload.
Download the registry source for file-upload 1.0.2.
API contracts
ts
export function* FileUpload(
props: {
label?: string;
accept?: string;
multiple?: boolean;
maxSizeBytes?: number;
hint?: string;
onChange?: (files: File[]) => void;
class?: string;
},
ctx: Ctx,
)