{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "citation",
  "type": "registry:ui",
  "title": "Citation",
  "description": "An inline source marker for a sentence that makes a claim. The reader taps or tabs to the marker and a popover lists every source behind the claim: who published it, what kind of source it is, and the exact words quoted from it. Several sources stack in…",
  "author": "ofri-peretz <https://github.com/ofri-peretz>",
  "categories": [
    "blog",
    "pattern"
  ],
  "dependencies": [],
  "registryDependencies": [
    "https://ds.interlace.tools/r/interlace-base.json",
    "https://ds.interlace.tools/r/badge.json",
    "https://ds.interlace.tools/r/cn.json",
    "https://ds.interlace.tools/r/popover.json"
  ],
  "files": [
    {
      "path": "registry/interlace-ui/patterns/citation.tsx",
      "target": "components/ui/patterns/citation.tsx",
      "type": "registry:ui",
      "content": "'use client';\n\nimport * as React from 'react';\n\n// @interlace/citation v1.0.0 — Interlace design system.\n// Docs, props and live preview: https://ds.interlace.tools/c/citation\n// What changed since: https://ds.interlace.tools/c/citation#history\n// Generated banner — keep it, the upgrade diff reads this version.\n\n/**\n * @interlace/ui — Citation\n *\n * An inline source marker for a sentence that makes a claim. The reader taps\n * or tabs to the marker and a popover lists every source behind the claim:\n * who published it, what kind of source it is, and the exact words quoted\n * from it. Several sources stack in one popover, in the order given.\n *\n * Built for writing that has to show its work (explainers, fact checks,\n * reports): the sentence stays readable, the evidence is one keystroke away.\n *\n * ## Anatomy\n *\n *   Citation                          (span — data-slot=\"citation\")\n *     ├─ {children}                   (the claim text; inside the trigger for `marker=\"underline\"`)\n *     └─ Popover\n *         ├─ PopoverTrigger           (sup > button for `number`, the underlined text for `underline`)\n *         └─ PopoverContent           (data-min-viewport=320)\n *             ├─ status Badge         (optional: enough / short of sources)\n *             └─ ol > CitationSource  (publisher link, kind, blockquote)\n *\n * ## Markers\n *\n * - `number` (default): the text renders as plain text and a superscript\n *   number follows it, like a footnote. Use it in dense prose.\n * - `underline`: the text itself is the trigger, with a dotted underline.\n *   Use it when the whole phrase is the claim.\n *\n * ## Safety\n *\n * Only `https:` URLs become links. Anything else (an `http:` page, a\n * `javascript:` string from bad data) renders the publisher as plain text,\n * so a malformed source can never put a dangerous link in front of a reader.\n *\n * ## Server rendering\n *\n * The claim text and the marker are in the server HTML; only the popover\n * needs the client. Without JavaScript the reader still sees the sentence and\n * the marker.\n *\n * ## MIN_VIEWPORT — 320\n *\n * The popover is `w-80` capped at `100vw - 2rem`, and Base UI's positioner\n * shifts it into view, so it fits the 320 floor.\n *\n * | Rule | Concept                          | Where in this file                                          |\n * | ---- | -------------------------------- | ----------------------------------------------------------- |\n * | R4   | Extends native el                | `React.ComponentProps<'span'>` on the root                  |\n * | R6   | data-slot per part               | citation / -trigger / -content / -source                    |\n * | R7   | cn + ...rest                     | `cn(…, className)` + `{...props}` on the root               |\n * | R8   | Enums, not booleans              | `marker`, `status`                                          |\n * | R12  | Reuse over wrap                  | Popover owns dismissal, focus return, positioning           |\n * | R14  | Declares min viewport            | `data-min-viewport` on the popup + exported const           |\n * | R19  | Tokens only                      | `text-primary`, `border-border`, Badge tones                |\n * | R25  | Client component                 | Required — Popover ships client hooks                       |\n * | R26  | Keyboard + touch                 | Native button trigger: Enter/Space/tap open, Escape closes  |\n */\n\nimport { cn } from '@/lib/utils';\nimport { Badge } from '@/components/ui/badge';\nimport { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';\n\nexport const MIN_VIEWPORT = 320 as const;\n\nexport interface CitationSourceData {\n  /** Who published it: \"Reuters\", \"UN OCHA\", \"The Times of Israel\". */\n  publisher: string;\n  /** Link to the page the quote comes from. Only `https:` URLs render as links. */\n  url?: string;\n  /** What kind of source: \"news report\", \"official record\", \"academic paper\". */\n  kind?: string;\n  /** The exact words from the source. Rendered as a blockquote. */\n  quote?: React.ReactNode;\n  /** Extra detail after the kind, such as \"AP wire copy\". */\n  note?: string;\n}\n\nexport type CitationStatus = 'enough' | 'short';\n\nconst STATUS_LABEL: Record<CitationStatus, string> = {\n  enough: 'Enough sources',\n  short: 'Short of sources',\n};\n\n/** `https:` only. Anything else renders as plain text. */\nexport const safeHref = (url: string | undefined): string | undefined => {\n  if (!url) return undefined;\n  try {\n    return new URL(url).protocol === 'https:' ? url : undefined;\n  } catch {\n    return undefined;\n  }\n};\n\nexport type CitationProps = Omit<React.ComponentProps<'span'>, 'children'> & {\n  /** The claim text the sources back. */\n  children: React.ReactNode;\n  /** One or more sources. They stack in the popover in this order. */\n  sources: CitationSourceData[];\n  /** `number` puts a footnote number after the text; `underline` makes the text the trigger. @default 'number' */\n  marker?: 'number' | 'underline';\n  /** The footnote number for `marker=\"number\"`. @default 1 */\n  number?: number;\n  /** Whether the claim meets your sourcing bar. `short` shows a warning badge. */\n  status?: CitationStatus;\n  /** Replace the status badge text, for example to translate it. */\n  statusLabel?: string;\n  /** Accessible name of the trigger. @default \"Sources (n)\" */\n  triggerLabel?: string;\n};\n\nfunction CitationSource({ source }: { source: CitationSourceData }) {\n  const href = safeHref(source.url);\n  const detail = [source.kind, source.note].filter(Boolean).join(', ');\n  return (\n    <li data-slot=\"citation-source\" className=\"space-y-2\">\n      <p className=\"text-sm\">\n        {href ? (\n          <a\n            href={href}\n            target=\"_blank\"\n            rel=\"noopener noreferrer\"\n            className=\"text-primary font-medium underline underline-offset-4\"\n          >\n            {source.publisher}\n          </a>\n        ) : (\n          <span className=\"font-medium\">{source.publisher}</span>\n        )}\n        {detail ? <span className=\"text-muted-foreground\"> ({detail})</span> : null}\n      </p>\n      {source.quote ? (\n        <blockquote className=\"border-border text-muted-foreground border-l-2 pl-3 text-sm italic\">\n          {source.quote}\n        </blockquote>\n      ) : null}\n    </li>\n  );\n}\n\nfunction Citation({\n  children,\n  sources,\n  marker = 'number',\n  number = 1,\n  status,\n  statusLabel,\n  triggerLabel,\n  className,\n  ...props\n}: CitationProps) {\n  const label = triggerLabel ?? `Sources (${sources.length})`;\n  const content = (\n    <PopoverContent\n      data-slot=\"citation-content\"\n      align=\"start\"\n      className=\"max-h-96 w-80 max-w-[calc(100vw-2rem)] space-y-3 overflow-y-auto\"\n    >\n      {status ? (\n        <Badge variant={status === 'short' ? 'warning' : 'success'}>\n          {statusLabel ?? STATUS_LABEL[status]}\n        </Badge>\n      ) : null}\n      <ol className=\"space-y-4\">\n        {sources.map((source, i) => (\n          <CitationSource key={`${source.publisher}-${i}`} source={source} />\n        ))}\n      </ol>\n    </PopoverContent>\n  );\n\n  return (\n    <span data-slot=\"citation\" data-marker={marker} className={cn('inline', className)} {...props}>\n      <Popover>\n        {marker === 'underline' ? (\n          // A span, not a button: a button is an atomic inline box, so a\n          // long phrase would refuse to wrap across lines. Base UI gives the\n          // span role=\"button\", tabIndex and Enter/Space handling.\n          <PopoverTrigger\n            data-slot=\"citation-trigger\"\n            nativeButton={false}\n            render={<span />}\n            className=\"decoration-muted-foreground hover:decoration-foreground cursor-help rounded-sm underline decoration-dotted underline-offset-4\"\n          >\n            {children}\n            <span className=\"sr-only\">, {label}</span>\n          </PopoverTrigger>\n        ) : (\n          <>\n            {children}\n            <sup className=\"ms-0.5\">\n              <PopoverTrigger\n                data-slot=\"citation-trigger\"\n                aria-label={label}\n                className=\"text-primary hover:bg-accent inline-flex min-h-6 min-w-6 items-center justify-center rounded-sm px-0.5 text-xs font-semibold tabular-nums\"\n              >\n                [{number}]\n              </PopoverTrigger>\n            </sup>\n          </>\n        )}\n        {content}\n      </Popover>\n    </span>\n  );\n}\n\nexport { Citation, CitationSource };\n"
    }
  ],
  "meta": {
    "tier": "pattern",
    "client": true,
    "minViewport": 320,
    "loading": false,
    "version": "1.0.0",
    "since": null
  },
  "docs": "## @interlace/citation\n\nInstalled to `components/ui/patterns/citation.tsx`.\n\n```tsx\nimport { /* … */ } from '@/components/ui/patterns/citation';\n```\n\nProps, a11y contract, live preview and source: https://ds.interlace.tools/c/citation\n\nRequires the `@interlace/interlace-base` CSS baseline (installed automatically as a registry dependency). For the Interlace look, also add `@interlace/theme`."
}
