File trigger API
Allows a user to access the file system with any pressable Cimpress UI component. The component doesn’t provide built-in UI for displaying selected files, it offers an onSelect callback that provides the selected FileList, allowing you to build custom file previews or upload interfaces.
Import
Section titled “Import”import { FileTrigger } from '@cimpress-ui/react';Example
Section titled “Example”No files selected
import { Button, FileTrigger, Stack, Text } from '@cimpress-ui/react';import { useState } from 'react';
export default function Demo() { const [files, setFiles] = useState<File[] | null>(null);
return ( <Stack gap={16}> <FileTrigger onSelect={(fileList) => setFiles(Array.from(fileList ?? []))} allowsMultiple> <Button>Upload</Button> </FileTrigger> <Text as="p"> {files == null || files.length === 0 ? 'No files selected' : files.map((file) => file.name).join(', ')} </Text> </Stack> );}Icon button
Section titled “Icon button”import { FileTrigger, IconButton } from '@cimpress-ui/react';import { IconAttachment } from '@cimpress-ui/react/icons';
export default function Demo() { return ( <FileTrigger> <IconButton icon={<IconAttachment />} aria-label="Upload" variant="tertiary" /> </FileTrigger> );}Accepted file types
Section titled “Accepted file types”import { Button, FileTrigger } from '@cimpress-ui/react';
export default function Demo() { return ( <FileTrigger allowsMultiple acceptedFileTypes={['image/*']}> <Button>Select images</Button> </FileTrigger> );}API reference
Section titled “API reference”- Ref<HTMLInputElement>
-
The
reftype for this component.
FileTriggerProps
- readonly string[]
acceptedFileTypes
Section titled “ acceptedFileTypes ” -
Specifies what mime type of files are allowed.
- boolean
allowsMultiple
Section titled “ allowsMultiple ” -
Whether multiple files can be selected.
- 'user' | 'environment'
defaultCamera
Section titled “ defaultCamera ” -
Specifies the use of a media capture mechanism to capture the media on the spot.
- (files: FileList | null) => void
onSelect
Section titled “ onSelect ” -
Handler when a user selects a file.
- ReactNode
children
Section titled “ children ” -
The children of the component.
- boolean
acceptDirectory
Section titled “ acceptDirectory ” -
Enables the selection of directories instead of individual files.