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
CLI
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
Manual
- Install the dependencies and add the
cnhelper:
- Copy the source into
components/ui/hover-card.tsx:
- Update the import paths to match your project.
Usage
The trigger renders an anchor, so give it an href. Position the card with the side, sideOffset, align and alignOffset props on HoverCardContent; by default it opens on the bottom, centered, with a 4 pixel offset. Set defaultOpen on the root to start open, or control it with open and onOpenChange.
Examples
Sides
Four hover cards opening on the top, right, bottom and left sides of their triggers.
API Reference
HoverCard
The root that manages the card's open state.
Also accepts the props of the primitive part it wraps.
HoverCardTrigger
The link that opens the card on hover or focus.
Also accepts the props of the primitive part it wraps.
HoverCardContent
The floating card, rendered in a portal and positioned relative to the trigger.
Also accepts the props of the primitive parts it wraps.
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.
Notes
- Styling hooks:
data-slotvalueshover-card-triggerandhover-card-content. The root renders no element of its own. HoverCardContentslides in from the side it opens on, and fades and zooms as it opens and closes.- The card is
w-72by default; override it withclassName. - Uses the
popoverandpopover-foregroundcolors.