Dropzone

A field that takes a file dropped on it or picked from the file dialog, and shows what was chosen as a preview that a click removes.

import { Dropzone } from "@produkto/ui/Dropzone";
import { Label } from "@produkto/ui/Label";

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-2">
    <Label htmlFor="dropzone-preview">Label</Label>
    <Dropzone id="dropzone-preview" />
  </div>
);

export default Example;

Usage

import { Dropzone, type DropzoneFile } from "@produkto/ui/Dropzone";
import { Label } from "@produkto/ui/Label";
const [files, setFiles] = useState<DropzoneFile[]>([]);

<div className="flex flex-col gap-2">
  <Label htmlFor="files">Label</Label>
  <Dropzone id="files" maxFiles={3} value={files} onValueChange={setFiles} />
</div>

Examples

Sizes

size sets the drop area and the tiles of the files it holds a step smaller or bigger: sm a 288px row with 6px between its tiles, base 320px with 8px (the default), or lg 384px with 12px, their icons, insets and words a step with them.

import { Dropzone, type DropzoneSize } from "@produkto/ui/Dropzone";
import { Label } from "@produkto/ui/Label";

const variants: { size: DropzoneSize; label: string }[] = [
  { size: "sm", label: "Small" },
  { size: "base", label: "Base" },
  { size: "lg", label: "Large" },
];

const Example = () => (
  <div className="flex w-full max-w-sm flex-col gap-4">
    {variants.map((variant) => (
      <div className="flex flex-col gap-2" key={variant.size}>
        <Label htmlFor={`dropzone-size-${variant.size}`}>{variant.label}</Label>
        <Dropzone id={`dropzone-size-${variant.size}`} size={variant.size} />
      </div>
    ))}
  </div>
);

export default Example;

Border radius

borderRadius sets the corners of the drop area, and of the tiles of the files it holds, on the kit's one scale: none, xs 4px, sm 6px, base 8px (the default), lg 12px or xl 16px.

import {
  Dropzone,
  type DropzoneBorderRadius,
} from "@produkto/ui/Dropzone";
import { Label } from "@produkto/ui/Label";

const variants: { borderRadius: DropzoneBorderRadius; label: string }[] = [
  { borderRadius: "none", label: "None" },
  { borderRadius: "xs", label: "xs" },
  { borderRadius: "sm", label: "sm" },
  { borderRadius: "base", label: "base" },
  { borderRadius: "lg", label: "lg" },
  { borderRadius: "xl", label: "xl" },
];

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    {variants.map((variant) => (
      <div className="flex flex-col gap-2" key={variant.borderRadius}>
        <Label htmlFor={`dropzone-radius-${variant.borderRadius}`}>
          {variant.label}
        </Label>
        <Dropzone
          id={`dropzone-radius-${variant.borderRadius}`}
          borderRadius={variant.borderRadius}
        />
      </div>
    ))}
  </div>
);

export default Example;

Media

media sets how a chosen file is drawn: "image", the default, as its photo, "icon" as its svg inline in the text colour, and "file" as a file icon over its name.

import {
  Dropzone,
  type DropzoneFile,
  type DropzoneMedia,
} from "@produkto/ui/Dropzone";
import { Label } from "@produkto/ui/Label";

const variants: { media: DropzoneMedia; label: string; file: DropzoneFile }[] =
  [
    {
      media: "image",
      label: "Image",
      file: {
        key: "item-1",
        name: "item-1.jpg",
        src: "https://images.unsplash.com/photo-1497366754035-f200968a6e72?w=300&auto=format&fit=crop&q=80",
      },
    },
    {
      media: "icon",
      label: "Icon",
      file: {
        key: "item-2",
        name: "item-2.svg",
        data: `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="5" /><circle cx="12" cy="12" r="4" /></svg>`,
      },
    },
    {
      media: "file",
      label: "File",
      file: { key: "item-3", name: "item-3.pdf" },
    },
  ];

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    {variants.map((variant) => (
      <div className="flex flex-col gap-2" key={variant.media}>
        <Label htmlFor={`dropzone-media-${variant.media}`}>
          {variant.label}
        </Label>
        <Dropzone
          defaultValue={[variant.file]}
          id={`dropzone-media-${variant.media}`}
          media={variant.media}
        />
      </div>
    ))}
  </div>
);

