HoverCard
A preview of what a link points to, shown while the pointer rests on it or it has keyboard focus.
A hover card shows a preview of a link's destination, such as a person's profile or a document's summary, while the reader hovers or focuses the link. It adds to the link, it never replaces it: everything in the preview must also be reachable by following the link, because touch screens and many readers never see it. It is built on Base UI's preview card.
Last changed by Sam de Vries on 14 October.
// apps/docs/examples/ui/hover-card.tsx'use client';
import { Avatar, AvatarFallback,} from '@workspace/active-ui/components/primitives/avatar';import { HoverCard, HoverCardContent, HoverCardTrigger,} from '@workspace/active-ui/components/primitives/hover-card';
/** * A preview of what a link points to, shown while the pointer rests on the * link or the link has keyboard focus. The preview only adds to the link: * everything in it is also on the page the link opens. */export default function HoverCardExample() { return ( <p className="text-sm"> Last changed by{' '} <HoverCard> <HoverCardTrigger href="#hover-card-profile" className="font-medium underline underline-offset-4" > Sam de Vries </HoverCardTrigger> <HoverCardContent className="w-72"> <div className="flex gap-3"> <Avatar> <AvatarFallback>SV</AvatarFallback> </Avatar> <div className="flex flex-col gap-1"> <p className="font-medium">Sam de Vries</p> <p className="text-muted-foreground text-sm"> Scheme administrator since March 2024. </p> <p className="text-muted-foreground text-xs"> 12 schemes · Utrecht office </p> </div> </div> </HoverCardContent> </HoverCard>{' '} on 14 October. </p> );}Usage#
import { HoverCard, HoverCardContent, HoverCardTrigger,} from '@workspace/active-ui/components/primitives/hover-card';
<HoverCard> <HoverCardTrigger href="/people/sam">Sam de Vries</HoverCardTrigger> <HoverCardContent>Scheme administrator since March 2024.</HoverCardContent></HoverCard>;Anatomy#
Hover: the open state.Card Hover: a link (Card Trigger <a>); give it anhref.delay(default600ms) andclose(defaultDelay 300ms) set how long the pointer must rest or leave.Hover: the preview, placed byCard Content side(defaultbottom) andalign. It is16remwide by default; set a width withclass.Name
What your application provides#
The link and the page it opens, with everything the preview shows.
The preview content. Keep it short and without actions: buttons in a card that disappears when the pointer leaves are hard to reach.
Accessibility and keyboard#
The trigger is an ordinary link:
Tabreaches it andEnterfollows it.Keyboard focus on the link opens the preview as hover does;
Escapecloses it.The preview is supplementary and not announced as part of the link; the link text must say where it goes.
API#
HoverCardContent#
Import from @workspace/
All props come from the Base UI part this component is built on.
Props
- Type
Align- Default
'center'
How to align the popup relative to the specified side.
- Type
number | OffsetFunction - Default
0
Additional offset along the alignment axis in pixels. Also accepts a function that returns the offset to read the dimensions of the anchor and positioner elements, along with its side and alignment.
The function takes a data object parameter with the following properties:
data.: the dimensions of the anchor element with propertiesanchor widthandheight.data.: the dimensions of the positioner element with propertiespositioner widthandheight.data.: which side of the anchor element the positioner is aligned against.side data.: how the positioner is aligned relative to the specified side.align
- Type
Side- Default
'bottom'
Which side of the anchor element to align the popup against. May automatically change to avoid collisions.
- Type
number | OffsetFunction - Default
0
Distance between the anchor and the popup in pixels. Also accepts a function that returns the distance to read the dimensions of the anchor and positioner elements, along with its side and alignment.
The function takes a data object parameter with the following properties:
data.: the dimensions of the anchor element with propertiesanchor widthandheight.data.: the dimensions of the positioner element with propertiespositioner widthandheight.data.: which side of the anchor element the positioner is aligned against.side data.: how the positioner is aligned relative to the specified side.align
- Type
ComponentRender Fn<…> ReactElement<…>
Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a React or a function that returns the element to render.
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
Other parts#
HoverCard
All props come from the Base UI part this component is built on.
Props
- Type
boolean
Whether the preview card is currently open.
- Type
boolean- Default
false
Whether the preview card is initially open.
To render a controlled preview card, use the open prop instead.
- Type
PayloadChild Render Function<…> ReactNode
The content of the preview card. This can be a regular React node or a render function that receives the payload of the active trigger.
- Type
string | null
ID of the trigger that the preview card is associated with. This is useful in conjunction with the default prop to create an initially open preview card.
- Type
PreviewCard Handle<unknown>
A handle to associate the preview card with a trigger. If specified, allows external triggers to control the card's open state. Can be created with the PreviewCard.createHandle() method.
- Type
string | null
ID of the trigger that the preview card is associated with. This is useful in conjunction with the open prop to create a controlled preview card. There's no need to specify this prop when the preview card is uncontrolled (that is, when the open prop is not set).
- Type
RefObject<…>
A ref to imperative actions.
unmount: Ends the closing phase of the preview card after an externally controlled closing animation finishes. CallpreventinUnmount On Close() onfirst, otherwise the preview card completes closing on its own. Whether it leaves the DOM is decided byOpen Change keepon the portal.Mounted close: Closes the preview card imperatively when called.
Events
Called by the component when something happens.
- Type
(open, eventDetails) => void
Event handler called when the preview card is opened or closed.
- Type
(open: boolean) => void
Event handler called after any animations complete when the preview card is opened or closed.
Takes only the props listed.
HoverCardTrigger
All props come from the Base UI part this component is built on.
Props
- Type
number- Default
300
How long to wait before closing the preview card. Specified in milliseconds.
- Type
number- Default
600
How long to wait before the preview card opens. Specified in milliseconds.
- Type
PreviewCard Handle<unknown>
A handle to associate the trigger with a preview card.
- Type
unknown
A payload to pass to the preview card when it is opened.
- Type
ComponentRender Fn<…> ReactElement<…>
Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a React or a function that returns the element to render.
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
This page adapts the Hover Card documentation of shadcn/ui (MIT licence) to this package; see Third-party notices.