dotUI
dotUI
beta
  1. Components
  2. Buttons
  3. File Trigger

File Trigger

Allows a user to access the file system with any pressable aria component.

<FileTrigger>
  <Button prefix={<UploadIcon />}>Upload</Button>
</FileTrigger>

Installation

Install the following dependencies:

npm install react-aria-components

Copy and paste the following code into your project.

"use client";

import { FileTrigger, type FileTriggerProps } from "react-aria-components";

export type { FileTriggerProps };
export { FileTrigger };

Update the import paths to match your project setup.

Usage

A FileTrigger wraps around a pressable child such as a button, and includes a visually hidden input element that allows the user to select files from their device.

Options

Accepted file types

By default, the file trigger will accept any file type. To support only certain file types, pass an array of the mime type of files via the acceptedFileTypes prop.

<FileTrigger acceptedFileTypes={["image/*"]}>
  <Button prefix={<UploadIcon />}>Upload</Button>
</FileTrigger>

Multiple files

A file trigger can accept multiple files by passsing the allowsMultiple prop.

<FileTrigger allowMultiple>
  <Button prefix={<UploadIcon />}>Upload</Button>
</FileTrigger>

Directory selection

To enable selecting directories instead of files, use the acceptDirectory property.
This reflects the webkitdirectory HTML attribute and allows users to select directories and their contents. Please note that support for this feature varies from browser to browser.

<FileTrigger acceptDirectory>
  <Button prefix={<UploadIcon />}>Upload</Button>
</FileTrigger>

Media capture

To specify the media capture mechanism to capture media on the spot, pass user for the user-facing camera or environment for the outward-facing camera via the defaultCamera prop.
This behavior only works on mobile devices. On desktop devices, it will open the file system like normal. Read more about capture.

<FileTrigger defaultCamera="environment">
  <Button prefix={<UploadIcon />}>Upload</Button>
</FileTrigger>

API Reference

PropTypeDescription
children
ReactNode
The children of the component.
acceptedFileTypes
Array<string>
Specifies what mime type of files are allowed.
allowsMultiple
boolean
Whether multiple files can be selected.
defaultCamera
'user' | 'environment'
Specifies the use of a media capture mechanism to capture the media on the spot.
acceptDirectory
boolean
Enables the selection of directories instead of individual files.
EventTypeDescription
onSelect
(files: FileList | null) => void
Handler when a user selects a file.

Accessibility

Keyboard interactions

KeyDescription
Space Enter
Activates the trigger.

Built by mehdibha. The source code is available on GitHub.