|Kit

FileTrigger

A button that opens the file picker, or a phone's camera, and hands you the picked files. No dropzone and no file list, so any Button or ActionIcon can become one.

FileTrigger is a button that opens the operating system's file picker. On a phone, it can open the camera instead. It renders a hidden <input type="file"> beside the button and hands the picked files to onSelect, as a File[]. What happens next is up to you: show a preview, upload, crop.

It has no dropzone and no file list. For a complete field with drag and drop, thumbnails and a remove button per file, use FileUpload.

import { FileTrigger } from '@42-central/ui-react/file-trigger';

<FileTrigger accept="image/*" onSelect={([file]) => setAvatar(file)}>
  Choose a photo
</FileTrigger>

Playground

Component

Loading preview

Props

Code

<Component size="md">
  Choose a file
</Component>

Basic

By default FileTrigger renders a kit Button, and takes all of its props: variant, color, size, startSlot, loading. The children are the label.

After each pick, the input is cleared. Picking the same file a second time fires onSelect again: try it below, the counter goes up.

No file picked yet.

onSelect fired 0 times

Code

const [files, setFiles] = useState<File[]>([]);

<FileTrigger startSlot={<Upload />} onSelect={setFiles}>
  Choose a file
</FileTrigger>

Any element as the trigger

With asChild, the child becomes the trigger: an ActionIcon, a Menu.Item, your own button. FileTrigger adds the behaviour only (the click, disabled, testId). It doesn't add Button's styling, so style the child itself. The child's own onClick still runs.

An icon-only trigger has no text, so give it an aria-label.

Attach a file to the message.

Code

<FileTrigger asChild onSelect={setFiles}>
  <ActionIcon variant="subtle" color="neutral" aria-label="Attach a file">
    <Paperclip />
  </ActionIcon>
</FileTrigger>

Accepting images

accept narrows what the picker offers. It takes what the native attribute takes: MIME types, globs or extensions, as one string or a list (['image/png', '.jpg']). It's a hint for the picker, not a check: a user can still choose "All files" on some systems, so validate the file you receive.

PNG, JPG, GIF or WebP.

Code

<FileTrigger accept="image/*" onSelect={([file]) => setFile(file)}>
  Choose an image
</FileTrigger>

Multiple files

multiple lets the picker return several files at once, in one onSelect call.

Pick several files with Ctrl, ⌘ or Shift.

Code

<FileTrigger multiple onSelect={setFiles}>
  Choose files
</FileTrigger>

Taking a photo on a phone

capture asks a phone for its camera instead of its photo library: "environment" for the back camera, "user" for the front one. The photo comes back through onSelect like any other file.

Offer two triggers, not one. On Android 14 and 15, Chrome can leave the camera out of the chooser when capture is absent, so a single "Choose a photo" button may give the user no way to take one. And with capture, there's no way to pick an existing photo. Give each path its own button: "Take photo" with capture, "Choose from library" without.

On a desktop, both open the file picker.

Code

<FileTrigger accept="image/*" capture="environment" onSelect={pick}>
  Take photo
</FileTrigger>
<FileTrigger accept="image/*" variant="outline" onSelect={pick}>
  Choose from library
</FileTrigger>
Desktop browsers ignore capture

On a laptop or desktop, capture does nothing and both buttons open the file picker. To take a photo with a webcam inside the page, you need a camera stream rather than a file input.

Disabled

disabled disables the trigger and the input. Under asChild the child gets disabled too, and a click on it opens nothing.

Code

<FileTrigger disabled>Choose a file</FileTrigger>

<FileTrigger asChild disabled>
  <ActionIcon variant="subtle" color="neutral" aria-label="Attach a file">
    <Paperclip />
  </ActionIcon>
</FileTrigger>

Forms

Give FileTrigger a name and the hidden input joins the surrounding <form>: FormData carries the picked files, with no state of your own. A named input keeps its files instead of being cleared, because it now holds the form's value. Picking the same file again then doesn't fire onSelect, since nothing changed.

The input is hidden, so show what was picked yourself, from onSelect.

No file picked yet.

Code

<form action={send}>
  <FileTrigger name="attachment" onSelect={setPicked}>
    Attach
  </FileTrigger>
  <Button type="submit">Send</Button>
</form>

API

Prop

Type

loading, startSlot, endSlot and every other Button prop are passed through to the default trigger. A caller's onClick runs before the picker opens: call event.preventDefault() in it to keep the picker shut.

Accessibility

  • One element to reach. The trigger is a real button (or your element, under asChild), named by its children. The file input is hidden, tabIndex={-1} and aria-hidden, so a keyboard user tabs to the button only and a screen reader never announces a second, unlabelled control.
  • Keyboard. Enter and Space on the button open the picker, like a click.
  • Disabled. disabled disables the button and the input. Under asChild, the child gets disabled and aria-disabled, and the click is ignored even when the child is an element disabled doesn't affect.
  • Icon-only triggers need an aria-label, as in Any element as the trigger.
  • After a pick, say what happened. The picker closes without any announcement. Show the picked file's name, or move focus to the next step, so the user knows it worked.
  • Right-to-left. The trigger is a Button, which uses logical properties only.

Internationalization

FileTrigger renders no text of its own, so it has no translations prop. The label is your children, and an icon-only trigger's name is your aria-label: translate them like the rest of the page. The picker and camera screens are the operating system's, in the user's own language.

Changelog

v0.20.0
  • New FileTrigger: a button, or any element through asChild, that opens the file picker and calls onSelect(files), with accept, multiple and capture; picking the same file twice fires twice

    #358

On this page