{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "attachment",
  "title": "Attachment",
  "description": "A file as a frame of cells that never change: media, name (the extension stays in view) and detail, one action. Uploading fills the bottom keyline; processing runs the pending segment; error stops the bar in red and floods the media cell with !; an upload that lands floods it yellow with a tick for 900ms. layout=\"cell\" makes it a cell of a Bubble.",
  "dependencies": [
    "@base-ui/react@1.9.0"
  ],
  "registryDependencies": [
    "@keyline-ui/keyline",
    "utils",
    "@keyline-ui/keyline-render"
  ],
  "files": [
    {
      "path": "src/registry/keyline/ui/attachment.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { mergeProps } from \"@base-ui/react/merge-props\";\nimport { useRender } from \"@base-ui/react/use-render\";\n\nimport { cn } from \"@/lib/utils\";\nimport { resolveClassName, resolveStyle, type PartProps } from \"../lib/keyline-render\";\n\n/* A file as a frame of cells: media (an icon or a thumbnail), the name and its detail (size, progress, or the\n   reason it failed), and an action cell (cancel, retry, open). Every state is the same object; the\n   cells never change, only what is in them, so the name and the action stay put from upload to done.\n   - idle: picked, not sent; the frame is line-soft until it goes;\n   - uploading: the bottom keyline fills from the start with the working colour (a progressbar), the percentage\n     in the detail;\n   - processing: the pending segment runs along the bottom keyline (busy);\n   - error: the bar stops where the upload stopped and turns red; the media cell floods red from that keyline\n     with a ! (the reason in the detail, announced); a retry resumes the same bar;\n   - done: arriving from an upload, the media cell floods yellow with a tick for 900ms (Signal: done), then the\n     file's glyph returns.\n   layout=\"cell\" makes it a cell of a Bubble (a file sent in a message); the default draws its own control\n   frame (a composer tray, a list of files). An AttachmentTrigger makes the whole frame open the file. */\n\ntype State = \"idle\" | \"uploading\" | \"processing\" | \"error\" | \"done\";\ntype Mark = \"error\" | \"done\" | null;\ntype Size = \"default\" | \"compact\" | \"tile\";\ntype Layout = \"control\" | \"cell\";\n\ntype Ctx = { state: State; progress?: number; titleId: string; mark: Mark; size: Size; layout: Layout };\nconst AttachmentContext = React.createContext<Ctx>({\n  state: \"done\",\n  titleId: \"\",\n  mark: null,\n  size: \"default\",\n  layout: \"control\",\n});\n\n/* ------------------------------------------------------------------ *\n * CLASSES\n * The frame's own state (status, size, layout) is mirrored as data-status, data-size and data-kl-layout on the\n * root; the cells read it from context, so each cell's classes are chosen here and sit on the cell itself.\n * ------------------------------------------------------------------ */\n\n/** What sits between cells but is not one: the bar, the pending segment and the overlay trigger. */\nconst frame = cn(\n  \"kl-ctl flex min-h-12 w-fit max-w-full min-w-0 items-stretch bg-kl-cell text-kl-ink\",\n  \"[--kl-att-x:var(--kl-msg-x,var(--kl-inset))]\",\n  // seams between cells: zero specificity on the cell (:where), so the states and the tile layout restyle them\n  \"[&>:where(:not([data-slot=attachment-bar],.kl-pending,[data-slot=attachment-trigger])+:not([data-slot=attachment-bar],.kl-pending,[data-slot=attachment-trigger]))]:border-s-(length:--kl-k1)\",\n  \"[&>:where(:not([data-slot=attachment-bar],.kl-pending,[data-slot=attachment-trigger])+:not([data-slot=attachment-bar],.kl-pending,[data-slot=attachment-trigger]))]:border-s-kl-line\",\n  \"data-[status=error]:border-kl-red data-[status=idle]:border-kl-line-soft data-[status=idle]:*:border-kl-line-soft\",\n  // a file in a message: a cell of its bubble. It fills the bubble's width; seams in the bubble's seam tone, and a\n  // failed one keeps them (the red ! cell and bar say it)\n  \"data-[kl-layout=cell]:w-auto data-[kl-layout=cell]:self-stretch data-[kl-layout=cell]:bg-transparent\",\n  \"data-[kl-layout=cell]:data-[status=error]:border-kl-seam\",\n  \"data-[kl-layout=cell]:[&>:where(:not([data-slot=attachment-bar],.kl-pending,[data-slot=attachment-trigger])+:not([data-slot=attachment-bar],.kl-pending,[data-slot=attachment-trigger]))]:border-s-kl-seam\",\n  // the whole frame opens the file: it tints under the pointer (a cell presses, on its bubble's tint) and presses\n  \"has-[>[data-slot=attachment-trigger]:hover:not(:active)]:bg-kl-tint\",\n  \"data-[kl-layout=cell]:has-[>[data-slot=attachment-trigger]:hover]:bg-kl-press\",\n  \"has-[>[data-slot=attachment-trigger]:active]:bg-kl-press\",\n  // compact: the composer row; tile: media above the name\n  \"data-[size=compact]:min-h-9\",\n  \"data-[size=tile]:w-[136px] data-[size=tile]:flex-col\",\n  \"data-[size=tile]:[&>:not(:first-child,[data-slot=attachment-bar],.kl-pending,[data-slot=attachment-trigger],[data-slot=attachment-actions])]:border-s-0\",\n  \"data-[size=tile]:[&>:not(:first-child,[data-slot=attachment-bar],.kl-pending,[data-slot=attachment-trigger],[data-slot=attachment-actions])]:border-t-(length:--kl-k1)\",\n  \"data-[size=tile]:[&>:not(:first-child,[data-slot=attachment-bar],.kl-pending,[data-slot=attachment-trigger],[data-slot=attachment-actions])]:border-t-kl-line\",\n);\n\n/** PROGRESS: the bottom keyline fills (a scaled segment, so it runs on the compositor), inside the frame, so it reads\n *  as the line thickening. It keeps its length when the upload fails and turns red; a retry resumes it. */\nconst bar = \"pointer-events-none absolute inset-x-0 bottom-0 z-1 h-[3px] overflow-hidden\";\nconst barFill = cn(\n  \"block h-full origin-left bg-kl-yellow forced-colors:bg-[Highlight] kl-rtl:origin-right\",\n  \"[transition:transform_var(--kl-t-unfold)_var(--kl-ease-out),background-color_var(--kl-t-flood)_linear]\",\n);\n\n/** the media cell; in a message its glyph starts on the text's leading edge */\nconst media = \"relative grid w-11 flex-none place-items-center overflow-hidden text-kl-ink-2\";\n/* the 16px glyphs carry 3px of their own side bearing: the cell starts their ink, not their box, on the text edge */\nconst mediaCell = cn(\n  \"w-[calc(2*var(--kl-att-x)+10px)] justify-items-start ps-[calc(var(--kl-att-x)-3px)]\",\n  \"[&>img]:ms-[calc(3px-var(--kl-att-x))] [&>img]:justify-self-stretch\",\n);\nconst mediaImg = \"[&>img]:block [&>img]:size-full [&>img]:object-cover\";\n\n/** the outcome lands in the media cell, flooding up from the bottom keyline the bar ran along */\nconst mark = cn(\n  \"pointer-events-none absolute inset-0 grid place-items-center bg-kl-yellow text-[15px] font-(--kl-weight-display) text-kl-on-yellow\",\n  \"[clip-path:circle(0px_at_50%_100%)] [transition:clip-path_var(--kl-t-unflood)_var(--kl-ease-in)]\",\n  \"data-[kind=error]:bg-kl-red data-[kind=error]:text-kl-on-red\",\n  \"forced-colors:[forced-color-adjust:none] forced-colors:not-data-[kind=error]:bg-[Mark] forced-colors:not-data-[kind=error]:text-[MarkText]\",\n);\nconst markOpen = \"[clip-path:circle(150%_at_50%_100%)] [transition:clip-path_var(--kl-t-flood)_var(--kl-ease-out)]\";\n\n/* padding is physical, as the stylesheet had it: the extra 2px sits on the right in both directions */\nconst content =\n  \"grid min-w-[20ch] content-center justify-items-start gap-0.5 pt-1.5 pr-[calc(var(--kl-inset)+2px)] pb-[7px] pl-(--kl-inset)\";\n\nconst title = \"flex max-w-[min(100%,32ch)] min-w-0 text-[13px]/[1.25] font-(--kl-weight-strong)\";\n\n/** the detail never sets the width (the name does): a percentage or a reason cannot make the frame jump */\nconst description = cn(\n  \"min-w-0 justify-self-stretch overflow-hidden text-start text-ellipsis whitespace-nowrap [contain:inline-size]\",\n  \"font-(family-name:--kl-font-mono) text-[11px]/[1.25] font-medium text-kl-ink-2 not-italic tabular-nums\",\n);\n\nconst actions = \"relative z-2 flex-none\";\nconst actionsTile = cn(\n  \"absolute end-0 top-0 bg-kl-raised\",\n  \"border-s-(length:--kl-k1) border-b-(length:--kl-k1) border-s-kl-line border-b-kl-line\",\n);\n\nconst ring = \"focus-visible:outline-(length:--kl-ring) focus-visible:outline-kl-focus focus-visible:outline-solid\";\nconst action = cn(\n  \"kl-key box-border grid w-10 cursor-pointer appearance-none place-items-center bg-transparent p-0 text-kl-ink-2\",\n  \"hover:bg-kl-tint hover:text-kl-ink\",\n  ring,\n  \"focus-visible:outline-offset-(--kl-ring-in)\",\n);\n\nconst trigger = cn(\n  \"absolute inset-0 z-1 cursor-pointer appearance-none bg-transparent p-0\",\n  ring,\n  \"focus-visible:outline-offset-(--kl-ring-out)\",\n);\n\n/* ------------------------------------------------------------------ *\n * PARTS\n * ------------------------------------------------------------------ */\n\ntype AttachmentState = { state: State; size: Size; layout: Layout; mark: Mark };\ntype AttachmentPartState = Record<string, never>;\nconst PART_STATE: AttachmentPartState = {};\n\ntype AttachmentProps = PartProps<\"div\", AttachmentState> & {\n  state?: State;\n  /** 0–100 while uploading; on error, where it stopped. */\n  progress?: number;\n  /** default: one row. compact: a smaller row for a composer. tile: media above the name (thumbnails). */\n  size?: Size;\n  /** control (default): its own frame. cell: a cell of the Bubble it sits in. */\n  layout?: Layout;\n};\n\nconst DONE_MS = 900;\n\nfunction Attachment({\n  state = \"done\",\n  progress,\n  size = \"default\",\n  layout = \"control\",\n  render,\n  className,\n  style,\n  children,\n  ...props\n}: AttachmentProps): React.ReactElement {\n  const titleId = React.useId();\n  // The done flash plays only when an upload lands, never for a file that was already there.\n  const prev = React.useRef(state);\n  const [flash, setFlash] = React.useState(false);\n  React.useEffect(() => {\n    const was = prev.current;\n    prev.current = state;\n    if (state === \"done\" && (was === \"uploading\" || was === \"processing\")) {\n      setFlash(true);\n      const t = setTimeout(() => setFlash(false), DONE_MS);\n      return () => clearTimeout(t);\n    }\n    setFlash(false);\n  }, [state]);\n  const outcome: Mark = state === \"error\" ? \"error\" : flash ? \"done\" : null;\n  const pct = Math.max(0, Math.min(100, progress ?? 0));\n  const showBar = state === \"uploading\" || state === \"error\" || flash;\n  const partState: AttachmentState = { state, size, layout, mark: outcome };\n  const own = {\n    \"data-slot\": \"attachment\",\n    \"data-status\": state,\n    \"data-size\": size,\n    \"data-kl-layout\": layout,\n    \"data-mark\": outcome ?? undefined,\n    \"aria-busy\": state === \"uploading\" || state === \"processing\" || undefined,\n    className: resolveClassName(frame, className)(partState),\n    style: resolveStyle(undefined, style)(partState),\n    children: (\n      <>\n        {children}\n        {showBar ? (\n          <span\n            data-slot=\"attachment-bar\"\n            className={bar}\n            role={state === \"uploading\" ? \"progressbar\" : undefined}\n            aria-labelledby={state === \"uploading\" ? titleId : undefined}\n            aria-valuemin={state === \"uploading\" ? 0 : undefined}\n            aria-valuemax={state === \"uploading\" ? 100 : undefined}\n            aria-valuenow={state === \"uploading\" && progress != null ? Math.round(pct) : undefined}\n            aria-hidden={state === \"uploading\" ? undefined : true}\n          >\n            <span\n              className={cn(barFill, state === \"error\" && \"bg-kl-red\")}\n              style={{ transform: `scaleX(${flash ? 1 : pct / 100})` }}\n            />\n          </span>\n        ) : null}\n        {state === \"processing\" ? <span className=\"kl-pending [--kl-bw:3px]\" aria-hidden /> : null}\n      </>\n    ),\n  };\n  const el = useRender({ defaultTagName: \"div\", render, state: partState, props: mergeProps<\"div\">(props, own) });\n  return (\n    <AttachmentContext.Provider value={{ state, progress, titleId, mark: outcome, size, layout }}>\n      {el}\n    </AttachmentContext.Provider>\n  );\n}\n\n/** The media cell: a 16px glyph, or an <img> (a thumbnail; give it alt=\"\" when the name says what it is). It is also\n *  where the outcome lands: it floods red with ! on failure, and yellow with a tick when an upload completes. */\nfunction AttachmentMedia({\n  render,\n  className,\n  style,\n  children,\n  ...props\n}: PartProps<\"div\", AttachmentPartState>): React.ReactElement {\n  const { mark: outcome, state, size, layout } = React.useContext(AttachmentContext);\n  // keep the last mark's glyph while the flood retracts\n  const last = React.useRef<Exclude<Mark, null>>(\"done\");\n  if (outcome) last.current = outcome;\n  const base = cn(\n    media,\n    layout === \"cell\" && mediaCell,\n    mediaImg,\n    (state === \"uploading\" || state === \"processing\") && \"[&>img]:opacity-60\",\n    size === \"compact\" && \"w-[34px]\",\n    size === \"tile\" && \"aspect-[4/3] w-full\",\n  );\n  const own = {\n    \"data-slot\": \"attachment-media\",\n    className: resolveClassName(base, className)(PART_STATE),\n    style: resolveStyle(undefined, style)(PART_STATE),\n    children: (\n      <>\n        {children}\n        <span\n          data-slot=\"attachment-mark\"\n          className={cn(mark, outcome && markOpen)}\n          data-kind={last.current}\n          aria-hidden\n        >\n          {last.current === \"error\" ? (\n            \"!\"\n          ) : (\n            <svg\n              viewBox=\"0 0 16 16\"\n              width=\"16\"\n              height=\"16\"\n              fill=\"none\"\n              stroke=\"currentColor\"\n              strokeWidth=\"2.25\"\n              strokeLinecap=\"square\"\n            >\n              <path d=\"m3.5 8.5 3 3 6-7\" />\n            </svg>\n          )}\n        </span>\n      </>\n    ),\n  };\n  return useRender({ defaultTagName: \"div\", render, state: PART_STATE, props: mergeProps<\"div\">(props, own) });\n}\n\nfunction AttachmentContent({\n  render,\n  className,\n  style,\n  ...props\n}: PartProps<\"div\", AttachmentPartState>): React.ReactElement {\n  const { size, layout } = React.useContext(AttachmentContext);\n  const base = cn(content, layout === \"cell\" && \"flex-auto\", size === \"compact\" && \"pt-[3px] pb-1\");\n  const own = {\n    \"data-slot\": \"attachment-content\",\n    className: resolveClassName(base, className)(PART_STATE),\n    style: resolveStyle(undefined, style)(PART_STATE),\n  };\n  return useRender({ defaultTagName: \"div\", render, state: PART_STATE, props: mergeProps<\"div\">(props, own) });\n}\n\n/** The file name; a long name keeps its extension in view (end-truncation would hide it). Its direction\n *  follows its own text (a Latin name stays \"CMR.pdf\" in an RTL page). */\nfunction AttachmentTitle({\n  render,\n  className,\n  style,\n  children,\n  ...props\n}: PartProps<\"span\", AttachmentPartState>): React.ReactElement {\n  const { titleId } = React.useContext(AttachmentContext);\n  const text = typeof children === \"string\" ? children : null;\n  const dot = text ? text.lastIndexOf(\".\") : -1;\n  // the id, dir and title are the part's defaults: a consumer's props come after them\n  const defaults = { id: titleId, dir: \"auto\", title: text ?? undefined };\n  const own = {\n    \"data-slot\": \"attachment-title\",\n    className: resolveClassName(title, className)(PART_STATE),\n    style: resolveStyle(undefined, style)(PART_STATE),\n    children:\n      text && dot > 0 && text.length - dot <= 6 ? (\n        <>\n          <span data-slot=\"attachment-title-stem\" className=\"min-w-0 truncate\">\n            {text.slice(0, dot)}\n          </span>\n          <span data-slot=\"attachment-title-extension\" className=\"flex-none whitespace-nowrap\">\n            {text.slice(dot)}\n          </span>\n        </>\n      ) : (\n        <span data-slot=\"attachment-title-stem\" className=\"min-w-0 truncate\">\n          {children}\n        </span>\n      ),\n  };\n  return useRender({\n    defaultTagName: \"span\",\n    render,\n    state: PART_STATE,\n    props: mergeProps<\"span\">(defaults, props, own),\n  });\n}\n\n/** Size and type in the Code voice; while uploading it adds the percentage, on error it is the reason (announced).\n *  It never widens the frame: the name sets the width, and a long reason wraps. */\nfunction AttachmentDescription({\n  render,\n  className,\n  style,\n  children,\n  ...props\n}: PartProps<\"span\", AttachmentPartState>): React.ReactElement {\n  const { state, progress } = React.useContext(AttachmentContext);\n  const base = cn(description, state === \"error\" && \"whitespace-normal text-kl-red-text\");\n  const own = {\n    \"data-slot\": \"attachment-description\",\n    role: state === \"error\" ? \"alert\" : undefined,\n    className: resolveClassName(base, className)(PART_STATE),\n    style: resolveStyle(undefined, style)(PART_STATE),\n    children: (\n      // the words take their own direction; the line keeps the row's, so it starts on the row's start edge\n      <bdi>\n        {children}\n        {state === \"uploading\" && progress != null ? <span> · {Math.round(progress)}%</span> : null}\n        {state === \"processing\" ? <span> · Processing</span> : null}\n      </bdi>\n    ),\n  };\n  return useRender({ defaultTagName: \"span\", render, state: PART_STATE, props: mergeProps<\"span\">(props, own) });\n}\n\n/** The action cell(s), each a square key on a seam (cancel, retry, open, remove). Name each button. Keep one\n *  action in every state of a sent file, so the frame keeps its width while the action changes. */\nfunction AttachmentActions({\n  render,\n  className,\n  style,\n  ...props\n}: PartProps<\"div\", AttachmentPartState>): React.ReactElement {\n  const { size } = React.useContext(AttachmentContext);\n  const own = {\n    \"data-slot\": \"attachment-actions\",\n    className: resolveClassName(cn(\"kl-cells\", actions, size === \"tile\" && actionsTile), className)(PART_STATE),\n    style: resolveStyle(undefined, style)(PART_STATE),\n  };\n  return useRender({ defaultTagName: \"div\", render, state: PART_STATE, props: mergeProps<\"div\">(props, own) });\n}\n\nfunction AttachmentAction({\n  render,\n  className,\n  style,\n  type,\n  ...props\n}: PartProps<\"button\", AttachmentPartState> & { \"aria-label\": string }): React.ReactElement {\n  const { size, layout } = React.useContext(AttachmentContext);\n  const base = cn(\n    action,\n    layout === \"cell\" && \"hover:bg-kl-press\",\n    size === \"compact\" && \"w-8\",\n    size === \"tile\" && \"size-7\",\n  );\n  const own = {\n    \"data-slot\": \"attachment-action\",\n    type: render ? type : (type ?? \"button\"),\n    className: resolveClassName(base, className)(PART_STATE),\n    style: resolveStyle(undefined, style)(PART_STATE),\n  };\n  return useRender({ defaultTagName: \"button\", render, state: PART_STATE, props: mergeProps<\"button\">(props, own) });\n}\n\n/** Makes the whole frame open the file: an overlay button or link (render={<a href=…/>}) under the actions. Name it. */\nfunction AttachmentTrigger({\n  render,\n  className,\n  style,\n  type,\n  ...props\n}: PartProps<\"button\", AttachmentPartState>): React.ReactElement {\n  const { layout } = React.useContext(AttachmentContext);\n  const base = cn(trigger, layout === \"cell\" && \"focus-visible:outline-offset-(--kl-ring-in)\");\n  const own = {\n    \"data-slot\": \"attachment-trigger\",\n    type: render ? type : (type ?? \"button\"),\n    className: resolveClassName(base, className)(PART_STATE),\n    style: resolveStyle(undefined, style)(PART_STATE),\n  };\n  return useRender({ defaultTagName: \"button\", render, state: PART_STATE, props: mergeProps<\"button\">(props, own) });\n}\n\nconst Glyph = {\n  file: \"M4 1.5h5.5L12.5 4.5v10h-8.5zM9.5 1.5v3h3\",\n  image: \"M2 3h12v10H2zM2 11l3.5-3.5 3 3 2-2L14 12M10.5 6.5h.01\",\n  close: \"M4 4l8 8M12 4l-8 8\",\n  retry: \"M13 8a5 5 0 1 1-1.6-3.7M13 2.5v3h-3\",\n  download: \"M8 2v8.5M4.5 7l3.5 3.5L11.5 7M2.5 13.5h11\",\n};\nfunction AttachmentGlyph({ name }: { name: keyof typeof Glyph }) {\n  return (\n    <svg\n      viewBox=\"0 0 16 16\"\n      width=\"16\"\n      height=\"16\"\n      fill=\"none\"\n      stroke=\"currentColor\"\n      strokeWidth=\"1.75\"\n      strokeLinecap=\"square\"\n      aria-hidden\n    >\n      <path d={Glyph[name]} />\n    </svg>\n  );\n}\n\nexport {\n  Attachment,\n  AttachmentMedia,\n  AttachmentContent,\n  AttachmentTitle,\n  AttachmentDescription,\n  AttachmentActions,\n  AttachmentAction,\n  AttachmentTrigger,\n  AttachmentGlyph,\n};\n",
      "type": "registry:ui",
      "target": "components/keyline/ui/attachment.tsx"
    }
  ],
  "type": "registry:ui"
}