# Upload zone

A responsive drop area with a floating file stack and clear validation.

## Usage

Use as a file selection step. This component only selects and validates a file; connect upload, progress, server validation, and failure handling in your application. Do not trust client-side MIME checks on the server.

Save the JSX and CSS Module in the same folder in a React project with CSS Modules support. Controlled components need parent state and change handlers.

## JSX

```jsx
"use client";
import styles from "./UploadZone.module.css";
import { useState, useRef, useId } from "react";
export function validateFile(file, maxBytes) {
  if (!file) return "";
  if (
    !["image/png", "image/jpeg", "image/webp", "image/svg+xml"].includes(
      file.type,
    )
  )
    return "Choose a PNG, JPEG, WebP, or SVG image.";
  if (file.size > maxBytes)
    return "This file is too large. Choose a smaller image.";
  return "";
}
export default function UploadZone({ onFile, maxBytes = 5 * 1024 * 1024 }) {
  const [dragging, setDragging] = useState(false);
  const [message, setMessage] = useState(
    `PNG, JPEG, WebP or SVG · up to ${Math.round((maxBytes / 1024 / 1024) * 10) / 10} MB`,
  );
  const input = useRef(null);
  const id = useId();
  function accept(file) {
    setDragging(false);
    if (!file) return;
    const error = validateFile(file, maxBytes);
    if (error) {
      setMessage(error);
      return;
    }
    setMessage(`${file.name} selected. Nothing has been uploaded.`);
    onFile?.(file);
  }
  return (
    <div
      className={styles.root}
      data-dragging={dragging}
      onDragOver={(event) => {
        event.preventDefault();
        setDragging(true);
      }}
      onDragLeave={(event) => {
        if (!event.currentTarget.contains(event.relatedTarget))
          setDragging(false);
      }}
      onDrop={(event) => {
        event.preventDefault();
        accept(event.dataTransfer.files[0]);
      }}
    >
      <div className={styles.files} aria-hidden="true">
        <i />
        <i />
        <span>↑</span>
      </div>
      <strong>Drop your next idea here.</strong>
      <button
        type="button"
        aria-describedby={id}
        onClick={() => input.current?.click()}
      >
        Choose an image
      </button>
      <input
        ref={input}
        type="file"
        accept="image/png,image/jpeg,image/webp,image/svg+xml"
        className={styles.hidden}
        tabIndex={-1}
        onChange={(event) => {
          accept(event.target.files[0]);
          event.target.value = "";
        }}
      />
      <p id={id} role="status">
        {message}
      </p>
    </div>
  );
}

```

## CSS

```css
.root {
  width: 320px;
  min-height: 235px;
  padding: 25px 20px;
  border: 1px dashed #b3a69a;
  border-radius: var(--tdp-radius, 8px);
  background: var(--tdp-surface, #fffdfa);
  text-align: center;
  transition:
    background 0.25s,
    border-color 0.25s;
}
.files {
  height: 65px;
  position: relative;
  display: grid;
  place-items: center;
  margin-bottom: 14px;
}
.files i {
  position: absolute;
  width: 39px;
  height: 49px;
  background: #e5d9ed;
  border: 1px solid #b8a7c6;
  border-radius: 5px;
  rotate: -13deg;
  translate: -10px 0;
  transition:
    translate 0.3s,
    rotate 0.3s;
}
.files i + i {
  background: #e1e9d8;
  border-color: #b0bfa1;
  rotate: 10deg;
  translate: 10px 2px;
}
.files > span {
  position: relative;
  font-size: 25px;
  color: #425d35;
}
.root:is(:hover, :focus-within) .files i {
  translate: -18px -5px;
  rotate: -20deg;
}
.root:is(:hover, :focus-within) .files i + i {
  translate: 18px -5px;
  rotate: 20deg;
}
.root[data-dragging="true"] {
  background: #eef3e9;
  border-color: #597346;
}
.root strong {
  font-size: 14px;
  font-weight: 500;
  display: block;
}
.root button {
  display: block;
  margin: 16px auto 0;
  min-height: 44px;
  padding: 8px 14px;
  border: 1px solid var(--tdp-border, #d9d1c9);
  border-radius: var(--tdp-radius, 8px);
  background: transparent;
  color: inherit;
  font-size: 12px;
}
.root p {
  font-size: 10px;
  line-height: 1.6;
  color: var(--tdp-muted, #6b625b);
  margin: 13px 0 0;
  overflow-wrap: anywhere;
}
.hidden {
  display: none;
}
.root {
  font-family: var(--tdp-font, "DM Sans", system-ui, sans-serif);
  color: var(--tdp-text, #302e2b);
  max-width: 100%;
  box-sizing: border-box;
}
.root * {
  box-sizing: border-box;
}
.root :is(button, input, a):focus-visible,
.root:focus-visible {
  outline: 3px solid var(--tdp-focus, #7851a9);
  outline-offset: 4px;
}
.root button {
  font: inherit;
  cursor: pointer;
}
.root button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}
.root button,
.root a {
  touch-action: manipulation;
}
@media (prefers-reduced-motion: reduce) {
  .root,
  .root *,
  .root::before,
  .root::after {
    animation: none !important;
    transition: none !important;
    transform: none !important;
    translate: none !important;
    rotate: none !important;
    scale: none !important;
    scroll-behavior: auto !important;
  }
}

```

## Props

- onFile (function): Receives the validated File; no network upload is performed.
- maxBytes (number): Maximum file size in bytes; defaults to 5 MiB.

## Accessibility

A keyboard-accessible button opens the file picker; drag-and-drop is optional. File selection and validation are announced. The native file input is triggered by the visible button.

Check accessibility again after customizing the component.

[Browse all components](/resources/components/)

Canonical: https://designproject.io/resources/components/upload-zone/

## Keep learning

- [How to create a UI color palette, step by step](https://designproject.io/resources/guides/create-ui-color-palette/)
- [Product design glossary: 44 terms explained](https://designproject.io/resources/glossary/)
- [How to choose font pairings for a product interface](https://designproject.io/resources/guides/choose-font-pairings/)
