Skip to content
Shaken UI
Sign in

Inline Citation

Save

A numbered citation chip inside a sentence that opens a card with the source title, snippet, and URL, paging between several sources.

Category
AI elements
Version
0.1.1
Frameworks
Next.js, Vite + React
Styling
Tailwind CSS v4

Preview

Inline Citation preview

The View Transitions API now works across documents in Chromium browsers, so a multi-page site can animate between routes without a client router. Safari shipped same-document transitions first and added cross-document support a release later.

Installation

npx @shakenui/cli add inline-citation

Review the plan first. A dry run lists every file and package without changing anything:

npx @shakenui/cli add inline-citation --dry-run

Usage

inline-citation-example.tsx
import { InlineCitation } from "@/components/shaken/agent/inline-citation";

export default function InlineCitationExample() {
  return (
    <p className="w-full max-w-lg text-sm leading-7 text-zinc-800 dark:text-zinc-200">
      The View Transitions API now works across documents in Chromium browsers, so a multi-page site can animate between
      routes without a client router
      <InlineCitation
        index={1}
        sources={[
          {
            title: "Cross-document view transitions",
            url: "https://developer.chrome.com/docs/web-platform/view-transitions/cross-document",
            snippet:
              "Opt in on both pages with the @view-transition at-rule. The browser captures the old page, loads the new one, and animates between the two snapshots.",
          },
        ]}
      />
      . Safari shipped same-document transitions first and added cross-document support a release later
      <InlineCitation
        index={2}
        sources={[
          {
            title: "WebKit features in Safari 18.2",
            url: "https://webkit.org/blog/16301/webkit-features-in-safari-18-2/",
            snippet: "Safari 18.2 adds support for cross-document view transitions, matching the at-rule syntax used in other engines.",
          },
          {
            title: "View Transition API browser support",
            url: "https://caniuse.com/view-transitions",
            snippet: "Same-document view transitions are supported in Chrome 111+, Edge 111+, and Safari 18+. Firefox support is in development.",
          },
          {
            title: "Smooth transitions with the View Transition API",
            url: "https://developer.mozilla.org/en-US/docs/Web/API/View_Transition_API",
            snippet: "The API lets you capture the current state, update the DOM, and then animate from the old state to the new one with CSS.",
          },
        ]}
      />
      .
    </p>
  );
}

API

PropTypeDefaultDescription
indexnumber—The number on the chip, matching the Sources list.
sources{ title, url, snippet?, domain? }[]—One or more sources behind the claim. With more than one the chip reads 2 +2 and the card pages between them.
delaynumber250Hover delay in milliseconds before the card opens. Click, Enter, and Space open it at once.

Accessibility

  • The chip is a button (Base UI Popover trigger) with an aria-label such as Source 2: WebKit features in Safari 18.2, and 2 more.
  • The card opens on hover and on click or Enter; focus moves into it, Escape closes it and returns focus to the chip.
  • With several sources, Previous and Next buttons are labelled, the 1 of 3 counter is a polite live region, and Left and Right arrow keys page without animation.
  • The source link in the card notes that it opens in a new tab. The card scales in from the chip in 160ms; instant with reduced motion.

Files

Installed into src/components/shaken, or components/shaken when the project has no src directory.

  • src/components/shaken/agent/inline-citation.tsxinline-citation
  • Also installs popover@0.2.2registry dependency
  • npm packages: noneReact and Tailwind CSS v4 required

Source

The exact source the CLI installs, from /r/items/react/inline-citation/0.1.1.json.

agent/inline-citation.tsxShow
agent/inline-citation.tsx
"use client";

import { type KeyboardEvent, useState } from "react";
import { Popover, PopoverContent, PopoverTrigger } from "../ui/popover";

function cx(...classes: (string | false | null | undefined)[]) {
  return classes.filter(Boolean).join(" ");
}

export interface CitationSource {
  /** Page title. */
  title: string;
  /** Full URL. */
  url: string;
  /** The passage the claim rests on. Clamped to four lines. */
  snippet?: string;
  /** Shown above the title. Defaults to the URL's host without `www.`. */
  domain?: string;
}

export interface InlineCitationProps {
  /** One or more sources behind the claim. Several sources page inside one card. */
  sources: CitationSource[];
  /** The number on the chip, matching the Sources list. */
  index: number;
  /** Hover delay before the card opens, in milliseconds. Click, Enter, and Space open it at once. */
  delay?: number;
  className?: string;
}

function domainOf(source: CitationSource): string {
  if (source.domain) return source.domain;
  try {
    return new URL(source.url).hostname.replace(/^www\./, "");
  } catch {
    return source.url;
  }
}

/**
 * A numbered chip that sits inside a sentence and opens a card with the source title, snippet,
 * and URL. It opens on hover and on click or Enter; a citation with several sources pages
 * between them with buttons or the arrow keys.
 */
