Thinking UI

Attachment Chip

A file chip with an upload ring that resolves into a check.

Code

components/thinking-ui/attachment-chip.tsx
"use client";

import { AnimatePresence, motion } from "motion/react";
import { useMotion } from "@/lib/motion";
import { cn } from "@/lib/utils";

export type AttachmentChipProps = {
  name: string;
  /** Human-readable size, e.g. "2.4 MB". */
  size?: string;
  /** Upload progress, 0–100. */
  progress?: number;
  status: "uploading" | "done" | "error";
  onRemove?: () => void;
  className?: string;
};

export function AttachmentChip({ name, size, progress = 0, status, onRemove, className }: AttachmentChipProps) {
  const m = useMotion();
  const ext = name.split(".").pop()?.slice(0, 4).toUpperCase() ?? "FILE";
  const meta =
    status === "uploading" ? `Uploading ${Math.round(progress)}%` : status === "error" ? "Upload failed" : [ext, size].filter(Boolean).join(" · ");
  const pop = m.pop();

  return (
    <motion.div
      layout
      className={cn("flex w-64 items-center gap-3 rounded-card border border-card-line bg-card p-2 pr-2.5 shadow-card", className)}
      style={{ borderRadius: "var(--radius-card)" }}
      initial={pop.initial}
      animate={pop.animate}
      exit={pop.exit}
      transition={m.spring("base")}
    >
      <span className="relative grid size-10 shrink-0 place-items-center rounded-control bg-fill-muted font-mono text-4xs font-semibold text-fg-tertiary">
        {ext}
        <AnimatePresence>
          {status === "uploading" && (
            <motion.svg key="ring" viewBox="0 0 40 40" className="absolute inset-0 size-10 -rotate-90" {...pop}>
              <circle cx="20" cy="20" r="17" fill="none" stroke="var(--border-default)" strokeWidth="2" />
              <motion.circle
                cx="20"
                cy="20"
                r="17"
                fill="none"
                stroke="var(--accent)"
                strokeWidth="2"
                strokeLinecap="round"
                initial={false}
                animate={{ pathLength: progress / 100 }}
                transition={m.spring("layout")}
              />
            </motion.svg>
          )}
          {status === "done" && (
            <motion.span key="done" className="absolute -bottom-1 -right-1 grid size-4 place-items-center rounded-full bg-success text-fg-on-status ring-2 ring-[var(--surface-panel)]" {...pop}>
              <svg viewBox="0 0 16 16" className="size-2.5">
                <path d="M4 8.5l2.5 2.5 5.5-6" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round" />
              </svg>
            </motion.span>
          )}
        </AnimatePresence>
      </span>

      <div className="min-w-0 flex-1">
        <p className="truncate text-sm font-medium text-fg">{name}</p>
        <span className="relative block h-4 overflow-hidden text-xs">
          <AnimatePresence mode="popLayout" initial={false}>
            <motion.span
              key={status}
              className={cn("block", status === "error" ? "text-fg-danger" : "text-fg-muted")}
              {...m.swap()}
            >
              {meta}
            </motion.span>
          </AnimatePresence>
        </span>
      </div>

      {onRemove && (
        <motion.button
          type="button"
          aria-label={`Remove ${name}`}
          onClick={onRemove}
          whileTap={m.press}
          className="transition-ui grid size-6 shrink-0 place-items-center rounded-full text-fg-muted hover:bg-fill-muted hover:text-fg"
        >
          <svg viewBox="0 0 16 16" className="size-3">
            <path d="M4 4l8 8M12 4l-8 8" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" />
          </svg>
        </motion.button>
      )}
    </motion.div>
  );
}

Motion notes

  • The progress ring follows the layout spring, so uneven upload updates still look smooth.
  • The meta line swaps from “Uploading 42%” to file details with the shared swap preset.
  • Removing a chip lets the others close the gap with a layout animation.

Installation

  1. 1

    Install dependencies

    Terminal
    npm install motion clsx tailwind-merge
  2. 2

    Add the design tokens and motion system (once per project)

    Copy styles/tokens/*, lib/motion.tsx and lib/utils.ts from Get started.

  3. 3

    Paste the source into components/thinking-ui/attachment-chip.tsx

    Copy it from the Code tab, or use Copy prompt to let your coding agent do all of this.

Usage

Example
<AttachmentChip name="deck.pdf" size="4.2 MB" progress={64} status="uploading" />

Props

PropTypeDefaultDescription
namestring—File name.
sizestring—Readable size, e.g. 2.4 MB.
progressnumber0Upload progress 0–100.
status"uploading" | "done" | "error"—Upload state.
onRemove() => void—Shows a remove button.