Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Inline Citation for Ark UI

A chip at the end of the sentence it backs that links to the source and previews its title in a card. An AI citation for React on Ark UI.

On this page10 sections

An inline citation is a small chip at the end of a sentence in an AI answer, naming the site that backs it, such as react.dev. The chip links to that page, and pointing at it or focusing it opens a card with the page's title and a line about it.

It is a pattern: a link with the look of a Button that previews its page in a Hover Card. To list every page the agent read under the reply instead, use Sources.

Preview width

Installation

Inline Citation is a pattern. The command adds the parts it's built from, Hover Card and Button, and the code to copy is the demo's.

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Inline Citation anatomy: hover card trigger, hover card content, link

Usage

import { buttonVariants } from "@/components/ui/button";
import {
  HoverCard, HoverCardContent, HoverCardTrigger,
} from "@/components/ui/hover-card";
export function Citation(props: { title: string; url: string }) {
  const hostname = new URL(props.url).hostname;

  return (
    <HoverCard closeDelay={100} openDelay={10}>
      <HoverCardTrigger asChild>
        <a
          className={buttonVariants({ size: "xs", variant: "secondary" })}
          href={props.url}
          rel="noreferrer"
          target="_blank"
        >
          {hostname}
          <span className="sr-only"> (opens in a new tab)</span>
        </a>
      </HoverCardTrigger>
      <HoverCardContent className="flex w-64 flex-col gap-1">
        <div className="font-medium">{props.title}</div>
        <a href={props.url} rel="noreferrer" target="_blank">
          {hostname}
          <span className="sr-only"> (opens in a new tab)</span>
        </a>
      </HoverCardContent>
    </HoverCard>
  );
}

Render one Citation per source, right after the sentence it supports. The chip is the link itself, so Enter or a tap opens the page, and the card is a preview for those who hover or focus it.

Accessibility

The chip is a link to the source, so it sits in the tab order, Enter or a tap opens the page in a new tab, and a screen reader hears the hostname followed by "opens in a new tab" from a visually hidden span. Focusing it also opens the card, as hovering does. Ark UI adds no ARIA to the chip or the card and closes the card once focus leaves the chip, so the title and description reach sighted users only. Keep the hostname meaningful on its own.

Tab
Moves focus to the next citation and opens its card. The card closes when focus leaves its chip.
Shift+Tab
Moves focus to the previous citation and opens its card.
Enter
Opens the citation's page in a new tab.
Escape
Closes the card, and focus stays on the chip.

AI SDK

With the AI SDK, a source reaches the client as a source-url part of the message, with a sourceId, a url and an optional title. The route sends those parts only when you pass sendSources: true to toUIMessageStreamResponse, or you can write one yourself from a custom stream.

app/api/chat/route.ts
const stream = createUIMessageStream({
  execute: ({ writer }) => {
    writer.write({
      type: "source-url",
      sourceId: "react-suspense",
      url: "https://react.dev/reference/react/Suspense",
      title: "<Suspense>",
    });
  },
});
const sources = message.parts.filter((part) => part.type === "source-url");

A part says nothing about where in the text it belongs, so placing each chip is up to you, as the FAQ explains. When title is missing, show the hostname of the url.

Migrating from AI SDK Elements

If you added Inline Citation from AI SDK Elements (opens in a new tab), your project has a components/ai-elements/inline-citation.tsx wrapper. Paste this prompt into your coding agent to rebuild it on Hover Card and Button. Every export keeps its name and props, so the code that uses it keeps working.

Prompt
Migrate components/ai-elements/inline-citation.tsx from AI SDK Elements to the shadcn.io Hover Card and Button components.

Keep every export, with the same name and props, so the code that imports it keeps working. Only change how each part renders.

