# FileTrigger (/docs/file-trigger)





`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`](/docs/file-upload).

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

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

## Playground [#playground]

<story.Playground />

## Basic [#basic]

By default `FileTrigger` renders a kit [`Button`](/docs/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.

<Preview code="&#x22;const [files, setFiles] = useState<File[]>([]);\n\n<FileTrigger startSlot={<Upload />} onSelect={setFiles}>\n  Choose a file\n</FileTrigger>&#x22;">
  <FileTriggerBasicDemo />
</Preview>

## Any element as the trigger [#any-element-as-the-trigger]

With `asChild`, the child becomes the trigger: an [`ActionIcon`](/docs/action-icon), 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`.

<Preview align="block" code="&#x22;<FileTrigger asChild onSelect={setFiles}>\n  <ActionIcon variant=\&#x22;subtle\&#x22; color=\&#x22;neutral\&#x22; aria-label=\&#x22;Attach a file\&#x22;>\n    <Paperclip />\n  </ActionIcon>\n</FileTrigger>&#x22;">
  <FileTriggerActionIconDemo />
</Preview>

## Accepting images [#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.

<Preview code="&#x22;<FileTrigger accept=\&#x22;image/*\&#x22; onSelect={([file]) => setFile(file)}>\n  Choose an image\n</FileTrigger>&#x22;">
  <FileTriggerImagesDemo />
</Preview>

## Multiple files [#multiple-files]

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

<Preview code="&#x22;<FileTrigger multiple onSelect={setFiles}>\n  Choose files\n</FileTrigger>&#x22;">
  <FileTriggerMultipleDemo />
</Preview>

## Taking a photo on a phone [#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.

<Preview code="&#x22;<FileTrigger accept=\&#x22;image/*\&#x22; capture=\&#x22;environment\&#x22; onSelect={pick}>\n  Take photo\n</FileTrigger>\n<FileTrigger accept=\&#x22;image/*\&#x22; variant=\&#x22;outline\&#x22; onSelect={pick}>\n  Choose from library\n</FileTrigger>&#x22;">
  <FileTriggerCameraDemo />
</Preview>

<Callout type="info" title="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.
</Callout>

## Disabled [#disabled]

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

<Preview code="&#x22;<FileTrigger disabled>Choose a file</FileTrigger>\n\n<FileTrigger asChild disabled>\n  <ActionIcon variant=\&#x22;subtle\&#x22; color=\&#x22;neutral\&#x22; aria-label=\&#x22;Attach a file\&#x22;>\n    <Paperclip />\n  </ActionIcon>\n</FileTrigger>&#x22;">
  <FileTrigger testId="disabled-button">
    Choose a file
  </FileTrigger>

  <FileTrigger testId="disabled-icon">
    <ActionIcon variant="subtle" color="neutral" aria-label="Attach a file (disabled)">
      <Paperclip />
    </ActionIcon>
  </FileTrigger>
</Preview>

## Forms [#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`.

<Preview align="block" code="&#x22;<form action={send}>\n  <FileTrigger name=\&#x22;attachment\&#x22; onSelect={setPicked}>\n    Attach\n  </FileTrigger>\n  <Button type=\&#x22;submit\&#x22;>Send</Button>\n</form>&#x22;">
  <FileTriggerFormDemo />
</Preview>

## API [#api]

<TypeTable
  type="{
  onSelect: {
    type: '(files: File[]) => void',
    description: 'Fires with the picked files.',
  },
  accept: {
    type: 'string | string[]',
    description: 'Accepted types: MIME types, globs or extensions. A list is joined with commas.',
  },
  capture: {
    type: '&#x22;user&#x22; | &#x22;environment&#x22;',
    description: &#x22;Opens a phone's front or back camera instead of the library. Desktop browsers ignore it.&#x22;,
  },
  multiple: { type: 'boolean', default: 'false', description: 'Allows picking several files at once.' },
  name: {
    type: 'string',
    description: 'Puts the hidden input in form submission. The input then keeps its files instead of being cleared after each pick.',
  },
  disabled: { type: 'boolean', default: 'false', description: 'Disables the trigger and the input.' },
  asChild: {
    type: 'boolean',
    default: 'false',
    description: &#x22;Makes the single child the trigger. Only behaviour is merged onto it, not Button's styling.&#x22;,
  },
  children: { type: 'ReactNode', description: 'The label, or the child element under asChild.' },
  variant: { type: '&#x22;filled&#x22; | &#x22;light&#x22; | &#x22;outline&#x22; | &#x22;subtle&#x22;', default: '&#x22;filled&#x22;', description: 'Button variant. Ignored under asChild.' },
  color: { type: 'Color', description: 'Button palette. Ignored under asChild.' },
  size: { type: '&#x22;xs&#x22; | &#x22;sm&#x22; | &#x22;md&#x22; | &#x22;lg&#x22; | &#x22;xl&#x22;', default: '&#x22;md&#x22;', description: 'Button size. Ignored under asChild.' },
  testId: {
    type: 'string',
    description: 'Renders as data-testid on the trigger, never on the hidden input. See [Testing](/docs/testing).',
  },
}"
/>

`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 [#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](#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 [#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.
