hovercard

onChange called 0 time(s)

Before this page, read
import { hovercard } from '@markless/ui';export default function HovercardBasic() @{	<div class="line">		<span>Before this page, read</span>		<hovercard.root class="mention">			<hovercard.trigger class="link" href="/ui/accordion">the accordion page</hovercard.trigger>			<hovercard.content class="card">				<span class="card-name">accordion</span>				<span class="card-note">Stacked sections. Each heading opens its own panel.</span>				<a class="link" href="/ui/accordion">Read it</a>			</hovercard.content>		</hovercard.root>	</div>;}

Rest the pointer on the link, or Tab to it, and the card opens below it. Change delay and closeDelay to set the waits.

A hover card is a preview of where a link goes. It shows when the pointer rests on the link, or when focus stays on it. Use it for a profile behind a name, or a summary behind a page link.

Unlike a tooltip, the card can hold links and buttons, and Tab can move into it. The trigger is always an <a>, because the card is only a shortcut. A touch screen never shows the card, so everything in it must also be behind the link.

Anatomy

hovercard anatomy
Anatomy perspectiveFlat
Layers
@designstudioA small team making good things.
hovercard.root

The hover card itself; the trigger and the card go inside it, in that order.

The root wraps both parts, and it listens for the pointer and focus. So moving from the link into the card does not count as leaving, whether by pointer or by Tab.

Timing

The card waits before it shows. delay is how long the pointer must rest on the link, or focus must stay on it. The default is 700 milliseconds. Focus waits the same delay, so tabbing past a row of links does not flash a card at each one.

The card also waits before it hides. closeDelay is how long it stays after the pointer leaves. The default is 300 milliseconds. That gives the pointer time to cross the gap between link and card.

<hovercard.root delay={400} closeDelay={500}>

A card does not hide while focus is inside it, even when the pointer leaves.

Keyboard

Key What it does
Tab onto the link After delay, show the card.
Tab again Move focus into the card. It stays open.
Tab past the card Leave the card and hide it.
Escape Hide the card. If focus was inside the card, it goes back to the link.

A press anywhere else on the page also hides the card, but it leaves focus where you pressed.

Placement

The card sits below the link by default. That rule ships in the markless CSS layer, so one plain rule of yours beats it:

.mention-card {
	position-area: block-start;
}

Write hovercard.content after hovercard.trigger. The card uses CSS anchor positioning, and an anchor must come before the element placed against it.

API

Every prop is optional.

hovercard.root

Prop Type What it does
open boolean Whether the card starts open. The default is false.
delay number Milliseconds the pointer or focus must rest on the link before the card shows. The default is 700.
closeDelay number Milliseconds the card stays after the pointer leaves. The default is 300.
onChange (open: boolean) => void Called with the new value when the card shows or hides, by any route.
Attribute When
ui-open The card is showing.
ui-closed The card is hidden.

Every other prop goes on the root <div>. Your own onPointerover, onPointerout, onFocusin and onFocusout run after the family's.

Selector Default in the markless layer
div:not([overlay]) anchor-scope: --ui-hovercard

The scope keeps two cards on one page from finding each other's link.

hovercard.trigger

An <a>, so give it an href. It has aria-expanded and an aria-controls that points at the card. It writes ui-open and ui-closed.

Selector Default in the markless layer
a anchor-name: --ui-hovercard

hovercard.content

A <div> with no role. It gets hidden while the card is closed, which also takes its links out of the Tab order. Your own onDismiss runs after the family has hidden the card.

Attribute When
ui-open The card is showing.
ui-closed The card is hidden.
Selector Default in the markless layer
[overlay] position: absolute; position-anchor: --ui-hovercard; position-area: block-end

It also sets --ui-anchor: --ui-hovercard, so your own anchor() rules can name the link. Opening a hover card hides any other hover card that is showing. A showing card does not lock the page's scroll.