Skip to content

File trigger

Pro

Makes any library button open the file picker, or the camera on phones; useFileTrigger does it from a menu item. Not an M3 component (@windmorph/react-pro).

import { FileTrigger, useFileTrigger } from '@windmorph/react-pro';

Part of Windmorph Pro, paid components on the free core, not on sale yet. See the Pro guide for what it includes and to register interest.

Props

FileTrigger 9 props

  • childrentype ReactNodeRequired

    One library button (Button, IconButton, Fab…) or anything reading TriggerContext.

  • inputReftype Ref<HTMLInputElement>

    A ref to the hidden <input type="file">, e.g. to read its files or reset it yourself.

  • accepttype readonly string[]

    MIME types, wildcards (image/*, or * for any file) or extensions the picker offers and takes, e.g. ['image/jpeg', '.pdf'], in any case; an entry may hold several, separated by commas. With no list, any file is taken. A file's type comes from the system and can differ from the one you expect (a CSV is application/vnd.ms-excel where a spreadsheet app owns the extension), so list the extensions too: ['text/csv', '.csv']. It checks the name and the system's type, not the content (a renamed file passes): check files on the server too. A file the system gives no type (an extension it doesn't know) matches only by extension.

  • multipletype booleandefault false

    Lets the person choose several files.

  • capturetype "user" | "environment"

    On phones, opens the front (user) or back (environment) camera instead of the files.

  • directorytype booleandefault false

    Chooses a folder (and everything in it) instead of files, where the browser supports it. The files the system keeps there for itself (.DS_Store, Thumbs.db, any dot file) are left out, neither taken nor reported.

Events

  • onFilePicktype ((pick: FilePick) => void)

    Called once for each pick from the system dialog, with every file it brought: the files taken and the files refused with their reasons, in the order they came (accepted is empty when every file was refused, rejected when none was). Called first, then onFileAccept and onFileReject, all in the same task, so an app can treat a pick as one thing (add what fits, and say once what was left out). A pick that brings no files (a closed dialog) calls nothing.

  • onFileAccepttype ((files: File[]) => void)

    Called with the chosen files that match accept; never with an empty list.

  • onFileRejecttype ((rejections: FileRejection[]) => void)

    Called with the chosen files that don't match accept, each with its reason (type), after onFileAccept; never with an empty list. accept is only a hint to the system dialog (a person can pick any file there, and a folder's contents ignore it), so such a file never reaches onFileAccept.

Hooks

useFileTrigger

useFileTrigger signature
useFileTrigger(options: FileTriggerOptions, inputRef?: Ref<HTMLInputElement>): FileTriggerResult

The file picker for a trigger that can't be wrapped in FileTrigger, such as a menu item's action: open() opens it and input is the hidden input that does the work. Not an M3 component (Windmorph Pro, docs/plans/shaped-icon-skip-link-file-trigger.md).

  • optionstype FileTriggerOptionsRequired

    What to accept and what to do with the chosen files.

  • options.onFilePicktype (pick: FilePick) => void

    Called once for each pick from the system dialog, with every file it brought: the files taken and the files refused with their reasons, in the order they came (accepted is empty when every file was refused, rejected when none was). Called first, then onFileAccept and onFileReject, all in the same task, so an app can treat a pick as one thing (add what fits, and say once what was left out). A pick that brings no files (a closed dialog) calls nothing.

  • options.onFileAccepttype (files: File[]) => void

    Called with the chosen files that match accept; never with an empty list.

  • options.onFileRejecttype (rejections: FileRejection[]) => void

    Called with the chosen files that don't match accept, each with its reason (type), after onFileAccept; never with an empty list. accept is only a hint to the system dialog (a person can pick any file there, and a folder's contents ignore it), so such a file never reaches onFileAccept.

  • options.accepttype readonly string[]

    MIME types, wildcards (image/*, or * for any file) or extensions the picker offers and takes, e.g. ['image/jpeg', '.pdf'], in any case; an entry may hold several, separated by commas. With no list, any file is taken. A file's type comes from the system and can differ from the one you expect (a CSV is application/vnd.ms-excel where a spreadsheet app owns the extension), so list the extensions too: ['text/csv', '.csv']. It checks the name and the system's type, not the content (a renamed file passes): check files on the server too. A file the system gives no type (an extension it doesn't know) matches only by extension.

  • options.multipletype booleandefault false

    Lets the person choose several files.

  • options.capturetype "user" | "environment"

    On phones, opens the front (user) or back (environment) camera instead of the files.

  • options.directorytype booleandefault false

    Chooses a folder (and everything in it) instead of files, where the browser supports it. The files the system keeps there for itself (.DS_Store, Thumbs.db, any dot file) are left out, neither taken nor reported.

  • inputReftype Ref<HTMLInputElement>

    A ref to the hidden <input type="file">, e.g. to read its files or reset it yourself; it's merged with the hook's own ref.

Returns FileTriggerResult

  • opentype () => voidRequired

    Opens the file picker. Call it from a press or key handler: browsers need a user action.

  • inputtype React.ReactElement<unknown, string | React.JSXElementConstructor<any>>Required

    The hidden file input. Render it once, anywhere near the trigger.

Spec status

Not part of Material Design 3: a Windmorph Pro component, built on M3's colour roles, type scale, shapes and motion. Its variants and configurations each link to a live example.

  • Any library button as the trigger

    Built

    Configuration · Photo

  • File types (accept)

    Built

    Configuration · Photo, Folder

  • The camera on phones (capture)

    Built

    Configuration · Photo

  • Several files or a folder (multiple, directory)

    Built

    Configuration · Folder

  • From a menu item (useFileTrigger)

    Built

    Use case · Menu

Options

  • onFileAccept gets the chosen files that match accept as an array, never empty. Choosing the same file again still calls it.
  • accept lists MIME types, wildcards or extensions, in any case. List the extensions beside the MIME types (['text/csv', '.csv']), since a file's type comes from the system and can differ from the one you expect (a CSV is application/vnd.ms-excel where a spreadsheet app owns it). * or */* takes any file, as does no list, and an entry may hold several types, comma-separated. The system dialog only hints at it (a person can pick any file there, and a folder's contents ignore it), so a chosen file off it never reaches onFileAccept. onFileReject(rejections) gets the files a pick brought that weren't taken, after onFileAccept and never with an empty list, each with its reason (type, off accept), so you can say why a file didn't appear. Check the real type on the server too, since a file's name and type can lie.
  • onFilePick(pick) is called once for each pick that brings files, with all of them: accepted, the files taken, and rejected, the files refused with their reasons, each list in the order the files came (accepted is empty when every file was refused). It runs first, then onFileAccept and onFileReject, all in the same task, so you can treat a pick as one thing: add what fits and say once what was left out. A closed dialog calls nothing. It is enough on its own to take files, and each callback gets its own copy of the lists. Its type, FilePick, comes from @windmorph/react-pro.
  • multiple allows several files, capture opens the front (user) or back (environment) camera on phones, and directory chooses a folder where the browser supports it, leaving out the files the system keeps there for itself (.DS_Store, Thumbs.db, dot files), neither taken nor reported.
  • useFileTrigger(options) returns { open, input } for triggers that can't be wrapped, such as a menu item: call open() from its action and render input once.