AI Attachments
Files on the way into a prompt: chips with an upload ring, a fanned image stack, and a drop zone that reacts as files hover.
"use client"
import * as React from "react"
import {
FileCode,
FileText,
Image as ImageIcon,
Images,
RotateCcw,
Table,
X,
} from "lucide-react"
import {
AiKitStyles,
AiStyle,
cx,
Face,
Mark,
Roll,
type AiMood,
type AiShape,
type AiTone,
} from "@/lib/ai-kit"
const CSS = `
.aik-at-chips{display:flex;flex-wrap:wrap;gap:8px;width:min(100%,420px)}
.aik-at-chip{display:flex;align-items:center;gap:10px;height:48px;padding:0 6px 0 8px;border-radius:14px;background:var(--aik-card);box-shadow:var(--aik-sh-card);min-width:0;max-width:100%;
animation:aik-pop var(--aik-d-enter) var(--aik-ease-out) both;transition:box-shadow var(--aik-d-state)}
.aik-at-chip[data-s="fail"]{box-shadow:0 0 0 1px color-mix(in oklab,var(--aik-red) 45%,transparent)}
.aik-at-tile{position:relative;display:grid;place-items:center;inline-size:32px;block-size:32px;flex:none;border-radius:50%;color:var(--aik-text-2);background:var(--aik-raised);
transition:background var(--aik-d-state),color var(--aik-d-state),border-radius var(--aik-d-panel) var(--aik-ease-soft)}
.aik-at-chip[data-s="done"] .aik-at-tile{border-radius:9px}
.aik-at-chip[data-s="up"] .aik-at-tile{background:transparent;color:var(--aik-text-3)}
.aik-at-chip[data-s="fail"] .aik-at-tile{background:color-mix(in oklab,var(--aik-red) 12%,transparent);color:var(--aik-red);border-radius:9px}
.aik-at-ring{position:absolute;inset:0;inline-size:32px;block-size:32px;transform:rotate(-90deg);opacity:0;transition:opacity var(--aik-d-state)}
.aik-at-chip[data-s="up"] .aik-at-ring{opacity:1}
.aik-at-ring circle{fill:none;stroke-width:1.75}
.aik-at-ring circle:first-child{stroke:var(--aik-line-2)}
.aik-at-ring circle:last-child{stroke:var(--aik-blue);stroke-linecap:round;stroke-dasharray:89.54;transition:stroke-dashoffset var(--aik-d-panel) linear}
.aik-at-meta{display:grid;min-width:0;padding-right:2px}
.aik-at-meta b{font-weight:600;font-size:13px;line-height:17px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.aik-at-meta .aik-roll{font:11.5px/15px var(--aik-mono);color:var(--aik-text-3);font-variant-numeric:tabular-nums}
.aik-at-chip[data-s="fail"] .aik-at-meta .aik-roll{color:var(--aik-red)}
.aik-at-act{display:grid;place-items:center;inline-size:26px;block-size:26px;flex:none;border-radius:8px;color:var(--aik-text-3);cursor:pointer;
transition:background var(--aik-d-state),color var(--aik-d-state),transform var(--aik-d-press) var(--aik-ease-out)}
.aik-at-act:hover{background:var(--aik-hover);color:var(--aik-text)}
.aik-at-act:active{transform:scale(.9)}
.aik-at-empty{display:flex;align-items:center;gap:8px;height:48px;padding:0 14px;border-radius:14px;color:var(--aik-text-3);font-size:13px;box-shadow:inset 0 0 0 1px var(--aik-line-2)}
.aik-at-stack{display:grid;gap:14px;justify-items:center;width:min(100%,420px);container-type:inline-size}
.aik-at-fan{position:relative;display:grid;place-items:center;width:100%;height:calc(min(104px,22cqw) + 28px);--size:min(104px,22cqw);--gap:calc(var(--size)*.2);--tilt:7deg;--drop:5px}
.aik-at-stack[data-open] .aik-at-fan{--gap:calc(var(--size) + 8px);--tilt:0deg;--drop:0px}
.aik-at-shot{grid-area:1/1;position:relative;inline-size:var(--size);block-size:var(--size);border-radius:calc(var(--size)*.17);background:var(--aik-card);padding:3px;box-shadow:var(--aik-sh-pop);cursor:pointer;
transform:translate(calc(var(--k)*var(--gap)),calc(var(--a)*var(--drop))) rotate(calc(var(--k)*var(--tilt)));transition:transform var(--aik-d-move) var(--aik-ease-move);z-index:calc(10 - var(--a)*2)}
.aik-at-shot > svg,.aik-at-shot > img{display:block;inline-size:100%;block-size:100%;border-radius:calc(var(--size)*.17 - 3px);object-fit:cover}
.aik-at-shot:focus-visible{outline:2px solid var(--aik-blue);outline-offset:2px}
.aik-at-rm{position:absolute;right:-6px;top:-6px;display:grid;place-items:center;inline-size:22px;block-size:22px;border-radius:50%;background:var(--aik-pri);color:var(--aik-pri-ink);cursor:pointer;
opacity:0;transform:scale(.5);pointer-events:none;transition:opacity var(--aik-d-state) var(--aik-ease-out),transform var(--aik-d-state) var(--aik-ease-out)}
.aik-at-stack[data-open] .aik-at-rm{opacity:1;transform:none;pointer-events:auto}
.aik-at-rm:active{transform:scale(.88)}
.aik-at-more{position:absolute;inset:3px;display:grid;place-items:center;border-radius:calc(var(--size)*.17 - 3px);background:rgb(0 0 0 / 55%);color:#fff;font:600 15px/1 var(--aik-sans);
transition:opacity var(--aik-d-state)}
.aik-at-stack[data-open] .aik-at-more{opacity:0}
.aik-at-toggle{display:inline-flex;align-items:center;gap:7px;height:30px;padding:0 11px 0 9px;border-radius:10px;background:var(--aik-raised);color:var(--aik-text-2);font-size:12.5px;font-weight:500;cursor:pointer;
transition:background var(--aik-d-state),color var(--aik-d-state),transform var(--aik-d-press) var(--aik-ease-out)}
.aik-at-toggle:hover{color:var(--aik-text)}
.aik-at-toggle:active{transform:scale(.96)}
.aik-at-drop{position:relative;display:grid;justify-items:center;align-content:center;gap:12px;width:min(100%,400px);min-height:208px;padding:24px 20px;border-radius:20px;text-align:center;cursor:pointer;
background:var(--aik-card);box-shadow:var(--aik-sh-card);transition:background var(--aik-d-state),transform var(--aik-d-state) var(--aik-ease-out)}
.aik-at-drop::before{content:"";position:absolute;inset:6px;border-radius:14px;border:1.5px dashed var(--aik-line-3);pointer-events:none;transition:border-color var(--aik-d-state),inset var(--aik-d-state) var(--aik-ease-out)}
.aik-at-drop:hover::before{border-color:var(--aik-text-3)}
.aik-at-drop[data-s="over"]{background:color-mix(in oklab,var(--aik-blue) 7%,var(--aik-card));transform:scale(1.012)}
.aik-at-drop[data-s="over"]::before{border-color:var(--aik-blue);border-style:solid;inset:9px}
.aik-at-drop[data-s="done"]::before{border-color:color-mix(in oklab,var(--aik-green) 55%,transparent);border-style:solid}
.aik-at-drop[aria-disabled="true"]{opacity:.5;cursor:default}
.aik-at-drop:focus-visible{outline:2px solid var(--aik-blue);outline-offset:2px;border-radius:20px}
.aik-at-drop input{position:absolute;inline-size:1px;block-size:1px;opacity:0;pointer-events:none}
.aik-at-hero{position:relative;display:grid;place-items:end center;inline-size:110px;block-size:86px}
.aik-at-ghost{position:absolute;top:0;display:grid;place-items:center;inline-size:34px;block-size:42px;border-radius:9px;background:var(--aik-card);color:var(--aik-blue);box-shadow:var(--aik-sh-pop);
opacity:0;transform:translate(calc(var(--gx)*.4),-26px) rotate(0deg) scale(.9);transition:opacity var(--aik-d-exit) var(--aik-ease-in),transform var(--aik-d-exit) var(--aik-ease-in)}
.aik-at-drop[data-s="over"] .aik-at-ghost{opacity:1;transform:translate(var(--gx),0) rotate(var(--gr)) scale(1);
transition:opacity var(--aik-d-enter) var(--aik-ease-out) calc(var(--i)*var(--aik-stagger)*2),transform var(--aik-d-panel) var(--aik-ease-out) calc(var(--i)*var(--aik-stagger)*2)}
.aik-at-hero .aik-face{position:relative;transform:translateY(-19px);transition:transform var(--aik-d-panel) var(--aik-ease-out)}
.aik-at-drop[data-s="over"] .aik-at-hero .aik-face{transform:translateY(0) scale(.84)}
.aik-at-hero .aik-mark{position:absolute;right:22px;bottom:17px;box-shadow:0 0 0 2.5px var(--aik-card);background:var(--aik-card)}
.aik-at-copy{display:grid;gap:3px;justify-items:center;min-width:0}
.aik-at-copy .aik-roll{font-weight:600;font-size:14.5px;line-height:20px;justify-items:center}
.aik-at-copy > span:last-child{color:var(--aik-text-3);font-size:12.5px;line-height:17px}
.aik-at-copy u{text-decoration:none;color:var(--aik-text);font-weight:500;box-shadow:inset 0 -1px 0 var(--aik-line-3)}
@media (prefers-reduced-motion: reduce){.aik-at-chip{animation:none}.aik-at-shot,.aik-at-ring circle:last-child{transition:none}}
`
export type AiAttachmentKind = "file" | "code" | "image" | "sheet"
export interface AiAttachment {
id: string
name: string
/** File size, already formatted. */
size?: string
kind?: AiAttachmentKind
/** Upload progress from 0 to 1. Leave undefined once the file is in. */
progress?: number
/** Why the upload failed. Shows the retry button. */
error?: string
}
const KIND_ICON = {
file: FileText,
code: FileCode,
image: ImageIcon,
sheet: Table,
}
const RING = 89.54
export interface AiFileChipsProps extends React.HTMLAttributes<HTMLDivElement> {
files: AiAttachment[]
onRemove?: (id: string) => void
/** Called from the retry button of a failed file. */
onRetry?: (id: string) => void
/** Shown when there are no files. */
emptyLabel?: string
}
/** Attached files as chips. A ring fills around the icon while a file uploads, then the tile squares off. */
export function AiFileChips({
files,
onRemove,
onRetry,
emptyLabel = "No files attached",
className,
...rest
}: AiFileChipsProps) {
return (
<div
{...rest}
className={cx("aik aik-at-chips", className)}
role="list"
aria-label="Attachments"
>
<AiKitStyles />
<AiStyle id="ai-attachments" css={CSS} />
{files.length === 0 ? (
<span className="aik-at-empty">
<FileText size={14} />
{emptyLabel}
</span>
) : null}
{files.map((file) => {
const Icon = KIND_ICON[file.kind ?? "file"]
const uploading = file.progress !== undefined && !file.error
const progress = Math.min(1, Math.max(0, file.progress ?? 1))
const state = file.error ? "fail" : uploading ? "up" : "done"
const line = file.error
? file.error
: uploading
? `${Math.round(progress * 100)}%${file.size ? ` of ${file.size}` : ""}`
: (file.size ?? "Attached")
return (
<div
key={file.id}
className="aik-at-chip"
role="listitem"
data-s={state}
>
<span className="aik-at-tile">
<Icon size={15} />
<svg
className="aik-at-ring"
viewBox="0 0 32 32"
aria-hidden="true"
>
<circle cx="16" cy="16" r="14.25" />
<circle
cx="16"
cy="16"
r="14.25"
style={{ strokeDashoffset: RING * (1 - progress) }}
/>
</svg>
</span>
<span className="aik-at-meta">
<b>{file.name}</b>
<Roll>{line}</Roll>
</span>
{file.error ? (
<button
type="button"
className="aik-at-act"
aria-label={`Retry ${file.name}`}
onClick={() => onRetry?.(file.id)}
>
<RotateCcw size={14} />
</button>
) : null}
<button
type="button"
className="aik-at-act"
aria-label={`Remove ${file.name}`}
onClick={() => onRemove?.(file.id)}
>
<X size={14} />
</button>
</div>
)
})}
</div>
)
}
const fill = (value: string): React.CSSProperties => ({ fill: value })
const stroke = (value: string): React.CSSProperties => ({
stroke: value,
fill: "none",
})
/** Four original thumbnails drawn in SVG, for demos and placeholders. `variant` picks one. */
export function AiThumbArt({ variant = 0 }: { variant?: number }) {
const pick = ((variant % 4) + 4) % 4
const id = React.useId()
if (pick === 0)
return (
<svg viewBox="0 0 100 100" aria-hidden="true">
<defs>
<linearGradient id={`${id}a`} x1="0" y1="0" x2="0" y2="1">
<stop offset="0" style={{ stopColor: "var(--aik-f-rose)" }} />
<stop offset="1" style={{ stopColor: "var(--aik-f-amber)" }} />
</linearGradient>
</defs>
<rect width="100" height="100" fill={`url(#${id}a)`} />
<circle cx="68" cy="40" r="15" fill="#fff" opacity=".9" />
<path
d="M0 70 Q28 46 54 66 T100 58 V100 H0Z"
fill="#000"
opacity=".22"
/>
<path
d="M0 84 Q30 62 60 80 T100 74 V100 H0Z"
fill="#000"
opacity=".32"
/>
</svg>
)
if (pick === 1)
return (
<svg viewBox="0 0 100 100" aria-hidden="true">
<rect width="100" height="100" style={fill("var(--aik-f-ink)")} />
<rect
x="10"
y="12"
width="80"
height="9"
rx="4.5"
fill="#fff"
opacity=".16"
/>
<rect
x="10"
y="29"
width="36"
height="59"
rx="6"
style={fill("var(--aik-f-blue)")}
/>
<rect
x="52"
y="29"
width="38"
height="26"
rx="6"
fill="#fff"
opacity=".2"
/>
<rect
x="52"
y="61"
width="38"
height="27"
rx="6"
fill="#fff"
opacity=".1"
/>
<rect
x="16"
y="36"
width="20"
height="4"
rx="2"
fill="#fff"
opacity=".85"
/>
<rect
x="16"
y="45"
width="13"
height="4"
rx="2"
fill="#fff"
opacity=".5"
/>
</svg>
)
if (pick === 2)
return (
<svg viewBox="0 0 100 100" aria-hidden="true">
<rect width="100" height="100" style={fill("var(--aik-f-violet)")} />
<circle
cx="50"
cy="52"
r="38"
style={stroke("#fff")}
opacity=".18"
strokeWidth="1.5"
/>
<circle
cx="50"
cy="52"
r="25"
style={stroke("#fff")}
opacity=".3"
strokeWidth="1.5"
/>
<circle cx="50" cy="52" r="11" fill="#fff" opacity=".92" />
<circle cx="82" cy="32" r="5" style={fill("var(--aik-f-amber)")} />
<circle cx="28" cy="66" r="3.5" fill="#fff" opacity=".8" />
</svg>
)
return (
<svg viewBox="0 0 100 100" aria-hidden="true">
<rect width="100" height="100" style={fill("var(--aik-f-teal)")} />
<path d="M0 38 Q25 24 50 38 T100 38 V100 H0Z" fill="#fff" opacity=".16" />
<path d="M0 56 Q25 42 50 56 T100 56 V100 H0Z" fill="#fff" opacity=".2" />
<path
d="M0 74 Q25 60 50 74 T100 74 V100 H0Z"
style={fill("var(--aik-f-green)")}
opacity=".7"
/>
<circle cx="24" cy="22" r="8" fill="#fff" opacity=".85" />
</svg>
)
}
export interface AiStackImage {
id: string
/** Used as the accessible name. */
name: string
/** An image URL. Without one, a built-in drawing is used. */
src?: string
/** Which built-in drawing to use when there is no `src`. */
art?: number
}
export interface AiImageStackProps extends Omit<
React.HTMLAttributes<HTMLDivElement>,
"onChange"
> {
images: AiStackImage[]
/** Whether the stack is spread out. Leave undefined to let it manage itself. */
expanded?: boolean
onExpandedChange?: (expanded: boolean) => void
onRemove?: (id: string) => void
/** How many images to show before folding the rest into a count. */
max?: number
}
/** Attached images as a fanned stack. Press it and the cards slide apart into a row you can remove from. */
export function AiImageStack({
images,
expanded,
onExpandedChange,
onRemove,
max = 4,
className,
...rest
}: AiImageStackProps) {
const [own, setOwn] = React.useState(false)
const open = expanded ?? own
const shown = images.slice(0, max)
const extra = images.length - shown.length
const toggle = () => {
if (expanded === undefined) setOwn(!open)
onExpandedChange?.(!open)
}
return (
<div
{...rest}
className={cx("aik aik-at-stack", className)}
data-open={open ? "" : undefined}
>
<AiKitStyles />
<AiStyle id="ai-attachments" css={CSS} />
<div className="aik-at-fan" role="list" aria-label="Attached images">
{shown.map((image, at) => {
const k = at - (shown.length - 1) / 2
return (
<div
key={image.id}
className="aik-at-shot"
role="listitem"
aria-label={image.name}
style={{ "--k": k, "--a": Math.abs(k) } as React.CSSProperties}
onClick={toggle}
>
{image.src ? (
<img src={image.src} alt={image.name} />
) : (
<AiThumbArt variant={image.art ?? at} />
)}
{extra > 0 && at === shown.length - 1 ? (
<span className="aik-at-more">+{extra}</span>
) : null}
<button
type="button"
className="aik-at-rm"
aria-label={`Remove ${image.name}`}
tabIndex={open ? 0 : -1}
onClick={(event) => {
event.stopPropagation()
onRemove?.(image.id)
}}
>
<X size={12} />
</button>
</div>
)
})}
</div>
<button
type="button"
className="aik-at-toggle"
aria-expanded={open}
onClick={toggle}
>
<Images size={14} />
<Roll>
{open
? "Stack them"
: `${images.length} image${images.length === 1 ? "" : "s"}`}
</Roll>
</button>
</div>
)
}
export interface AiDropzoneProps extends Omit<
React.HTMLAttributes<HTMLDivElement>,
"onDrop" | "title"
> {
/** Called with the dropped or chosen files. */
onFiles?: (files: File[]) => void
/** Passed to the file input, such as "image/*,.pdf". */
accept?: string
multiple?: boolean
disabled?: boolean
/** Overrides the state. Leave undefined to follow real drag events. */
status?: "idle" | "over" | "done"
/** The headline. Defaults to a line for the status. */
title?: string
/** The small line under the headline. */
hint?: string
tone?: AiTone | (string & {})
shape?: AiShape
}
const DROP_TITLE = {
idle: "Drop files here",
over: "Let go to attach",
done: "Files attached",
}
const DROP_MOOD: Record<"idle" | "over" | "done", AiMood> = {
idle: "idle",
over: "needs",
done: "happy",
}
/** A drop zone that reacts while files hover over it. It also opens the file picker on click, Enter or Space. */
export function AiDropzone({
onFiles,
accept,
multiple = true,
disabled = false,
status,
title,
hint = "PNG, TSX or PDF, up to 20 MB each",
tone = "blue",
shape = "blob",
className,
...rest
}: AiDropzoneProps) {
const input = React.useRef<HTMLInputElement>(null)
const depth = React.useRef(0)
const [over, setOver] = React.useState(false)
const state = status ?? (over ? "over" : "idle")
const browse = () => {
if (!disabled) input.current?.click()
}
return (
<div
{...rest}
className={cx("aik aik-at-drop", className)}
data-s={state}
role="button"
tabIndex={disabled ? -1 : 0}
aria-disabled={disabled}
aria-label="Attach files"
onClick={browse}
onKeyDown={(event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault()
browse()
}
}}
onDragEnter={(event) => {
event.preventDefault()
if (disabled) return
depth.current += 1
setOver(true)
}}
onDragOver={(event) => event.preventDefault()}
onDragLeave={() => {
depth.current = Math.max(0, depth.current - 1)
if (depth.current === 0) setOver(false)
}}
onDrop={(event) => {
event.preventDefault()
depth.current = 0
setOver(false)
if (!disabled && event.dataTransfer.files.length > 0)
onFiles?.(Array.from(event.dataTransfer.files))
}}
>
<AiKitStyles />
<AiStyle id="ai-attachments" css={CSS} />
<input
ref={input}
type="file"
tabIndex={-1}
aria-hidden="true"
accept={accept}
multiple={multiple}
onClick={(event) => event.stopPropagation()}
onChange={(event) => {
const files = Array.from(event.target.files ?? [])
if (files.length > 0) onFiles?.(files)
event.target.value = ""
}}
/>
<div className="aik-at-hero" aria-hidden="true">
{[
{ x: "-30px", r: "-10deg", icon: <ImageIcon size={15} /> },
{ x: "0px", r: "2deg", icon: <FileCode size={15} /> },
{ x: "30px", r: "11deg", icon: <FileText size={15} /> },
].map((ghost, at) => (
<span
key={at}
className="aik-at-ghost"
style={
{
"--gx": ghost.x,
"--gr": ghost.r,
"--i": at,
} as React.CSSProperties
}
>
{ghost.icon}
</span>
))}
<Face size={48} shape={shape} tone={tone} mood={DROP_MOOD[state]} />
{state === "done" ? <Mark status="done" /> : null}
</div>
<div className="aik-at-copy">
<Roll>{title ?? DROP_TITLE[state]}</Roll>
<span>
{state === "idle" ? (
<>
or <u>browse</u>. {hint}
</>
) : (
hint
)}
</span>
</div>
</div>
)
}
Installation
One command adds ai-attachments and the shared AI Kit layer it is built on (tokens, faces, marks and brand icons). No animation library, light and dark built in.
$npx shadcn@latest add https://tweenui.dev/r/ai-attachments.json
ai-attachments-demo
"use client"
import * as React from "react"
import { AiDemo } from "@/lib/ai-demo"
import { useAiLoop } from "@/lib/ai-kit"
import {
AiDropzone,
AiFileChips,
AiImageStack,
type AiAttachment,
type AiStackImage,
} from "@/registry/tween/ai-attachments"
const frame: React.CSSProperties = {
display: "grid",
justifyItems: "center",
width: "100%",
}
const FILES: AiAttachment[] = [
{ id: "hero", name: "hero.tsx", size: "4 KB", kind: "code" },
{ id: "ref", name: "reference.png", size: "1.8 MB", kind: "image" },
{ id: "copy", name: "launch-copy.pdf", size: "640 KB", kind: "file" },
{ id: "plans", name: "pricing.csv", size: "12 KB", kind: "sheet" },
]
function Chips() {
const root = React.useRef<HTMLDivElement>(null)
const [files, setFiles] = React.useState<AiAttachment[]>([
FILES[0]!,
{ ...FILES[1]!, progress: 0.62 },
FILES[2]!,
])
const [hands, setHands] = React.useState(false)
const patch = (id: string, change: Partial<AiAttachment>) =>
setFiles((list) =>
list.map((file) => (file.id === id ? { ...file, ...change } : file))
)
useAiLoop(
root,
async ({ sleep }) => {
const upload = async (file: AiAttachment, failAt?: number) => {
setFiles((list) => [...list, { ...file, progress: 0 }])
for (let step = 1; step <= 10; step++) {
await sleep(170)
if (failAt && step === failAt)
return patch(file.id, { error: "Upload dropped" })
patch(file.id, { progress: step / 10 })
}
await sleep(200)
patch(file.id, { progress: undefined })
}
setFiles([])
await sleep(600)
await upload(FILES[0]!)
await sleep(250)
await Promise.all([
upload(FILES[1]!),
sleep(500).then(() => upload(FILES[2]!, 6)),
])
await sleep(1200)
patch("copy", { error: undefined, progress: 0.5 })
for (let step = 6; step <= 10; step++) {
await sleep(170)
patch("copy", { progress: step / 10 })
}
await sleep(200)
patch("copy", { progress: undefined })
await sleep(900)
await upload(FILES[3]!)
await sleep(2000)
},
{ gap: 400, enabled: !hands }
)
return (
<div ref={root} style={frame} onPointerDown={() => setHands(true)}>
<AiFileChips
files={files}
onRemove={(id) =>
setFiles((list) => list.filter((file) => file.id !== id))
}
onRetry={(id) => patch(id, { error: undefined, progress: undefined })}
/>
</div>
)
}
const SHOTS: AiStackImage[] = [
{ id: "dunes", name: "Dunes at dusk", art: 0 },
{ id: "board", name: "Dashboard mock", art: 1 },
{ id: "orbit", name: "Orbit study", art: 2 },
{ id: "tide", name: "Tide lines", art: 3 },
{ id: "dunes-2", name: "Dunes, wide crop", art: 0 },
{ id: "orbit-2", name: "Orbit, second pass", art: 2 },
]
function Stack() {
const root = React.useRef<HTMLDivElement>(null)
const [images, setImages] = React.useState(SHOTS)
const [open, setOpen] = React.useState(false)
const [hands, setHands] = React.useState(false)
useAiLoop(
root,
async ({ sleep }) => {
setImages(SHOTS)
setOpen(false)
await sleep(1500)
setOpen(true)
await sleep(2600)
setOpen(false)
await sleep(900)
},
{ gap: 300, enabled: !hands, still: () => setOpen(true) }
)
return (
<div
ref={root}
style={frame}
onPointerDown={() => setHands(true)}
onKeyDown={() => setHands(true)}
>
<AiImageStack
images={images}
expanded={open}
onExpandedChange={setOpen}
onRemove={(id) =>
setImages((list) => list.filter((image) => image.id !== id))
}
/>
</div>
)
}
function Drop() {
const root = React.useRef<HTMLDivElement>(null)
const [status, setStatus] = React.useState<
"idle" | "over" | "done" | undefined
>("over")
const [title, setTitle] = React.useState<string | undefined>(undefined)
const [hands, setHands] = React.useState(false)
useAiLoop(
root,
async ({ sleep }) => {
setTitle(undefined)
setStatus("idle")
await sleep(1500)
setStatus("over")
await sleep(1900)
setStatus("done")
setTitle("3 files attached")
await sleep(2200)
},
{ gap: 300, enabled: !hands, still: () => setStatus("over") }
)
const take = () => {
if (hands) return
setHands(true)
setStatus(undefined)
setTitle(undefined)
}
return (
<div
ref={root}
style={frame}
onPointerDown={take}
onDragEnter={take}
onKeyDown={take}
>
<AiDropzone
status={status}
title={title}
onFiles={(files) => {
setStatus("done")
setTitle(
`${files.length} file${files.length === 1 ? "" : "s"} attached`
)
}}
/>
</div>
)
}
export function AiAttachmentsDemo({ only }: { only?: string }) {
return (
<AiDemo
only={only}
tabs={[
{ name: "File chips", node: <Chips /> },
{ name: "Image stack", node: <Stack /> },
{ name: "Drop zone", node: <Drop /> },
]}
/>
)
}
Installation
Usage
import {
AiDropzone,
AiFileChips,
AiImageStack,
} from "@/components/ui/ai-attachments"const [files, setFiles] = useState([
{ id: "hero", name: "hero.tsx", size: "4 KB", kind: "code" },
{ id: "ref", name: "reference.png", size: "1.8 MB", kind: "image", progress: 0.4 },
])
<AiFileChips
files={files}
onRemove={(id) => setFiles(files.filter((file) => file.id !== id))}
/>
<AiDropzone accept="image/*,.pdf" onFiles={(dropped) => upload(dropped)} />Set progress (0 to 1) while a file uploads and clear it when the upload finishes. Set error to show the failed state with a retry button.
Versions
AiFileChipsshows attached files as chips. A ring fills around the icon while a file uploads, then the tile squares off. Failed files turn red and offer retry.AiImageStackshows images as a fanned stack. Press it and the cards slide apart into a row with remove buttons. Pastmax, the last card carries a count.AiDropzonereacts to real drag events: the frame turns solid, three files rise above the face, and the headline changes. Click, Enter or Space opens the file picker.
AiThumbArt is also exported: four original SVG thumbnails for demos and placeholders.
Props
AiFileChips
| Prop | Type | Default | Description |
|---|---|---|---|
files | AiAttachment[] | { id, name, size?, kind?, progress?, error? } each. | |
onRemove | (id: string) => void | Called from a chip's remove button. | |
onRetry | (id: string) => void | Called from the retry button of a failed file. | |
emptyLabel | string | "No files attached" | Shown when there are no files. |
kind is "file", "code", "image" or "sheet" and picks the icon.
AiImageStack
| Prop | Type | Default | Description |
|---|---|---|---|
images | AiStackImage[] | { id, name, src?, art? } each. | |
expanded | boolean | Controls the spread. Omit to let the stack manage it. | |
onExpandedChange | (expanded: boolean) => void | Called when the stack opens or closes. | |
onRemove | (id: string) => void | Called from a card's remove button. | |
max | number | 4 | Cards shown before the rest fold into a count. |
AiDropzone
| Prop | Type | Default | Description |
|---|---|---|---|
onFiles | (files: File[]) => void | Called with dropped or chosen files. | |
accept | string | Passed to the file input. | |
multiple | boolean | true | Allow more than one file. |
disabled | boolean | false | Dims the zone and ignores input. |
status | "idle" | "over" | "done" | Overrides the state. Omit to follow real drag events. | |
title, hint | string | The headline and the line under it. | |
tone, shape | face look | "blue", "blob" | The face in the zone. |