# Citations

> Numbered references in text, with the source on hover.

Page: https://uicontext.com/docs/components/citations

Group: Documents.

## When to use

Numbered references in text, with the source on hover. Use [Attribution](https://uicontext.com/docs/components/attribution.md) instead when you show who wrote it, not where it came from.

## Usage

```ts
import { type Citation, CitedProse } from "./ui/uicontext/components/Cite.ts"

const Message = UiSchema.TaggedEnum({ OpenedSource: { id: Schema.String } })

// `[n]` markers in the agent's Markdown become chips; a click opens the source in the app
export const Answer = (h: Html.H<typeof Message.Type>, text: string, sources: ReadonlyArray<Citation>) =>
	CitedProse(h, text, sources, { onOpen: (id) => Message.OpenedSource({ id }) })
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### An answer with sources

The example the page opens with: Citations in “An answer with sources”.

```ts title="src/demo.ts" source="www/src/examples/citations/demo.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { type Citation, CitedProse, CiteList } from "./ui/uicontext/components/Cite.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

// An assistant's answer about churn, with its sources. Hover or focus a number to see what it
// points to and the passage it used; press it to open the source beside the answer.

const styles = stylex.create({
	answer: { display: "flex", flexDirection: "column", gap: space.lg, inlineSize: "min(36rem, 100%)", paddingBlockStart: space.xxl },
	heading: { margin: 0, color: color.muted, fontSize: font.xs, fontWeight: 600, textTransform: "uppercase", letterSpacing: "0.04em" },
	opened: { margin: 0, color: color.muted, fontSize: font.sm },
})

const sources: ReadonlyArray<Citation> = [
	{
		id: "forecast",
		label: "1",
		title: "Q3 retention forecast",
		kind: "file",
		source: "Finance / Forecasts",
		snippet: "Monthly churn fell from 3.1% in June to 2.4% in September.",
	},
	{
		id: "interviews",
		label: "2",
		title: "Customer interviews: onboarding",
		kind: "page",
		source: "Research",
		snippet: "Seven of nine admins said the import checklist got them live in a day.",
	},
	{
		id: "thread",
		label: "3",
		title: "Pricing change rollout",
		kind: "message",
		source: "#revenue",
		snippet: "Annual plans moved to the default on the pricing page on August 4.",
	},
]

const answer = [
	"Churn dropped about **a fifth** this quarter [1]. Two changes line up with it:",
	"",
	"- The new import checklist, which admins credit for getting their team live in a day [2].",
	"- Annual plans became the default on the pricing page in early August [3].",
	"",
	"The forecast doesn't separate the two, so treat the split between them as unknown.",
].join("\n")

const Message = UiSchema.TaggedEnum({ OpenedSource: { id: Schema.String } })

export const ChurnAnswer = Html.component({
	name: "ChurnAnswer",
	model: Schema.Struct({ opened: Schema.NullOr(Schema.String) }),
	message: Message,
}).make({
	init: () => ({ opened: null }),
	update: { OpenedSource: ($, { id }) => $.evolve({ opened: id }) },
	view: (h, { opened }) =>
		h.article.attrs({ "aria-label": "Answer", ...sx(styles.answer) }).children(
			CitedProse(h, answer, sources, { onOpen: (id) => Message.OpenedSource({ id }) }),
			h.h3.attrs(sx(styles.heading)).children("Sources"),
			CiteList(h, { citations: sources, onOpen: (id) => Message.OpenedSource({ id }) }),
			h.p
				.attrs({ role: "status", ...sx(styles.opened) })
				.children(opened === null ? "" : `Opened “${sources.find((s) => s.id === opened)?.title ?? opened}” beside the answer.`),
		),
})
```

### Chips

A number, a named chip, a link and a deleted source. Each has a full accessible name and shows its source on hover or focus.

```ts title="src/chips.ts" source="www/src/examples/citations/chips.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { CiteChip } from "./ui/uicontext/components/Cite.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

// Every kind of chip: a number in running text, a named chip with its source's kind, a link that
// follows `href`, and a source that was deleted, which still says what it was.

const styles = stylex.create({
	list: { display: "grid", gap: space.lg, margin: 0, padding: 0, paddingBlockStart: space.xxl, listStyle: "none", inlineSize: "min(34rem, 100%)" },
	row: { display: "flex", alignItems: "baseline", flexWrap: "wrap", gap: space.md, color: color.fg, fontSize: font.base, lineHeight: 1.6 },
	label: { minInlineSize: "9rem", color: color.muted, fontSize: font.sm },
})

export const CitationChips = <M>(h: Html.H<M>) =>
	h.ul.attrs(sx(styles.list)).children(
		h.li.attrs(sx(styles.row)).children(
			h.span.attrs(sx(styles.label)).children("A number"),
			h.span.children(
				"Response times halved after the queue split",
				CiteChip(h, { id: "a", label: "1", title: "Support metrics, October", kind: "row", source: "Support dashboard", snippet: "Median first reply: 2h 10m → 58m." }),
				".",
			),
		),
		h.li.attrs(sx(styles.row)).children(
			h.span.attrs(sx(styles.label)).children("A named chip"),
			CiteChip(h, { id: "b", label: "billing-api", title: "Retry failed charges", kind: "code", source: "northwind/billing-api #412", snippet: "Retries back off for up to 24 hours." }),
		),
		h.li.attrs(sx(styles.row)).children(
			h.span.attrs(sx(styles.label)).children("A web page"),
			CiteChip(h, { id: "c", label: "Card rules", title: "Card network dispute timelines", kind: "web", source: "example.com", href: "https://example.com/disputes" }),
		),
		h.li.attrs(sx(styles.row)).children(
			h.span.attrs(sx(styles.label)).children("No longer available"),
			CiteChip(h, { id: "d", label: "4", title: "Vendor contract draft", kind: "file", source: "Legal", unavailable: true }),
		),
	)
```

### A summary with its sources

`[n]` markers become chips; a link opens its record, and a deleted source still says what it was.

```ts title="src/summary.ts" source="www/src/examples/citations/summary.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { type Citation, CitedProse } from "./ui/uicontext/components/Cite.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, corner, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

// An issue's summary, written by an agent from the thread and two linked records. Each claim
// points at what it came from; one source was deleted since, and says so.

const styles = stylex.create({
	card: {
		inlineSize: "min(100%, 30rem)",
		marginBlockStart: space.xxl,
		padding: space.lg,
		borderRadius: corner.card,
		borderWidth: 1,
		borderStyle: "solid",
		borderColor: color.line,
		backgroundColor: color.panel,
		fontSize: font.sm,
	},
})

const sources: ReadonlyArray<Citation> = [
	{ id: "c1", label: "1", title: "Ben Okafor's comment, Dec 1", kind: "comment", source: "PAY-398", snippet: "Card retries fail with declined since the 5 PM deploy." },
	{ id: "c2", label: "2", title: "Deploy 2026.12.01-3", kind: "row", source: "Deploys", href: "#deploys", snippet: "Changed: idempotency key now built from the cart id." },
	{ id: "c3", label: "3", title: "Rollback checklist", kind: "page", source: "Runbooks", unavailable: true },
]

const summary =
	"Retries have failed since the evening deploy [1], which started building the idempotency key from the cart id [2]. " +
	"Mira rolled back at 10:02 following the old checklist [3]; nobody has confirmed retries work again."

export const IssueSummary = <M>(h: Html.H<M>) => h.div.attrs(sx(styles.card)).children(CitedProse(h, summary, sources))
```

## API reference

### CiteList (view function)

**Options**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `citations` (required) | `Citation[]` | — | The sources, numbered in the order they come |
| `label` | `string` | `"Sources"` | The list's accessible name; "Sources" by default |
| `onOpen` | `(id: string) => Message` | — | The message to send when a source is opened; without it, sources with a link open it |

### SourceList (view function)

**Options**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `citations` (required) | `Citation[]` | — | — |
| `label` | `string` | `"Sources"` | The list's accessible name; "Sources" by default |

### Cite (view function)

**Options**

| Name | Type | Description |
| --- | --- | --- |
| `onOpen` | `(id: string) => Message` | The app opens the source itself (a side panel, a row); otherwise the chip follows `href` |
| `variant` | `"number" \| "chip"` | "number" for an inline marker ("1"), "chip" for the label with the source's kind |

## Keyboard and accessibility

- A chip is named for what it points to ("Source 2: Q3 forecast"), not only its number, and shows the quote or title as a tooltip.
- It's a link when the source has one and a button when the app opens it; a source that's gone is plain text that says so.
- The list of sources is an ordered list named Sources.

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/citations
```

With npm: `npx shadcn@latest add checkfu/uicontext/citations`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/citations`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/citations`.

It writes these files:

- `src/ui/uicontext/components/Cite.ts`
- `src/ui/uicontext/components/Prose.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/cite.stylex.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/Cite.ts](https://github.com/checkfu/uicontext/blob/main/src/Cite.ts)

## Used in

- [Chat](https://uicontext.com/blocks/ai-chat.md)
- [Company brain](https://uicontext.com/blocks/doc-workspace.md)
- [Team chat](https://uicontext.com/blocks/team-chat.md)

## Related

- [Editor](https://uicontext.com/docs/components/editor.md) (`editor`): A block editor with a slash menu for headings, lists, to-dos and more.
- [Document Blocks](https://uicontext.com/docs/components/document-blocks.md) (`document-blocks`): Read-only rendering of a document's blocks.
- [Markdown](https://uicontext.com/docs/components/markdown.md) (`markdown`): Renders Markdown with readable type.
- [Code Block](https://uicontext.com/docs/components/code-block.md) (`code-block`): Code to read and copy, with highlighting, a file name and line numbers.
- [JSON Viewer](https://uicontext.com/docs/components/json-viewer.md) (`json-viewer`): A JSON value as a tree to open, search and walk with the arrow keys, such as a webhook's payload or a tool call's result.
- [Log Viewer](https://uicontext.com/docs/components/log-viewer.md) (`log-viewer`): A log that follows its newest line, with a filter, level buttons and Wrap, such as a deploy's or a server's.
