shadimage
blode
uiattachmentsource: ok · refreshed

Attachment

A card for files and images with upload, processing, error, and done states, an optional full-card trigger, and a scrollable group.

npx shadcn add "https://blode.co/ui/r/attachment.json"
free

Where it goes

stats

Interaction

animated-counter

Tags auto-generated by rules on 9/24/2026 — empty means no evidence, not lack of support.

Dependencies

Bundle weight · light
ui-primitives@base-ui/react

Registry dependencies

button

Files1 · source cached 10/8/2026, 4:25:07 AM

ui/attachment.tsx
import { mergeProps } from "@base-ui/react/merge-props";
import { useRender } from "@base-ui/react/use-render";
import { cva } from "class-variance-authority";
import type { VariantProps } from "class-variance-authority";
import type * as React from "react";

import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";

const attachmentVariants = cva(
  "group/attachment relative flex w-fit max-w-full min-w-0 shrink-0 flex-wrap rounded-2xl border bg-card text-card-foreground transition-colors focus-within:ring-1 focus-within:ring-ring/30 has-[>a,>button]:hover:bg-muted/50 data-[state=error]:border-destructive/30 data-[state=idle]:border-dashed",
  {
    variants: {
      orientation: {
        horizontal: "min-w-40 items-center",
        vertical: "w-24 flex-col has-data-[slot=attachment-content]:w-30",
      },
      size: {
        default:
          "gap-2 text-sm has-data-[slot=attachment-content]:px-2.5 has-data-[slot=attachment-content]:py-2 has-data-[slot=attachment-media]:p-2",
        sm: "gap-2.5 text-xs has-data-[slot=attachment-content]:px-2 has-data-[slot=attachment-content]:py-1.5 has-data-[slot=attachment-media]:p-1.5",
        xs: "gap-1.5 rounded-xl text-xs has-data-[slot=attachment-content]:px-1.5 has-data-[slot=attachment-content]:py-1 has-data-[slot=attachment-media]:p-1",
      },
    },
  },
);

const Attachment = ({
  className,
  state = "done",
  size = "default",
  orientation = "horizontal",
  ...props
}: React.ComponentProps<"div"> &
  VariantProps<typeof attachmentVariants> & {
    state?: "idle" | "uploading" | "processing" | "error" | "done";
  }) => (
  <div
    className={cn(attachmentVariants({ orientation, size }), className)}
    data-orientation={orientation}
    data-size={size}
    data-slot="attachment"
    data-state={state}
    {...props}
  />
);

const attachmentMediaVariants = cva(
  "relative flex aspect-square w-10 shrink-0 items-center justify-center overflow-hidden rounded-lg bg-muted text-foreground group-data-[orientation=vertical]/attachment:w-full group-data-[size=sm]/attachment:w-8 group-data-[size=xs]/attachment:w-7 group-data-[size=xs]/attachment:rounded-md group-data-[state=error]/attachment:bg-destructive/10 group-data-[state=error]/attachment:text-destructive group-data-[orientation=vertical]/attachment:*:data-[slot=spinner]:size-6! [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4 group-data-[orientation=vertical]/attachment:[&_svg:not([class*='size-'])]:size-6 group-data-[size=xs]/attachment:[&_svg:not([class*='size-'])]:size-3.5",
  {
    defaultVariants: {
      variant: "icon",
    },
    variants: {
      variant: {
        icon: "",
        image:
          "opacity-60 group-data-[state=done]/attachment:opacity-100 group-data-[state=idle]/attachment:opacity-100 *:[img]:aspect-square *:[img]:w-full *:[img]:object-cover",
      },
    },
  },
);

const AttachmentMedia = ({
  className,
  variant = "icon",
  ...props
}: React.ComponentProps<"div"> & VariantProps<typeof attachmentMediaVariants>) => (
  <div
    className={cn(attachmentMediaVariants({ variant }), className)}
    data-slot="attachment-media"
    data-variant={variant}
    {...props}
  />
);

const AttachmentContent = ({ className, ...props }: React.ComponentProps<"div">) => (
  <div
    className={cn(
      "max-w-full min-w-0 flex-1 leading-tight group-data-[orientation=vertical]/attachment:px-1",
      className,
    )}
    data-slot="attachment-content"
    {...props}
  />
);

const AttachmentTitle = ({ className, ...props }: React.ComponentProps<"span">) => (
  <span
    className={cn(
      "block max-w-full min-w-0 truncate font-medium group-data-[state=processing]/attachment:shimmer group-data-[state=uploading]/attachment:shimmer",
      className,
    )}
    data-slot="attachment-title"
    {...props}
  />
);

const AttachmentDescription = ({ className, ...props }: React.ComponentProps<"span">) => (
  <span
    className={cn(
      "mt-0.5 block min-w-0 truncate text-xs text-muted-foreground group-data-[state=error]/attachment:text-destructive/80",
      "max-w-full",
      className,
    )}
    data-slot="attachment-description"
    {...props}
  />
);

const AttachmentActions = ({ className, ...props }: React.ComponentProps<"div">) => (
  <div
    className={cn(
      "relative z-20 flex shrink-0 items-center group-data-[orientation=vertical]/attachment:absolute group-data-[orientation=vertical]/attachment:top-3 group-data-[orientation=vertical]/attachment:right-3 group-data-[orientation=vertical]/attachment:gap-1",
      className,
    )}
    data-slot="attachment-actions"
    {...props}
  />
);

const AttachmentAction = ({
  className,
  variant,
  size = "icon-xs",
  ...props
}: React.ComponentProps<typeof Button>) => (
  <Button
    className={cn(className)}
    data-slot="attachment-action"
    size={size}
    variant={variant ?? "ghost"}
    {...props}
  />
);

const AttachmentTrigger = ({
  className,
  render,
  type,
  ...props
}: useRender.ComponentProps<"button">) =>
  useRender({
    defaultTagName: "button",
    props: mergeProps<"button">(
      {
        className: cn("absolute inset-0 z-10 outline-none", className),
        type: render ? type : (type ?? "button"),
      },
      props,
    ),
    render,
    state: {
      slot: "attachment-trigger",
    },
  });

const AttachmentGroup = ({ className, ...props }: React.ComponentProps<"div">) => (
  <div
    className={cn(
      "flex min-w-0 scroll-fade-x snap-x snap-mandatory scroll-px-1 scrollbar-none gap-3 overflow-x-auto overscroll-x-contain py-1 *:data-[slot=attachment]:flex-none *:data-[slot=attachment]:snap-start",
      className,
    )}
    data-slot="attachment-group"
    {...props}
  />
);

export {
  Attachment,
  AttachmentGroup,
  AttachmentMedia,
  AttachmentContent,
  AttachmentTitle,
  AttachmentDescription,
  AttachmentActions,
  AttachmentAction,
  AttachmentTrigger,
};

More from blode

Browse all components in this library →