Hover Card
For sighted users to preview content available behind a link.
Installation
Install the following dependencies:
Set up the token layer and the StyleX compiler, if you have not already. See
Installation, and paste states.ts and state-styles.ts from the same page.
The card's opaque box is the shared popup surface. state-styles.ts reads the popup's transition state.
Copy and paste the following code into your project.
Update the import paths to match your project setup.
Usage
Composition
HoverCardTrigger renders an <a>. Pass render to anchor the card to a
different element.
A hover card opens on hover and on keyboard focus, never on press. A touch screen has no hover, so the card never opens there. Put nothing in it that a reader cannot also reach another way.
Trigger delays
Use delay and closeDelay on the trigger to set how long the pointer has to
rest before the card opens, and how long it stays open after the pointer leaves.
closeDelay keeps the card open while the pointer crosses the gap into it.
Sides
Use side on HoverCardContent to pick the edge it opens from, and align to
slide it along that edge. Base UI flips the side when there is no room.
API reference
Pass render to replace a part's element or compose it with another component.
Every part wraps the Base UI PreviewCard part of the same name and accepts its
props. See the
Base UI reference for the
full list. Each styled part also takes xstyle and className.
HoverCard
| Prop | Type | Default |
|---|---|---|
open | boolean | — |
defaultOpen | boolean | false |
onOpenChange | (open: boolean, details) => void | — |
handle | PreviewCardHandle | — |
HoverCardTrigger
| Prop | Type | Default |
|---|---|---|
delay | number | 600 |
closeDelay | number | 300 |
payload | Payload | — |
HoverCardContent
| Prop | Type | Default |
|---|---|---|
side | "top" | "bottom" | "left" | "right" | "bottom" |
sideOffset | number | 4 |
align | "start" | "center" | "end" | "center" |
alignOffset | number | 0 |
The card is 16rem wide and clamped to --available-width, so it narrows
rather than overflowing a phone-width viewport. Change the width through
xstyle.
Data attributes
| Attribute | On | Effect |
|---|---|---|
data-slot="hover-card-content" | the popup | Popup surface |
data-side | the popup | Which way it slides in from |
data-starting-style, data-ending-style | the popup | Base UI writes them while the card animates |
data-open, data-closed | the popup | Base UI writes them |