{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "citations",
  "title": "Citations",
  "description": "Inline numbered source chips that open a spring hover card with title, domain, snippet and date, a CitedText helper that turns [1] markers into chips, and a sources list whose row lights up when its chip is hovered.",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json",
    "https://goodui.dev/r/locale.json"
  ],
  "files": [
    {
      "path": "registry/components/ai/citations.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\"\nimport { ChevronDown, ExternalLink, Globe } from \"lucide-react\"\nimport { cn } from \"@/lib/utils\"\nimport { useLabels } from \"@/lib/goodui-locale\"\nimport { GooduiTokens, spring } from \"@/lib/goodui-motion\"\n\n/*\n * Inline numbered citations for AI answers. <Citations> holds the sources; <Cite n={1} /> renders a small chip\n * that opens a hover card with title, domain, snippet and date; <CitedText> turns \"[1]\" markers in a string\n * into chips; <SourceList> lists every source and lights up the one whose chip is hovered.\n */\n\nexport type CitationSource = {\n  /** Number shown in the chip and the list, 1-based. */\n  n: number\n  title: string\n  url?: string\n  /** Shown under the title; derived from the url when omitted. */\n  domain?: string\n  snippet?: string\n  /** Any short text, e.g. \"Oct 2026\". */\n  date?: string\n  /** Favicon or logo; a globe otherwise. */\n  icon?: React.ReactNode\n}\n\nexport type CitationsLabels = {\n  source: (n: number) => string\n  sources: string\n  open: string\n  showAll: (n: number) => string\n  showLess: string\n}\nconst DEFAULT_LABELS: CitationsLabels = { source: (n) => `Source ${n}`, sources: \"Sources\", open: \"Open source\", showAll: (n) => `Show all ${n} sources`, showLess: \"Show less\" }\n\ntype Ctx = {\n  sources: CitationSource[]\n  active: number | null\n  setActive: (n: number | null) => void\n  labels: CitationsLabels\n}\nconst CitationsContext = React.createContext<Ctx | null>(null)\n\nexport type CitationsProps = React.ComponentProps<\"div\"> & {\n  sources: CitationSource[]\n  /** Text for built-in controls and screen readers; pass translations here. */\n  labels?: Partial<CitationsLabels>\n}\n\n/** Provides the sources to <Cite>, <CitedText> and <SourceList> inside it. */\nexport function Citations({ sources, labels, className, children, ...props }: CitationsProps) {\n  const [active, setActive] = React.useState<number | null>(null)\n  const l = useLabels(\"citations\", DEFAULT_LABELS, labels)\n  const value = React.useMemo<Ctx>(() => ({ sources, active, setActive, labels: l }), [sources, active, l])\n  return (\n    <CitationsContext.Provider value={value}>\n      <GooduiTokens />\n      <div className={cn(\"pc-citations\", className)} {...props}>\n        {children}\n      </div>\n    </CitationsContext.Provider>\n  )\n}\n\nconst domainOf = (s: CitationSource) => {\n  if (s.domain) return s.domain\n  try {\n    return s.url ? new URL(s.url).hostname.replace(/^www\\./, \"\") : \"\"\n  } catch {\n    return \"\"\n  }\n}\n\nexport type CiteProps = Omit<React.HTMLAttributes<HTMLElement>, \"children\"> & {\n  /** Source number, matching a source's `n`. */\n  n: number\n  /** Where the hover card opens. */\n  side?: \"top\" | \"bottom\"\n}\n\n/** Numbered chip with a hover card for the source: a link to the URL in a new tab, or a button that opens the card when there is none. */\nexport function Cite({ n, side = \"top\", className, ...props }: CiteProps) {\n  const ctx = React.useContext(CitationsContext)\n  const reduce = useReducedMotion() ?? false\n  const uid = React.useId()\n  const ref = React.useRef<HTMLElement | null>(null)\n  const [open, setOpen] = React.useState(false)\n  const [pos, setPos] = React.useState<{ x: number; y: number; below: boolean } | null>(null)\n  const timer = React.useRef<number | undefined>(undefined)\n  const source = ctx?.sources.find((s) => s.n === n)\n  const l = ctx?.labels ?? DEFAULT_LABELS\n\n  const show = () => {\n    window.clearTimeout(timer.current)\n    const r = ref.current?.getBoundingClientRect()\n    if (!r) return\n    const below = side === \"bottom\" || r.top < 190\n    setPos({ x: r.left + r.width / 2, y: below ? r.bottom : r.top, below })\n    setOpen(true)\n    ctx?.setActive(n)\n  }\n  const hide = (delay = 120) => {\n    window.clearTimeout(timer.current)\n    timer.current = window.setTimeout(() => {\n      setOpen(false)\n      ctx?.setActive(null)\n    }, delay)\n  }\n\n  React.useEffect(() => () => window.clearTimeout(timer.current), [])\n  React.useEffect(() => {\n    if (!open) return\n    const close = () => hide(0)\n    const onKey = (e: KeyboardEvent) => e.key === \"Escape\" && close()\n    window.addEventListener(\"scroll\", close, true)\n    window.addEventListener(\"resize\", close)\n    window.addEventListener(\"keydown\", onKey)\n    return () => {\n      window.removeEventListener(\"scroll\", close, true)\n      window.removeEventListener(\"resize\", close)\n      window.removeEventListener(\"keydown\", onKey)\n    }\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [open])\n\n  if (!source) return null\n  const domain = domainOf(source)\n  const width = 288\n\n  const shared = {\n    ref: (el: HTMLElement | null) => {\n      ref.current = el\n    },\n    \"aria-label\": `${l.source(n)}: ${source.title}`,\n    \"aria-describedby\": open ? `${uid}-card` : undefined,\n    onPointerEnter: (e: React.PointerEvent) => e.pointerType === \"mouse\" && show(),\n    onPointerLeave: () => hide(),\n    onFocus: show,\n    onBlur: () => hide(0),\n    className: cn(\n      \"mx-0.5 inline-flex h-[1.3em] min-w-[1.3em] items-center justify-center rounded-md bg-muted px-1 align-[0.2em] text-[0.68em] font-semibold text-foreground/70 no-underline tabular-nums outline-none transition-colors hover:bg-foreground hover:text-background focus-visible:bg-foreground focus-visible:text-background\",\n      className,\n    ),\n    ...props,\n  }\n\n  return (\n    <>\n      <GooduiTokens />\n      {source.url ? (\n        <a {...shared} href={source.url} target=\"_blank\" rel=\"noreferrer\">\n          {n}\n        </a>\n      ) : (\n        <button {...shared} type=\"button\" onClick={() => !open && show()}>\n          {n}\n        </button>\n      )}\n      <AnimatePresence>\n        {open && pos && (\n          <motion.span\n            key=\"card\"\n            id={`${uid}-card`}\n            role=\"tooltip\"\n            initial={reduce ? { opacity: 0 } : { opacity: 0, scale: 0.94, y: pos.below ? -4 : 4 }}\n            animate={{ opacity: 1, scale: 1, y: 0 }}\n            exit={reduce ? { opacity: 0, transition: { duration: 0.08 } } : { opacity: 0, scale: 0.96, transition: { duration: 0.12 } }}\n            transition={reduce ? { duration: 0 } : spring.ui}\n            onPointerEnter={() => window.clearTimeout(timer.current)}\n            onPointerLeave={() => hide()}\n            style={{\n              position: \"fixed\",\n              width,\n              left: Math.max(8, Math.min(pos.x - width / 2, (typeof window === \"undefined\" ? 1024 : window.innerWidth) - width - 8)),\n              top: pos.below ? pos.y + 8 : undefined,\n              bottom: pos.below ? undefined : (typeof window === \"undefined\" ? 760 : window.innerHeight) - pos.y + 8,\n              transformOrigin: pos.below ? \"top center\" : \"bottom center\",\n            }}\n            className=\"z-50 block rounded-xl border bg-popover p-3 text-start text-[15px] leading-normal font-normal text-popover-foreground shadow-[var(--pc-e3)] shadow-black/10 dark:shadow-black/40\"\n          >\n            <span className=\"flex items-center gap-2 text-xs text-muted-foreground\">\n              <span className=\"grid size-5 place-items-center overflow-hidden rounded-md border bg-background [&_img]:size-full [&_svg]:size-3\">{source.icon ?? <Globe aria-hidden=\"true\" />}</span>\n              <span className=\"truncate\">{domain || l.source(n)}</span>\n              {source.date && <span className=\"ms-auto shrink-0\">{source.date}</span>}\n            </span>\n            <span className=\"mt-1.5 line-clamp-2 block text-sm leading-5 font-medium\">{source.title}</span>\n            {source.snippet && <span className=\"mt-1 line-clamp-3 block text-xs leading-5 text-muted-foreground\">{source.snippet}</span>}\n          </motion.span>\n        )}\n      </AnimatePresence>\n    </>\n  )\n}\n\nexport type CitedTextProps = Omit<React.ComponentProps<\"span\">, \"children\"> & {\n  /** Text with \"[1]\" style markers. */\n  text: string\n}\n\n/** Renders a string, replacing \"[n]\" markers with <Cite> chips. */\nexport function CitedText({ text, className, ...props }: CitedTextProps) {\n  const parts = text.split(/\\[(\\d+)\\]/g)\n  return (\n    <span className={className} {...props}>\n      {parts.map((p, i) => (i % 2 === 1 ? <Cite key={i} n={Number(p)} /> : <React.Fragment key={i}>{p}</React.Fragment>))}\n    </span>\n  )\n}\n\nexport type SourceListProps = React.ComponentProps<\"section\"> & {\n  /** Rows shown before a \"Show all\" button. */\n  limit?: number\n  /** Hide the \"Sources\" heading. */\n  hideTitle?: boolean\n}\n\n/** The sources as a numbered list; the row of a hovered chip lights up. */\nexport function SourceList({ limit = 3, hideTitle = false, className, ...props }: SourceListProps) {\n  const ctx = React.useContext(CitationsContext)\n  const reduce = useReducedMotion() ?? false\n  const [all, setAll] = React.useState(false)\n  if (!ctx) return null\n  const { sources, active, labels: l } = ctx\n  const shown = all ? sources : sources.slice(0, limit)\n  const more = sources.length - limit\n\n  return (\n    <section className={cn(\"pc-sources\", className)} {...props}>\n      <GooduiTokens />\n      {!hideTitle && <h4 className=\"mb-2 text-xs font-medium tracking-wide text-muted-foreground uppercase\">{l.sources}</h4>}\n      <ol className=\"flex flex-col gap-1\">\n        <AnimatePresence initial={false}>\n          {shown.map((s) => {\n            const domain = domainOf(s)\n            const Row = s.url ? \"a\" : \"div\"\n            return (\n              <motion.li\n                key={s.n}\n                layout\n                initial={reduce ? { opacity: 0 } : { opacity: 0, y: 6 }}\n                animate={{ opacity: 1, y: 0 }}\n                exit={{ opacity: 0, transition: { duration: 0.1 } }}\n                transition={reduce ? { duration: 0 } : spring.snap}\n                data-active={active === s.n || undefined}\n                onPointerEnter={() => ctx.setActive(s.n)}\n                onPointerLeave={() => ctx.setActive(null)}\n                className=\"rounded-lg transition-colors data-active:bg-muted\"\n              >\n                <Row\n                  {...(s.url ? { href: s.url, target: \"_blank\", rel: \"noreferrer\", title: l.open } : {})}\n                  className=\"group/src flex items-start gap-2.5 rounded-lg px-2 py-1.5 outline-none hover:bg-muted focus-visible:ring-3 focus-visible:ring-ring/50\"\n                >\n                  <span className={cn(\"mt-0.5 grid size-5 shrink-0 place-items-center rounded-md text-[11px] font-semibold tabular-nums transition-colors\", active === s.n ? \"bg-foreground text-background\" : \"bg-muted text-foreground/70\")}>{s.n}</span>\n                  <span className=\"min-w-0 flex-1\">\n                    <span className=\"block truncate text-sm leading-5 font-medium\">{s.title}</span>\n                    <span className=\"flex items-center gap-1.5 text-xs text-muted-foreground\">\n                      <span className=\"grid size-3.5 place-items-center [&_img]:size-full [&_svg]:size-3\">{s.icon ?? <Globe aria-hidden=\"true\" />}</span>\n                      <span className=\"truncate\">{domain}</span>\n                      {s.date && <span className=\"shrink-0\">· {s.date}</span>}\n                    </span>\n                  </span>\n                  {s.url && <ExternalLink className=\"mt-1 size-3.5 shrink-0 text-muted-foreground opacity-0 transition-opacity group-hover/src:opacity-100 group-focus-visible/src:opacity-100\" aria-hidden=\"true\" />}\n                </Row>\n              </motion.li>\n            )\n          })}\n        </AnimatePresence>\n      </ol>\n      {more > 0 && (\n        <button\n          type=\"button\"\n          aria-expanded={all}\n          onClick={() => setAll(!all)}\n          className=\"pc-press mt-1 inline-flex items-center gap-1 rounded-md px-2 py-1 text-xs font-medium text-muted-foreground outline-none hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50\"\n        >\n          {all ? l.showLess : l.showAll(sources.length)}\n          <ChevronDown className={cn(\"size-3.5 transition-transform duration-200\", all && \"rotate-180\")} aria-hidden=\"true\" />\n        </button>\n      )}\n    </section>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/citations.tsx"
    }
  ],
  "categories": [
    "components",
    "ai"
  ],
  "type": "registry:component"
}