Chat Sources
Shows where a reply came from. Hovering or clicking a citation previews the cited passage and its source. Checked citations recheck access each time they open, and a count opens the list of every source the reply used.
Installation
pnpm dlx shadcn@latest add https://sprout.tulipfarm.ai/r/chat-sources.jsonCopies Chat Sources and the components it uses (Button, Collapsible, Popover, and Spinner) into your app, installs the packages, and adds the styles. Then import it from @/components/ui/chat-sources. Needs Tailwind CSS 4 and a components.json from shadcn init.
Install the following dependencies:
pnpm add clsx tailwind-merge @hugeicons/core-free-icons @hugeicons/reactAdd Sprout base once per app: the
cn()helper and the design tokens.lib/sprout/classes.ts- Design tokens, for your global CSS
Add the components it uses: Button, Collapsible, Popover, and Spinner.
Copy and paste the following code into your project.
'use client'; import { useRef, type ComponentProps } from 'react'; import { ArrowUpRightIcon } from '@/lib/sprout/icons/arrow-up-right-icon'; import { ChevronDownIcon } from '@/lib/sprout/icons/chevron-down-icon'; import { DocumentTextIcon } from '@/lib/sprout/icons/document-text-icon'; import { cn } from '@/lib/sprout/classes'; import { Button } from '@/components/ui/button'; import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible'; import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover'; import { Spinner } from '@/components/ui/spinner'; export type ChatSource = { id: string; title: string; location: string; excerpt: string; href: string; }; const hoverDelay = 300; const hoverCloseDelay = 150; function hostOf(href: string) { try { return new URL(href).hostname.replace(/^www\./, ''); } catch { return null; } } function SourceContent({ source, number }: { source: ChatSource; number?: number }) { const host = number === undefined ? null : hostOf(source.href); return ( <div data-slot="chat-source" className="sprout-chat-sources-source"> {number !== undefined && ( <p className="sprout-chat-sources-preview-head"> <span className="sprout-chat-sources-badge" aria-hidden="true"> {number} </span> <span>{host ?? `Source ${number}`}</span> </p> )} <p className="sprout-chat-sources-title">{source.title}</p> <p className="sprout-chat-sources-location">{source.location}</p> <p className="sprout-chat-sources-excerpt">{source.excerpt}</p> <a href={source.href} target="_blank" rel="noopener noreferrer" className="sprout-chat-sources-link"> Open source <ArrowUpRightIcon size="sm" aria-hidden="true" /> <span className="sprout-chat-sources-sr-only"> in a new tab</span> </a> </div> ); } export interface InlineCitationProps { source: ChatSource; number: number; } export function InlineCitation({ source, number }: InlineCitationProps) { return ( <Popover> <PopoverTrigger openOnHover delay={hoverDelay} closeDelay={hoverCloseDelay} data-slot="inline-citation" className="sprout-chat-sources-citation" render={<Button variant="secondary" size="icon-xs" aria-label={`Source ${number}: ${source.title}`} />} > {number} </PopoverTrigger> <PopoverContent className="sprout-chat-sources-preview"> <SourceContent source={source} number={number} /> </PopoverContent> </Popover> ); } export interface ChatSourcesProps extends Omit<ComponentProps<'div'>, 'children'> { sources: readonly ChatSource[]; defaultOpen?: boolean | undefined; } export function ChatSources({ sources, defaultOpen, className, ...props }: ChatSourcesProps) { if (sources.length === 0) return ( <div data-slot="chat-sources" data-empty="" {...props} className={cn('sprout-chat-sources', className)}> <p className="sprout-chat-sources-empty">No sources were used for this response.</p> </div> ); return ( <Collapsible data-slot="chat-sources" defaultOpen={defaultOpen} {...props} className={cn('sprout-chat-sources', className)} > <CollapsibleTrigger className="sprout-chat-sources-trigger"> <DocumentTextIcon size="md" aria-hidden="true" /> <span> {sources.length} {sources.length === 1 ? 'source' : 'sources'} </span> <ChevronDownIcon size="sm" aria-hidden="true" data-slot="collapsible-indicator" /> </CollapsibleTrigger> <CollapsibleContent variant="sidebar"> <ol className="sprout-chat-sources-list"> {sources.map((source, index) => ( <li key={source.id} className="sprout-chat-sources-item"> <span className="sprout-chat-sources-number">{index + 1}</span> <SourceContent source={source} /> </li> ))} </ol> </CollapsibleContent> </Collapsible> ); } export const citationStateKindNames = ['idle', 'loading', 'ready', 'unavailable', 'error'] as const; export type CitationStateKind = (typeof citationStateKindNames)[number]; export type CitationState = | { kind: 'idle' } | { kind: 'loading' } | { kind: 'ready'; title: string; location: string; excerpt: string } | { kind: 'unavailable'; message: string } | { kind: 'error'; message: string }; export interface CheckedCitationProps { label: string; state: CitationState; onOpen: () => void; onRetry: () => void; } export function CheckedCitation({ label, state, onOpen, onRetry }: CheckedCitationProps) { const popup = useRef<HTMLDivElement>(null); const openReason = useRef<string | undefined>(undefined); return ( <Popover onOpenChange={(open, details) => { if (open) { openReason.current = details.reason; onOpen(); return; } if (details.reason === 'trigger-hover' && openReason.current === 'trigger-press') details.cancel(); }} > <PopoverTrigger openOnHover delay={hoverDelay} closeDelay={hoverCloseDelay} data-slot="checked-citation" className="sprout-chat-sources-checked" render={<Button variant="link" aria-label={`Source: ${label}`} />} > {label} </PopoverTrigger> <PopoverContent ref={popup} data-citation-state={state.kind} className="sprout-chat-sources-preview"> {state.kind === 'idle' || state.kind === 'loading' ? ( <p role="status" className="sprout-chat-sources-checking"> <Spinner aria-hidden="true" /> Checking access to this source… </p> ) : state.kind === 'ready' ? ( <div data-slot="chat-source" className="sprout-chat-sources-source"> <p className="sprout-chat-sources-title">{state.title}</p> <p className="sprout-chat-sources-location">{state.location}</p> <p className="sprout-chat-sources-excerpt" data-preserve-lines=""> {state.excerpt} </p> </div> ) : ( <div className="sprout-chat-sources-source"> <p className="sprout-chat-sources-title"> {state.kind === 'unavailable' ? 'Source unavailable' : 'Couldn’t load this source'} </p> <p role="alert" className="sprout-chat-sources-excerpt"> {state.message} </p> {state.kind === 'error' && ( <Button variant="outline" size="sm" className="sprout-chat-sources-retry" onClick={() => { popup.current?.focus(); onRetry(); }} > Load source again </Button> )} </div> )} </PopoverContent> </Popover> ); }And the files it imports:
lib/sprout/icons/arrow-up-right-icon.tsxlib/sprout/icons/chevron-down-icon.tsxlib/sprout/icons/create-icon.tsxlib/sprout/icons/document-text-icon.tsx
Add the styles to your global CSS, after
@import "tailwindcss".@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), background-color var(--sprout-motion-fast, .12s) var(--sprout-motion-ease, ease-out); } } }Import it from
@/components/ui/chat-sources. Update the paths if your@/alias points elsewhere.
pnpm add @tulipfarm/sprout@import "tailwindcss";
@import "@tulipfarm/sprout/styles.css";The package keeps the source and ships fixes with each release. Import from @tulipfarm/sprout/chat-sources.
Usage
import { ChatSources, InlineCitation, type ChatSource } from "@tulipfarm/sprout/chat-sources";
const sources: ChatSource[] = [
{
id: "checklist",
title: "Supplier onboarding checklist",
location: "Note · Before the first order",
excerpt: "Collect the W-9 and bank details before the first purchase order.",
href: "/notes/checklist",
},
];
<p>
Collect the tax form first. <InlineCitation source={sources[0]} number={1} />
</p>
<ChatSources sources={sources} />ChatSource:{ id, title, location, excerpt, href }.hrefopens in a new tab.InlineCitation:sourceand itsnumber. It is a small secondary icon button.ChatSources:sourcesin citation order, anddefaultOpento start with the list open.CheckedCitation: the linkedlabel, astate,onOpen(called on every open), andonRetry. It is a link button that wraps with the text.
Inline citations and sources
Numbers in the reply match the numbered list. Each citation and each list item shows the title, where it lives, the passage, and an “Open source” link.
Collect the tax form and bank details before the first order. Payment terms default to Net 30, but suppliers with repeated late deliveries move to Net 15.
One source
The count reads “1 source”. The list starts closed.
Water bulbs after planting.
Empty sources
With no sources, it says none were used.
No sources were used for this response.
Checked citations
Use CheckedCitation when a source can change or access can end. Your app checks access in onOpen and passes the result as state. A removed source shows why, never its old passage. A failed check offers “Load source again”.
type CitationState =
| { kind: "idle" } | { kind: "loading" }
| { kind: "ready"; title: string; location: string; excerpt: string }
| { kind: "unavailable"; message: string }
| { kind: "error"; message: string };
<MessageResponse
renderLink={(href, label) =>
isCitation(href)
? <CheckedCitation label={text(label)} state={state} onOpen={check} onRetry={check} />
: <a href={href}>{label}</a>}
>
{reply}
</MessageResponse>Plant the bulbs in late autumn, then water them once ().
Long content
Long file names, paths, and links wrap inside a narrow chat.
Twelve deliveries still need a supplier.
Accessibility
Inline citations are named “Source 1: title”; checked citations “Source: label”. Both open a popover after a 300ms hover, on click or tap, or with Enter or Space. The pointer can move into the popover to reach “Open source”. Escape closes it and returns focus. The sources count is a disclosure button witharia-expanded, and the list is ordered. “Open source” tells screen readers it opens a new tab.
Checking access is announced as a status; unavailable and error messages as alerts. After “Load source again”, focus stays in the popover. Colors change over 120ms and the list grows over 180ms; with reduced motion both change at once.
Theme tokens
Cards use --sprout-line and --sprout-surface, secondary text --sprout-muted, and links--sprout-primary.