← Components

Communication

Attachment

A file in the composer or a message. Drops in; a wash fills behind the name while uploading; the icon pops when done; remove shrinks it away.

Examples and variants

Uploading and done

Watch the wash fill.

brief.pdf10%0% uploaded
moodboard.png840 KBUploaded
Example
function AttachmentStates() {
  const [progress, setProgress] = useState<number | undefined>(10)
  const [shown, setShown] = useState(true)
  useEffect(() => {
    if (progress === undefined) return
    const timer = window.setTimeout(() => setProgress((p) => (p === undefined || p >= 100 ? undefined : p + 15)), tokenMs("--duration-medium"))
    return () => window.clearTimeout(timer)
  }, [progress])
  return (
    <div className="flex flex-col items-start gap-3">
      <div className="flex flex-wrap gap-2">
        {shown ? <Attachment name="brief.pdf" size="1.2 MB" progress={progress} onRemove={() => setShown(false)} /> : null}
        <Attachment name="moodboard.png" kind="image" size="840 KB" />
      </div>
      <Button
        size="sm"
        variant="secondary"
        onClick={() => {
          setShown(true)
          setProgress(0)
        }}
      >
        Upload again
      </Button>
    </div>
  )
}

Spec drawing

Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.

Install

With the CLI

The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/attachment.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install cn lucide-react
  2. Copy this file to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/attachment.tsxattachment
      components/ui/attachment.tsx
      "use client"
      
      import * as React from "react"
      import { FileIcon, ImageIcon, XIcon } from "lucide-react"
      import { cn } from "cn"
      
      /*
        Attachment: a file in the composer or a message. Drops in when added;
        while uploading, a wash fills behind the name with the percent; when done,
        the icon pops; removing it shrinks it away and its neighbours slide over.
      */
      function Attachment({
        name,
        size,
        kind = "file",
        progress,
        onRemove,
        className,
      }: {
        name: string
        size?: string
        kind?: "file" | "image"
        /** 0 to 100 while uploading; undefined when done. */
        progress?: number
        onRemove?: () => void
        className?: string
      }) {
        const uploading = progress !== undefined && progress < 100
        const announce = uploading ? `${Math.floor(progress / 25) * 25}% uploaded` : "Uploaded"
        return (
          <div
            data-slot="attachment"
            data-uploading={uploading || undefined}
            className={cn(
              "attachment relative inline-flex max-w-64 items-center gap-2 overflow-hidden rounded-(--attachment-corner) bg-(--attachment-container) p-(--attachment-padding) pe-3 text-sm",
              className
            )}
          >
            {uploading ? (
              <span aria-hidden className="attachment-wash absolute inset-y-0 start-0 bg-(--attachment-wash)" style={{ width: `${progress}%` }} />
            ) : null}
            <span key={uploading ? "up" : "done"} className={cn("relative inline-flex size-8 shrink-0 items-center justify-center rounded-(--shape-sm) bg-(--surface-container-lowest) [&_svg]:size-(--size-icon-md)", !uploading && "attachment-done")}>
              {kind === "image" ? <ImageIcon aria-hidden /> : <FileIcon aria-hidden />}
            </span>
            <span className="relative min-w-0">
              <span className="block truncate font-medium">{name}</span>
              <span className="block text-caption text-muted-foreground tabular-nums">{uploading ? `${Math.round(progress)}%` : size}</span>
            </span>
            <span className="sr-only" role="status">
              {announce}
            </span>
            {onRemove ? (
              <button
                type="button"
                aria-label={`Remove ${name}`}
                onClick={onRemove}
                className="liquid-press relative ms-1 inline-flex size-6 shrink-0 items-center justify-center rounded-(--shape-full) text-muted-foreground outline-none hover:bg-(--attachment-remove-container-hover) hover:text-foreground active:bg-(--attachment-remove-container-pressed) focus-visible:shadow-(--focus-ring)"
              >
                <XIcon className="size-(--size-icon-sm)" />
              </button>
            ) : null}
          </div>
        )
      }
      
      export { Attachment }
      

Imports

What the examples on this page import:

