Todae

Todae

useDropzone

Adds drag and drop to a native file input. The input stays the control: a click or the keyboard opens the browser's file picker, and a form submits the files in it. The hook adds the drop target and checks every file against your rules.

On this page

Spread getInputProps() on an <input> and getRootProps() on the drop target. A <label> around the input works well, since a click anywhere on it opens the picker.

const zone = useDropzone({
  accept: 'image/*',
  multiple: true,
  maxSize: 2_000_000,
  onFiles: (accepted, rejected) => { … },
});

<label {...zone.getRootProps()}>
  Drop images here, or choose them
  <input {...zone.getInputProps()} name="images" />
</label>

The browser's picker applies accept; the hook checks dropped and pasted files against it (a .ext rule matches the file name, other rules match the file's MIME type). Files from a drop, a paste or a pick are checked against maxSize, minSize and the file count. Dropped folders are skipped. onFiles gets the files that passed and a { file, reasons } entry for each one that did not. Without multiple, only the first passing file is taken. After the checks, the input holds only the accepted files.

dragging is true while files are dragged over the target, and the target carries data-dragging for CSS. Drags that carry no files, such as selected text, are left to the browser.

With paste: true, files pasted while focus is inside the target are taken too, such as a screenshot pasted into a textarea in it. The hook cancels that paste so the field does not also get it. A paste that also carries plain text, such as cells copied from a spreadsheet, is left to the browser, and so is a paste another handler already cancelled.

const zone = useDropzone({ paste: true, accept: 'image/*', onFiles });

<div {...zone.getRootProps()}>
  <Field.Root aria-label="Message">
    <Field.Textarea value={message} onChange={(event) => setMessage(event.target.value)} autoGrow />
  </Field.Root>
  <input {...zone.getInputProps()} aria-label="Attach images" />
</div>

This target takes images. Drop a file on it, or choose one. The border turns solid while a file is over it, and the line below names the files that passed.

import { Stack, Text, useDropzone } from '@tounsoo/todae';
import { useState } from 'react';

export default function ImageDrop() {
  const [names, setNames] = useState<string[]>([]);
  const zone = useDropzone({
    accept: 'image/*',
    multiple: true,
    onFiles: (accepted) => setNames(accepted.map((file) => file.name)),
  });
  return (
    <Stack.Root gap="condensed" align="start">
      <label
        {...zone.getRootProps()}
        style={{
          display: 'grid',
          justifyItems: 'center',
          gap: 'var(--todae-space-3)',
          padding: 'var(--todae-space-6)',
          border: `2px ${zone.dragging ? 'solid' : 'dashed'} var(--todae-color-border-default)`,
          borderRadius: 'var(--todae-radius-md)',
          background: zone.dragging ? 'var(--todae-color-bg-subtle)' : undefined,
        }}
      >
        <Text.Root as="span">Drop images here, or choose them</Text.Root>
        <input {...zone.getInputProps()} name="images" />
      </label>
      <Text.Root>{names.length > 0 ? names.join(', ') : 'No images yet'}</Text.Root>
    </Stack.Root>
  );
}

No images yet

See the File upload recipe for a full example with errors and a file list.

Accessible composition

Todae handles: The native <input type="file"> stays the control, so its role, the keyboard and the file picker come from the browser. getInputProps() sets type, accept, multiple and disabled; with disabled, drops and pastes are refused as well. getRootProps() adds only drag handlers, a paste handler with paste, and data-dragging. The hook sets no ARIA and announces nothing.

You supply: The input's name, such as the text of a <label> around it. A hint about the rules, joined with aria-describedby. When files are rejected: the reasons in words, since reasons are codes; aria-invalid and aria-describedby pointing at the message; and an announcement through useAnnouncer if it must be heard. A labelled list of the accepted files.

Watch for: Hiding the input with display: none or hidden: dragging is pointer-only, so keyboard and screen reader users lose the only way in. If you restyle it, keep it focusable and show its focus. Adding role="button" or a click handler to the drop target, which adds a second control for the same picker. The drag state or an error shown by color alone.

When to use

When: Letting people drop files onto a form as well as pick them.

When not: A plain file picker → <input type="file"> alone. Uploading, progress and retries → your own code, after onFiles.

Props

useDropzone props
NameTypeDescription
acceptstring

File types to take, in the input's accept syntax: '.pdf', 'image/*', 'application/json', comma separated. The picker applies it; the hook checks dropped and pasted files against it.

multipleboolean

Take more than one file.

Default: false

maxFilesnumber

Most files to take when multiple is set. Files past it are rejected as 'too-many'.

maxSizenumber

Largest file size in bytes.

minSizenumber

Smallest file size in bytes.

pasteboolean

Also take files pasted while focus is inside the root, such as a screenshot pasted into a textarea there. A paste that also carries plain text is left to the browser.

Default: false

disabledboolean

Turn off the input, drops and pastes.

Default: false

onFiles(accepted: File[], rejected: FileRejection[]) => void

Called with the files that passed and the files that did not, after a drop, a paste or a pick.