# Citation

Inline source reference pill for RAG and AI answers that opens a keyboard- and touch-accessible preview popover with the source domain, external title link and optional excerpt.

**Dependencies:** npm: 1 direct, 3 total; registry: 3 direct, 3 total.

## Usage

### When to use

- Attach inline numbered source references to RAG or AI answers with a keyboard- and touch-accessible preview.
- Let readers verify the source domain, title and excerpt before opening an external link.

### When not to use

- Standard navigation links in prose. Use Link.
- Design token names or short machine values. Use Token.
- Hover-only previews that do not need touch activation. Use HoverCard.

## Examples

### Inline citations

Compact numbered pills inside an assistant answer that open a source preview popover on press.

```tsx
<div className="flex max-w-md flex-col gap-xs text-sm text-text-primary">
  <p>
    Every public role token points to a derived value.
    <Citation index={1} title="Design tokens reference" href="https://www.werewolf-ui.dev/docs/tokens" snippet="Two DTCG layers hold user choices and public role tokens." />
  </p>
  <p>
    Compact controls extend their hit area on touch.
    <Citation index={2} title="Accessibility guide" href="https://www.werewolf-ui.dev/docs/accessibility" snippet="Compact controls extend their hit area to 44px on touch." />
  </p>
</div>
```

### With domain in the pill

showDomain displays the host beside the reference index inside the inline pill.

```tsx
<Citation index={1} showDomain title="Color system" href="https://www.werewolf-ui.dev/docs/foundations/colors" snippet="Neutral and accent pairs are checked for WCAG and APCA contrast." />
```

### Without an index

When index is omitted, the pill falls back to displaying the source domain.

```tsx
<Citation title="CLI reference" href="https://www.werewolf-ui.dev/docs/cli" />
```

## Do and don't

### Do

- Place compact `[1]` citations inline at the end of the claim they support.
- Include a short `snippet` so readers can verify the source before leaving the page.
- Always pass a descriptive `title` so screen readers and preview cards name the source clearly.

### Don't

- Do not pack long paragraphs into `snippet`; keep excerpts to one or two sentences.
- Do not use `Citation` for general navigation links; use Link.

## Props

| Name | Type | Values | Default | Description |
| --- | --- | --- | --- | --- |
| `index` | `number \| string` |  |  | Optional reference number or marker shown inside the inline pill, such as `1`. |
| `title` (required) | `string` |  |  | Required source title shown as the external link in the preview popover and used in the trigger `aria-label`. |
| `href` (required) | `string` |  |  | Required source URL opened by the external link and used to derive `domain` when omitted. |
| `domain` | `string` |  |  | Optional short host label. Derived safely from `href` when omitted. |
| `snippet` | `node` |  |  | Optional quoted excerpt shown inside the preview popover. |
| `showDomain` | `boolean` |  | `false` | Shows the domain text beside the index inside the inline pill. When `false`, the pill shows `index`, or `domain` if `index` is omitted. |
| `open` | `boolean` |  |  | Controlled open state of the preview popover. Pair with `onOpenChange`. |
| `defaultOpen` | `boolean` |  | `false` | Initial open state when uncontrolled. |
| `onOpenChange` | `(open: boolean) => void` |  |  | Called with `true` or `false` when the preview popover opens or closes. |

## Anatomy

| Part | Description |
| --- | --- |
| `root` | The inline pill button: `size-inline-sm` (20px) tall, `radius-full`, gray fill and monospace secondary text. |
| `hitArea` | Transparent `::before` centered on the pill, never smaller than `size-target-min` (24px on mouse, 44px on touch). |
| `content` | The Popover panel holding the source domain, external title link and optional excerpt. |
| `domain` | Host label and external-link icon at the top of the preview card in tertiary text. |
| `link` | Underlined external link for the source title inside the preview card (`target="_blank"` and `rel="noopener noreferrer"`). |
| `snippet` | Optional quoted excerpt in small secondary text inside the preview card. |

## Accessibility

Role: `button`

| Keys | Action |
| --- | --- |
| `Enter, Space` | Opens or closes the source preview popover. |
| `Tab` | Moves focus to the external link inside the open popover. |
| `Escape` | Closes the popover and returns focus to the citation pill. |

- The trigger defaults its `aria-label` to `${title} (${domain})` so compact `[1]` pills remain meaningful to screen readers.
- The 20px pill extends its touch target to `size-target-min` (44px on touch) via a centered transparent `::before` pseudo-element.

## Install

```bash
npx werewolf-ui add https://www.werewolf-ui.dev/r/citation.json
```

Run `npx werewolf-ui init` once in your app before adding components. Init sets up tokens, their CSS import and agent rules. Add installs this component and its dependencies while preserving your theme. See [Installation](https://www.werewolf-ui.dev/docs/installation.md) for requirements and the alternative registry setup.
