suiss/ui · Component
Hover Card
A card that previews content behind a link when people hover over or focus it.
Hover Card is an open-source component from suiss/ui for React. The @esuiss/ui CLI copies its source into your project, so you own the code.
HoverCard wraps the hover card primitive of the chosen base. The HoverCard root manages the open state, HoverCardTrigger renders the link that opens it, and HoverCardContent renders the floating card in a portal, positioned next to the trigger.
Use a hover card for supplementary, non-essential previews, such as a profile summary behind a username. Because it only opens on hover or focus, do not put content there that people need to complete a task. For interactive content opened with a click, use a popover.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
- Install the following dependencies and add the
cnhelper:
- Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Composition
Use the following composition to build a HoverCard:
Examples
Sides
Use the side prop on HoverCardContent to open the card on the top, right, bottom or left of its trigger.
Accessibility
- The trigger renders as a link and is reachable with Tab.
- The card opens when the pointer rests on the trigger or when the trigger receives keyboard focus, and closes when the pointer leaves.
- The card is a visual preview; make sure the same information is available elsewhere, for example on the linked page.
API Reference
HoverCard
The root that manages the card's open state. It renders no element of its own.
Also accepts the props of the primitive part it wraps.
Set defaultOpen to start open, or control it with open and onOpenChange.
Set defaultOpen to start open, or control it with isOpen and onOpenChange.
HoverCardTrigger
The link that opens the card on hover or focus.
Also accepts the props of the primitive part it wraps.
The trigger renders an anchor, so give it an href.
HoverCardContent
The floating card, rendered in a portal and positioned relative to the trigger.
| Prop | Type | Default |
|---|---|---|
side | "top" | "right" | "bottom" | "left" | "inline-start" | "inline-end" | "bottom" |
sideOffset | number | 4 |
align | "start" | "center" | "end" | "center" |
alignOffset | number | 4 |
| Prop | Type | Default |
|---|---|---|
side | "top" | "right" | "bottom" | "left" | "bottom" |
sideOffset | number | 4 |
align | "start" | "center" | "end" | "center" |
alignOffset | number | 4 |
Also accepts the props of the primitive parts it wraps.
side picks the side of the trigger the card opens on, and align how it lines up along that side. sideOffset is the gap in pixels between the trigger and the card, and alignOffset the offset in pixels along the alignment axis. The card is w-72 by default; override it with className. It slides in from the side it opens on, and fades and zooms as it opens and closes.
Each part sets a data-slot, such as hover-card-trigger or hover-card-content, for styling.