Dropzone
Dropzoneは、ファイルをドラッグアンドドロップでアップロードするために使用されるコンポーネントです。
Drag file here or click to select file
<Dropzone.Root>
<Text>Drag file here or click to select file</Text>
</Dropzone.Root>
使い方
import { Dropzone } from "@yamada-ui/react"
import { Dropzone } from "@/components/ui"
import { Dropzone } from "@workspaces/ui"
<Dropzone.Root>
<Dropzone.Title />
<Dropzone.Description />
<Dropzone.Accept>
<Dropzone.Icon />
</Dropzone.Accept>
<Dropzone.Reject>
<Dropzone.Icon />
</Dropzone.Reject>
<Dropzone.Idle>
<Dropzone.Icon />
</Dropzone.Idle>
</Dropzone.Root>
Dropzoneは、内部でreact-dropzoneを使用しています。バリアントを変更する
Drag file here or click to select file
Drag file here or click to select file
Drag file here or click to select file
<VStack>
<For each={["dashed", "solid", "panel"]}>
{(variant, index) => (
<Dropzone.Root key={index} variant={variant}>
<Text>Drag file here or click to select file</Text>
</Dropzone.Root>
)}
</For>
</VStack>
サイズを変更する
Drag file here or click to select file
Drag file here or click to select file
Drag file here or click to select file
Drag file here or click to select file
<VStack>
<For each={["xs", "sm", "md", "lg"]}>
{(size, index) => (
<Dropzone.Root key={index} size={size}>
<Text>Drag file here or click to select file</Text>
</Dropzone.Root>
)}
</For>
</VStack>
複数選択を許容する
複数選択を許容する場合は、multipleをtrueに設定します。
Drag files here or click to select files
<Dropzone.Root multiple>
<Text>Drag files here or click to select files</Text>
</Dropzone.Root>
拡張子を指定する
拡張子を指定する場合は、acceptに文字列の配列または、オブジェクトを設定します。
また、acceptを簡単に指定できるIMAGE_ACCEPT_TYPEやPDF_ACCEPT_TYPEなどを提供しています。
Drag image here or click to select image
Drag image here or click to select image
<VStack>
<Dropzone.Root accept={Dropzone.IMAGE_ACCEPT_TYPE}>
<Text>Drag image here or click to select image</Text>
</Dropzone.Root>
<Dropzone.Root accept={{ "image/*": [] }}>
<Text>Drag image here or click to select image</Text>
</Dropzone.Root>
</VStack>
ファイルサイズの最大値を指定する
ファイルサイズの最大値を指定する場合は、maxSizeに数値(バイト)を設定します。
Drag file here or click to select file
Attach as many files as you like, each file should not exceed 5mb
<Dropzone.Root maxSize={3 * 1024 ** 2}>
<VStack alignItems="center" gap="xs">
<Dropzone.Title>Drag file here or click to select file</Dropzone.Title>
<Dropzone.Description>
Attach as many files as you like, each file should not exceed 5mb
</Dropzone.Description>
</VStack>
</Dropzone.Root>
アップロード可能な最大数を指定する
アップロード可能な最大数を指定する場合は、maxFilesに数値を設定します。
Drag file here or click to select file
Attach as many files as you like, can upload up to 3 files
<Dropzone.Root maxFiles={3} multiple>
<VStack alignItems="center" gap="xs">
<Dropzone.Title>Drag file here or click to select file</Dropzone.Title>
<Dropzone.Description>
Attach as many files as you like, can upload up to 3 files
</Dropzone.Description>
</VStack>
</Dropzone.Root>
状態に応じた表示をする
ファイルを受け入れたか、拒否されたかの状態に応じて、表示を制御する場合は、DropzoneAccept, DropzoneReject, DropzoneIdleを使用します。
DropzoneAccept: ファイルが受け入れられたときに表示されるコンポーネントです。DropzoneReject: ファイルが拒否されたときに表示されるコンポーネントです。DropzoneIdle: アイドル状態のときに表示されるコンポーネントです。
Drag images here or click to select files
Attach as many files as you like, each file should not exceed 5mb
<Dropzone.Root accept={Dropzone.IMAGE_ACCEPT_TYPE} maxSize={3 * 1024 ** 2}>
<HStack>
<Dropzone.Accept>
<Dropzone.Icon as={UploadIcon} />
</Dropzone.Accept>
<Dropzone.Reject>
<Dropzone.Icon as={XIcon} />
</Dropzone.Reject>
<Dropzone.Idle>
<Dropzone.Icon as={ImageIcon} />
</Dropzone.Idle>
<VStack gap="xs">
<Dropzone.Title>Drag images here or click to select files</Dropzone.Title>
<Dropzone.Description>
Attach as many files as you like, each file should not exceed 5mb
</Dropzone.Description>
</VStack>
</HStack>
</Dropzone.Root>
ドロップされたイベントをハンドルする
ドロップされたイベントをハンドルする場合は、onDropを使用します。
Drag images here or click to select files
Attach as many files as you like, each file should not exceed 5mb
<Dropzone.Root
accept={Dropzone.IMAGE_ACCEPT_TYPE}
maxSize={3 * 1024 ** 2}
onDrop={(acceptedFiles, fileRejections) =>
console.log("dropped", acceptedFiles, fileRejections)
}
>
<HStack>
<Dropzone.Accept>
<Dropzone.Icon as={UploadIcon} />
</Dropzone.Accept>
<Dropzone.Reject>
<Dropzone.Icon as={XIcon} />
</Dropzone.Reject>
<Dropzone.Idle>
<Dropzone.Icon as={ImageIcon} />
</Dropzone.Idle>
<VStack gap="xs">
<Dropzone.Title>Drag images here or click to select files</Dropzone.Title>
<Dropzone.Description>
Attach as many files as you like, each file should not exceed 5mb
</Dropzone.Description>
</VStack>
</HStack>
</Dropzone.Root>
受け入れられたファイルのみをハンドルする
受け入れられたファイルのみをハンドルする場合は、onDropAcceptedを使用します。
Drag images here or click to select files
Attach as many files as you like, each file should not exceed 5mb
<Dropzone.Root
accept={Dropzone.IMAGE_ACCEPT_TYPE}
maxSize={3 * 1024 ** 2}
onDropAccepted={(files) => console.log("accepted", files)}
>
<HStack>
<Dropzone.Accept>
<Dropzone.Icon as={UploadIcon} />
</Dropzone.Accept>
<Dropzone.Reject>
<Dropzone.Icon as={XIcon} />
</Dropzone.Reject>
<Dropzone.Idle>
<Dropzone.Icon as={ImageIcon} />
</Dropzone.Idle>
<VStack gap="xs">
<Dropzone.Title>Drag images here or click to select files</Dropzone.Title>
<Dropzone.Description>
Attach as many files as you like, each file should not exceed 5mb
</Dropzone.Description>
</VStack>
</HStack>
</Dropzone.Root>
拒否されたファイルのみをハンドルする
拒否されたファイルのみをハンドルする場合は、onDropRejectedを使用します。
Drag images here or click to select files
Attach as many files as you like, each file should not exceed 5mb
<Dropzone.Root
accept={Dropzone.IMAGE_ACCEPT_TYPE}
maxSize={3 * 1024 ** 2}
onDropRejected={(files) => console.log("rejected", files)}
>
<HStack>
<Dropzone.Accept>
<Dropzone.Icon as={UploadIcon} />
</Dropzone.Accept>
<Dropzone.Reject>
<Dropzone.Icon as={XIcon} />
</Dropzone.Reject>
<Dropzone.Idle>
<Dropzone.Icon as={ImageIcon} />
</Dropzone.Idle>
<VStack gap="xs">
<Dropzone.Title>Drag images here or click to select files</Dropzone.Title>
<Dropzone.Description>
Attach as many files as you like, each file should not exceed 5mb
</Dropzone.Description>
</VStack>
</HStack>
</Dropzone.Root>
ボーダーの色を変更する
ボーダーの色を変更する場合は、focusBorderColorまたはerrorBorderColorに値を設定します。
Drag file here or click to select file
Drag file here or click to select file
<VStack>
<Dropzone.Root focusBorderColor="green.500">
<Text>Drag file here or click to select file</Text>
</Dropzone.Root>
<Dropzone.Root errorBorderColor="orange.500" invalid>
<Text>Drag file here or click to select file</Text>
</Dropzone.Root>
</VStack>
無効にする
無効にする場合は、disabledをtrueに設定します。
Drag file here or click to select file
Drag file here or click to select file
<VStack>
<Dropzone.Root disabled>
<Text>Drag file here or click to select file</Text>
</Dropzone.Root>
<Field.Root disabled label="Upload file">
<Dropzone.Root>
<Text>Drag file here or click to select file</Text>
</Dropzone.Root>
</Field.Root>
</VStack>
読み取り専用にする
読み取り専用にする場合は、readOnlyをtrueに設定します。
Drag file here or click to select file
Drag file here or click to select file
<VStack>
<Dropzone.Root readOnly>
<Text>Drag file here or click to select file</Text>
</Dropzone.Root>
<Field.Root label="Upload file" readOnly>
<Dropzone.Root>
<Text>Drag file here or click to select file</Text>
</Dropzone.Root>
</Field.Root>
</VStack>
無効な入力にする
無効な入力にする場合は、invalidをtrueに設定します。
Drag file here or click to select file
Drag file here or click to select file
<VStack>
<Dropzone.Root invalid>
<Text>Drag file here or click to select file</Text>
</Dropzone.Root>
<Field.Root errorMessage="File is required." invalid label="Upload file">
<Dropzone.Root>
<Text>Drag file here or click to select file</Text>
</Dropzone.Root>
</Field.Root>
</VStack>
ローディングアニメーションを使う
ローディングアニメーションを使う場合は、loadingをtrueにします。
Drag file here or click to select file
Drag file here or click to select file
Drag file here or click to select file
<VStack>
<Dropzone.Root loading>
<Text>Drag file here or click to select file</Text>
</Dropzone.Root>
<Dropzone.Root loading loadingScheme="grid">
<Text>Drag file here or click to select file</Text>
</Dropzone.Root>
<Dropzone.Root loading loadingProps={{ color: "blue" }}>
<Text>Drag file here or click to select file</Text>
</Dropzone.Root>
</VStack>
制御する
Drag file here or click to select file
const openRef = useRef<() => void>(noop)
return (
<VStack>
<Button onClick={() => openRef.current()}>Select file</Button>
<Dropzone.Root openRef={openRef}>
<Text>Drag file here or click to select file</Text>
</Dropzone.Root>
</VStack>
)
Props
アクセシビリティ
Field.Rootを使用しない場合は、Dropzone.Rootにaria-labelまたはaria-labelledbyを設定します。
<Dropzone.Root aria-label="Upload file">
<Text>Drag file here or click to select file</Text>
</Dropzone.Root>
<VStack gap="sm">
<Text as="h3" id="label">
Upload file
</Text>
<Dropzone.Root aria-labelledby="label">
<Text>Drag file here or click to select file</Text>
</Dropzone.Root>
</VStack>
キーボード操作
| キー | 説明 | 状態 |
|---|---|---|
Space, Enter | ファイル選択ダイアログを開きます。 | - |
ARIAロールと属性
| コンポーネント | ロールと属性 | 使い方 |
|---|---|---|
Dropzone.Root | role="presentation" | プレゼンテーションであることを示します。 |
aria-disabled | disabled、readOnly、またはloadingが設定されている場合は"true"を設定します。 | |
input | aria-labelledby | 関連したDropzone.Rootのidを設定します。 |
aria-disabled | disabledが設定されている場合は"true"を設定します。 | |
aria-invalid | invalidが設定されている場合は"true"を設定します。 | |
aria-readonly | readOnlyが設定されている場合は"true"を設定します。 | |
aria-required | requiredが設定されている場合は"true"を設定します。 | |
aria-describedby | Dropzone.RootがField.Root内にあり、Field.RootにerrorMessageまたはhelperMessage、もしくはField.ErrorMessageまたはField.HelperMessageが設定されている場合は、そのidを設定します。 |
類似のコンポーネント
FileInput
FileInputは、ユーザーがファイルを選択するために使用されるコンポーネントです。
FileButton
FileButtonは、ユーザーがファイルを選択するために使用されるボタンのコンポーネントです。
Textarea
Textareaは、複数行のテキスト入力を取得するために使用されるコンポーネントです。
Switch
Switchは、オンとオフの状態を切り替えるために使用されるコンポーネントです。
Slider
Sliderは、ユーザーが値の範囲から選択するために使用されるコンポーネントです。
SegmentedControl
SegmentedControlは、ユーザーが複数の選択肢の中から1つを選択するために使用されるコンポーネントです。
Radio
Radioは、ユーザーが複数の選択肢の中から1つの値を選択するために使用されるコンポーネントです。
Rating
Ratingは、ユーザーが評価を行うために使用させるコンポーネントです。