--- title: Dropzone description: "`Dropzone`は、ファイルをドラッグアンドドロップでアップロードするために使用されるコンポーネントです。" links: - style: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/dropzone/dropzone.style.ts - source: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/dropzone - storybook: https://yamada-ui.github.io/yamada-ui?path=/story/components-dropzone--basic --- ```tsx Drag file here or click to select file ``` ## 使い方 ```tsx import { Dropzone } from "@yamada-ui/react" ``` ```tsx import { Dropzone } from "@/components/ui" ``` ```tsx import { Dropzone } from "@workspaces/ui" ``` ```tsx ``` :::note `Dropzone`は、内部で[react-dropzone](https://react-dropzone.js.org/)を使用しています。 ::: ### バリアントを変更する ```tsx {(variant, index) => ( Drag file here or click to select file )} ``` ### サイズを変更する ```tsx {(size, index) => ( Drag file here or click to select file )} ``` ### 複数選択を許容する 複数選択を許容する場合は、`multiple`を`true`に設定します。 ```tsx Drag files here or click to select files ``` ### 拡張子を指定する 拡張子を指定する場合は、`accept`に文字列の配列または、[オブジェクト](https://developer.mozilla.org/ja/docs/Web/API/window/showOpenFilePicker)を設定します。 また、`accept`を簡単に指定できる`IMAGE_ACCEPT_TYPE`や`PDF_ACCEPT_TYPE`などを提供しています。 ```tsx Drag image here or click to select image Drag image here or click to select image ``` ### ファイルサイズの最大値を指定する ファイルサイズの最大値を指定する場合は、`maxSize`に数値(バイト)を設定します。 ```tsx Drag file here or click to select file Attach as many files as you like, each file should not exceed 5mb ``` ### アップロード可能な最大数を指定する アップロード可能な最大数を指定する場合は、`maxFiles`に数値を設定します。 ```tsx Drag file here or click to select file Attach as many files as you like, can upload up to 3 files ``` ### 状態に応じた表示をする ファイルを受け入れたか、拒否されたかの状態に応じて、表示を制御する場合は、`DropzoneAccept`, `DropzoneReject`, `DropzoneIdle`を使用します。 - `DropzoneAccept`: ファイルが受け入れられたときに表示されるコンポーネントです。 - `DropzoneReject`: ファイルが拒否されたときに表示されるコンポーネントです。 - `DropzoneIdle`: アイドル状態のときに表示されるコンポーネントです。 ```tsx Drag images here or click to select files Attach as many files as you like, each file should not exceed 5mb ``` ### ドロップされたイベントをハンドルする ドロップされたイベントをハンドルする場合は、`onDrop`を使用します。 :::warning ドロップされたファイルが受け入れられたか拒否されたかに関係なく、常に呼び出されることに注意してください。 ::: ```tsx console.log("dropped", acceptedFiles, fileRejections) } > Drag images here or click to select files Attach as many files as you like, each file should not exceed 5mb ``` ### 受け入れられたファイルのみをハンドルする 受け入れられたファイルのみをハンドルする場合は、`onDropAccepted`を使用します。 ```tsx console.log("accepted", files)} > Drag images here or click to select files Attach as many files as you like, each file should not exceed 5mb ``` ### 拒否されたファイルのみをハンドルする 拒否されたファイルのみをハンドルする場合は、`onDropRejected`を使用します。 ```tsx console.log("rejected", files)} > Drag images here or click to select files Attach as many files as you like, each file should not exceed 5mb ``` ### ボーダーの色を変更する ボーダーの色を変更する場合は、`focusBorderColor`または`errorBorderColor`に値を設定します。 ```tsx Drag file here or click to select file Drag file here or click to select file ``` ### 無効にする 無効にする場合は、`disabled`を`true`に設定します。 ```tsx Drag file here or click to select file Drag file here or click to select file ``` ### 読み取り専用にする 読み取り専用にする場合は、`readOnly`を`true`に設定します。 ```tsx Drag file here or click to select file Drag file here or click to select file ``` ### 無効な入力にする 無効な入力にする場合は、`invalid`を`true`に設定します。 ```tsx Drag file here or click to select file Drag file here or click to select file ``` ### ローディングアニメーションを使う ローディングアニメーションを使う場合は、`loading`を`true`にします。 ```tsx Drag file here or click to select file Drag file here or click to select file Drag file here or click to select file ``` ### 制御する ```tsx const openRef = useRef<() => void>(noop) return ( Drag file here or click to select file ) ``` ## Props ### Dropzone.Root | Prop | Default | Type | Description | | ----------------------- | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `size` | `"sm"` | `"full" \| "lg" \| "md" \| "sm" \| "xs"` | The size of the component. | | `variant` | `"dashed"` | `"dashed" \| "panel" \| "plain" \| "solid"` | The variant of the component. | | `accept` | - | `Accept \| string[]` | Set accepted file types. | | `autoFocus` | `false` | `boolean` | Set to true to focus the root element on render. | | `disabled` | `false` | `boolean` | If `true`, the field will be disabled. | | `errorBorderColor` | - | `"-moz-initial" \| "ActiveBorder" \| "ActiveCaption" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" \| "amber.400" \| "amber.50" \| "amber.500" ...` | The border color when the input is invalid. | | `focusBorderColor` | - | `"-moz-initial" \| "ActiveBorder" \| "ActiveCaption" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" \| "amber.400" \| "amber.50" \| "amber.500" ...` | The border color when the input is focused. | | `getFilesFromEvent` | - | `DropzoneOptions["getFilesFromEvent"]` | Use this to provide a custom file aggregator. | | `inputProps` | - | `HTMLStyledProps<"input">` | Props for dropzone input element. | | `invalid` | `false` | `boolean` | If `true`, the field will be invalid. | | `loading` | `false` | `boolean` | If `true`, display the dropzone loading icon. | | `loadingProps` | - | `DropzoneLoadingProps` | Props for dropzone loading icon element. | | `loadingScheme` | `"oval"` | `LoadingScheme` | The loading scheme. | | `maxFiles` | `0` | `number` | Maximum accepted number of files. The default value is 0 which means there is no limitation to how many files are accepted. | | `maxSize` | `Infinity` | `number` | Maximum file size (in bytes). | | `minSize` | `0` | `number` | Minimum file size (in bytes). | | `multiple` | `false` | `boolean` | Allow drag and drop (or selection from the file dialog) of multiple files. | | `name` | - | `string` | The HTML `name` attribute used for forms. | | `noClick` | `false` | `boolean` | If true, disables click to open the native file selection dialog. | | `noDrag` | `false` | `boolean` | If true, disables drag and drop. | | `noDragEventsBubbling` | `false` | `boolean` | If true, stops drag event propagation to parents. | | `noKeyboard` | `false` | `boolean` | If true, disables `space` and `enter` to open the native file selection dialog. Note that it also stops tracking the focus state. | | `onDragEnter` | - | `DropzoneOptions["onDragEnter"]` | Callback for when the dragenter event occurs. | | `onDragLeave` | - | `DropzoneOptions["onDragLeave"]` | Callback for when the dragleave event occurs. | | `onDragOver` | - | `DropzoneOptions["onDragOver"]` | Callback for when the dragover event occurs. | | `onDrop` | - | `DropzoneOptions["onDrop"]` | Callback for when the drop event occurs. Note that this callback is invoked after the `getFilesFromEvent` callback is done. | | `onDropAccepted` | - | `DropzoneOptions["onDropAccepted"]` | Callback for when the drop event occurs. Note that if no files are accepted, this callback is not invoked. | | `onDropRejected` | - | `DropzoneOptions["onDropRejected"]` | Callback for when the drop event occurs. Note that if no files are rejected, this callback is not invoked. | | `onError` | - | `DropzoneOptions["onError"]` | Callback for when there's some error from any of the promises. | | `onFileDialogCancel` | - | `DropzoneOptions["onFileDialogCancel"]` | Callback for when closing the file dialog with no selection. | | `onFileDialogOpen` | - | `DropzoneOptions["onFileDialogOpen"]` | Callback for when opening the file dialog. | | `openRef` | - | `RefObject<() => void>` | Ref to a open function. | | `overlayProps` | - | `DropzoneOverlayProps` | Props for dropzone overlay element. | | `preventDropOnDocument` | `true` | `boolean` | If false, allow dropped items to take over the current browser window. | | `readOnly` | `false` | `boolean` | If `true`, the field will be readonly. | | `required` | `false` | `boolean` | If `true`, the field will be required. | | `useFsAccessApi` | `false` | `boolean` | If true, use the [File System API](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API) to read files. | | `validator` | - | `DropzoneOptions["validator"]` | Custom validation function. It must return null if there's no errors. | ### Dropzone.Description | Prop | Default | Type | Description | | ------------- | ------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | ### Dropzone.Icon | Prop | Default | Type | Description | | ------------- | ------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | ### Dropzone.Title | Prop | Default | Type | Description | | ------------- | ------- | ------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | ## アクセシビリティ 現在、v2の移行に伴い、このセクションは更新中です。