Single file uploader
A compact row with a file picker and a progress fill, for forms that need exactly one file and run the upload in the host.
This sample has no status, so the component simulates an upload on its own after you pick a file. Nothing is sent anywhere. In the product, always control status and progress.
When to use
- A form field that needs one file, such as an invoice PDF or a receipt.
- When the file name and the progress should fit in one line.
When not to use
- Several files, or a drop zone with a file list. Use File upload cards or File upload table.
- A large, prominent upload area. The component has no drop zone; the host can add drag and drop around it.
- Sending the file itself. It only captures the file and reports it through
onFileSelect.
States
Control the row with status: idle, uploading, completed and error.
Idle
Uploading
The fill grows with progress (capped at 99% until the status is completed). The button becomes Cancel.
Completed
The fill is complete and the button is disabled.
Error
Disabled
| Status | Button text | Button |
|---|---|---|
idle |
actionLabels.upload |
Enabled |
uploading |
actionLabels.cancel |
Enabled |
completed |
actionLabels.uploaded |
Disabled |
error |
actionLabels.upload, plus errorMessage below the row |
Enabled |
Content
- The label starts as the field name (“Comprovante de pagamento”) and becomes the file name once chosen.
- Button text names the action: “Selecionar arquivo”, “Substituir arquivo”. While uploading, show the percentage in the cancel text: “Enviando 45%”, as the app does.
- Error messages say what is wrong and what to do: “O arquivo excede 15 MB. Escolha um PDF menor.”
Do and don’t
Accessibility
- The picker is a native
input type="file"hidden withdisplay: none; the visibleButtoncallsclick()on it, so the button is the keyboard control (Tab, thenEnterorSpace). - The input accepts the standard props (
accept,name,id,aria-*) through the rest props. Use them to name it for forms. - Errors appear as text with an icon, not only in color. They are not announced automatically (see Known issues).
- Validation of type and size is the host’s job: the component does not check the file.
Code
import { SingleFileUploader } from "@repo/ui/single-file-uploader";
<SingleFileUploader
accept="application/pdf,.pdf"
actionLabels={{
cancel: t("uploader.progress", { percent }),
upload: t("uploader.pick"),
uploaded: t("uploader.replace"),
}}
errorMessage={error}
label={file ? file.name : t("uploader.empty")}
onFileSelect={handleFile}
progress={percent}
status={status}
/>;
Props
SingleFileUploader| Prop | Type | Default | Description |
|---|---|---|---|
accept | string | — | Value of the input `accept` attribute |
actionLabels | { upload: string; cancel: string; uploaded: string } | — | Button copy for each status: `upload` (idle and error), `cancel` (uploading), `uploaded` (completed) |
disabled | boolean | — | Disables the button and the input |
errorMessage | string | — | Shown below the row when `status` is `error` |
label | string | — | Text on the left of the row. Usually the field name or the file name |
onCancel | () => void | — | Called when the user cancels while uploading |
onFileSelect | (file: File) => void | — | Called with the chosen file. Start the transfer here |
onUploadComplete | () => void | — | Called when the built-in simulated upload ends (only when `status` is not controlled) |
progress | number | — | Percentage from 0 to 100 shown while uploading |
status | "idle" | "uploading" | "completed" | "error" | "idle" (internal) | Controls the row. Leave it out only in prototypes: the component then simulates an upload |
The component also accepts the other props of input (except onChange and type), and className for the row. ref points to the input.
Known issues
| Issue | Effect |
|---|---|
When status is not passed, the component runs its own fake upload: it counts progress to 100 every 10ms and calls onUploadComplete. |
A forgotten status makes the row look uploaded without any transfer. |
The progress fill is bg-green-400/20 (and /30 when completed): a hardcoded color with transparency. |
It does not follow the theme tokens and goes against the solid colors rule. |
The error state adds border-critical but the row has no border width. |
The row shows no red border. Only the message below signals the error. |
The error message has no role="alert" and the X icon is not aria-hidden. |
Screen readers do not announce the failure when it appears. |
The progress fill has no progressbar role or value. |
Assistive technology cannot read the percentage. Put it in the button text, as the app does. |
cancel resets the internal state only; with a controlled status the host must change it. |
If the host ignores onCancel, the row keeps showing Cancel. |