hovercard
onChange called 0 time(s)
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>;}
.line { display: grid; gap: 0.2em; justify-items: center; padding-block-end: 8.5em; font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step--1); line-height: 1.5; color: var(--ink);}.link { border-radius: 4px; color: var(--ink); font-family: 'Shantell Sans', system-ui, sans-serif; text-decoration: underline; text-decoration-color: var(--pink); text-decoration-thickness: 2px; text-underline-offset: 0.2em;}.link:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.link:focus-visible { outline: 2px solid var(--ink); outline-offset: 3px;}.card { z-index: 5; display: grid; gap: 0.35em; box-sizing: border-box; width: min(17rem, calc(100vw - 4rem)); margin-block-start: 0.6em; padding: 0.9em 1em; border: 1px solid var(--ink); border-radius: 6px; background: var(--raised); box-shadow: 3px 3px 0 color-mix(in srgb, var(--purple) 45%, var(--cream)); text-align: left;}.card[hidden] { display: none;}.card-name { font-family: 'Joy Elia', system-ui, sans-serif; font-size: var(--step-0);}.card-note { font-family: 'Shantell Sans', system-ui, sans-serif;}
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
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.