Skip to content
Application

File Uploader

Drag-and-drop dropzone with click-to-browse and an animated file list.

Motionfile rows: layout + AnimatePresence on tween.quick

Install

Install Kineto and follow the theme setup once in your app.

bash
npm install kineto
tsx
import { FileUploader } from "kineto/file-uploader";

Preview

Click to upload or drag and drop

SVG, PNG, JPG or PDF (max. 10MB)

tsx
"use client";

import * as React from "react";
import { AnimatePresence, motion } from "motion/react";
import { File as FileIcon, UploadCloud, X } from "lucide-react";

import { cn } from "@/lib/cn";
import { FeaturedIcon } from "@/components/ui/featured-icon";
import { tween } from "@/lib/motion-presets";

function formatBytes(bytes: number): string {
  if (bytes < 1024) return `${bytes} B`;
  if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(0)} KB`;
  return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}

export interface FileUploaderProps
  extends Omit<React.HTMLAttributes<HTMLDivElement>, "onChange"> {
  accept?: string;
  multiple?: boolean;
  hint?: string;
  onFilesChange?: (files: File[]) => void;
}

const FileUploader = React.forwardRef<HTMLDivElement, FileUploaderProps>(
  (
    {
      className,
      accept,
      multiple = true,
      hint = "SVG, PNG, JPG or PDF (max. 10MB)",
      onFilesChange,
      ...props
    },
    ref
  ) => {
    const inputRef = React.useRef<HTMLInputElement>(null);
    const [files, setFiles] = React.useState<File[]>([]);
    const [dragging, setDragging] = React.useState(false);

    const addFiles = (incoming: FileList | null) => {
      if (!incoming || incoming.length === 0) return;
      const next = multiple
        ? [...files, ...Array.from(incoming)]
        : [Array.from(incoming)[0] as File];
      setFiles(next);
      onFilesChange?.(next);
    };

    const removeAt = (i: number) => {
      const next = files.filter((_, idx) => idx !== i);
      setFiles(next);
      onFilesChange?.(next);
    };

    return (
      <div ref={ref} className={cn("flex flex-col gap-3", className)} {...props}>
        <div
          role="button"
          tabIndex={0}
          onClick={() => inputRef.current?.click()}
          onKeyDown={(e) => {
            if (e.key === "Enter" || e.key === " ") inputRef.current?.click();
          }}
          onDragOver={(e) => {
            e.preventDefault();
            setDragging(true);
          }}
          onDragLeave={() => setDragging(false)}
          onDrop={(e) => {
            e.preventDefault();
            setDragging(false);
            addFiles(e.dataTransfer.files);
          }}
          className={cn(
            "flex cursor-pointer flex-col items-center justify-center gap-3 rounded-xl border border-dashed px-6 py-8 text-center transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
            dragging ? "border-brand bg-brand/5" : "border-border bg-card/50 hover:bg-accent/50"
          )}
        >
          <FeaturedIcon icon={UploadCloud} variant="gray" shape="circle" />
          <div className="text-sm">
            <span className="font-medium text-brand">Click to upload</span>{" "}
            <span className="text-muted-foreground">or drag and drop</span>
          </div>
          <p className="text-xs text-muted-foreground">{hint}</p>
          <input
            ref={inputRef}
            type="file"
            accept={accept}
            multiple={multiple}
            className="sr-only"
            onChange={(e) => addFiles(e.target.files)}
          />
        </div>

        <AnimatePresence initial={false}>
          {files.map((file, i) => (
            <motion.div
              key={`${file.name}-${i}`}
              layout
              initial={{ opacity: 0, y: -6 }}
              animate={{ opacity: 1, y: 0 }}
              exit={{ opacity: 0, x: -8 }}
              transition={tween.quick}
              className="flex items-center gap-3 rounded-lg border border-border bg-card p-3"
            >
              <FeaturedIcon icon={FileIcon} variant="brand" size="sm" />
              <div className="min-w-0 flex-1">
                <p className="truncate text-sm font-medium text-foreground">
                  {file.name}
                </p>
                <p className="text-xs text-muted-foreground">
                  {formatBytes(file.size)}
                </p>
              </div>
              <button
                type="button"
                aria-label={`Remove ${file.name}`}
                onClick={() => removeAt(i)}
                className="inline-flex size-7 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"
              >
                <X className="size-4" />
              </button>
            </motion.div>
          ))}
        </AnimatePresence>
      </div>
    );
  }
);
FileUploader.displayName = "FileUploader";

export { FileUploader };

src/components/ui/file-uploader.tsx

Props

PropTypeDefaultDescription
multiplebooleantrueAccept multiple files.
acceptstring—Input accept filter.
onFilesChange(files: File[]) => void—Fires on change.