{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chat-sources",
  "type": "registry:ui",
  "title": "Chat Sources",
  "description": "Numbered citations and the list of sources a reply used.",
  "categories": [
    "ai-&-chat"
  ],
  "dependencies": [
    "@hugeicons/core-free-icons@4.4.0",
    "@hugeicons/react@1.1.10"
  ],
  "registryDependencies": [
    "https://sprout.tulipfarm.ai/r/sprout-base.json",
    "https://sprout.tulipfarm.ai/r/button.json",
    "https://sprout.tulipfarm.ai/r/collapsible.json",
    "https://sprout.tulipfarm.ai/r/popover.json",
    "https://sprout.tulipfarm.ai/r/spinner.json"
  ],
  "files": [
    {
      "path": "registry/sprout/components/ui/chat-sources.tsx",
      "type": "registry:ui",
      "target": "components/ui/chat-sources.tsx",
      "content": "'use client';\n\nimport { useRef, type ComponentProps } from 'react';\nimport { ArrowUpRightIcon } from '@/lib/sprout/icons/arrow-up-right-icon';\nimport { ChevronDownIcon } from '@/lib/sprout/icons/chevron-down-icon';\nimport { DocumentTextIcon } from '@/lib/sprout/icons/document-text-icon';\nimport { cn } from '@/lib/sprout/classes';\nimport { Button } from '@/components/ui/button';\nimport { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible';\nimport { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';\nimport { Spinner } from '@/components/ui/spinner';\n\nexport type ChatSource = {\n  id: string;\n  title: string;\n  location: string;\n  excerpt: string;\n  href: string;\n};\n\nconst hoverDelay = 300;\nconst hoverCloseDelay = 150;\n\nfunction hostOf(href: string) {\n  try {\n    return new URL(href).hostname.replace(/^www\\./, '');\n  } catch {\n    return null;\n  }\n}\n\nfunction SourceContent({ source, number }: { source: ChatSource; number?: number }) {\n  const host = number === undefined ? null : hostOf(source.href);\n  return (\n    <div data-slot=\"chat-source\" className=\"sprout-chat-sources-source\">\n      {number !== undefined && (\n        <p className=\"sprout-chat-sources-preview-head\">\n          <span className=\"sprout-chat-sources-badge\" aria-hidden=\"true\">\n            {number}\n          </span>\n          <span>{host ?? `Source ${number}`}</span>\n        </p>\n      )}\n      <p className=\"sprout-chat-sources-title\">{source.title}</p>\n      <p className=\"sprout-chat-sources-location\">{source.location}</p>\n      <p className=\"sprout-chat-sources-excerpt\">{source.excerpt}</p>\n      <a href={source.href} target=\"_blank\" rel=\"noopener noreferrer\" className=\"sprout-chat-sources-link\">\n        Open source\n        <ArrowUpRightIcon size=\"sm\" aria-hidden=\"true\" />\n        <span className=\"sprout-chat-sources-sr-only\"> in a new tab</span>\n      </a>\n    </div>\n  );\n}\n\nexport interface InlineCitationProps {\n  source: ChatSource;\n  number: number;\n}\n\nexport function InlineCitation({ source, number }: InlineCitationProps) {\n  return (\n    <Popover>\n      <PopoverTrigger\n        openOnHover\n        delay={hoverDelay}\n        closeDelay={hoverCloseDelay}\n        data-slot=\"inline-citation\"\n        className=\"sprout-chat-sources-citation\"\n        render={<Button variant=\"secondary\" size=\"icon-xs\" aria-label={`Source ${number}: ${source.title}`} />}\n      >\n        {number}\n      </PopoverTrigger>\n      <PopoverContent className=\"sprout-chat-sources-preview\">\n        <SourceContent source={source} number={number} />\n      </PopoverContent>\n    </Popover>\n  );\n}\n\nexport interface ChatSourcesProps extends Omit<ComponentProps<'div'>, 'children'> {\n  sources: readonly ChatSource[];\n  defaultOpen?: boolean | undefined;\n}\n\nexport function ChatSources({ sources, defaultOpen, className, ...props }: ChatSourcesProps) {\n  if (sources.length === 0)\n    return (\n      <div data-slot=\"chat-sources\" data-empty=\"\" {...props} className={cn('sprout-chat-sources', className)}>\n        <p className=\"sprout-chat-sources-empty\">No sources were used for this response.</p>\n      </div>\n    );\n  return (\n    <Collapsible\n      data-slot=\"chat-sources\"\n      defaultOpen={defaultOpen}\n      {...props}\n      className={cn('sprout-chat-sources', className)}\n    >\n      <CollapsibleTrigger className=\"sprout-chat-sources-trigger\">\n        <DocumentTextIcon size=\"md\" aria-hidden=\"true\" />\n        <span>\n          {sources.length} {sources.length === 1 ? 'source' : 'sources'}\n        </span>\n        <ChevronDownIcon size=\"sm\" aria-hidden=\"true\" data-slot=\"collapsible-indicator\" />\n      </CollapsibleTrigger>\n      <CollapsibleContent variant=\"sidebar\">\n        <ol className=\"sprout-chat-sources-list\">\n          {sources.map((source, index) => (\n            <li key={source.id} className=\"sprout-chat-sources-item\">\n              <span className=\"sprout-chat-sources-number\">{index + 1}</span>\n              <SourceContent source={source} />\n            </li>\n          ))}\n        </ol>\n      </CollapsibleContent>\n    </Collapsible>\n  );\n}\n\nexport const citationStateKindNames = ['idle', 'loading', 'ready', 'unavailable', 'error'] as const;\n\nexport type CitationStateKind = (typeof citationStateKindNames)[number];\n\nexport type CitationState =\n  | { kind: 'idle' }\n  | { kind: 'loading' }\n  | { kind: 'ready'; title: string; location: string; excerpt: string }\n  | { kind: 'unavailable'; message: string }\n  | { kind: 'error'; message: string };\n\nexport interface CheckedCitationProps {\n  label: string;\n  state: CitationState;\n  onOpen: () => void;\n  onRetry: () => void;\n}\n\nexport function CheckedCitation({ label, state, onOpen, onRetry }: CheckedCitationProps) {\n  const popup = useRef<HTMLDivElement>(null);\n  const openReason = useRef<string | undefined>(undefined);\n  return (\n    <Popover\n      onOpenChange={(open, details) => {\n        if (open) {\n          openReason.current = details.reason;\n          onOpen();\n          return;\n        }\n        if (details.reason === 'trigger-hover' && openReason.current === 'trigger-press') details.cancel();\n      }}\n    >\n      <PopoverTrigger\n        openOnHover\n        delay={hoverDelay}\n        closeDelay={hoverCloseDelay}\n        data-slot=\"checked-citation\"\n        className=\"sprout-chat-sources-checked\"\n        render={<Button variant=\"link\" aria-label={`Source: ${label}`} />}\n      >\n        {label}\n      </PopoverTrigger>\n      <PopoverContent ref={popup} data-citation-state={state.kind} className=\"sprout-chat-sources-preview\">\n        {state.kind === 'idle' || state.kind === 'loading' ? (\n          <p role=\"status\" className=\"sprout-chat-sources-checking\">\n            <Spinner aria-hidden=\"true\" />\n            Checking access to this source…\n          </p>\n        ) : state.kind === 'ready' ? (\n          <div data-slot=\"chat-source\" className=\"sprout-chat-sources-source\">\n            <p className=\"sprout-chat-sources-title\">{state.title}</p>\n            <p className=\"sprout-chat-sources-location\">{state.location}</p>\n            <p className=\"sprout-chat-sources-excerpt\" data-preserve-lines=\"\">\n              {state.excerpt}\n            </p>\n          </div>\n        ) : (\n          <div className=\"sprout-chat-sources-source\">\n            <p className=\"sprout-chat-sources-title\">\n              {state.kind === 'unavailable' ? 'Source unavailable' : 'Couldn’t load this source'}\n            </p>\n            <p role=\"alert\" className=\"sprout-chat-sources-excerpt\">\n              {state.message}\n            </p>\n            {state.kind === 'error' && (\n              <Button\n                variant=\"outline\"\n                size=\"sm\"\n                className=\"sprout-chat-sources-retry\"\n                onClick={() => {\n                  popup.current?.focus();\n                  onRetry();\n                }}\n              >\n                Load source again\n              </Button>\n            )}\n          </div>\n        )}\n      </PopoverContent>\n    </Popover>\n  );\n}\n"
    },
    {
      "path": "registry/sprout/lib/sprout/icons/arrow-up-right-icon.tsx",
      "type": "registry:lib",
      "target": "lib/sprout/icons/arrow-up-right-icon.tsx",
      "content": "import { ArrowUpRight01Icon as glyph } from '@hugeicons/core-free-icons';\nimport { createIcon } from '@/lib/sprout/icons/create-icon';\n\nexport const ArrowUpRightIcon = /* @__PURE__ */ createIcon(glyph, 'ArrowUpRightIcon');\n"
    },
    {
      "path": "registry/sprout/lib/sprout/icons/chevron-down-icon.tsx",
      "type": "registry:lib",
      "target": "lib/sprout/icons/chevron-down-icon.tsx",
      "content": "import { ArrowDown01Icon as glyph } from '@hugeicons/core-free-icons';\nimport { createIcon } from '@/lib/sprout/icons/create-icon';\n\nexport const ChevronDownIcon = /* @__PURE__ */ createIcon(glyph, 'ChevronDownIcon');\n"
    },
    {
      "path": "registry/sprout/lib/sprout/icons/create-icon.tsx",
      "type": "registry:lib",
      "target": "lib/sprout/icons/create-icon.tsx",
      "content": "import { HugeiconsIcon, type IconSvgElement } from '@hugeicons/react';\nimport type { ReactElement, Ref, SVGProps } from 'react';\n\nexport const iconSizes = {\n  xs: 12,\n  sm: 14,\n  md: 16,\n  lg: 18,\n  xl: 20,\n  '2xl': 24,\n} as const;\n\nexport type IconSize = keyof typeof iconSizes;\n\nexport const iconStrokeWidth = 1.5;\n\nexport type IconProps = Omit<SVGProps<SVGSVGElement>, 'ref'> & {\n  size?: IconSize | number;\n  ref?: Ref<SVGSVGElement>;\n};\n\nexport type IconComponent = (props: IconProps) => ReactElement;\n\nexport function createIcon(glyph: IconSvgElement, name: string): IconComponent {\n  const Icon = ({ size = '2xl', strokeWidth = iconStrokeWidth, ...props }: IconProps) => (\n    <HugeiconsIcon\n      icon={glyph}\n      size={typeof size === 'number' ? size : iconSizes[size]}\n      strokeWidth={Number(strokeWidth)}\n      {...props}\n    />\n  );\n  Icon.displayName = name;\n  return Icon;\n}\n"
    },
    {
      "path": "registry/sprout/lib/sprout/icons/document-text-icon.tsx",
      "type": "registry:lib",
      "target": "lib/sprout/icons/document-text-icon.tsx",
      "content": "import { File02Icon as glyph } from '@hugeicons/core-free-icons';\nimport { createIcon } from '@/lib/sprout/icons/create-icon';\n\nexport const DocumentTextIcon = /* @__PURE__ */ createIcon(glyph, 'DocumentTextIcon');\n"
    }
  ],
  "css": {
    "@layer components": {
      ".sprout-chat-sources": {
        "min-width": "0",
        "color": "var(--sprout-text)",
        "font-family": "var(--sprout-font)"
      },
      ".sprout-chat-sources-empty": {
        "color": "var(--sprout-muted)",
        "font-size": "var(--sprout-text-sm)",
        "margin": "0",
        "line-height": "1.25rem"
      },
      ".sprout-chat-sources .sprout-chat-sources-trigger": {
        "min-height": "32px",
        "color": "var(--sprout-muted)",
        "font-size": "var(--sprout-text-sm)",
        "font-variant-numeric": "tabular-nums",
        "align-items": "center",
        "gap": "8px",
        "margin-inline": "-4px",
        "padding-inline": "4px",
        "line-height": "1.25rem",
        "display": "inline-flex"
      },
      ".sprout-chat-sources .sprout-chat-sources-trigger[data-panel-open]": {
        "color": "var(--sprout-text)"
      },
      "@media (hover: hover)": {
        ".sprout-chat-sources .sprout-chat-sources-trigger:hover": {
          "color": "var(--sprout-text)"
        },
        ".sprout-chat-sources-link:hover": {
          "color": "var(--sprout-primary-hover)"
        },
        ".sprout-chat-sources-citation.sprout-button:hover": {
          "background": "var(--sprout-hover)",
          "color": "var(--sprout-text)"
        },
        ".sprout-chat-sources-checked.sprout-button:hover": {
          "text-decoration-thickness": "2px"
        }
      },
      ".sprout-chat-sources-list": {
        "gap": "8px",
        "margin": "8px 0 0",
        "padding": "0",
        "list-style": "none",
        "display": "grid"
      },
      ".sprout-chat-sources-item": {
        "border": "1px solid var(--sprout-line)",
        "border-radius": "var(--sprout-radius-panel)",
        "background": "var(--sprout-surface)",
        "gap": "12px",
        "min-width": "0",
        "padding": "12px",
        "display": "flex"
      },
      ".sprout-chat-sources-number": {
        "min-width": "12px",
        "color": "var(--sprout-muted)",
        "font-size": "var(--sprout-text-xs)",
        "font-variant-numeric": "tabular-nums",
        "flex-shrink": "0",
        "line-height": "1.25rem"
      },
      ".sprout-chat-sources-source": {
        "min-width": "0",
        "font-size": "var(--sprout-text-sm)",
        "justify-items": "start",
        "gap": "4px",
        "line-height": "1.25rem",
        "display": "grid"
      },
      ".sprout-chat-sources-source > p": {
        "max-width": "100%",
        "margin": "0"
      },
      ".sprout-chat-sources-title": {
        "overflow-wrap": "anywhere",
        "font-weight": "500"
      },
      ".sprout-chat-sources-location": {
        "color": "var(--sprout-muted)",
        "font-size": "var(--sprout-text-xs)",
        "overflow-wrap": "anywhere",
        "line-height": "1rem"
      },
      ".sprout-chat-sources-excerpt": {
        "color": "var(--sprout-muted)",
        "overflow-wrap": "anywhere"
      },
      ".sprout-chat-sources-excerpt[data-preserve-lines]": {
        "white-space": "pre-wrap"
      },
      ".sprout-chat-sources-link": {
        "color": "var(--sprout-primary)",
        "text-underline-offset": "4px",
        "border-radius": "4px",
        "align-items": "center",
        "gap": "4px",
        "margin-top": "4px",
        "font-weight": "500",
        "text-decoration": "underline",
        "display": "inline-flex"
      },
      ".sprout-chat-sources-link:focus-visible": {
        "outline": "var(--sprout-focus-width) solid var(--sprout-focus-ring)",
        "outline-offset": "0"
      },
      ".sprout-chat-sources-retry": {
        "margin-top": "4px"
      },
      ".sprout-chat-sources-checking": {
        "color": "var(--sprout-muted)",
        "align-items": "center",
        "gap": "8px",
        "margin": "0",
        "display": "flex"
      },
      ".sprout-chat-sources-citation.sprout-button": {
        "vertical-align": "middle",
        "font-size": "var(--sprout-text-xs)",
        "font-variant-numeric": "tabular-nums",
        "margin-inline": "2px"
      },
      ".sprout-chat-sources-citation.sprout-button[data-popup-open]": {
        "background": "var(--sprout-hover)",
        "color": "var(--sprout-text)"
      },
      ".sprout-chat-sources-preview-head": {
        "color": "var(--sprout-muted)",
        "font-size": "var(--sprout-text-xs)",
        "overflow-wrap": "anywhere",
        "align-items": "center",
        "gap": "8px",
        "margin-bottom": "4px",
        "line-height": "1rem",
        "display": "flex"
      },
      ".sprout-chat-sources-badge": {
        "background": "var(--sprout-hover)",
        "min-width": "18px",
        "height": "18px",
        "color": "var(--sprout-text)",
        "font-variant-numeric": "tabular-nums",
        "border-radius": "6px",
        "flex-shrink": "0",
        "place-items": "center",
        "padding-inline": "4px",
        "font-weight": "500",
        "display": "inline-grid"
      },
      ".sprout-chat-sources-preview .sprout-chat-sources-excerpt": {
        "border-inline-start": "2px solid var(--sprout-line)",
        "color": "var(--sprout-text)",
        "margin-top": "4px",
        "padding-inline-start": "10px"
      },
      ".sprout-chat-sources-checked.sprout-button": {
        "height": "auto",
        "min-height": "0",
        "font": "inherit",
        "text-underline-offset": "3px",
        "vertical-align": "baseline",
        "padding": "0",
        "text-decoration": "underline 1px",
        "display": "inline"
      },
      ".sprout-chat-sources-checked.sprout-button[data-popup-open]": {
        "text-decoration-thickness": "2px"
      },
      ".sprout-chat-sources-sr-only": {
        "clip-path": "inset(50%)",
        "white-space": "nowrap",
        "border": "0",
        "width": "1px",
        "height": "1px",
        "margin": "-1px",
        "padding": "0",
        "position": "absolute",
        "overflow": "hidden"
      },
      "@media (prefers-reduced-motion: no-preference)": {
        ".sprout-chat-sources .sprout-chat-sources-trigger, .sprout-chat-sources-link": {
          "transition": "color var(--sprout-motion-fast, .12s) var(--sprout-motion-ease, ease-out)"
        },
        ".sprout-chat-sources-citation.sprout-button": {
          "transition": "color var(--sprout-motion-fast, .12s) var(--sprout-motion-ease, ease-out),\n        background-color var(--sprout-motion-fast, .12s) var(--sprout-motion-ease, ease-out)"
        }
      }
    }
  },
  "docs": "Docs and examples: https://sprout.tulipfarm.ai/components/chat-sources/",
  "meta": {
    "source": "packages/sprout/src/components/chat-sources.tsx"
  }
}
