Skip to content

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>
No file chosen
<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 empties value. accept, multiple, name, capture, and onchange pass straight through.
  • value is a File[]. Without multiple it 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, and aria-describedby from an enclosing Field.

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.

No file chosen

chosen: 0

Code
TwoWay.svelte
<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>

Inside a Field the label and hint wire themselves to the native input.

No file chosen

A WAV or MP3 recording.

Code
InField.svelte
<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>

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.

No file chosen

Up to 1 MB.

Code
Validated.svelte
<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>
No file chosen
greeting.wav
No file chosen
greeting.wav
No file chosen
greeting.wav

This recording is longer than a minute.

notes.txt

Unsupported audio format.

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

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.

PropTypeDefaultDescription
bind:valueFile[]The chosen files, in the order the browser lists them. Without multiple it holds at most one. A cleared input is an empty array.
placeholderstring'No file chosen'Text shown while no file is chosen.
clearLabelstring'Clear selection'Accessible name of the button that clears the choice.
stateFieldStateContextual state. Defaults to the enclosing field's state.
childrenSnippetReplaces the prompt, an upload icon and "Choose file". It is decoration, hidden from assistive technology, which reads the native input.
classClassValueClasses 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.