Citation
An inline source chip that opens a preview card on hover or focus.
Code
components/thinking-ui/citation.tsx
"use client";
import { AnimatePresence, motion } from "motion/react";
import { useId, useMemo, useRef, useState } from "react";
import { useMotion } from "@/lib/motion";
import { cn } from "@/lib/utils";
export type CitationSource = {
title: string;
url: string;
description?: string;
};
export type CitationProps = {
index: number;
source: CitationSource;
className?: string;
};
export function Citation({ index, source, className }: CitationProps) {
const [open, setOpen] = useState(false);
const closeTimer = useRef<ReturnType<typeof setTimeout>>(undefined);
const m = useMotion();
const cardId = useId();
const domain = useMemo(() => {
try {
return new URL(source.url).hostname.replace(/^www\./, "");
} catch {
return source.url;
}
}, [source.url]);
const show = () => {
clearTimeout(closeTimer.current);
setOpen(true);
};
// Small delay so the pointer can travel from the chip to the card
const hide = () => {
closeTimer.current = setTimeout(() => setOpen(false), 120);
};
return (
<span className="relative inline-block" onMouseEnter={show} onMouseLeave={hide}>
<a
href={source.url}
target="_blank"
rel="noreferrer"
aria-describedby={open ? cardId : undefined}
onFocus={show}
onBlur={hide}
className={cn(
"transition-ui inline-grid h-[1.15rem] min-w-[1.15rem] place-items-center rounded-sm bg-chip px-1 align-[0.1em] text-2xs font-medium tabular-nums text-chip-fg hover:bg-fill-contrast hover:text-fg-inverse focus-visible:bg-fill-contrast focus-visible:text-fg-inverse focus-visible:outline-none",
className,
)}
>
{index}
</a>
<AnimatePresence>
{open && (
<motion.span
id={cardId}
role="tooltip"
className="absolute bottom-full left-1/2 z-20 block w-64 pb-2"
style={{ transformOrigin: "50% 100%" }}
initial={{ opacity: 0, x: "-50%", y: m.distance.sm, scale: m.personality.scaleFrom, filter: m.blur() }}
animate={{ opacity: 1, x: "-50%", y: 0, scale: 1, filter: m.blur(0) }}
exit={{ opacity: 0, x: "-50%", y: m.distance.sm, scale: 0.98, filter: m.blur(), transition: m.tween("fast") }}
transition={m.spring("micro")}
>
<span className="surface-popover block rounded-popover p-3 text-left">
<span className="flex items-center gap-1.5 text-xs text-fg-muted">
<span className="grid size-icon place-items-center rounded-xs bg-chip text-4xs font-semibold uppercase text-fg">
{domain[0]}
</span>
{domain}
</span>
<span className="mt-1.5 block text-sm font-medium leading-snug text-fg">{source.title}</span>
{source.description && (
<span className="mt-1 line-clamp-2 block text-xs leading-relaxed text-fg-tertiary">
{source.description}
</span>
)}
</span>
</motion.span>
)}
</AnimatePresence>
</span>
);
}Motion notes
- The card grows from the chip's position (transform-origin bottom center), so it's clear where it came from.
- It opens on a spring and closes on a quick tween: generous coming in, fast getting out of the way.
- A 120ms close delay lets the pointer move onto the card without it closing.
Installation
- 1
Install dependencies
Terminalnpm install motion clsx tailwind-merge - 2
Add the design tokens and motion system (once per project)
Copy
styles/tokens/*,lib/motion.tsxandlib/utils.tsfrom Get started. - 3
Paste the source into components/thinking-ui/citation.tsx
Copy it from the Code tab, or use Copy prompt to let your coding agent do all of this.
Usage
Example
<Citation index={1} source={{ title: "Docs", url: "https://…" }} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
| index | number | — | Citation number shown in the chip. |
| source | { title, url, description? } | — | The source to preview. |