Citations

Inline source markers with a collapsible list of references.

Preview

"use client";

import { FileText, RotateCcw } from "lucide-react";
import { useReducedMotion } from "motion/react";
import { useEffect, useState } from "react";
import {
  Citation,
  Citations,
  type CitationItem,
} from "@/components/agents/citations";

const CITATION_ITEMS: CitationItem[] = [
  {
    id: "motion",
    title: "Motion documentation",
    domain: "motion.dev",
    url: "https://motion.dev/docs/react",
  },
  {
    id: "wai",
    title: "WAI accessibility patterns",
    domain: "w3.org",
    url: "https://www.w3.org/WAI/ARIA/apg/",
  },
  {
    id: "guidelines",
    title: "Motion guidelines",
    domain: "Design handbook · PDF",
    icon: <FileText />,
  },
];

function CitationsDemo() {
  const reduce = useReducedMotion() ?? false;
  const [visible, setVisible] = useState(reduce ? CITATION_ITEMS.length : 0);

  useEffect(() => {
    if (reduce) return;
    const timers = CITATION_ITEMS.map((_, index) =>
      window.setTimeout(() => setVisible(index + 1), 500 + index * 700),
    );
    return () => timers.forEach(window.clearTimeout);
  }, [reduce]);

  return (
    <div className="space-y-4">
      <p className="text-sm leading-6 text-foreground/90">
        Use layout-aware motion for newly appended results{" "}
        <Citation citationId="motion" index={1} idPrefix="preview-source" /> and preserve accessible
        disclosure behavior <Citation citationId="wai" index={2} idPrefix="preview-source" /> as the list
        grows, keeping exits shorter than entrances{" "}
        <Citation
          citationId="guidelines"
          index={3}
          idPrefix="preview-source"
          label="Citation 3: Motion guidelines"
        />
        .
      </p>
      <Citations
        idPrefix="preview-source"
        citations={CITATION_ITEMS.slice(0, visible)}
        defaultOpen
      />
    </div>
  );
}

export function CitationsPreview() {
  const [run, setRun] = useState(0);

  return (
    <div className="relative h-[410px] w-full max-w-lg">
      <CitationsDemo key={run} />
      <button
        type="button"
        onClick={() => setRun((value) => value + 1)}
        className="absolute bottom-0 left-0 inline-flex items-center gap-1.5 rounded-full px-2.5 py-1 text-xs font-medium text-muted-foreground outline-none transition-colors hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring"
      >
        <RotateCcw className="size-3" />
        Replay
      </button>
    </div>
  );
}

Install

Add it with the shadcn CLI, or copy the source manually.

$ bunx --bun shadcn add @beui/citations

API Reference

Citation

citationId
string

Id of the `CitationItem` this marker links to.

index
number

Number shown on the marker.

idPrefix
string

Must match the related Citations idPrefix.

label?
string

Accessible name for the marker. Defaults to `View citation {index}`. Include the index so voice control users can say the number they see.

className?
string

Classes merged onto the marker link.

CitationFavicon

url?
string

Page whose site favicon is shown. A globe icon shows when absent or broken.

icon?
ReactNode

Drawn instead of the favicon when truthy.

className?
string

Classes merged onto the root element.

CitationStack

citations
CitationItem[]

Sources whose favicons are stacked.

limit?
number

Most favicons to show. Default: 3

Default3
className?
string

Classes merged onto the root element.

CitationList

citations
CitationItem[]

Sources listed in order, numbered from 1.

idPrefix?
string

Prefix for row ids that `Citation` markers link to. Generated when omitted.

highlightedId?
string

The citation a marker just led to; its row is lit.

className?
string

Classes merged onto the root element.

Citations

citations
CitationItem[]

Sources listed in order, numbered from 1.

title?
ReactNode

Text of the toggle button. Default: "Sources"

open?
boolean

Whether the list is expanded, for controlled use.

defaultOpen?
boolean

Whether the list starts expanded when uncontrolled. Default: false

Defaultfalse
onOpenChange?
((open: boolean) => void)

Called with the new state when the list expands or collapses.

idPrefix?
string

Prefix for row ids that `Citation` markers link to. Generated when omitted.

className?
string

Classes merged onto the root element.

labels?
Partial<{ title: string; viewCitation: string; }>

Override any built-in string.

Composition

Pair inline citation markers with a source disclosure owned by the same response.

StreamingResponse
├── Citation
└── Citations
    ├── CitationStack
    └── CitationList

Note: Streaming Response reveals the source summary when the answer completes. Message keeps cited answers within a semantic conversation row.

How it works

Citations connect a claim to supporting material without interrupting the answer. Inline markers should stay lightweight while the source collection provides enough information to inspect the evidence.

Also on React Native. Tap a number and its source springs up.

Updated