Skip to content

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

Components

Free

Inline Citation for React

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 Base 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";
interface Source {
  description: string;
  title: string;
  url: string;
}

const sources = {
  chunked: {
    description: "Send a response as a series of chunks.",
    title: "Transfer-Encoding",
    url: "https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Transfer-Encoding",
  },
  loading: {
    description: "Show an instant loading state for a route segment.",
    title: "loading.js",
    url: "https://nextjs.org/docs/app/api-reference/file-conventions/loading",
  },
  serverComponents: {
    description: "Components that render ahead of time, on the server.",
    title: "Server Components",
    url: "https://react.dev/reference/rsc/server-components",
  },
  streaming: {
    description: "How the App Router streams a route to the browser.",
    title: "Linking and Navigating",
    url: "https://nextjs.org/docs/app/getting-started/linking-and-navigating#streaming",
  },
  suspense: {
    description: "Display a fallback until its children have finished loading.",
    title: "<Suspense>",
    url: "https://react.dev/reference/react/Suspense",
  },
} satisfies Record<string, Source>;

const WWW = /^www\./;

function hostname(url: string) {
  return new URL(url).hostname.replace(WWW, "");
}

function Citation({ source }: { source: Source }) {
  const host = hostname(source.url);

  return (
    <HoverCard>
      {/* The chip is the link (Base UI's trigger renders an `<a>`), so a
          keyboard, a tap or a screen reader can follow it: the card is a
          preview that hover and focus open. */}
      <HoverCardTrigger
        className={buttonVariants({ size: "xs", variant: "secondary" })}
        closeDelay={100}
        delay={10}
        href={source.url}
        rel="noreferrer"
        target="_blank"
      >
        {host}
        <span className="sr-only"> (opens in a new tab)</span>
      </HoverCardTrigger>
      <HoverCardContent className="flex w-64 flex-col gap-1">
        <div className="font-medium">{source.title}</div>
        <p className="text-muted-foreground">{source.description}</p>
        <a
          className="text-muted-foreground text-xs underline-offset-4 hover:underline"
          href={source.url}
          rel="noreferrer"
          target="_blank"
        >
          {host}
          <span className="sr-only"> (opens in a new tab)</span>
        </a>
      </HoverCardContent>
    </HoverCard>
  );
}

export function InlineCitationDemo() {
  return (
    <div className="mx-auto flex max-w-sm flex-col gap-3 py-6 text-sm leading-relaxed">
      <p>
        Next.js streams a route in chunks, so the page shows up before every
        request finishes. <Citation source={sources.streaming} />
      </p>
      <p>
        Wrap a slow part of the page in a Suspense boundary and React sends the
        fallback first. When the data resolves, the finished HTML streams into
        the same response and replaces the fallback, with no extra request from
        the browser. <Citation source={sources.suspense} /> Server Components
        help here, because their code never ships to the browser.{" "}
        <Citation source={sources.serverComponents} />
      </p>
      <p>
        A loading.tsx file does the same for a whole route segment.{" "}
        <Citation source={sources.loading} /> It all runs over chunked transfer
        encoding, so there is nothing extra to set up.{" "}
        <Citation source={sources.chunked} />
      </p>
    </div>
  );
}

Accessibility

The chip is a link to the source, since Base UI's trigger renders an <a>, 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". Base UI opens the card on keyboard focus as well as on hover, 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/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.
   - InlineCitationCardTrigger renders a HoverCardTrigger instead of a Badge: a link 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. Set delay and closeDelay on the trigger.
   - 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 stays closed on a phone, because Base UI opens it on mouse hover and on keyboard focus and ignores both a touch and the focus a tap gives the link.

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 Base 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 delay and closeDelay on HoverCardTrigger, in milliseconds. Base UI waits 600 to open and 300 to close when you leave them out, and the demo sets 10 and 100.

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.