Forms

File Upload

Drop area with a browse button, and file cards for uploading, completed and failed states.

react-aria-componentslucide-react

Preview

Drop or browse files; progress is simulated.

Choose a file or drag & drop it here.

CSV, XLSX or PDF, up to 50 MB.

csv
q3-prospects.csv
41 KB of 117 KB Uploading…
pdf
icp-brief.pdf
2.3 MBCompleted
xlsx
lost-deals.xlsx
840 KBFailed — file is password protected
Try again

Installation

Components are copied into your project — no runtime package.

  1. Install the following dependencies:

    terminal
    npm install react-aria-components lucide-react
  2. Create a file-upload.tsx file and paste the following code into it.

    src/components/ui/file-upload.tsx
    "use client";
    
    import { CloudUpload, Loader2, RotateCcw, Trash2, X } from "lucide-react";
    import { DropZone, FileTrigger } from "react-aria-components";
    import { cx } from "@/utils/cx";
    import { Button } from "./button";
    import { LinkButton } from "./link-button";
    
    export interface FileUploadAreaProps {
      onFiles: (files: File[]) => void;
      /** Accepted MIME types or extensions, e.g. ["application/pdf", ".csv"]. */
      accept?: string[];
      hint?: string;
      allowsMultiple?: boolean;
      className?: string;
    }
    
    /** Dashed drop area with a browse button. Drag files in or pick them. */
    export function FileUploadArea({ onFiles, accept, hint = "CSV, XLSX or PDF, up to 50 MB.", allowsMultiple = true, className }: FileUploadAreaProps) {
      const take = async (items: { kind: string; getFile?: () => Promise<File> }[]) => {
        const files = await Promise.all(items.filter((i) => i.kind === "file" && i.getFile).map((i) => i.getFile!()));
        if (files.length) onFiles(files);
      };
      return (
        <DropZone
          aria-label="Upload files"
          onDrop={(e) => take(e.items as unknown as { kind: string; getFile?: () => Promise<File> }[])}
          className={cx(
            "flex flex-col items-center rounded-lg border border-dashed border-line-strong bg-surface px-6 py-8 text-center outline-none transition-colors",
            "data-drop-target:border-accent data-drop-target:bg-accent-soft data-focus-visible:shadow-focus",
            className,
          )}
        >
          <span className="flex size-10 items-center justify-center rounded-[10px] bg-surface text-fg-secondary shadow-button">
            <CloudUpload className="size-4.5" aria-hidden />
          </span>
          <p className="mt-4 text-base font-medium text-fg">Choose a file or drag &amp; drop it here.</p>
          <p className="mt-1 text-sm text-fg-muted">{hint}</p>
          <FileTrigger acceptedFileTypes={accept} allowsMultiple={allowsMultiple} onSelect={(list) => list && onFiles(Array.from(list))}>
            <Button size="sm" className="mt-4">
              Browse file
            </Button>
          </FileTrigger>
        </DropZone>
      );
    }
    
    export type FileItemStatus = "uploading" | "done" | "error";
    
    export interface FileItemProps {
      name: string;
      /** Bytes. */
      size: number;
      status: FileItemStatus;
      /** 0–100 while uploading. */
      progress?: number;
      error?: string;
      onRemove?: () => void;
      onRetry?: () => void;
      className?: string;
    }
    
    function formatBytes(n: number) {
      if (n < 1024) return `${n} B`;
      if (n < 1024 * 1024) return `${Math.round(n / 1024)} KB`;
      return `${(n / 1024 / 1024).toFixed(1)} MB`;
    }
    
    const extColor: Record<string, string> = { pdf: "bg-error", csv: "bg-success", xlsx: "bg-success", xls: "bg-success", png: "bg-info", jpg: "bg-info", doc: "bg-info", docx: "bg-info" };
    
    /** Uploaded file card: in progress · done · error with retry. */
    export function FileItem({ name, size, status, progress = 0, error = "Upload failed", onRemove, onRetry, className }: FileItemProps) {
      const ext = name.split(".").pop()?.toLowerCase() ?? "";
      return (
        <div className={cx("flex gap-3 rounded-md border bg-surface p-3.5", status === "error" ? "border-error-border" : "border-line", className)}>
          <span className="relative flex h-10 w-8 shrink-0 items-end justify-center rounded-[5px] border border-line bg-subtle pb-1">
            <span className={cx("rounded-[3px] px-1 text-[8px] font-bold uppercase leading-[12px] text-white", extColor[ext] ?? "bg-fg-muted")}>{ext.slice(0, 4) || "file"}</span>
          </span>
          <div className="min-w-0 flex-1">
            <div className="flex items-start gap-2">
              <div className="min-w-0 flex-1">
                <div className="truncate text-base font-medium text-fg">{name}</div>
                <div className="mt-0.5 flex items-center gap-1.5 text-sm text-fg-muted">
                  <span className="tabular">
                    {status === "uploading" ? `${formatBytes((size * progress) / 100)} of ${formatBytes(size)}` : formatBytes(size)}
                  </span>
                  <span aria-hidden>·</span>
                  {status === "uploading" && (
                    <span className="inline-flex items-center gap-1">
                      <Loader2 className="size-3 animate-spin text-accent" aria-hidden /> Uploading…
                    </span>
                  )}
                  {status === "done" && <span className="text-success-text">Completed</span>}
                  {status === "error" && <span className="text-error-text">{error}</span>}
                </div>
              </div>
              {onRemove && (
                <button
                  type="button"
                  aria-label={status === "uploading" ? `Cancel ${name}` : `Remove ${name}`}
                  onClick={onRemove}
                  className="inline-flex size-6 items-center justify-center rounded-xs text-fg-muted outline-none hover:bg-muted hover:text-fg focus-visible:shadow-focus"
                >
                  {status === "uploading" ? <X className="size-3.5" /> : <Trash2 className="size-3.5" />}
                </button>
              )}
            </div>
            {status === "uploading" && (
              <div className="mt-2.5 h-1 overflow-hidden rounded-full bg-active" role="progressbar" aria-valuenow={progress} aria-valuemax={100} aria-label={`Uploading ${name}`}>
                <div className="h-full rounded-full bg-accent transition-[width]" style={{ width: `${progress}%` }} />
              </div>
            )}
            {status === "error" && onRetry && (
              <LinkButton tone="error" size="sm" onPress={onRetry} leadingIcon={<RotateCcw />} className="mt-2">
                Try again
              </LinkButton>
            )}
          </div>
        </div>
      );
    }
  3. Create a button.tsx file and paste the following code into it.

    src/components/ui/button.tsx
    "use client";
    
    import { Loader2 } from "lucide-react";
    import type { ReactNode } from "react";
    import { Button as AriaButton, type ButtonProps as AriaButtonProps } from "react-aria-components";
    import { cx } from "@/utils/cx";
    
    export type ButtonVariant = "primary" | "secondary" | "ghost" | "danger" | "neutral" | "soft" | "outline" | "danger-soft";
    export type ButtonSize = "2xs" | "xs" | "sm" | "md" | "lg";
    
    export interface ButtonProps extends Omit<AriaButtonProps, "className" | "children"> {
      variant?: ButtonVariant;
      size?: ButtonSize;
      /** Icon rendered before the label. */
      leadingIcon?: ReactNode;
      /** Icon rendered after the label. */
      trailingIcon?: ReactNode;
      /** Square button with only an icon. Requires `aria-label`. */
      iconOnly?: boolean;
      className?: string;
      children?: ReactNode;
    }
    
    const base =
      "group relative inline-flex shrink-0 cursor-default select-none items-center justify-center gap-2 whitespace-nowrap rounded-md text-base font-medium outline-none transition-[background,box-shadow,color] duration-150 " +
      "data-focus-visible:ring-3 data-focus-visible:ring-accent/25 " +
      "data-disabled:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0";
    
    const variants: Record<ButtonVariant, string> = {
      primary:
        "bg-linear-to-b from-accent-light to-accent text-fg-inverse shadow-button-primary " +
        "data-hovered:from-accent data-hovered:to-accent-hover data-pressed:from-accent-pressed data-pressed:to-accent-pressed " +
        "data-disabled:bg-none data-disabled:bg-muted data-disabled:text-fg-disabled data-disabled:shadow-none",
      secondary:
        "bg-surface text-fg shadow-button data-hovered:bg-subtle data-pressed:bg-active " +
        "data-disabled:bg-muted data-disabled:text-fg-disabled data-disabled:shadow-none [&_svg]:text-fg-secondary",
      ghost:
        "text-fg data-hovered:bg-muted data-pressed:bg-active data-disabled:text-fg-disabled [&_svg]:text-fg-secondary",
      danger:
        "bg-error text-fg-inverse data-hovered:bg-error-hover data-pressed:bg-error-text " +
        "data-disabled:bg-muted data-disabled:text-fg-disabled",
      // Dark filled — for high-emphasis actions on light surfaces when accent would compete.
      neutral:
        "bg-inverse text-fg-inverse shadow-[0_1px_2px_rgb(0_0_0/0.12),inset_0_1px_0_rgb(255_255_255/0.08)] data-hovered:bg-fg/90 data-pressed:bg-fg/80 " +
        "data-disabled:bg-muted data-disabled:text-fg-disabled data-disabled:shadow-none",
      // Accent "lighter" — tinted, no shadow.
      soft:
        "bg-accent-soft text-accent-text data-hovered:bg-accent-soft-border data-pressed:bg-accent-soft-border " +
        "data-disabled:bg-muted data-disabled:text-fg-disabled",
      // Accent stroke on white.
      outline:
        "bg-surface text-accent-text shadow-[inset_0_0_0_1px_var(--color-accent-soft-border)] data-hovered:bg-accent-soft data-pressed:bg-accent-soft-border " +
        "data-disabled:bg-muted data-disabled:text-fg-disabled data-disabled:shadow-none",
      "danger-soft":
        "bg-error-soft text-error-text data-hovered:bg-error-border data-pressed:bg-error-border " +
        "data-disabled:bg-muted data-disabled:text-fg-disabled",
    };
    
    // Heights: 28 / 32 / 36 / 40 / 44 (size/button-sm|md|lg plus compact sizes). Padding tightens on the icon side.
    const sizes: Record<ButtonSize, { base: string; lead: string; trail: string; icon: string }> = {
      "2xs": { base: "h-7 gap-1 rounded-sm px-2 text-sm [&_svg]:size-3.5", lead: "pl-1.5", trail: "pr-1.5", icon: "size-7 px-0" },
      xs: { base: "h-8 gap-1.5 rounded-[10px] px-2.5", lead: "pl-2", trail: "pr-2", icon: "size-8 px-0" },
      sm: { base: "h-9 px-3", lead: "pl-2", trail: "pr-2.5", icon: "size-9 px-0" },
      md: { base: "h-10 px-4", lead: "pl-3", trail: "pr-3", icon: "size-10 px-0" },
      lg: { base: "h-11 px-5", lead: "pl-4", trail: "pr-4", icon: "size-11 px-0" },
    };
    
    export function Button({
      variant = "secondary",
      size = "md",
      leadingIcon,
      trailingIcon,
      iconOnly,
      isPending,
      className,
      children,
      ...props
    }: ButtonProps) {
      const s = sizes[size];
      return (
        <AriaButton
          {...props}
          isPending={isPending}
          className={cx(
            base,
            variants[variant],
            s.base,
            Boolean(leadingIcon) && !iconOnly && s.lead,
            Boolean(trailingIcon) && !iconOnly && s.trail,
            iconOnly && s.icon,
            className,
          )}
        >
          {isPending ? <Loader2 className="animate-spin" aria-hidden /> : leadingIcon}
          {iconOnly ? (isPending ? null : children) : children}
          {!iconOnly && trailingIcon}
        </AriaButton>
      );
    }
  4. Create a link-button.tsx file and paste the following code into it.

    src/components/ui/link-button.tsx
    "use client";
    
    import type { ReactNode } from "react";
    import { Link as AriaLink, type LinkProps as AriaLinkProps } from "react-aria-components";
    import { cx } from "@/utils/cx";
    
    export interface LinkButtonProps extends Omit<AriaLinkProps, "className" | "children"> {
      tone?: "gray" | "black" | "accent" | "error";
      size?: "sm" | "md";
      underline?: boolean;
      leadingIcon?: ReactNode;
      trailingIcon?: ReactNode;
      className?: string;
      children: ReactNode;
    }
    
    const tones = {
      gray: "text-fg-secondary data-hovered:text-fg",
      black: "text-fg data-hovered:text-fg-secondary",
      accent: "text-accent-text data-hovered:text-accent-pressed",
      error: "text-error-text data-hovered:text-error-hover",
    };
    
    /** Inline text action. Underline on hover, or always with `underline`. */
    export function LinkButton({ tone = "accent", size = "md", underline, leadingIcon, trailingIcon, className, children, ...props }: LinkButtonProps) {
      return (
        <AriaLink
          {...props}
          className={cx(
            "inline-flex cursor-pointer items-center gap-1 rounded-xs font-medium underline-offset-[3px] outline-none transition-colors",
            "data-hovered:underline data-focus-visible:shadow-focus data-disabled:cursor-default data-disabled:text-fg-disabled data-disabled:no-underline",
            size === "md" ? "text-base [&_svg]:size-4" : "text-sm [&_svg]:size-3.5",
            underline && "underline decoration-current/40",
            tones[tone],
            className,
          )}
        >
          {leadingIcon}
          {children}
          {trailingIcon}
        </AriaLink>
      );
    }
  5. Add the theme tokens and the cx helper from Installation if you haven’t yet, then update the import paths to match your project setup.