react-dropzone

  • Version 20.1.1
  • Published
  • 340 kB
  • 2 dependencies
  • MIT license

Install

npm i react-dropzone
yarn add react-dropzone
pnpm add react-dropzone

Overview

Simple HTML5 drag-drop zone with React.js

Index

Variables

variable Dropzone

const Dropzone: React.ForwardRefExoticComponent<any>;
  • Convenience wrapper component for the useDropzone hook

    <Dropzone>
    {({getRootProps, getInputProps}) => (
    <div {...getRootProps()}>
    <input {...getInputProps()} />
    <p>Drag 'n' drop some files here, or click to select files</p>
    </div>
    )}
    </Dropzone>

Functions

function useDropzone

useDropzone: (props?: DropzoneOptions) => DropzoneState;
  • A React hook that creates a drag 'n' drop area.

    function MyDropzone(props) {
    const {getRootProps, getInputProps} = useDropzone({
    onDrop: acceptedFiles => {
    // do something with the File objects, e.g. upload to some server
    }
    });
    return (
    <div {...getRootProps()}>
    <input {...getInputProps()} />
    <p>Drag and drop some files here, or click to select files</p>
    </div>
    )
    }

Interfaces

interface Accept

interface Accept {}
  • A map of accepted MIME types to file extensions, as passed to the accept prop.

    An extension value may be a single extension string or an array of them - both are accepted, mirroring the shape of window.showOpenFilePicker's accept.

index signature