Imports
import { Attachment } from "@/components/ui/attachment"
import { Avatar, AvatarFallback } from "@/components/ui/avatar"
import { Bubble, BubbleGroup, BubbleReactions } from "@/components/ui/bubble"
import { Button } from "@/components/ui/button"
import { CodeBlock } from "@/components/ui/code-block"
import { Marker } from "@/components/ui/marker"
import { Message, MessageAvatar, MessageContent, MessageFooter, MessageHeader } from "@/components/ui/message"
import { MessageScroller } from "@/components/ui/message-scroller"
import { PromptInput } from "@/components/ui/prompt-input"
import { Questionnaire } from "@/components/ui/questionnaire"
import { StreamingText } from "@/components/ui/streaming-text"
import { Trace, TraceItem } from "@/components/ui/trace"
import { VoiceInput } from "@/components/ui/voice-input"
import { tokenMs } from "@/lib/motion/token-ms"

Anatomy

  1. 1Container · --attachment-container, --attachment-corner, --attachment-padding.
  2. 2Wash · --attachment-wash.
  3. 3Icon, name, size · Percent while uploading.
  4. 4Remove · Labelled with the file name.

Props

PropTypeDefaultDescription
name, sizestring—File details.
kind"file" | "image""file"Icon.
progressnumber—0 to 100 while uploading.
onRemove() => void—Shows the remove button.

Tokens

Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.

TokenPoints toLightDark
--attachment-containervar(--surface-container)oklch(0.965 0.005 95)oklch(0.12 0 0)
--attachment-cornervar(--shape-md)14pxsame
--attachment-paddingvar(--space-2)8pxsame
--attachment-washvar(--color-fill-progress)color-mix(in oklch, var(--color-brand) 12%, transparent)color-mix(in oklch, var(--color-brand) 16%, transparent)
--attachment-remove-container-hovervar(--color-bg-hover)oklch(0.965 0.005 95)oklch(0.22 0 0)
--attachment-remove-container-pressedvar(--color-bg-pressed)oklch(0.94 0.007 95)oklch(0.27 0 0)

Motion

TriggerWhat happensTokens
Add / upload / doneDrops in; wash fills; icon pops.--duration-settle, --duration-medium

Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.

Accessibility

  • Progress announced at 25% steps.

Usage

Do

  • Show uploads where they'll be sent.

Don’t

  • Don't use for big galleries.

Code

components/ui/attachment.tsx
"use client"

import * as React from "react"
import { FileIcon, ImageIcon, XIcon } from "lucide-react"
import { cn } from "cn"

/*
  Attachment: a file in the composer or a message. Drops in when added;
  while uploading, a wash fills behind the name with the percent; when done,
  the icon pops; removing it shrinks it away and its neighbours slide over.
*/
function Attachment({
  name,
  size,
  kind = "file",
  progress,
  onRemove,
  className,
}: {
  name: string
  size?: string
  kind?: "file" | "image"
  /** 0 to 100 while uploading; undefined when done. */
  progress?: number
  onRemove?: () => void
  className?: string
}) {
  const uploading = progress !== undefined && progress < 100
  const announce = uploading ? `${Math.floor(progress / 25) * 25}% uploaded` : "Uploaded"
  return (
    <div
      data-slot="attachment"
      data-uploading={uploading || undefined}
      className={cn(
        "attachment relative inline-flex max-w-64 items-center gap-2 overflow-hidden rounded-(--attachment-corner) bg-(--attachment-container) p-(--attachment-padding) pe-3 text-sm",
        className
      )}
    >
      {uploading ? (
        <span aria-hidden className="attachment-wash absolute inset-y-0 start-0 bg-(--attachment-wash)" style={{ width: `${progress}%` }} />
      ) : null}
      <span key={uploading ? "up" : "done"} className={cn("relative inline-flex size-8 shrink-0 items-center justify-center rounded-(--shape-sm) bg-(--surface-container-lowest) [&_svg]:size-(--size-icon-md)", !uploading && "attachment-done")}>
        {kind === "image" ? <ImageIcon aria-hidden /> : <FileIcon aria-hidden />}
      </span>
      <span className="relative min-w-0">
        <span className="block truncate font-medium">{name}</span>
        <span className="block text-caption text-muted-foreground tabular-nums">{uploading ? `${Math.round(progress)}%` : size}</span>
      </span>
      <span className="sr-only" role="status">
        {announce}
      </span>
      {onRemove ? (
        <button
          type="button"
          aria-label={`Remove ${name}`}
          onClick={onRemove}
          className="liquid-press relative ms-1 inline-flex size-6 shrink-0 items-center justify-center rounded-(--shape-full) text-muted-foreground outline-none hover:bg-(--attachment-remove-container-hover) hover:text-foreground active:bg-(--attachment-remove-container-pressed) focus-visible:shadow-(--focus-ring)"
        >
          <XIcon className="size-(--size-icon-sm)" />
        </button>
      ) : null}
    </div>
  )
}

export { Attachment }