1. Read https://www.shadcn.io/ui/ark/inline-citation.md and build Inline Citation the same way. If the project hasn't got Hover Card and Button, run the command in its Installation section.
2. Rebuild each part:
   - InlineCitationCard renders a HoverCard with openDelay and closeDelay.
   - InlineCitationCardTrigger renders a HoverCardTrigger with asChild around a link instead of a Badge: an <a> to the first source's URL with target="_blank", rel="noreferrer", className={buttonVariants({ variant: "secondary", size: "xs" })} and "(opens in a new tab)" in an sr-only span, so keyboard and touch users can follow it.
   - InlineCitationCardBody renders a HoverCardContent.
   - InlineCitationSource renders the title and a link to the URL that opens in a new tab, with "(opens in a new tab)" in an sr-only span.
   - Keep InlineCitation, InlineCitationText, InlineCitationQuote and the carousel parts as they are.
3. Remove the imports the file no longer uses.
4. Run the type checker and fix any errors.

FAQ

How do I put an Inline Citation next to the sentence it supports?

Have the model mark its claims, because the AI SDK doesn't say where a source belongs. A source-url part carries a sourceId, a url and an optional title, and nothing about its place in the text.

Ask in the system prompt for the source's number in brackets after each claim it backs, such as [2]. Then split the text on those marks and render the chip of that source in place of each one. A source the model never marks still belongs in a Sources list under the reply.

Why is the Inline Citation chip a link and not a Badge?

A Badge renders a span, which takes no focus and goes nowhere, so neither a keyboard nor a tap could reach the source. A link sits in the tab order, opens the page on Enter or a tap, and still opens the card when it is hovered or focused.

The demo styles the link with buttonVariants, variant secondary at size xs, so the chip reads as a quiet label inside a line of text.

Why does the Inline Citation chip show the hostname instead of a number?

Because the hostname tells people where a claim comes from before they open anything. react.dev reads as the React docs at a glance, while [2] means nothing until someone hovers it.

A page title would be too long for the middle of a paragraph, so it waits in the card along with a line about the page. The demo's hostname helper also strips a leading www, so a page on www.npmjs.com gets an npmjs.com chip.

Does Inline Citation work on a phone?

Yes, as a link: a tap on the chip opens the page in a new tab. The card is a hover preview, and Ark UI ignores touch when it tracks hover, so on a phone it opens only when the tap also gives the link focus, which depends on the browser.

Few people on a phone will see the card's title and description, so make the hostname in the chip say where the claim comes from on its own.

Can keyboard users open the source of an Inline Citation?

Yes, through the chip, which is the link: Tab reaches it and Enter opens the page in a new tab. Focusing it also opens the card, but Ark UI closes the card as soon as focus leaves the chip, so Tab goes on to the next citation and the card's own link is there for the mouse.

How do I change how fast an Inline Citation card opens?

Set openDelay and closeDelay on HoverCard, in milliseconds. The component in components/ui/hover-card already defaults them to 10 and 100, the values the demo repeats, while Ark UI's own defaults are 600 and 300.

A short open delay suits citations, which people point at on purpose. Raise it if cards flash open while the pointer crosses a paragraph on its way somewhere else.

Should I use Inline Citation or Sources for AI answers?

Use Inline Citation when people need to check one claim at a time, in a long answer where each paragraph rests on a different page. The chip sits right beside the sentence it backs.

Use Sources to list every page under the reply, which suits a short answer drawn from a few pages. The two work together: the chips back single claims, and the list gathers every page the agent read, including the ones it never cited inline.

Is this the same as AI SDK Elements' Inline Citation?

It does the same job with fewer parts. In AI SDK Elements the trigger was a Badge and the card held a carousel of sources, and here a link with a Button's look opens the source and previews it in a Hover Card.

The Migrating from AI SDK Elements section has a prompt that rebuilds the old file on these parts and keeps its exports, so the code that imports it doesn't change.

API Reference

Inline Citation has no props of its own. Each chip is a link styled as a Button with variant secondary and size xs. It is the trigger of a Hover Card whose content holds the title, the description and a second link to the page.