[key: string]: string | readonly string[];

    interface AcceptGroup

    interface AcceptGroup {}
    • A labeled group of accepted types, mirroring one entry of window.showOpenFilePicker's types option. Passing the accept prop as an array of these lets the File System Access picker present multiple named filter rows instead of a single one (see pickerOptionsFromAccept). The optional description labels the row; when omitted it is derived from the group's extensions.

      Grouping only surfaces when the FS Access picker is actually used (useFsAccessApi + a secure context + browser support). The native <input> fallback has no concept of groups or descriptions, so every group is flattened into one accept attribute there.

    property accept

    accept: Accept;

      property description

      description?: string;

        interface DropzoneInputProps

        interface DropzoneInputProps extends React.InputHTMLAttributes<HTMLInputElement> {}

          property refKey

          refKey?: string;

            interface DropzoneProps

            interface DropzoneProps extends DropzoneOptions {}

              property children

              children?: (state: DropzoneState) => React.ReactElement;

                interface DropzoneRef

                interface DropzoneRef {}

                  property open

                  open: () => void;

                    interface DropzoneRootProps

                    interface DropzoneRootProps extends React.HTMLAttributes<HTMLElement> {}

                      property refKey

                      refKey?: string;

                        index signature

                        [key: string]: any;

                          interface FileError

                          interface FileError {}
                          • A file rejection error.

                          property code

                          code: ErrorCode | string;

                            property message

                            message: string;

                              interface FileRejection

                              interface FileRejection {}

                                property errors

                                errors: readonly FileError[];

                                  property file

                                  file: FileWithPath;

                                    Enums

                                    enum ErrorCode

                                    enum ErrorCode {
                                    FileInvalidType = 'file-invalid-type',
                                    FileTooLarge = 'file-too-large',
                                    FileTooSmall = 'file-too-small',
                                    TooManyFiles = 'too-many-files',
                                    }

                                      member FileInvalidType

                                      FileInvalidType = 'file-invalid-type'

                                        member FileTooLarge

                                        FileTooLarge = 'file-too-large'

                                          member FileTooSmall

                                          FileTooSmall = 'file-too-small'

                                            member TooManyFiles

                                            TooManyFiles = 'too-many-files'

                                              Type Aliases

                                              type DragFileRejection

                                              type DragFileRejection = DragFileRejection$1<FileWithPath | DataTransferItem>;

                                                type DropEvent

                                                type DropEvent =
                                                | React.DragEvent<HTMLElement>
                                                | React.ChangeEvent<HTMLInputElement>
                                                | React.ClipboardEvent<HTMLElement>
                                                | DragEvent
                                                | ClipboardEvent
                                                | Event;

                                                  type DropzoneOptions

                                                  type DropzoneOptions = Pick<React.HTMLProps<HTMLElement>, SharedProps> & {
                                                  accept?: Accept | AcceptGroup[];
                                                  minSize?: number;
                                                  maxSize?: number;
                                                  maxFiles?: number;
                                                  preventDropOnDocument?: boolean;
                                                  noClick?: boolean;
                                                  noKeyboard?: boolean;
                                                  noDrag?: boolean;
                                                  noDragEventsBubbling?: boolean;
                                                  /**
                                                  * If true, disables paste-to-upload. By default, when the dropzone (or a focused child) receives a
                                                  * paste that carries files - e.g. a screenshot pasted with Ctrl/Cmd+V - those files go through the
                                                  * same `accept`/size/`validator` checks and `onDrop` callbacks as a drop. Pastes with no files
                                                  * (plain text, etc.) are ignored and left untouched. See
                                                  * https://github.com/react-dropzone/react-dropzone/issues/1210
                                                  */
                                                  noPaste?: boolean;
                                                  disabled?: boolean;
                                                  onDrop?: <T extends File>(
                                                  acceptedFiles: T[],
                                                  fileRejections: FileRejection[],
                                                  event: DropEvent
                                                  ) => void;
                                                  onDropAccepted?: <T extends File>(files: T[], event: DropEvent) => void;
                                                  onDropRejected?: (fileRejections: FileRejection[], event: DropEvent) => void;
                                                  getFilesFromEvent?: (
                                                  event: DropEvent | Array<FileSystemFileHandle>
                                                  ) => Promise<Array<File | DataTransferItem>>;
                                                  onFileDialogCancel?: () => void;
                                                  onFileDialogOpen?: () => void;
                                                  onError?: (err: Error) => void;
                                                  /**
                                                  * Custom validation, run once per file on drop/selection. Return `null` to accept the file, or a
                                                  * {@link FileError} (or array of them) to reject it. May be `async` (return a `Promise`) to support
                                                  * checks that can't run synchronously - e.g. reading image dimensions, inspecting file contents,
                                                  * or calling an external service. While an async validator is pending, {@link DropzoneState.isProcessing}
                                                  * is `true`, and `onDrop`/`onDropAccepted`/`onDropRejected` fire only once it settles. If the
                                                  * validator throws or rejects, `onError` is called and the drop is discarded.
                                                  *
                                                  * Note: the validator never runs during a drag (a `DataTransferItem` has no name/size), so a
                                                  * validator-configured dropzone is `isDragUnknown` until drop.
                                                  */
                                                  validator?: <T extends File>(
                                                  file: T
                                                  ) => ValidatorResult | Promise<ValidatorResult>;
                                                  /**
                                                  * Override the message of any rejection error (built-in or custom). Called once per error;
                                                  * receives the error and the file it belongs to and returns the message to use. Return
                                                  * `error.message` for codes you don't want to change. Useful for localizing error messages.
                                                  */
                                                  getErrorMessage?: (error: FileError, file: File) => string;
                                                  useFsAccessApi?: boolean;
                                                  autoFocus?: boolean;
                                                  };

                                                    type DropzoneState

                                                    type DropzoneState = DropzoneRef & {
                                                    isFocused: boolean;
                                                    isDragActive: boolean;
                                                    isDragAccept: boolean;
                                                    isDragReject: boolean;
                                                    isDragUnknown: boolean;
                                                    isDragGlobal: boolean;
                                                    isFileDialogActive: boolean;
                                                    /**
                                                    * `true` while a drop/selection is being processed asynchronously - i.e. while `getFilesFromEvent`
                                                    * reads the files and/or an async {@link DropzoneOptions.validator} runs. Spans the whole pipeline,
                                                    * from when files start being read until validation settles. When both are synchronous (the default
                                                    * `getFilesFromEvent` with no/async-free validator) the work resolves within a microtask, so it's
                                                    * only observable for genuinely async work. Use it to show a spinner or disable UI while processing.
                                                    */
                                                    isProcessing: boolean;
                                                    acceptedFiles: readonly FileWithPath[];
                                                    fileRejections: readonly FileRejection[];
                                                    /**
                                                    * Rejections that can be determined before drop. Standard drag events expose
                                                    * `DataTransferItem`s with a MIME type but no name or size, so extension, size, and custom
                                                    * validator failures are only available in {@link fileRejections} after drop. In practice this
                                                    * state can report MIME-type and surplus-file errors during a standard browser drag.
                                                    */
                                                    dragFileRejections: readonly DragFileRejection[];
                                                    rootRef: React.RefObject<HTMLElement>;
                                                    inputRef: React.RefObject<HTMLInputElement>;
                                                    getRootProps: <T extends DropzoneRootProps>(props?: T) => T;
                                                    getInputProps: <T extends DropzoneInputProps>(props?: T) => T;
                                                    };

                                                      type ValidatorResult

                                                      type ValidatorResult = FileError | readonly FileError[] | null;
                                                      • What a custom validator returns: a single error, a list of errors, or null when the file passes. A validator may return the result directly (synchronous) or wrapped in a Promise (asynchronous, e.g. reading image dimensions or calling an external service).

                                                      Package Files (1)

                                                      Dependencies (2)

                                                      Dev Dependencies (25)

                                                      Peer Dependencies (2)

                                                      Badge

                                                      To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                      You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/react-dropzone.

                                                      • Markdown
                                                        [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/react-dropzone)
                                                      • HTML
                                                        <a href="https://www.jsdocs.io/package/react-dropzone"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>