Hover card
A rich preview that opens on hover or keyboard focus.
Demo
Usage
import { Avatar, HoverCard, Text } from '@tuttn/slant'
// The root renders no element: it holds the state and ties the parts together.
// The trigger is your link (asChild); the card holds anything, links included.
<HoverCard>
<HoverCard.Trigger asChild><a href="/people/ada">@ada</a></HoverCard.Trigger>
<HoverCard.Content label="Ada Lovelace">
<Avatar name="Ada Lovelace" />
<Text>Design systems · <a href="/people/ada/reviews">12 reviews</a></Text>
</HoverCard.Content>
</HoverCard>
// A commit on its hash: open sooner, above the line.
<HoverCard openDelay={300} placement="top-start">
<HoverCard.Trigger asChild><a href={commitUrl}>4f2c9e1</a></HoverCard.Trigger>
<HoverCard.Content label="Commit 4f2c9e1"><CommitSummary /></HoverCard.Content>
</HoverCard>
// It keeps its own open state, starting from defaultOpen; pass open and
// onOpenChange to control it, and disabled while something else owns the
// pointer (a drag, an open menu).
<HoverCard open={open} onOpenChange={setOpen} disabled={dragging}>
<HoverCard.Trigger>Ada Lovelace</HoverCard.Trigger>
<HoverCard.Content>…</HoverCard.Content>
</HoverCard>Props
| Prop | Type | Default | Description |
|---|---|---|---|
open | boolean | — | Whether the card is open. Pass it with onOpenChange to control the card; leave it out and it keeps its own state. |
defaultOpen | boolean | false | Whether the card starts open, when uncontrolled. |
onOpenChange | (open: boolean) => void | — | Called with the new state whenever the card asks to open or close. |
openDelay | number | 500 | How long the pointer or keyboard focus rests on the trigger before the card opens, in milliseconds. Skipped while another hover card is open or just after one closed. |
closeDelay | number | 300 | How long the card stays after the pointer leaves the trigger and the card, in milliseconds: time to cross the gap between them. |
placement | Placement | 'bottom-start' | The side of the trigger the card sits on, and the edge it lines up with. Start and end follow the text direction. The card flips to the other side when this one lacks room, and slides along it to stay in view. |
offset | number | 6 | The gap between the trigger and the card, in pixels. |
disabled | boolean | false | Never opens the card, and closes an open one. The trigger keeps working as itself. |
children | ReactNode | — | HoverCard.Trigger and HoverCard.Content, at any depth. The hover card renders no element of its own. |
HoverCard.Trigger | { asChild?: boolean } & HTMLAttributes<HTMLElement> | — | What the pointer rests on and keyboard focus lands on: a <span> by default, or with asChild its single child (a link, a button), which keeps its own look and tab stop. A bare span gets tabIndex={0} and a dotted underline, so it can be reached by keyboard; prefer a link or a button. The ref, className, style and other attributes reach the element. Your handlers run first, and a preventDefault in one skips the card’s own. |
HoverCard.Content | { label?: string } & HTMLAttributes<HTMLDivElement> | — | The card (role="dialog", not modal), rendered in a portal only while open. Named by label, else by the trigger’s text. The ref, className, style and other attributes go on the card element. |
…rest | HTMLAttributes | — | HoverCard itself renders nothing, so it takes no attributes; each part forwards its own to its element. |
Keyboard
| Tab | onto the trigger, opens the card after openDelay; focus stays on the trigger |
| Tab | on the trigger of an open card, moves to the card’s first link or control |
| Tab / Shift+Tab | moves through the card; past its last stop, closes it and goes on to what follows the trigger; before its first, back to the trigger |
| Escape | closes the card; focus inside it goes back to the trigger, focus anywhere else stays put |
| Enter | on a link or button trigger, follows it and closes the card |
Accessibility
- The card is a non-modal dialog (
role="dialog") named bylabel, else by the trigger’s text througharia-labelledby. Opening it moves no focus and traps nothing. - The trigger gets no ARIA of its own: a hover card is supplementary, so a link stays a link. A screen reader hears nothing when the card opens, and a touch never opens it, so the card must only repeat what the trigger leads to, such as the page behind the link.
- Keyboard focus (
:focus-visible) opens it as resting the pointer does; a click or a tap does not. While it is open, Tab from the trigger moves into it, so its links are in reach. - It meets WCAG 1.4.13: Escape dismisses it without moving the pointer, the pointer can cross onto the card and stay there, and it stays open until the pointer and focus have left both the trigger and the card.
- Use a link or a button as the trigger, with asChild. A bare span takes
tabIndex={0}so the keyboard can reach it, but it announces no role.
Opening and closing
The card opens when a mouse or pen rests on the trigger for openDelay (500ms), or keyboard focus lands on it for as long. It stays while the pointer is on the trigger or the card, or focus is in either, and closes closeDelay (300ms) after both are left, time enough to cross the gap onto the card. While one hover card is open, or for a moment after it closed, the next opens without the wait and the first closes at once, so a pointer moving down a list of names meets one card at a time. A press on the trigger, following its link, closes the card and drops an open that was still waiting; a press outside closes it too. disabled keeps it from opening and closes an open one, and the trigger keeps working.
Never the only path
Touch screens have no hover, and a screen reader isn’t told the card opened, so anything in a card must also be reachable without it: the person’s page behind the name, the commit behind the hash. Use it to save a trip, never to hold the only copy. For content the reader asks for, use a Popover, which opens on a press; for a few words about a control, a Tooltip.
Placement
The card renders in a portal at the end of the body, carrying its root’s theme, direction and density, so no overflow or stacking context clips it. It hangs from the side placement names (bottom-start), offset pixels away, flips to the other side when that one lacks room, slides along it to stay in view, and follows the trigger when anything scrolls. Inside a Dialog it renders in the dialog, and a tooltip or menu opened inside the card renders beside it, so the pointer and focus on one count as on the card. It shares Popover’s surface and rises in quickly, or simply appears under reduced motion.