File Input
<wa-file-input>
File inputs allow users to select files from their device.
File inputs allow users to select one or more files from their device using a dropzone that supports both click and drag-and-drop interactions.
<wa-file-input label="Select a file"></wa-file-input>
This component works with standard <form> elements. Please refer to the section on form controls to learn more about form submission and client-side validation.
Examples
Jump to heading
Labels
Jump to heading
Use the label attribute to give the file input an accessible label. For labels that contain HTML, use the label slot instead.
<wa-file-input label="Upload your resume"></wa-file-input>
Hints
Jump to heading
Add descriptive help text with the hint attribute. For hints that contain HTML, use the hint slot instead.
<wa-file-input label="Profile photo" hint="Upload a photo to personalize your account." ></wa-file-input>
Multiple Files
Jump to heading
Add the multiple attribute to allow the file input to accept more than one file. If the user drops a folder, all files within it will be added to the file input.
<wa-file-input label="Upload documents" hint="You can select multiple files at once." multiple ></wa-file-input>
Accepting File Types
Jump to heading
Use the accept attribute to limit the file input to certain file types. Set it to a comma-separated string of unique file type specifiers.
<wa-file-input label="Upload an image" hint="Only JPEG, PNG, GIF, and WebP images are allowed." accept="image/jpeg, image/png, image/gif, image/webp" ></wa-file-input>
You can also use file extensions such as accept="pdf, .doc, .docx".
<wa-file-input label="Upload a document" hint="PDF and Word documents only." accept=".pdf, .doc, .docx" ></wa-file-input>
Disabled
Jump to heading
Use the disabled attribute to disable the file input.
<wa-file-input label="Upload disabled" disabled></wa-file-input>
Sizes
Jump to heading
Use the size attribute to change the file input's size.
<wa-file-input label="Small" size="small"></wa-file-input> <br> <wa-file-input label="Medium" size="medium"></wa-file-input> <br> <wa-file-input label="Large" size="large"></wa-file-input>
Custom Dropzone Content
Jump to heading
Use the dropzone slot to customize what appears inside the dropzone area.
<wa-file-input label="Upload files" multiple> <div slot="dropzone" style="display: flex; flex-direction: column; align-items: center; gap: 0.5rem;"> <wa-icon name="cloud-arrow-up" style="font-size: 2.5rem;"></wa-icon> <strong>Drag and drop your files here</strong> <span style="color: var(--wa-color-neutral-on-quiet);">or click to browse</span> </div> </wa-file-input>
Working with Files
Jump to heading
The files property gives you access to an array of selected files. Unlike the native file input's FileList, this is a standard JavaScript array, making it easier to manipulate.
<wa-file-input id="file-input-demo" label="Select some files" hint="Try the buttons below after selecting files." multiple ></wa-file-input> <br> <wa-button id="reverse-btn" appearance="filled">Reverse Order</wa-button> <wa-button id="clear-btn" appearance="filled">Clear All</wa-button> <wa-button id="log-btn" appearance="filled">Log Files</wa-button> <script> const fileInput = document.getElementById('file-input-demo'); const reverseBtn = document.getElementById('reverse-btn'); const clearBtn = document.getElementById('clear-btn'); const logBtn = document.getElementById('log-btn'); reverseBtn.addEventListener('click', () => { fileInput.files = fileInput.files.toReversed(); }); clearBtn.addEventListener('click', () => { fileInput.files = []; }); logBtn.addEventListener('click', () => { console.log('Selected files:', fileInput.files); }); </script>
The files property must be reassigned, not mutated! Avoid using functions that mutate the array, such as reverse() and sort(), because they won't trigger an update. Use non-mutating alternatives like toReversed() and toSorted() instead.
Uploading with Forms
Jump to heading
When uploading files from a form, add method="post" and enctype="multipart/form-data" to the form so files are sent correctly to the server.
<form id="upload-form" method="post" enctype="multipart/form-data" action="about:blank" > <wa-file-input name="documents" label="Select files to upload" multiple ></wa-file-input> <br> <wa-button type="submit" variant="brand">Upload</wa-button> </form> <script> const form = document.getElementById('upload-form'); form.addEventListener('submit', event => { event.preventDefault(); const formData = new FormData(form); console.log('Files to upload:', [...formData.getAll('documents')]); }); </script>
Required Validation
Jump to heading
Add the required attribute to make file selection mandatory. Form submission will be blocked until a file is selected.
<form id="required-form" action="about:blank" method="get"> <wa-file-input name="file" label="Required file" hint="You must select a file to submit." required ></wa-file-input> <br> <wa-button type="submit" variant="brand">Submit</wa-button> <wa-button type="reset" appearance="filled">Reset</wa-button> </form>
Custom Validation
Jump to heading
Use the setCustomValidity() method to set a custom error message. This will override standard validation and prevent form submission. Clear the error by passing an empty string.
<form id="custom-validation-form" action="about:blank" method="get"> <wa-file-input id="custom-file-input" name="file" label="Upload a small file" hint="Files must be smaller than 500 KB." ></wa-file-input> <br> <wa-button type="submit" variant="brand">Submit</wa-button> <wa-button type="reset" appearance="filled">Reset</wa-button> </form> <script type="module"> const form = document.getElementById('custom-validation-form'); const fileInput = document.getElementById('custom-file-input'); const maxSize = 500 * 1024; // 500 KB fileInput.addEventListener('change', () => { const tooLarge = fileInput.files.some(file => file.size > maxSize); if (tooLarge) { fileInput.setCustomValidity('One or more files exceed the 500 KB limit.'); } else { fileInput.setCustomValidity(''); } }); // Don't actually submit in the demo form.addEventListener('submit', event => { event.preventDefault(); }); </script>
Styling Validation States
Jump to heading
Use the :state(user-valid) and :state(user-invalid) custom states to style the file input based on its validation status. These states only apply after the user has interacted with the control or attempted to submit the form.
<form id="styling-form" action="about:blank" method="get" class="validation-styles"> <wa-file-input name="file" label="Select a file" required ></wa-file-input> <br> <wa-button type="submit" variant="brand">Submit</wa-button> <wa-button type="reset" appearance="filled">Reset</wa-button> </form> <style> .validation-styles wa-file-input:state(user-valid) { outline: solid 2px var(--wa-color-success-fill-loud); outline-offset: 0.5rem; } .validation-styles wa-file-input:state(user-invalid) { outline: solid 2px var(--wa-color-danger-fill-loud); outline-offset: 0.5rem; } </style>
You can also style based on the :state(blank) and :state(dragging) states:
<wa-file-input class="drag-styles" label="Watch the border change while dragging" multiple ></wa-file-input> <style> .drag-styles::part(dropzone) { transition: transform 0.2s ease; } .drag-styles:state(dragging)::part(dropzone) { transform: scale(1.02); } </style>
Importing
Jump to heading
Autoloading components via projects is the recommended way to import components. If you prefer to do it manually, use one of the following code snippets.
Let your project code do the work! Sign up for free to use a project with your very own CDN — it's the fastest and easiest way to use Web Awesome.
To manually import this component from NPM, use the following code.
import '@awesome.me/webawesome/dist/components/file-input/file-input.js';
To manually import this component from React, use the following code.
import WaFileInput from '@awesome.me/webawesome/dist/react/file-input';
Slots
Jump to heading
Learn more about using slots.
| Name | Description |
|---|---|
dropzone
|
Custom content to show in the dropzone. |
file-icon
|
Custom icon for non-image files. |
hint
|
Text that describes how to use the file input. Alternatively, you can use the hint attribute. |
label
|
The file input's label. Alternatively, you can use the label attribute. |
Attributes & Properties
Jump to heading
Learn more about attributes and properties.
| Name | Description | Reflects | |
|---|---|---|---|
acceptaccept |
A comma-separated list of acceptable file types. Must be a list of
unique file type specifiers.
Type
stringDefault
'' |
||
dragging |
Whether files are being dragged over the dropzone.
Type
booleanDefault
false |
||
fileCount |
The number of selected files. Used for validation.
Type
number |
||
files |
The selected files.
Type
File[]Default
[] |
||
hinthint |
The file input's hint. If you need to display HTML, use the
hint slot instead.Type
stringDefault
'' |
||
labellabel |
The file input's label. If you need to display HTML, use the
label slot instead.Type
stringDefault
'' |
||
multiplemultiple |
Allows more than one file to be selected.
Type
booleanDefault
false |
|
|
requiredrequired |
Makes the file input a required field.
Type
booleanDefault
false |
|
|
sizesize |
The file input's size.
Type
'small' | 'medium' | 'large'Default
'medium' |
|
|
withHintwith-hint |
Used for SSR. Will determine if the SSRed component will have the hint slot rendered on initial paint.
Type
booleanDefault
false |
||
withLabelwith-label |
Used for SSR. Will determine if the SSRed component will have the label slot rendered on initial paint.
Type
booleanDefault
false |
Methods
Jump to heading
Learn more about methods.
| Name | Description | Arguments |
|---|---|---|
blur() |
Removes focus from the file input. | |
focus() |
Sets focus on the file input. |
options: FocusOptions
|
Events
Jump to heading
Learn more about events.
| Name | Description |
|---|---|
blur |
Emitted when the dropzone loses focus. |
change |
Emitted when files are added or removed. |
focus |
Emitted when the dropzone gains focus. |
input |
Emitted when file selection changes. |
wa-invalid |
Emitted when the form control has been checked for validity and its constraints aren't satisfied. |
Custom States
Jump to heading
Learn more about custom states.
| Name | Description | CSS selector |
|---|---|---|
blank |
No files selected. |
:state(blank)
|
dragging |
Files being dragged over dropzone. |
:state(dragging)
|
CSS parts
Jump to heading
Learn more about CSS parts.
| Name | Description | CSS selector |
|---|---|---|
base |
The main component wrapper. |
::part(base)
|
dropzone |
The drag-and-drop area. |
::part(dropzone)
|
dropzone-icon |
The upload icon in the dropzone. |
::part(dropzone-icon)
|
dropzone-text |
The instruction text in the dropzone. |
::part(dropzone-text)
|
file |
Individual file item container. |
::part(file)
|
file-details |
Container for file name and size. |
::part(file-details)
|
file-icon |
The icon for non-image files. |
::part(file-icon)
|
file-image |
The image element for image thumbnails. |
::part(file-image)
|
file-list |
The container for selected files. |
::part(file-list)
|
file-name |
The file name text. |
::part(file-name)
|
file-size |
The file size text. |
::part(file-size)
|
file-thumbnail |
The thumbnail/icon container for a file. |
::part(file-thumbnail)
|
hint |
The hint element. |
::part(hint)
|
label |
The label element. |
::part(label)
|
remove-button |
The remove button for each file. |
::part(remove-button)
|
Dependencies
Jump to heading
This component automatically imports the following elements. Sub-dependencies, if any exist, will also be included in this list.