File input
Let people attach one or more files with a file input. See examples for accepted file types and multiple uploads.
This component requires the following JavaScript plugin:
Examples
Default
Mobile
320pxMobile Lg
480pxTablet
640pxDesktop
1024pxSpecific File Types
Mobile
320pxMobile Lg
480pxTablet
640pxDesktop
1024pxAccept Multiple Files
Mobile
320pxMobile Lg
480pxTablet
640pxDesktop
1024pxComponent API
Initialization
import { fileInputInit } from '@uswds-tailwind/compat/file-input'
fileInputInit()FileInput is the corresponding instance class. Initialize after all parts and the preview template exist.
Anatomy
Use data-scope="file-input" data-part="root" on the wrapper. Required descendants are input, dropzone, instructions, and item-group. The item-group must contain an initial item template with an item-preview-image image and an element with data-file-name for the filename. Initialization removes this template and clones it for selected files.
Optional parts are label, box, drag-text, choose, preview-heading, error-text, and sr-status. Include a visible label, error text, and status region in accessible examples. The runtime writes selection copy and announcement text into these parts.
| Attribute | Description |
|---|---|
accept |
Comma-separated file types; the accept option takes precedence. A rejected file rejects the entire batch and clears the accepted selection. |
disabled |
Disables the native picker; the disabled option takes precedence. |
multiple |
Allows multiple files in the native picker; the multiple option takes precedence. |
aria-disabled |
Presence, including false, supplies the fallback for ariaDisabled. This option marks the control without blocking interaction. |
data-errormessage |
Invalid-type message; the errorText option takes precedence. Defaults to Error: This is not a valid file type. |
name |
Native form submission name. |
The runtime generates data-dragging on the dropzone, data-invalid on the dropzone and error text, and data-valid on the item group. It hides instructions when files are selected and hides the item group when no files are selected.
Preview images use object URLs. Failed image loads receive data-preview-type with pdf, word, excel, video, or generic for fallback styling.
Configuration and methods
Use FileInput.getOrCreateInstance(root, options) to pass accept, multiple, disabled, ariaDisabled, errorText, id, or ids. Options take precedence over markup fallbacks.
setFiles(files) accepts a File[] and returns Promise<void> after the queued selection update. It applies client-side type validation and updates previews; it does not populate the native file input’s files. Use the native picker for normal form submission. api.acceptedFiles, api.invalid, and api.errorText expose selection state. This component has no selection-change callback.
accept is a file-picker hint and client-side constraint. The component does not upload files. Your application owns submission and server validation.
Cleanup and accessibility
fileInputInit() returns initialized instances. Call destroy() before removing markup to release preview URLs and stop the machine. Removed previews also release their URLs. See instance lifecycle.
The separate status region uses role="status" and aria-live="polite". Selection announcements follow a one-second delay. An initially disabled component hides this region. Keep hints linked to the input and retain visible focus styles.