export default Example;

Max files

maxFiles is how many files the field takes, one by default, after which its drop area hides; here each holds one file, and the one that takes four still offers more.

import { Dropzone, type DropzoneFile } from "@produkto/ui/Dropzone";
import { Label } from "@produkto/ui/Label";

const files: DropzoneFile[] = [
  {
    key: "item-1",
    name: "item-1.jpg",
    src: "https://images.unsplash.com/photo-1497366754035-f200968a6e72?w=300&auto=format&fit=crop&q=80",
  },
];

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    <div className="flex flex-col gap-2">
      <Label htmlFor="dropzone-max-default">Label</Label>
      <Dropzone defaultValue={files} id="dropzone-max-default" />
    </div>
    <div className="flex flex-col gap-2">
      <Label htmlFor="dropzone-max">Label</Label>
      <Dropzone defaultValue={files} id="dropzone-max" maxFiles={4} />
    </div>
  </div>
);

export default Example;

Source

source="library" picks from the host's FilesPicker, a library of the files the app already holds, here a stand-in, beside "device", the default, which takes a file from the device.

import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
} from "@produkto/ui/Dialog";
import {
  Dropzone,
  type DropzoneFile,
  type FilesPickerProps,
} from "@produkto/ui/Dropzone";
import { Image } from "@produkto/ui/Image";
import { Label } from "@produkto/ui/Label";
import { UiHostProvider, useUiHost } from "@produkto/ui/host";

const library: DropzoneFile[] = [
  { key: "photo-1497366754035-f200968a6e72", name: "Item 1" },
  { key: "photo-1497215728101-856f4ea42174", name: "Item 2" },
  { key: "photo-1497366811353-6870744d04b2", name: "Item 3" },
  { key: "photo-1588345921523-c2dcdb7f1dcd", name: "Item 4" },
  { key: "photo-1497366216548-37526070297c", name: "Item 5" },
  { key: "photo-1524758631624-e2822e304c36", name: "Item 6" },
].map((file) => ({
  ...file,
  src: `https://images.unsplash.com/${file.key}?w=300&auto=format&fit=crop&q=80`,
}));

const FilesPicker = ({
  open,
  onOpenChange,
  value,
  onValueChange,
  maxFiles,
}: FilesPickerProps) => {
  const chosen = value.map((file) => file.key);

  const toggle = (file: DropzoneFile) =>
    onValueChange(
      chosen.includes(file.key)
        ? value.filter((item) => item.key !== file.key)
        : [...value, file],
    );

  return (
    <Dialog onOpenChange={onOpenChange} open={open}>
      <DialogContent className="sm:max-w-md">
        <DialogHeader>
          <DialogTitle>Title</DialogTitle>
          <DialogDescription>Description</DialogDescription>
        </DialogHeader>
        <div className="grid grid-cols-3 gap-2">
          {library.map((file) => (
            <button
              aria-label={file.name}
              aria-pressed={chosen.includes(file.key)}
              className="overflow-hidden rounded-lg outline-none transition focus-visible:ring-2 focus-visible:ring-ring/60 disabled:opacity-40 aria-pressed:ring-2 aria-pressed:ring-primary"
              disabled={!chosen.includes(file.key) && chosen.length >= maxFiles}
              key={file.key}
              onClick={() => toggle(file)}
              type="button"
            >
              <Image
                alt=""
                className="aspect-square size-full object-cover"
                src={file.src}
              />
            </button>
          ))}
        </div>
      </DialogContent>
    </Dialog>
  );
};

const Example = () => {
  const host = useUiHost();

  return (
    <UiHostProvider host={{ ...host, FilesPicker }}>
      <div className="flex w-full max-w-xs flex-col gap-4">
        <div className="flex flex-col gap-2">
          <Label htmlFor="dropzone-source-device">Device</Label>
          <Dropzone id="dropzone-source-device" />
        </div>
        <div className="flex flex-col gap-2">
          <Label htmlFor="dropzone-source-library">Library</Label>
          <Dropzone id="dropzone-source-library" source="library" />
        </div>
      </div>
    </UiHostProvider>
  );
};

