File upload
A drop zone and file chooser, with type and size checks.
Example
<div class="stack"> <focsd-file-upload label="Attachments" accept=".pdf,image/png" multiple max-size="1000000" hint="PDF or PNG, up to 1 MB"></focsd-file-upload></div>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-file-upload.js';</script>
<focsd-file-upload label="Attachments" accept=".pdf,image/png" multiple max-size="1000000" hint="PDF or PNG, up to 1 MB"></focsd-file-upload>import { FileUpload } from '@focsd-dev/react';
export function Example() { return <FileUpload onChange={(e) => console.log(e.target)} label="Attachments" accept=".pdf,image/png" multiple max-size="1000000" hint="PDF or PNG, up to 1 MB"></FileUpload>;}<script setup>import '@focsd-dev/components/focsd-file-upload.js';</script>
<template> <focsd-file-upload @change="onChange" label="Attachments" accept=".pdf,image/png" multiple max-size="1000000" hint="PDF or PNG, up to 1 MB"></focsd-file-upload></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-file-upload.js';
// template<focsd-file-upload (change)="onChange($event)" label="Attachments" accept=".pdf,image/png" multiple max-size="1000000" hint="PDF or PNG, up to 1 MB"></focsd-file-upload><script> import '@focsd-dev/components/focsd-file-upload.js';</script>
<focsd-file-upload onchange={(e) => console.log(e.target)} label="Attachments" accept=".pdf,image/png" multiple max-size="1000000" hint="PDF or PNG, up to 1 MB"></focsd-file-upload>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
name | name | string | — |
disabled | disabled | boolean | false |
required | required | boolean | false |
readonly | readOnly | boolean | false |
aria-label | hostAriaLabel | string | null | null |
aria-description | hostAriaDescription | string | null | null |
aria-labelledby | hostAriaLabelledBy | string | null | null |
aria-describedby | hostAriaDescribedBy | string | null | null |
label-position | labelPosition | 'top' | 'start' | 'end' | undefined | — |
hint | hint | string | — |
error | error | — | — |
warning | warning | string | undefined | — |
success | success | string | undefined | — |
error-message | errorMessage | string | undefined | — |
warning-message | warningMessage | string | undefined | — |
warning-pattern | warningPattern | string | undefined | — |
label | label | string | — |
accept | accept | string | — |
multiple | multiple | boolean | false |
max-size | maxSize | number | 0 |
capture | capture | string | — |
webkitdirectory | webkitdirectory | boolean | false |
Methods
| Method | Description |
|---|---|
describedByIds() | IDs to add to the inner control's aria-describedby: the hint, then the shown message. |
checkValidity() | True when the value is valid; otherwise fires invalid and returns false, like native. |
reportValidity() | Like checkValidity(), and also shows the browser's validation message when invalid. |
setCustomValidity(message) | Set (non-empty) or clear (empty string) a custom validation error, like native controls. |
setValidityState(flags, message, anchor) | For subclasses: set the validity flags and message. anchor is the element the browser points its message at. |
validate() | Show (or clear) the error from the control's own checks: required, type, pattern, min,
max and the like. Uses error-message, else the browser's localized message. An error the
author set (a server error, say) is left as it is. |
clearValidation() | Clear the error and warning that validate() and warning-pattern showed (a form reset does). |
emitValue(commit) | For subclasses: fire the value events, as a native control does on a user edit: input
(bubbles, composed), then with commit also change (bubbles, stays in its tree). |
idrefText(ids) | For subclasses: the text of the elements an ID reference list names, resolved in the host's tree (ID references cannot cross a shadow root), joined by spaces. |
showPicker() | Open the native file picker from code, like native showPicker(). |
addFiles(list) | Validate and add files; rejected ones are reported, never added. |
removeFile(file) | Remove a selected file and return focus to the choose button. |
Events
| Event | Description |
|---|---|
input | Fired on every user edit of the value. |
change | Fired when the user commits a change to the value or state. |
CSS parts
Style the inside of the component with ::part(), for example focsd-file-upload::part(dragging).
| Part | Description |
|---|---|
dragging | Added to the drop zone while files are dragged over it. |
drop-text | The "or drop files here" text. |
dropzone | The area that accepts dropped files. |
error | One rejected file and why; also matches the control's `error` message (use `rejection` for the file only). |
errors | The list of rejected files and why. |
file | One selected file. |
file-name | A file's name. |
file-size | A file's size. |
files | The list of selected files. |
group | The group element. |
label | The label element. |
layout | The wrapper that places the label beside or above the body. |
body | The drop zone, file lists, hint and message. |
hint | The author's hint. |
message | The shown error, warning or success message (also `error`, `warning` or `success`). |
message-icon | The message's icon. |
announcer | The visually hidden status region. |
rejection | One rejected file and why (never the control's `error` message). |
remove | A file's remove button. |
status | The status element. |
trigger | The trigger element. |
Accessibility
capture,webkitdirectoryandshowPicker()reach the native file picker.- A visible "Choose files" button opens the native picker, so dragging is never required (WCAG 2.5.7).
- Rejected files are reported in a
role="alert"region and never added; a polite status reports "2 files selected". - Each file has a "Remove report.pdf" button; focus returns to the choose button after removal.
- Files cannot be restored after back/forward navigation (browsers do not allow it).
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.