A preview card for a link: the pointer rests on it, or keyboard focus does, and after a delay a card appears next to it. Unlike a Tooltip the card may hold links and buttons and is not the link's name or description; unlike a Popover nothing is clicked to open it, and opening never moves focus. Showing interest starts a transition: openChangeAction(true) is awaited, the content mounts inside it and may suspend, and the link is data-pending meanwhile. If the user moves on before the data arrives, the open is cancelled and the card never appears. Fifty mentions on a page render fifty empty popovers and fetch nothing until one is hovered.
"use client";
import { use } from "react";
import { HoverCard } from "@funstack/ui/hover-card";
import { sleep } from "./fake-api.js";
interface User {
readonly name: string;
readonly bio: string;
readonly repos: number;
}
const users: Record<string, User> = {
ada: { name: "Ada Lovelace", bio: "Notes on the Analytical Engine.", repos: 12 },
babbage: { name: "Charles Babbage", bio: "Difference engines, mostly.", repos: 3 },
};
// A pretend user API with a cache: one promise per login, read with `use()`. The first hover of
// a mention fetches (900 ms); later ones open at once.
const cache = new Map<string, Promise<User>>();
function fetchUser(login: string): Promise<User> {
let promise = cache.get(login);
if (promise === undefined) {
promise = sleep(900).then(() => users[login] ?? { name: login, bio: "", repos: 0 });
cache.set(login, promise);
}
return promise;
}
// Suspends until the user has loaded. The Popup has no Suspense boundary of its own, so the card
// appears complete; meanwhile the mention is `data-pending` (a dotted underline here).
function UserPreview({ login }: { login: string }) {
const user = use(fetchUser(login));
return (
<>
<strong className="demo-card-name">{user.name}</strong>
<span className="demo-card-bio">{user.bio}</span>
<span className="demo-card-row">
<a href={`https://example.com/u/${login}?tab=repositories`}>{user.repos} repositories</a>
<button type="button" className="demo-button">
Follow
</button>
</span>
</>
);
}
// A mention inside running text: the Popup is a <span> (a <div> cannot follow the link inside a
// <p>) and holds phrasing content only, laid out with CSS. Loading starts on pointerenter and
// focus, while the 600 ms delay runs.
function Mention({ login }: { login: string }) {
return (
<HoverCard.Root>
<HoverCard.Trigger
href={`https://example.com/u/${login}`}
className="demo-mention"
onPointerEnter={() => void fetchUser(login)}
onFocus={() => void fetchUser(login)}
onClick={(event) => event.preventDefault()}
>
@{login}
</HoverCard.Trigger>
<HoverCard.Popup render={<span />} className="demo-card" align="start">
<UserPreview login={login} />
</HoverCard.Popup>
</HoverCard.Root>
);
}
export function HoverCardMentions() {
return (
<p>
Thanks <Mention login="ada" /> for the review, and <Mention login="babbage" /> for the
original design.
</p>
);
}/* HoverCard: the Trigger is a link (`data-open` optimistic, `data-pending` while the card
loads); the Popup is a manual popover right after it. Open state is `:popover-open`. */
.demo-mention {
color: var(--color-link, #2563eb);
font-weight: 600;
text-decoration: none;
}
.demo-mention[data-open] {
text-decoration: underline;
text-decoration-thickness: 2px;
}
/* The card is loading: a quiet hint on the link. */
.demo-mention[data-pending] {
cursor: progress;
text-decoration: underline dotted;
}
.demo-card {
/* The popover UA stylesheet sets `inset: 0; margin: auto`: reset both for anchored placement. */
inset: auto;
margin: 6px 0;
/* A <span> card works too: the popover's `position: fixed` makes it a block box. */
inline-size: 16rem;
padding: 12px 14px;
border: 1px solid var(--color-border);
border-radius: 10px;
background: var(--color-bg);
color: var(--color-text);
box-shadow: 0 12px 40px rgb(0 0 0 / 0.25);
font-size: 0.9rem;
line-height: 1.4;
font-weight: normal;
}
.demo-card-name,
.demo-card-bio,
.demo-card-row {
display: block;
margin: 0 0 6px;
}
.demo-card-row {
display: flex;
align-items: center;
gap: 10px;
margin: 0;
}
.demo-skeleton {
color: var(--color-text-secondary);
}
.demo-repo-list {
display: flex;
gap: 24px;
margin: 0;
padding: 0;
list-style: none;
}
/* Every close the user causes is a fact, so CSS is the animation for a HoverCard: the enter
everywhere, the exit in Chromium (with `overlay`) and Safari. */
.demo-card {
opacity: 0;
transition:
opacity 120ms,
display 120ms allow-discrete,
overlay 120ms allow-discrete;
}
.demo-card:popover-open {
opacity: 1;
}
@starting-style {
.demo-card:popover-open {
opacity: 0;
}
}
@media (prefers-reduced-motion: reduce) {
.demo-card {
transition: none;
}
}Hover a mention or Tab to it: the first time, the link shows a dotted underline while the profile loads, then the card appears complete. Loading starts on pointerenter and focus, while the 600 ms delay runs, through the Trigger's own event props.
import { HoverCard } from "@funstack/ui/hover-card";
<HoverCard.Root> {/* no DOM: ids, state, timers */}
<HoverCard.Trigger href="/u/ada">@ada</HoverCard.Trigger> {/* a */}
<HoverCard.Popup> {/* div[popover=manual][role=group] */}
<UserPreview login="ada" /> {/* mounted on open; may suspend */}
</HoverCard.Popup>
</HoverCard.Root><span> card: render={<span />} with phrasing content only (img, a, button, span), laid out with CSS: a <div> cannot follow a link inside a <p>. The other valid shape puts the Popup after the paragraph; Safari then does not put the card next in the Tab order when other focusable content sits between link and card.render), or a button whose click opens nothing of this kind. The card opens on interest only; the click is yours, and a press on the Trigger closes the card. A button that should open the same content on click is a Popover.| What happens | Channel | `reason` | `details.event` |
|---|---|---|---|
Hover (not touch) or keyboard focus (:focus-visible) held for delay | openChangeAction(true) | "activate" | the pointerenter or focus event (flagged path: the beforetoggle of the platform's interest show) |
| Lost interest, Escape or a press while the open is still pending | openChangeAction(false) | "dismiss" | the pointerleave, blur, focusout, loseinterest, keydown or pointerdown |
Lost interest while shown: the pointer left the link and the card, or focus left both, for closeDelay | onOpenChange(false) | "dismiss" | the event that began the close delay (pointerup after a text selection dragged out); loseinterest on the flagged path |
| Escape while shown | onOpenChange(false) | "dismiss" | the keydown |
| A press outside the card while shown, the link included | onOpenChange(false) | "dismiss" | the pointerdown |
disabled became true while shown | onOpenChange(false) | "dismiss" | undefined |
Script hidePopover(); with experimental_hint, the browser's light dismiss or another hint | onOpenChange(false) | "dismiss" | the toggle event |
open={true} re-shows the card, without moving focus.manual), and it follows its link in the DOM, so inside a modal Dialog, a Popover or a Menu it is not inert and a press in it is no light dismiss of the outer popup. A Menu, Popover or Tooltip opened from inside the card keeps it open, and so does a nested card.HoverCard.Root| Prop | Type | Default | Description |
|---|---|---|---|
open | boolean | – | Controlled open state. Pass onOpenChange too: every close the user causes is a fact (development warns). open with onOpenChange={() => {}} pins a card open, for styling it. |
defaultOpen | boolean | false | Initial open state when uncontrolled. Shown after hydration. |
openChangeAction | ChangeAction<boolean, HoverCardOpenChangeDetails> | – | Requests only. Awaited for true when the user shows interest: the card appears when it resolves and the content has rendered. Called with false only to cancel an open that has not appeared yet. Never called for the close of a shown card. Runs in the Root's transition; may be a Server Function. A rejection reaches the nearest Error Boundary above the Root, which takes the link with it: catch inside the action, or load in the content with an Error Boundary inside the Popup. |
onOpenChange | UrgentChangeHandler<boolean, HoverCardOpenChangeDetails> | – | Facts only: the card was closed (lost interest, Escape, a press outside). Always false, with reason: "dismiss". Called synchronously, outside any transition, not awaited. In controlled mode set state directly here (onOpenChange={setOpen}), never in startTransition. May return a promise; a rejection or a throw goes to reportError. On a controlled Root with no openChangeAction, hover and focus do not open the card (a development warning says so): open it from your own state. |
delay | number | 600 | Milliseconds of interest (hover or keyboard focus) before the open is requested. Longer than Tooltip's 250 on purpose: a card loads data and, once open, puts its controls in the keyboard user's path. |
closeDelay | number | 300 | Milliseconds from losing interest to the close: the time the pointer has to cross from the link into the card. |
disabled | boolean | false | Interest never opens the card, and a shown card is closed. Does not disable the link, which still navigates; a controlled open={true} still shows the card. |
children | ReactNode | ((state: HoverCardRootState) => ReactNode) | – | The parts. The function form receives the optimistic state and must not suspend. |
HoverCardRootState is { open, pending, disabled }; open is optimistic. HoverCardOpenChangeDetails is { reason } plus the virtual event; reason is "activate" for an open, "dismiss" for every close, "platform" for a card found open at hydration.ref, className or style).HoverCard.Trigger| Prop | Type | Default | Description |
|---|---|---|---|
experimental_interestFor | boolean | false | Chromium 142+. Writes interestfor (after hydration, through the ref) and renders interest-delay-start / -end from the Root's delays, so the browser decides when interest starts and ends, long-press on touch included. The library still owns the open, Escape and a press outside. Firefox and Safari ignore the attribute and run the JS path. See Experimental features. |
className | string | ((state: HoverCardTriggerState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: HoverCardTriggerState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
children | ReactNode | ((state: HoverCardTriggerState) => ReactNode) | – | Rendered from optimistic state; must not suspend. |
render | ReactElement | ((props, state: HoverCardTriggerState) => ReactNode) | – | Replace the default <a> (element form) or take over rendering (function form). |
HoverCardTriggerState is { open, pending, disabled }; open is optimistic, pending is true while an open (or its cancel) is in flight: the card is loading.data-open, data-pending, data-disabled, and interestfor with experimental_interestFor (removed while disabled). No ARIA attribute: not aria-describedby, aria-expanded, aria-haspopup or aria-details.href and every other anchor prop pass through, and so do your onPointerEnter, onFocus and onClick: the library listens with addEventListener. The element must be focusable; a development warning fires when it is not (an <a> without href).
HoverCard.Popup| Prop | Type | Default | Description |
|---|---|---|---|
side | "top" | "bottom" | "left" | "right" | "inline-start" | "inline-end" | null | "bottom" | Placement relative to the Trigger through inline position-area, with position-try-fallbacks flipping it when it does not fit. null emits no inline placement and no data-side / data-align: your stylesheet owns placement. |
align | "start" | "center" | "end" | "center" | Alignment along the side. Ignored when side is null. |
keepMounted | boolean | false | false: the children are not rendered while closed; mounting them is part of the opening transition, so content that suspends holds the open. true: rendered in <Activity mode="hidden"> while closed (pre-rendered at low priority). Suits a single prominent card, not a list of them. |
experimental_hint | boolean | false | Renders popover="hint" instead of "manual" (Chrome 151+, Firefox 153+; Safari 27.2 treats it as manual): opening the card closes an open Tooltip, and the browser's light dismiss applies next to the library's. |
transition | ViewTransitionClass | – | The class of the <ViewTransition> around the popup element; without it no boundary is rendered. A string animates this card's own open (and the cancel of an open that has not appeared) and its own mount and unmount (only as the top of what is inserted or removed), in all engines; nothing else (not another card, not an unrelated transition, not content that changes inside). A controlled open you change in your own transition needs a map keyed on fsui-hover-card-open / fsui-hover-card-close plus addTransitionType (recipe). A type map is React's: it names every card that has it and that a commit with its types reaches, and each plays the class; end it with default: "none" (Type maps). Closes the user causes are facts and animate with CSS only. Keep it static: changing it remounts the popup. Any value, "none" included, makes a mount of the Popup while it is open, or a second reveal of a mounted Popup by <Suspense> or <Activity>, inside a transition run the page cross-fade; a plain open does not, and neither does the mount of a closed Popup: it renders its element one commit after its mount, before paint, so its ref is still empty in the mount effects of the component that renders it (two-step mount). While a view transition runs, the popup's CSS transitions are cut for the library's own show or hide (a @keyframes animation is not, and a dismissal keeps its CSS exit). |
className | string | ((state: HoverCardPopupState) => string | undefined) | – | A class name, or a function of the part's state. |
style | CSSProperties | ((state: HoverCardPopupState) => CSSProperties | undefined) | – | Inline style, or a function of the part's state. |
children | ReactNode | ((state: HoverCardPopupState) => ReactNode) | – | Content, or a function of the part's state. Rendered from committed state; may suspend. |
render | ReactElement | ((props, state: HoverCardPopupState) => ReactNode) | – | Replace the default <div> (element form) or take over rendering (function form). A <span> for a card inside running text (phrasing content only). |
HoverCardPopupState is { open, side, align }; open is committed (:popover-open is the DOM truth); side / align are where the browser placed the card while it is shown, your props otherwise.id, popover ("manual", or "hint" with the flag), data-side / data-align (unless side is null).role="group" is a default you can override: a card with real actions may pass role="dialog" with an aria-label. onToggle and onBeforeToggle are not accepted; use a ref. Every show is a no-focus show: the card is inert for the showPopover() call, so an autofocus inside it cannot take focus on hover.
| Attribute | On | Meaning |
|---|---|---|
:popover-open | Popup | The card is shown (DOM truth). |
data-side, data-align | Popup | While shown: where the browser placed the card (after any flip). Otherwise the requested placement. Absent with side={null}. |
data-open | Trigger | Optimistic open state: from the dispatch of the open. |
data-pending | Trigger | An open is in flight: the card is loading. |
data-disabled | Trigger | The Root's disabled. |
| Key | Where | Behaviour |
|---|---|---|
| Tab | before the Trigger | Focus reaches the link and the card opens after delay (keyboard focus is :focus-visible); focus stays on the link. |
| Tab | Trigger, card open | Focus moves into the card, to its first focusable element; with none, to what follows. |
| Tab | the card's last element | Focus moves on; the card closes after closeDelay. |
| Shift+Tab | the card's first element | Back to the link; the card stays open. |
| Tab | Trigger, before delay has passed | The open is cancelled; focus moves on. |
| Escape | anywhere | Closes the card (or cancels the coming one). With focus inside the card, focus returns to the link and the card does not re-open. Inside a Dialog or a toast only the card closes; a popover or tooltip open inside the card closes first. With a tooltip showing elsewhere too, the one that opened first closes. A preventDefault() on the Escape keeps the card open; stopPropagation() does not, and inside a modal Dialog it makes the dialog close on the same Escape (the browser closes it before the library can claim the key). |
| Enter | Trigger | The link's own activation; the library does nothing. |
delay has the card's controls in the way of the next link. That is why the delay is long, why Escape is cheap and why a card with many controls is the wrong component: content the user works in belongs in a Popover that a click opens.<body>, or where the press landed.role="group", HTML-AAM's minimum role for a popover, which WebKit does not apply by itself. Nothing is announced when the card appears.closeDelay covers the gap), persistent (until interest is lost).Concept: Transitions & ViewTransition
fsui-hover-card plus fsui-hover-card-open for an open, and fsui-hover-card-close for the cancel of an open that has not appeared. Every close of a shown card is a fact and starts no transition. These types are public API: key a transition map on them, and add them with addTransitionType for a controlled change (Transitions). A string transition is keyed on an internal per-instance type instead, which this component's own dispatch adds.
CSS is the animation to use for a HoverCard, because every close the user causes is a fact and only CSS animates those (the exit animates in Chromium and Safari, not in Firefox). transition earns its place for a skeleton swapped for the content and for a parent that drives open itself.
.card {
margin: 6px; inset: auto;
opacity: 0;
transition: opacity 120ms, display 120ms allow-discrete, overlay 120ms allow-discrete;
}
.card:popover-open { opacity: 1; }
@starting-style { .card:popover-open { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .card { transition: none; } }Concept: Pending & optimistic state
During delay nothing is rendered and nothing is pending. From the dispatch of the open the Trigger has data-open and data-pending; the card is hidden and its children unmounted until the action resolves and the content has rendered. Leaving meanwhile cancels the open: openChangeAction(false) supersedes it and nothing appears.
Last: (none yet)
"use client";
import { Suspense, startTransition, use, useState } from "react";
import { HoverCard } from "@funstack/ui/hover-card";
import type { HoverCardOpenChangeDetails } from "@funstack/ui/hover-card";
import { DemoErrorBoundary } from "./DemoErrorBoundary.js";
import { sleep } from "./fake-api.js";
const repos = ["funstack-ui", "funstack-router", "funstack-static"];
// The repository's summary, loaded in the content: the Popup has a Suspense boundary, so the
// card opens after the delay with a skeleton and the summary swaps in when it arrives.
const cache = new Map<string, Promise<string>>();
function loadSummary(repo: string): Promise<string> {
let promise = cache.get(repo);
if (promise === undefined) {
promise = sleep(1200).then(() => `${repo}: ${repo.length * 7} stars, updated today.`);
cache.set(repo, promise);
}
return promise;
}
function Summary({ repo }: { repo: string }) {
return <p className="demo-card-bio">{use(loadSummary(repo))}</p>;
}
// Requests and facts side by side. `openChangeAction` hears the open (a 400 ms pretend analytics
// call: the link is pending meanwhile) and the cancel of an open that never appeared;
// `onOpenChange` hears every close of a shown card (lost interest, Escape, a press outside),
// synchronously. The readout is plain text, not a live region.
export function HoverCardRepos() {
const [log, setLog] = useState("(none yet)");
const request = async (open: boolean, { reason }: HoverCardOpenChangeDetails) => {
const call = `openChangeAction(${open}, "${reason}")`;
setLog(`${call}: running…`);
await sleep(400);
// After an await: wrap the update in startTransition (React limitation).
startTransition(() => setLog(`${call}: settled`));
};
const fact = (open: boolean, { reason, event }: HoverCardOpenChangeDetails) => {
setLog(`onOpenChange(${open}, "${reason}") after ${event?.type ?? "no event"}`);
};
return (
<div className="demo-stack">
<ul className="demo-repo-list">
{repos.map((repo) => (
<li key={repo}>
<HoverCard.Root openChangeAction={request} onOpenChange={fact}>
<HoverCard.Trigger
href={`https://github.com/uhyo/${repo}`}
className="demo-mention"
onClick={(event) => event.preventDefault()}
>
{repo}
</HoverCard.Trigger>
<HoverCard.Popup className="demo-card">
<strong className="demo-card-name">uhyo/{repo}</strong>
<DemoErrorBoundary>
<Suspense fallback={<p className="demo-card-bio demo-skeleton">Loading…</p>}>
<Summary repo={repo} />
</Suspense>
</DemoErrorBoundary>
<span className="demo-card-row">
<button type="button" className="demo-button">
Star
</button>
</span>
</HoverCard.Popup>
</HoverCard.Root>
</li>
))}
</ul>
<p className="demo-readout">
Last: <code>{log}</code>
</p>
</div>
);
}/* HoverCard: the Trigger is a link (`data-open` optimistic, `data-pending` while the card
loads); the Popup is a manual popover right after it. Open state is `:popover-open`. */
.demo-mention {
color: var(--color-link, #2563eb);
font-weight: 600;
text-decoration: none;
}
.demo-mention[data-open] {
text-decoration: underline;
text-decoration-thickness: 2px;
}
/* The card is loading: a quiet hint on the link. */
.demo-mention[data-pending] {
cursor: progress;
text-decoration: underline dotted;
}
.demo-card {
/* The popover UA stylesheet sets `inset: 0; margin: auto`: reset both for anchored placement. */
inset: auto;
margin: 6px 0;
/* A <span> card works too: the popover's `position: fixed` makes it a block box. */
inline-size: 16rem;
padding: 12px 14px;
border: 1px solid var(--color-border);
border-radius: 10px;
background: var(--color-bg);
color: var(--color-text);
box-shadow: 0 12px 40px rgb(0 0 0 / 0.25);
font-size: 0.9rem;
line-height: 1.4;
font-weight: normal;
}
.demo-card-name,
.demo-card-bio,
.demo-card-row {
display: block;
margin: 0 0 6px;
}
.demo-card-row {
display: flex;
align-items: center;
gap: 10px;
margin: 0;
}
.demo-skeleton {
color: var(--color-text-secondary);
}
.demo-repo-list {
display: flex;
gap: 24px;
margin: 0;
padding: 0;
list-style: none;
}
/* Every close the user causes is a fact, so CSS is the animation for a HoverCard: the enter
everywhere, the exit in Chromium (with `overlay`) and Safari. */
.demo-card {
opacity: 0;
transition:
opacity 120ms,
display 120ms allow-discrete,
overlay 120ms allow-discrete;
}
.demo-card:popover-open {
opacity: 1;
}
@starting-style {
.demo-card:popover-open {
opacity: 0;
}
}
@media (prefers-reduced-motion: reduce) {
.demo-card {
transition: none;
}
}openChangeAction(true, "activate") runs (400 ms here) and the link is pending.openChangeAction(false, "dismiss") cancels the open, and no card flashes under a pointer that has gone.onOpenChange is called at once, with the event that caused it. The action is not called.openChangeAction may see true, false, true as the user leaves and comes back; only the last dispatch commits. An action that loads data should be idempotent or cached. There is no whilePending: no part of a HoverCard runs an action of its own.
Concept: Suspense
The children render from committed state, so they mount in the opening transition. Content that suspends holds the open (wait, then open: the default); a <Suspense> inside the Popup opens at once with a skeleton instead.
<HoverCard.Popup className="card">
<ErrorBoundary fallback={<span>Could not load the profile.</span>}>
<UserPreview login="ada" /> {/* use(fetchUser("ada")) */}
</ErrorBoundary>
</HoverCard.Popup>openChangeAction reaches the boundary above the Root, which takes the link with it; a failure inside the content only replaces the card's body.onPointerEnter, onFocus). The library does not do this by itself: a pointer crossing a list of links would fetch for each of them.Concept: Browser-owned state
The card is a manual popover the library shows and hides. Opening is library-owned (a request); every close the user causes is browser-shaped: the library hides the card at once and reports the fact from the toggle event. With experimental_interestFor in Chromium the browser decides when interest starts and ends; the rest is the same code.
| Flags | Chromium | Firefox | Safari |
|---|---|---|---|
| none | manual + JS interest; Escape and a press outside by the library | same | same |
experimental_hint | hint: opening closes an open Tooltip; native light dismiss next to the library's | as Chromium | treated as manual |
experimental_interestFor | the platform's interest signal, long-press on touch | ignored: JS path | ignored: JS path |
Concept: Server Components
The card opens only after hydration: interest is tracked by the library, and interestfor is written after hydration so that a hover before it cannot open an empty card. The server HTML has the link and the closed, empty card.
// A Server Component. `recordPreview` is a Server Function; it hears opens only.
<HoverCard.Root openChangeAction={recordPreview.bind(null, repo.id)}>
<HoverCard.Trigger href={repo.url}>{repo.name}</HoverCard.Trigger>
<HoverCard.Popup className="card">
<RepoSummary repo={repo} />
</HoverCard.Popup>
</HoverCard.Root>const [open, setOpen] = useState(false);
<HoverCard.Root open={open} openChangeAction={setOpen} onOpenChange={setOpen}>
…
</HoverCard.Root>setOpen in the action is a transition update, in the handler an urgent one. Never wrap the handler's update in startTransition.
.card { margin: 6px; }
/* Extend the card's hit area over the gap, on the side that faces the link. */
.card[data-side="bottom"]::before { content: ""; position: absolute; inset: -6px 0 auto; height: 6px; }
.card[data-side="top"]::before { content: ""; position: absolute; inset: auto 0 -6px; height: 6px; }<HoverCard.Root delay={400}>
<HoverCard.Trigger href="/u/ada" experimental_interestFor>@ada</HoverCard.Trigger>
<HoverCard.Popup experimental_hint className="card">…</HoverCard.Popup>
</HoverCard.Root>openOnFocus={false}: keyboard focus opens the card on both paths.