export default Example;

Disabled

disabled dims the field, stops it taking or removing files and takes it out of the tab order, beside one as it is by default.

import { Dropzone } from "@produkto/ui/Dropzone";
import { Label } from "@produkto/ui/Label";

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    <div className="flex flex-col gap-2">
      <Label htmlFor="dropzone-enabled">Label</Label>
      <Dropzone id="dropzone-enabled" />
    </div>
    <div className="flex flex-col gap-2">
      <Label htmlFor="dropzone-disabled">Label</Label>
      <Dropzone disabled id="dropzone-disabled" />
    </div>
  </div>
);

export default Example;

Invalid

error writes what is wrong under the dropzone and reaches its file input as aria-invalid, turning the drop area's border to the destructive colour, beside one without an error.

import { Dropzone } from "@produkto/ui/Dropzone";
import { Label } from "@produkto/ui/Label";

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    <div className="flex flex-col gap-2">
      <Label htmlFor="dropzone-valid">Label</Label>
      <Dropzone id="dropzone-valid" />
    </div>
    <div className="flex flex-col gap-2">
      <Label htmlFor="dropzone-error">Label</Label>
      <Dropzone error="Description" id="dropzone-error" />
    </div>
  </div>
);

export default Example;

API Reference

Dropzone

DropzoneProps · components/Dropzone/Dropzone.types.ts
PropTypeDefault
sourceDropzoneSource"device" | "library""device"
mediaDropzoneMedia"image" | "icon" | "file" | "video""image"
sizeDropzoneSize"sm" | "base" | "lg""base"
borderRadiusDropzoneBorderRadius"none" | "xs" | "sm" | "base" | "lg" | "xl""base"
valueDropzoneFile[]-
defaultValueDropzoneFile[]-
onValueChange(value: DropzoneFile[]) => void-
acceptstring | readonly string[]-
maxFilesnumber1
maxSizenumberMAX_UPLOAD_BYTES
disabledbooleanfalse
requiredbooleanfalse
namestring-
formstring-
openboolean-
defaultOpenboolean-
onOpenChange(open: boolean) => void-
blockbooleanfalse
pickerComponentType<FilesPickerProps>-
errorReactNode-

DropzoneItem

DropzoneItemProps · also takes the props of <button>
PropTypeDefault
onRemoverequired( event: MouseEvent<HTMLButtonElement> | KeyboardEvent<HTMLButtonElement>, ) => void-
loadingbooleanfalse
invalidbooleanfalse
mediaDropzoneMedia"image" | "icon" | "file" | "video""image"
sizeDropzoneSize"sm" | "base" | "lg""base"
borderRadiusDropzoneBorderRadius"none" | "xs" | "sm" | "base" | "lg" | "xl""base"
srcstring-
altstring-
iconIconSvgElement-
labelstring-

DropzoneItemLink

DropzoneItemLinkProps · also takes the props of <a>
PropTypeDefault
actionIconIconSvgElementDownload01Icon
mediaDropzoneMedia"image" | "icon" | "file" | "video""image"
sizeDropzoneSize"sm" | "base" | "lg""base"
borderRadiusDropzoneBorderRadius"none" | "xs" | "sm" | "base" | "lg" | "xl""base"
srcstring-
altstring-
iconIconSvgElement-
labelstring-

DropzoneFile

used by value
PropTypeDefault
keyrequiredstring-
namestring-
srcstring-
previewstring-
altstring-
datastring-
fileFile-
mediaDropzoneMedia"image" | "icon" | "file" | "video"-

FilesPickerProps

used by picker
PropTypeDefault
openrequiredboolean-
onOpenChangerequired(open: boolean) => void-
valuerequiredDropzoneFile[]-
onValueChangerequired(value: DropzoneFile[]) => void-
maxFilesrequirednumber-
acceptreadonly string[]-

It also takes the props of <div>.