Attachment
A file or picture attached to something: its media, name, what is known of it, how its upload is going and what can be done to it.
import { Attachment, AttachmentAction, AttachmentActions, AttachmentContent, AttachmentDescription, AttachmentMedia, AttachmentTitle } from "@/components/ui/attachment";
import { InkIcon } from "@/components/ui/ink-icons";
export default function AttachmentDemo() {
return (
<Attachment seed="att-demo" className="w-full max-w-sm">
<AttachmentMedia seed="att-demo-media">
<InkIcon name="file-text" />
</AttachmentMedia>
<AttachmentContent>
<AttachmentTitle>sales-dashboard.pdf</AttachmentTitle>
<AttachmentDescription>PDF · 2.4 MB</AttachmentDescription>
</AttachmentContent>
<AttachmentActions>
<AttachmentAction aria-label="Remove sales-dashboard.pdf" seed="att-demo-x">
<InkIcon name="close" />
</AttachmentAction>
</AttachmentActions>
</Attachment>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ attachment"use client";
import { mergeProps } from "@base-ui/react/merge-props";
import { useRender } from "@base-ui/react/use-render";
import type { ComponentProps } from "react";
import { cn } from "@/lib/utils";
import { penRadius, usePen, type Pen } from "@/hooks/use-ink-box";
import { InkOutline } from "@/lib/ink-outline";
import { Button } from "@/components/ui/button";
type State = "idle" | "uploading" | "processing" | "error" | "done";
/**
* A file, or a picture, attached to something: what it is, its name, what is
* known about it and what can be done to it. `state` follows an upload: the
* name pulses while it goes up or is looked over, and an error is in red pen.
*/
function Attachment({
className,
state = "done",
size = "default",
orientation = "horizontal",
children,
seed,
roughness,
passes,
radius,
corners,
draw,
weight,
speed,
...props
}: ComponentProps<"div"> & Pick<Pen, "roughness" | "passes" | "radius" | "corners" | "draw" | "weight" | "speed"> & {
state?: State;
size?: "default" | "sm" | "xs";
orientation?: "horizontal" | "vertical";
seed?: string | number;
}) {
const pen = usePen({ roughness, passes, radius, corners, draw, weight, speed });
return (
<div
data-slot="attachment"
data-state={state}
data-size={size}
data-orientation={orientation}
className={cn(
"group/attachment relative isolate flex w-fit max-w-full min-w-0 items-center gap-3 p-3 text-base text-foreground",
"data-[size=sm]:gap-2.5 data-[size=sm]:p-2.5 data-[size=xs]:gap-2 data-[size=xs]:p-1.5 data-[size=xs]:pr-2.5 data-[size=xs]:text-sm",
"data-[orientation=vertical]:flex-col data-[orientation=vertical]:items-start",
className,
)}
{...props}
>
<InkOutline pen={pen} seed={seed} estimate={[240, 64]} maxRadius={12} className={cn("text-ink-line transition-colors", state === "error" && "text-destructive")} />
{children}
</div>
);
}
/** The icon or picture: an icon is circled with the pen. */
function AttachmentMedia({ className, variant = "icon", seed, children, ...props }: ComponentProps<"div"> & { variant?: "icon" | "image"; seed?: string | number }) {
const pen = usePen({});
return (
<div
data-slot="attachment-media"
style={{ "--pen-r": `${penRadius(pen, 8)}px` } as React.CSSProperties}
data-variant={variant}
className={cn(
"relative flex size-10 shrink-0 items-center justify-center",
"group-data-[size=sm]/attachment:size-8 group-data-[size=xs]/attachment:size-6 group-data-[orientation=vertical]/attachment:h-24 group-data-[orientation=vertical]/attachment:w-full",
variant === "icon" && "text-ink-2 group-data-[orientation=vertical]/attachment:rounded-(--pen-r) group-data-[orientation=vertical]/attachment:bg-ink-5/40 [&_svg:not(.ink-sketch):not([class*='size-'])]:size-5 group-data-[size=xs]/attachment:[&_svg:not(.ink-sketch):not([class*='size-'])]:size-3.5",
variant === "image" && "overflow-hidden rounded-(--pen-r) [&_img]:size-full [&_img]:object-cover",
className,
)}
{...props}
>
{variant === "icon" && <InkOutline pen={pen} seed={seed} shape="ring" estimate={[40, 40]} pad={4} className="text-ink-3 group-data-[orientation=vertical]/attachment:hidden" />}
{children}
</div>
);
}
function AttachmentContent({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="attachment-content" className={cn("relative flex min-w-0 flex-col gap-0.5", className)} {...props} />;
}
function AttachmentTitle({ className, ...props }: ComponentProps<"div">) {
return (
<div
data-slot="attachment-title"
className={cn("truncate leading-snug font-bold group-data-[state=processing]/attachment:motion-safe:animate-pulse group-data-[state=uploading]/attachment:motion-safe:animate-pulse", className)}
{...props}
/>
);
}
function AttachmentDescription({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="attachment-description" className={cn("truncate text-sm text-ink-3 group-data-[state=error]/attachment:text-destructive", className)} {...props} />;
}
/** The buttons, at the end of the card and above its trigger. */
function AttachmentActions({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="attachment-actions" className={cn("relative z-10 ml-auto flex shrink-0 items-center gap-1", className)} {...props} />;
}
function AttachmentAction({ size = "icon-xs", variant = "ghost", ...props }: ComponentProps<typeof Button>) {
return <Button data-slot="attachment-action" variant={variant} size={size} {...props} />;
}
/** Makes the whole card something to press: render it as a link, or as a dialog's trigger. */
function AttachmentTrigger({ className, render, ...props }: useRender.ComponentProps<"button">) {
return useRender({
defaultTagName: "button",
props: mergeProps<"button">(
{
type: "button",
className: cn(
"absolute inset-0 z-0 cursor-pointer outline-none",
"focus-visible:outline-solid focus-visible:outline-[1.5px] focus-visible:outline-offset-3 focus-visible:outline-ring focus-visible:[border-radius:var(--hand-radius)]",
className,
),
},
props,
),
render,
state: { slot: "attachment-trigger" },
});
}
/** Attachments in a row that scrolls sideways, each snapping to the edge. */
function AttachmentGroup({ className, ...props }: ComponentProps<"div">) {
return (
<div
role="group"
tabIndex={0}
data-slot="attachment-group"
className={cn(
"flex w-full snap-x snap-mandatory gap-2 overflow-x-auto p-1 outline-none [scrollbar-width:none] [&::-webkit-scrollbar]:hidden",
"focus-visible:outline-solid focus-visible:outline-[1.5px] focus-visible:outline-offset-2 focus-visible:outline-ring [&>*]:shrink-0 [&>*]:snap-start",
className,
)}
{...props}
/>
);
}
export { Attachment, AttachmentMedia, AttachmentContent, AttachmentTitle, AttachmentDescription, AttachmentActions, AttachmentAction, AttachmentTrigger, AttachmentGroup };Usage
import { Attachment, AttachmentContent, AttachmentDescription, AttachmentMedia, AttachmentTitle } from "@/components/ui/attachment"
<Attachment>
<AttachmentMedia><FileTextIcon /></AttachmentMedia>
<AttachmentContent>
<AttachmentTitle>sales-dashboard.pdf</AttachmentTitle>
<AttachmentDescription>PDF · 2.4 MB</AttachmentDescription>
</AttachmentContent>
</Attachment>Examples
States
Uploading and processing pulse the name; an error is in red pen, with the reason in words.
import { Attachment, AttachmentAction, AttachmentActions, AttachmentContent, AttachmentDescription, AttachmentMedia, AttachmentTitle } from "@/components/ui/attachment";
import { InkIcon } from "@/components/ui/ink-icons";
const files = [
{ name: "field-sketches.zip", note: "Uploading · 42%", state: "uploading", icon: "upload" },
{ name: "letter-to-ada.docx", note: "Processing…", state: "processing", icon: "file-text" },
{ name: "scan-001.png", note: "Couldn't upload: the file is over 10 MB", state: "error", icon: "alert-circle" },
{ name: "notes.md", note: "Markdown · 4 KB", state: "done", icon: "file" },
] as const;
export default function AttachmentStates() {
return (
<div className="flex w-full max-w-sm flex-col gap-3">
{files.map((file) => (
<Attachment key={file.name} state={file.state} seed={`att-${file.state}`} className="w-full">
<AttachmentMedia seed={`att-m-${file.state}`}>
<InkIcon name={file.icon} />
</AttachmentMedia>
<AttachmentContent>
<AttachmentTitle>{file.name}</AttachmentTitle>
<AttachmentDescription>{file.note}</AttachmentDescription>
</AttachmentContent>
<AttachmentActions>
<AttachmentAction aria-label={`Remove ${file.name}`} seed={`att-x-${file.state}`}>
<InkIcon name="close" />
</AttachmentAction>
</AttachmentActions>
</Attachment>
))}
</div>
);
}Sizes
Default, small and extra small.
import { Attachment, AttachmentContent, AttachmentDescription, AttachmentMedia, AttachmentTitle } from "@/components/ui/attachment";
import { InkIcon } from "@/components/ui/ink-icons";
export default function AttachmentSizes() {
return (
<div className="flex flex-col items-start gap-3">
{(["default", "sm", "xs"] as const).map((size) => (
<Attachment key={size} size={size} seed={`att-size-${size}`}>
<AttachmentMedia seed={`att-size-m-${size}`}>
<InkIcon name="file-text" />
</AttachmentMedia>
<AttachmentContent>
<AttachmentTitle>report-{size}.pdf</AttachmentTitle>
{size !== "xs" && <AttachmentDescription>PDF · 1.1 MB</AttachmentDescription>}
</AttachmentContent>
</Attachment>
))}
</div>
);
}Group
A row that scrolls sideways and snaps, with pictures and files together.
import { Attachment, AttachmentContent, AttachmentDescription, AttachmentGroup, AttachmentMedia, AttachmentTitle } from "@/components/ui/attachment";
import { InkIcon } from "@/components/ui/ink-icons";
export default function AttachmentGroupExample() {
return (
<div className="w-full max-w-sm">
<AttachmentGroup aria-label="Attachments">
<Attachment orientation="vertical" seed="attg-a" className="w-40">
<AttachmentMedia variant="image">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src="/doodle-portrait.svg" alt="" />
</AttachmentMedia>
<AttachmentContent className="w-full">
<AttachmentTitle>portrait.svg</AttachmentTitle>
<AttachmentDescription>Image · 12 KB</AttachmentDescription>
</AttachmentContent>
</Attachment>
{["notes.md", "budget.xlsx", "scan.pdf"].map((name, i) => (
<Attachment key={name} orientation="vertical" seed={`attg-${i}`} className="w-40">
<AttachmentMedia seed={`attg-m-${i}`}>
<InkIcon name="file-text" />
</AttachmentMedia>
<AttachmentContent className="w-full">
<AttachmentTitle>{name}</AttachmentTitle>
<AttachmentDescription>File</AttachmentDescription>
</AttachmentContent>
</Attachment>
))}
</AttachmentGroup>
</div>
);
}With a trigger
Make the whole card open a dialog or a link; the buttons stay above it.
import { Attachment, AttachmentAction, AttachmentActions, AttachmentContent, AttachmentDescription, AttachmentMedia, AttachmentTitle, AttachmentTrigger } from "@/components/ui/attachment";
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
import { InkIcon } from "@/components/ui/ink-icons";
export default function AttachmentTriggerExample() {
return (
<Dialog>
<Attachment seed="att-trigger" className="w-full max-w-sm">
<AttachmentMedia seed="att-trigger-m">
<InkIcon name="file-text" />
</AttachmentMedia>
<AttachmentContent>
<AttachmentTitle>research-summary.pdf</AttachmentTitle>
<AttachmentDescription>PDF · 860 KB · Open to preview</AttachmentDescription>
</AttachmentContent>
<AttachmentActions>
<AttachmentAction aria-label="Remove research-summary.pdf" seed="att-trigger-x">
<InkIcon name="close" />
</AttachmentAction>
</AttachmentActions>
<DialogTrigger render={<AttachmentTrigger aria-label="Preview research-summary.pdf" />} />
</Attachment>
<DialogContent>
<DialogHeader>
<DialogTitle>research-summary.pdf</DialogTitle>
<DialogDescription>A preview goes here.</DialogDescription>
</DialogHeader>
</DialogContent>
</Dialog>
);
}API reference
- statedefault
"done" "idle" | "uploading" | "processing" | "error" | "done"Uploading and processing pulse the title; error is red pen.
- sizedefault
"default" "default" | "sm" | "xs"Padding and type size.
- orientationdefault
"horizontal" "horizontal" | "vertical"Media beside the text, or above it.
- AttachmentMedia variantdefault
"icon" "icon" | "image"An icon circled with the pen, or a picture.
- AttachmentAction
Button propsA small button; give an icon-only one an aria-label.
- AttachmentTrigger render
ReactElementA full-card overlay: render it as a link, or as a dialog's trigger. Give it an aria-label.
- seed
string | numberPins the drawing.
Pen settings
The drawing also takes draw, roughness, passes, radius, corners, weight and speed, as props or from the nearest InkProvider. What each one does.