FileInput
A file chooser on a native <input type="file">, drawn as one framed row: a prompt, the chosen
file names, and a button that clears them. The native input covers the row, so a click, a drop,
the keyboard, and form submission are the browser’s. Inside a Field it
takes its id (the field’s controlId), disabled, required, state, and aria-describedby
from there.
<Field class="flex flex-col gap-1"> <Label>Audio file</Label> <FileInput accept=".wav,.mp3" class="w-64" bind:value /></Field><FileInput
placeholder="No file chosen"
multiple={false}
disabled={false}
state="none"
/>- A real
<input type="file">covers the row, so the picker, a dropped file, the keyboard, and form submission are the browser’s, and a form reset emptiesvalue.accept,multiple,name,capture, andonchangepass straight through. valueis aFile[]. Withoutmultipleit holds at most one file, and a cleared input is an empty array.- The chosen names show in the row and truncate to its width. A clear button appears beside them.
- Reads
controlId,disabled,required,state, andaria-describedbyfrom an enclosingField.
Two-way binding
Section titled “Two-way binding”bind:value holds the chosen files. Writing the value from outside, such as an empty array, also updates the native input, so the same file can be chosen again. State & binding covers the controlled mode, where a function binding can refuse a file.
chosen: 0
Code
<script lang="ts"> import { FileInput } from "@viamrobotics/prime-ui";
let files = $state<File[]>([]);</script>
<div class="not-content"> <FileInput class="w-72" accept="image/*" multiple bind:value={files} aria-label="Images" />
<p class="mt-3 text-sm text-subtle-2">chosen: <code>{files.length}</code></p> <button type="button" class="mt-2 button button-compact" onclick={() => (files = [])}> Clear from outside </button></div>In a field
Section titled “In a field”Inside a Field the label and hint wire themselves to the native input.
A WAV or MP3 recording.
Code
<script lang="ts"> import { Field, FileInput, Hint, Label } from "@viamrobotics/prime-ui";
let files = $state<File[]>([]);</script>
<Field class="not-content flex max-w-xs flex-col gap-1"> <Label>Audio file</Label> <FileInput accept=".wav,.mp3" bind:value={files} /> <Hint>A WAV or MP3 recording.</Hint></Field>Validation
Section titled “Validation”accept only filters the picker, and a dropped file skips it, so check the files yourself. Set state on the field and the frame, the hint, and aria-invalid all follow it.
Up to 1 MB.
Code
<script lang="ts"> import { Field, FileInput, Hint, Label } from "@viamrobotics/prime-ui";
const MAX_BYTES = 1024 * 1024;
let files = $state<File[]>([]);
const tooLarge = $derived(files.some((file) => file.size > MAX_BYTES));</script>
<Field state={tooLarge ? "error" : "none"} class="not-content flex max-w-xs flex-col gap-1"> <Label>Image</Label> <FileInput accept="image/*" bind:value={files} /> <Hint>{tooLarge ? "Choose an image under 1 MB." : "Up to 1 MB."}</Hint></Field>States
Section titled “States”This recording is longer than a minute.
Unsupported audio format.
Styling
Section titled “Styling”class lands on the frame, the element that holds the native input. It carries no width, so set one yourself or let a flex or grid parent stretch it. The frame is the input component class from @viamrobotics/tailwind-config, the same one Input wears, so input-compact sizes it for a dense panel.
The prompt, an upload icon and “Choose file”, is decoration. Pass children to replace it. placeholder is the text shown while nothing is chosen, and clearLabel names the clear button.
| Hook | Where |
|---|---|
data-field-state |
On the native input for info, warn, and error |
aria-invalid |
On the native input in the error state |
Keyboard
Section titled “Keyboard”Native file input behavior: Enter or Space on the input opens the picker. The clear button is the next tab stop while a file is chosen, and clearing moves focus back to the input.
| Prop | Type | Default | Description |
|---|---|---|---|
bind:value | File[] | The chosen files, in the order the browser lists them. Without multiple it holds at most
one. A cleared input is an empty array. | |
placeholder | string | 'No file chosen' | Text shown while no file is chosen. |
clearLabel | string | 'Clear selection' | Accessible name of the button that clears the choice. |
state | FieldState | Contextual state. Defaults to the enclosing field's state. | |
children | Snippet | Replaces the prompt, an upload icon and "Choose file". It is decoration, hidden from assistive technology, which reads the native input. | |
class | ClassValue | Classes for the root element, the frame that holds the native input. |
Plus everything in Omit<
HTMLInputAttributes,
'value' | 'files' | 'type' | 'children' | 'class' | 'placeholder' | 'readonly'
>, spread onto the element.