Forms & Inputs
@hacline/ui/file-inputFileInput
Select, drop, or paste files and display the selected items.
Usage
<FileInputList /> renders a ready-to-use file card with image thumbnails, semantic file icons, file size, and a remove button by default. Format validation errors cleanly with formatFileRejection.
Drop files here or paste from clipboard
Images, PDF, or Markdown (up to 4 files)
"use client";
import { Upload } from "lucide-react";
import { useState } from "react";
import {
FileInput,
FileInputDropzone,
FileInputList,
FileInputTrigger,
formatFileRejection,
} from "@/components/ui/hacline/components/file-input";
export const Example = () => {
const [feedback, setFeedback] = useState("");
return (
<FileInput
accept="image/*,.pdf,.md"
className="max-w-md"
maxFiles={4}
multiple
name="attachments"
onReject={(rejected) => {
const [first] = rejected;
if (first) {
setFeedback(formatFileRejection(first));
}
}}
onValueChange={() => setFeedback("")}
>
<FileInputDropzone>
<Upload aria-hidden="true" className="text-hl-fg-muted size-5" />
<p className="text-hl-fg text-sm font-medium">
Drop files here or paste from clipboard
</p>
<p className="text-hl-fg-muted text-xs">
Images, PDF, or Markdown (up to 4 files)
</p>
<FileInputTrigger className="mt-1">Choose files</FileInputTrigger>
</FileInputDropzone>
{feedback ? (
<output className="text-hl-danger text-xs">{feedback}</output>
) : null}
<FileInputList aria-label="Files to upload" />
</FileInput>
);
};Async Upload Queue (useFileUpload)
Pair FileInput with useFileUpload to manage concurrent uploads, per-file progress bars, cancellation via AbortController, and retries. Connect onValueChange={uploader.syncFiles} to keep the queue automatically synchronized.
Drop files or load samples to test concurrent uploads
"use client";
import { useMemo } from "react";
import { Button } from "@/components/ui/hacline/components/button";
import {
createFileInputController,
FileInput,
FileInputDropzone,
FileInputItem,
FileInputItemName,
FileInputItemPreview,
FileInputItemRemove,
FileInputItemSize,
FileInputList,
FileInputTrigger,
} from "@/components/ui/hacline/components/file-input";
import {
Progress,
ProgressIndicator,
ProgressTrack,
} from "@/components/ui/hacline/components/progress";
import { useFileUpload } from "@/components/ui/hacline/hooks/use-file-upload";
const UPLOAD_STEPS = [20, 48, 76, 100] as const;
const STEP_DELAY_MS = 220;
const simulateUpload = (
file: File,
{
onProgress,
signal,
}: { onProgress: (percent: number) => void; signal: AbortSignal }
) =>
// oxlint-disable-next-line promise/avoid-new -- Simulate staged network upload with abort support.
new Promise<{ url: string }>((resolve, reject) => {
let stepIndex = 0;
const handleAbort = () => {
reject(new DOMException("Upload aborted", "AbortError"));
};
if (signal.aborted) {
handleAbort();
return;
}
signal.addEventListener("abort", handleAbort, { once: true });
const tick = () => {
if (signal.aborted) {
return;
}
const next = UPLOAD_STEPS[stepIndex];
stepIndex += 1;
if (next !== undefined) {
onProgress(next);
}
if (stepIndex >= UPLOAD_STEPS.length) {
signal.removeEventListener("abort", handleAbort);
resolve({ url: `/uploads/${file.name}` });
return;
}
setTimeout(tick, STEP_DELAY_MS);
};
setTimeout(tick, STEP_DELAY_MS);
});
const createSampleFiles = () => [
new File(["# Release Notes\nAll checks passed."], "release-notes.md", {
type: "text/markdown",
}),
new File(['{ "name": "hacline" }'], "tokens.json", {
type: "application/json",
}),
];
const statusLabel = (status: string, progress: number) => {
if (status === "uploading") {
return `${progress}%`;
}
if (status === "success") {
return "Uploaded";
}
if (status === "error") {
return "Failed";
}
if (status === "canceled") {
return "Canceled";
}
return "Queued";
};
export const Example = () => {
const controller = useMemo(() => createFileInputController(), []);
const uploader = useFileUpload({
concurrency: 2,
upload: simulateUpload,
});
return (
<div className="grid w-full max-w-md gap-3">
<FileInput
controller={controller}
multiple
onValueChange={(files) => {
uploader.syncFiles(files);
}}
>
<FileInputDropzone variant="compact">
<span className="text-hl-fg-muted text-xs">
Drop files or load samples to test concurrent uploads
</span>
<div className="flex items-center gap-1.5">
<Button
onClick={() => {
controller.addFiles(createSampleFiles());
}}
size="sm"
variant="outline"
>
Add sample files
</Button>
<FileInputTrigger className="min-h-8 px-2.5 text-xs">
Browse
</FileInputTrigger>
</div>
</FileInputDropzone>
<FileInputList aria-label="Upload queue">
<FileInputItem className="flex-col items-stretch gap-2">
{({ file }) => {
const entry = uploader.getEntry(file);
const status = entry?.status ?? "idle";
const progress = entry?.progress ?? 0;
return (
<>
<div className="flex items-center gap-2.5">
<FileInputItemPreview size="sm" />
<div className="min-w-0 flex-1">
<FileInputItemName />
<div className="flex items-center gap-2">
<FileInputItemSize />
<span className="text-hl-fg-muted text-xs">
· {statusLabel(status, progress)}
</span>
</div>
</div>
{entry && status === "uploading" ? (
<Button
onClick={() => uploader.cancel(entry.id)}
size="sm"
variant="ghost"
>
Cancel
</Button>
) : null}
{entry && (status === "error" || status === "canceled") ? (
<Button
onClick={() => uploader.retry(entry.id)}
size="sm"
variant="outline"
>
Retry
</Button>
) : null}
<FileInputItemRemove />
</div>
{status === "uploading" ? (
<Progress
aria-label={`Uploading ${file.name}`}
value={progress}
>
<ProgressTrack>
<ProgressIndicator />
</ProgressTrack>
</Progress>
) : null}
</>
);
}}
</FileInputItem>
</FileInputList>
</FileInput>
</div>
);
};Reading Files in a Form
Set name and required on the component. On form submission, read the File objects directly from FormData.
"use client";
import { useState } from "react";
import { Button } from "@/components/ui/hacline/components/button";
import {
FileInput,
FileInputTrigger,
} from "@/components/ui/hacline/components/file-input";
export const Example = () => {
const [filename, setFilename] = useState("");
const [submitted, setSubmitted] = useState("");
return (
<form
action={(data) => {
const file = data.get("brief");
if (file instanceof File) {
setSubmitted(file.name);
}
}}
className="grid w-full max-w-xs gap-2"
>
<FileInput
accept=".txt,.md"
name="brief"
onValueChange={(files) => setFilename(files[0]?.name ?? "")}
required
>
<FileInputTrigger>Choose project brief</FileInputTrigger>
</FileInput>
<p className="text-hl-fg-muted text-xs">
{filename || "Supports TXT or Markdown"}
</p>
<Button className="justify-self-start" type="submit" variant="outline">
Read form file
</Button>
{submitted && (
<output className="text-hl-fg-muted text-xs">Loaded {submitted}</output>
)}
</form>
);
};Parts
| Part | Description | Extended Props |
|---|---|---|
FileInputDropzone | Takes dropped and pasted files. Put the trigger inside it, which is how keyboard and screen reader users choose files. | variant |
FileInputItem | One file. Its render state has the file, for its name, size and preview. Arrow keys move between items, and Backspace or Delete removes the focused one. | — |
FileInputItemPreview | Renders an automatic image thumbnail (with safe ObjectURL cleanup) or a semantic file-type icon for non-image files. | filesizefallback |
FileInputItemName | Displays the file name (or folder-relative path when showPath is enabled) with single-line truncation. | fileshowPath |
FileInputItemSize | Displays the formatted file size using formatFileSize with tabular numerals. | filelocale |
FileInputItemRemove | Removes its item's file, or the file it's given. It's named "Remove <file name>" unless you name it. | — |
FileInputList | The files, as a list. Its children render once for each file, so put a FileInputItem in it. | variant |
FileInputTrigger | Opens the file picker. Inside a Field, the field's label names it with the button's own text. Outside the file input, it connects through controller. | — |
formatFileRejectionhelper | Helper that formats a FileRejection from onReject into a human-readable validation message. | — |
useFileUploadhook | Headless React hook managing concurrent async uploads, per-file progress, AbortController cancellation, and retries. | — |
Props
Prop
Type