ComponentsCitations

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. 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

PropTypeDefaultDescription
sources*CitationSource[]—Every source the answer may cite.
display
"number""domain"
"number"Chip style: a number, or favicon + domain.
getFavicon(source) => stringsource.favicon or Google's favicon serviceFavicon URL resolver.

CitationSource

PropTypeDefaultDescription
id*string—Referenced by <Citation ids>.
url*string—Opened in a new tab.
title*string—Clamped to two lines in the card.
domainstringthe URL hostname
faviconstring—Custom favicon URL.
snippetstring—Clamped to three lines.
highlightstring—Sentence inside the snippet to highlight — the cited claim.
publishedAtstring | Date—Shown as a short date.
authorstring—Shown in the card footer.

Citation

PropTypeDefaultDescription
ids*string[]—Source ids. Several ids show “1 +2” and a carousel in the card.
display
"number""domain"
—Override the provider's display.

SourcesStrip

PropTypeDefaultDescription
maxnumber4Cards before a “+N more” card that opens the full list.
showbooleantrueSet 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, Esc closes 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.