# Citation

> An inline numbered source that previews the site, title and snippet on hover or focus.

Answers that cite sources earn trust, but a wall of links at the bottom gets ignored. This puts a small numbered marker right in the sentence. Hover or focus it and a card rises out of a light blur to show where the claim comes from, then sinks back when you leave; click and the source opens. The card flips above near the bottom of the screen and shifts to stay on it, and a source that couldn’t be fetched says so instead of failing quietly.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/citation.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: `lucide-react`
- Hairline UI components it needs (the CLI installs them too): none
- Docs and live preview: https://hairlineui.com/ui/citation
- Registry item (source): https://hairlineui.com/r/citation.json

## Usage

```tsx
import { Citation } from "@/components/citation";

<p>
  Highlights style text without touching the DOM
  <Citation index={1} source={{ title: "CSS Custom Highlight API", url: "https://developer.mozilla.org/…", snippet: "…" }} />
</p>
```

## With the AI SDK

```tsx
// app/api/chat/route.ts: send search results to the client as sources
return result.toUIMessageStreamResponse({ sendSources: true });

// The answer: sources arrive as "source-url" parts; swap each [n] the model writes for a Citation
import type { UIMessage } from "ai";
import { Citation } from "@/components/citation";

function Answer({ message }: { message: UIMessage }) {
  const sources = message.parts.filter((part) => part.type === "source-url");
  const text = message.parts.map((part) => (part.type === "text" ? part.text : "")).join("");
  return (
    <p>
      {text.split(/\[(\d+)\]/).map((chunk, i) => {
        if (i % 2 === 0) return chunk;
        const source = sources[Number(chunk) - 1];
        return (
          <Citation
            key={i}
            index={Number(chunk)}
            source={source && { title: source.title ?? source.url, url: source.url }}
          />
        );
      })}
    </p>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `index` | `number` | – | The number shown in the marker. |
| `source` | `{ title; url; snippet?; icon? }` | – | What the card shows. icon takes a favicon element; a globe is shown without one. |
| `status` | `"available" \| "unavailable"` | `"available"` | Unavailable (or no source) strikes the marker and explains in the card. |

## States

- **closed**: A quiet numbered marker that sits on the text’s baseline.
- **open**: After a short hover delay, or at once on focus: site, title and a snippet. The card rises 4px out of a 2px blur (220ms) and sinks back the same way when it closes (140ms).
- **unavailable**: The marker is struck through and the card says the source couldn’t be loaded.

## What it handles

Answers that cite sources earn trust, but a wall of links at the bottom gets ignored. This puts the source right next to the claim it supports.

- **A marker in the sentence**: A small numbered marker that sits on the text’s baseline, where the claim is.
- **A preview of the source**: Hover or focus it to see the site, the title and a snippet.
- **It stays on screen**: The card flips above near the bottom of the screen and shifts to stay inside it.
- **It opens the source**: The marker is a real link, so middle-click and Ctrl-click work.
- **Broken sources say so**: A source that couldn’t be fetched is struck through and the card explains it.
- **No flicker**: Short open and close delays stop cards flashing as the pointer crosses a paragraph. Escape closes it.

## Replaces

a tooltip, hover delay timers, popover positioning, a footnote link, a broken-source state

## Where it fits

- **AI answers with sources**: Numbered markers inside the sentences of a chatbot answer.
- **Research and search tools**: Answers where each claim points to the page it came from.
- **Documentation assistants**: Show which docs page an answer was taken from.
- **Reports and articles**: Footnote-style references that preview the source in place.

## Notes

- The marker is a real link, so middle-click and Ctrl-click work, and it’s labelled "Source 1: title".
- The card is a tooltip linked with aria-describedby; it opens on keyboard focus and closes on Escape.
- Short open and close delays stop cards flickering as the pointer crosses a paragraph.

License: MIT (https://hairlineui.com/license#components)
