Components
File Trigger
Opens the system file picker from any button. Built on react-aria-components, so it works with the mouse, touch, keyboard and screen readers.
Introduction
A native file input is hard to style and easy to get wrong. FileTrigger keeps it out of sight: wrap a Button (or any pressable component) and pressing it opens the file picker, with all the states, sizes and effects of the button you chose.
onSelectreceives a plainFile[], empty when the picker is dismissed.- Restrict types with
acceptedFileTypes, allow several files, pick a whole folder, or open the camera on phones. - Keyboard and screen reader behavior come from the child, so a Button stays focusable and announced as a button.
Installation
centerUI has no CLI or registry yet: components are copied into your project. FileTrigger works with Button, so it needs everything Button needs.
# bun
bun add react-aria-components tailwind-variants tailwind-merge clsx
# pnpm
pnpm add react-aria-components tailwind-variants tailwind-merge clsx
# npm
npm install react-aria-components tailwind-variants tailwind-merge clsxAdd Button first (see its Manual installation), then follow the step below.
Manual installation
Copy this file into your project and keep the path, or adjust the imports.
1. Add the FileTrigger file
src/components/ui/file-trigger/file-trigger.tsxShow code
"use client";
import {
FileTrigger as AriaFileTrigger,
type FileTriggerProps as AriaFileTriggerProps,
} from "react-aria-components";
export interface FileTriggerProps
extends Omit<AriaFileTriggerProps, "onSelect"> {
/** Called with the chosen files. An empty array if the picker is dismissed. */
onSelect?: (files: File[]) => void;
}
/**
* Opens the system file picker when its child is pressed. The child can be any
* pressable component, usually a Button. It renders the child plus a hidden
* `<input type="file">`, so it works with the keyboard and screen readers
* exactly like the child does.
*
* @example
* <FileTrigger acceptedFileTypes={["image/*"]} onSelect={(files) => upload(files)}>
* <Button>Upload image</Button>
* </FileTrigger>
*/
export function FileTrigger({ onSelect, ...props }: FileTriggerProps) {
return (
<AriaFileTrigger
{...props}
onSelect={(files) => onSelect?.(files ? Array.from(files) : [])}
/>
);
}2. Use it
import { Button } from "@/components/ui/button/button";
import { FileTrigger } from "@/components/ui/file-trigger/file-trigger";Anatomy
A trigger is the button you pass in, plus a hidden file input.
<FileTrigger onSelect={(files) => upload(files)}>
<Button>Upload file</Button>
</FileTrigger><button>Upload file</button> <!-- the child you passed -->
<input type="file" hidden /> <!-- rendered by FileTrigger -->| Part | Rendered as | Notes |
|---|---|---|
| Trigger | your child, usually Button | Any pressable component. Pressing it, with a mouse, a finger or the keyboard, opens the picker. |
| File input | hidden input type=file | Rendered next to the trigger. It carries the accepted types, multiple, directory and camera settings. |
Because the hidden input is rendered next to the button, put a FileTrigger in a spaced ButtonGroup (isAttached={false}). In an attached group the input would count as a sibling and throw off the joined corners.
Examples
Live previews with code you can copy. The pickers open real system dialogs.
Usage
No file chosen
Multiple files
Set allowsMultiple to let the user pick several files at once.
No files chosen
Accepted types
Limit the picker to the MIME types or extensions you can handle.
PNG, JPEG or WebP
Directory
acceptDirectory picks a whole folder instead of individual files.
No folder chosen
Camera
defaultCamera asks phones to open the camera. Other devices show the normal picker.
Opens the camera on phones, the picker elsewhere
Styles
The trigger is a regular Button, so every variant, size and icon-only style works.
Disabled
Disable the button and the picker cannot be opened.
Component API
FileTrigger takes the props below. Everything else is the behavior of the button you pass in.
| Prop | Type | Default |
|---|---|---|
| onSelect | (files: File[]) => void | - |
| acceptedFileTypes | readonly string[] | - |
| allowsMultiple | boolean | false |
| acceptDirectory | boolean | false |
| defaultCamera | "user" | "environment" | - |
| children | ReactNode (a pressable, e.g. Button) | - |