Citations
Inline numbered source chips inside AI answers, with spring hover cards, a sources strip and a remark plugin for [1] markers.
Preview
Why are honey bees so important?
Installation
- 1
Run the following command
npx shadcn@latest add https://localhostui.com/r/citations.json
Also copy remark-citations.ts from the same folder.
Usage
Inline, by hand
<CitationProvider sources={sources}>
<p>
Bees communicate with the waggle dance <Citation ids={["s2"]} /> and navigate by polarised light{" "}
<Citation ids={["s3", "s1"]} />.
</p>
<SourcesStrip />
</CitationProvider>Numbers are assigned in order of first appearance, not by array position.
From markdown with remarkCitations
remarkCitations turns [1] and [1, 3] markers into chips. The numbers are 1-based positions in sources. Markers in code and links are ignored, and a half-typed [1 is hidden while streaming.
import { remarkCitations, MarkdownCitation } from "@/components/lokalhost_io/micro-ai-components/citations/citations";
const remarkPlugins = [remarkCitations];
const components = { citation: MarkdownCitation };
<Message.Content content={answer} remarkPlugins={remarkPlugins} components={components} />Mapping search API results
const sources: CitationSource[] = results.map((r, i) => ({
id: `s${i + 1}`,
url: r.url,
title: r.title,
snippet: r.content,
publishedAt: r.published_date,
}));Ask the model to cite with [n] markers that match the order you sent the results in.
Theming the chip
The chip uses bg-muted, border-border and text-muted-foreground and inverts to bg-foreground on hover. Pass className to <Citation>, or change the tokens to restyle every chip at once.
Props
CitationProvider
| Prop | Type | Default | Description |
|---|---|---|---|
sources* | CitationSource[] | — | Every source the answer may cite. |
display | "number""domain" | "number" | Chip style: a number, or favicon + domain. |
getFavicon | (source) => string | source.favicon or Google's favicon service | Favicon URL resolver. |
CitationSource
| Prop | Type | Default | Description |
|---|---|---|---|
id* | string | — | Referenced by <Citation ids>. |
url* | string | — | Opened in a new tab. |
title* | string | — | Clamped to two lines in the card. |
domain | string | the URL hostname | |
favicon | string | — | Custom favicon URL. |
snippet | string | — | Clamped to three lines. |
highlight | string | — | Sentence inside the snippet to highlight — the cited claim. |
publishedAt | string | Date | — | Shown as a short date. |
author | string | — | Shown in the card footer. |
Citation
| Prop | Type | Default | Description |
|---|---|---|---|
ids* | string[] | — | Source ids. Several ids show “1 +2” and a carousel in the card. |
display | "number""domain" | — | Override the provider's display. |
SourcesStrip
| Prop | Type | Default | Description |
|---|---|---|---|
max | number | 4 | Cards before a “+N more” card that opens the full list. |
show | boolean | true | Set false while streaming; cards stagger in when it flips to true. |
Accessibility
- Each chip is a button labelled
Source 1: <title>. Focus opens the card,Esccloses it; on touch, tap opens and tapping outside closes. - Links open in a new tab with
rel="noopener noreferrer"and a visually hidden “(opens in new tab)”. - Hovering a chip highlights its card in the strip and vice versa — a visual aid only; nothing depends on it.
- With reduced motion, cards and chips fade instead of springing.
Favicons load from google.com/s2/favicons by default, so each visitor's browser requests them from Google. Pass getFavicon to serve them yourself.