export function InlineCitation({ sources, index, delay = 250, className }: InlineCitationProps) {
  const [page, setPage] = useState(0);
  const total = sources.length;
  const current = sources[Math.min(page, total - 1)];
  if (!current) return null;
  const extra = total - 1;

  function go(step: number) {
    setPage((value) => (value + step + total) % total);
  }

  function onKeyDown(event: KeyboardEvent<HTMLDivElement>) {
    if (total < 2) return;
    if (event.key === "ArrowRight") {
      event.preventDefault();
      go(1);
    } else if (event.key === "ArrowLeft") {
      event.preventDefault();
      go(-1);
    }
  }

  return (
    <Popover onOpenChange={(open) => !open && setPage(0)}>
      <PopoverTrigger
        openOnHover
        delay={delay}
        closeDelay={120}
        aria-label={`Source ${index}: ${sources[0]?.title ?? ""}${extra > 0 ? `, and ${extra} more` : ""}`}
        className={cx(
          "relative -top-px mx-0.5 inline-flex h-[18px] cursor-pointer items-center gap-1 rounded-md px-1.5 align-middle font-mono text-[11px] leading-none font-medium tabular-nums",
          "border border-zinc-950/10 text-zinc-600 transition-[background-color,border-color,color] duration-150 ease-out motion-reduce:transition-none",
          "hover:border-zinc-950/20 hover:bg-zinc-950/[0.04] hover:text-zinc-950 data-popup-open:border-zinc-950/20 data-popup-open:bg-zinc-950/[0.06] data-popup-open:text-zinc-950",
          "dark:border-white/15 dark:text-zinc-400 dark:hover:border-white/25 dark:hover:bg-white/[0.06] dark:hover:text-white dark:data-popup-open:border-white/25 dark:data-popup-open:bg-white/[0.09] dark:data-popup-open:text-white",
          "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-sky-500",
          className,
        )}
      >
        {index}
        {extra > 0 ? <span className="text-zinc-500">+{extra}</span> : null}
      </PopoverTrigger>
      <PopoverContent side="top" sideOffset={6} className="w-80 p-0!" onKeyDown={onKeyDown} aria-label={`Source ${index}`}>
        {total > 1 ? (
          <div className="flex items-center justify-between gap-3 border-b border-zinc-950/[0.07] py-1.5 pr-1.5 pl-3.5 dark:border-white/[0.08]">
            <p className="font-mono text-xs text-zinc-500 tabular-nums" aria-live="polite">
              {page + 1} of {total}
            </p>
            <div className="flex items-center gap-0.5">
              <PageButton label="Previous source" onClick={() => go(-1)} direction="previous" />
              <PageButton label="Next source" onClick={() => go(1)} direction="next" />
            </div>
          </div>
        ) : null}
        <a
          href={current.url}
          target="_blank"
          rel="noreferrer"
          className={cx(
            "group/source block p-3.5",
            "transition-colors duration-150 ease-out motion-reduce:transition-none",
            "hover:bg-zinc-950/[0.025] dark:hover:bg-white/[0.04]",
            "focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-sky-500",
            total > 1 ? "rounded-b-xl" : "rounded-xl",
          )}
        >
          <span className="flex items-center gap-2 text-xs text-zinc-500">
            <span
              aria-hidden="true"
              className="inline-flex size-4 shrink-0 items-center justify-center rounded-full text-[9px] font-semibold text-zinc-600 shadow-[inset_0_0_0_1px_rgb(9_9_11/0.1)] dark:text-zinc-300 dark:shadow-[inset_0_0_0_1px_rgb(255_255_255/0.16)]"
            >
              {domainOf(current).charAt(0).toUpperCase()}
            </span>
            <span className="min-w-0 truncate">{domainOf(current)}</span>
          </span>
          <span className="mt-2 block text-sm leading-5 font-semibold text-zinc-950 group-hover/source:underline group-hover/source:decoration-zinc-950/30 group-hover/source:underline-offset-2 dark:text-white dark:group-hover/source:decoration-white/40">
            {current.title}
          </span>
          {current.snippet ? (
            <span className="mt-1 line-clamp-4 block text-[13px] leading-5 text-zinc-600 dark:text-zinc-400">{current.snippet}</span>
          ) : null}
          <span className="mt-2.5 block truncate font-mono text-[11px] text-zinc-500">{current.url.replace(/^https?:\/\//, "")}</span>
          <span className="sr-only">(opens in a new tab)</span>
        </a>
      </PopoverContent>
    </Popover>
  );
}

function PageButton({ label, onClick, direction }: { label: string; onClick: () => void; direction: "previous" | "next" }) {
  return (
    <button
      type="button"
      aria-label={label}
      onClick={onClick}
      className={cx(
        "inline-flex size-7 cursor-pointer items-center justify-center rounded-md text-zinc-500",
        "transition-[background-color,color] duration-150 ease-out motion-reduce:transition-none",
        "hover:bg-zinc-950/[0.04] hover:text-zinc-950 dark:hover:bg-white/[0.06] dark:hover:text-white",
        "focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-sky-500",
      )}
    >
      <svg aria-hidden="true" viewBox="0 0 16 16" fill="none" className={cx("size-4", direction === "previous" && "rotate-180")}>
        <path d="m6.25 4.5 3.5 3.5-3.5 3.5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
      </svg>
    </button>
  );
}