Hover card
A preview card on hover, on Base UI PreviewCard.
A preview of where a link goes. It opens for a pointer and for keyboard focus only: on a touch screen a tap follows the link and no card appears, so the card must never hold anything the page needs.
Default
Every colour, radius and type role comes from the foundation theme.
import { HoverCard, HoverCardContent, HoverCardTrigger } from "@/components/ui/hover-card"
export default function HoverCardDefault() {
return (
<p>
Every colour, radius and type role comes from the{" "}
<HoverCard>
<HoverCardTrigger href="/themes/foundation" className="underline underline-offset-4">
foundation theme
</HoverCardTrigger>
<HoverCardContent className="flex flex-col gap-1">
<p className="font-medium">Foundation</p>
<p className="text-muted-foreground">
The tokens every item reads: colour, radius, shadow, type and motion. Install it first.
</p>
</HoverCardContent>
</HoverCard>
.
</p>
)
}
Placement
import { HoverCard, HoverCardContent, HoverCardTrigger } from "@/components/ui/hover-card"
const placements = [
{ side: "top", align: "start", href: "/components/popover", label: "Popover" },
{ side: "right", align: "center", href: "/components/tooltip", label: "Tooltip" },
{ side: "bottom", align: "end", href: "/components/dialog", label: "Dialog" },
] as const
export default function HoverCardPlacement() {
return (
<ul className="flex flex-wrap items-center justify-center gap-6">
{placements.map(({ side, align, href, label }) => (
<li key={side}>
<HoverCard>
<HoverCardTrigger href={href} className="underline underline-offset-4">
{label}
</HoverCardTrigger>
<HoverCardContent side={side} align={align} className="w-56">
Opens on the {side} side, aligned to the {align} of the link.
</HoverCardContent>
</HoverCard>
</li>
))}
</ul>
)
}
Installation
npx shadcn@latest add @khaeli/hover-cardbunx --bun shadcn@latest add @khaeli/hover-cardInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- @base-ui/react ^1.7.0
Keyboard
Tab- Focusing the link opens the card after the same short wait as hovering. Moving focus on closes it. Nothing inside the card can be reached with Tab, so keep links and buttons out of it.
Escape- Closes the card. Focus stays on the link.
Enter- Follows the link.
Accessible name
HoverCardTriggeris a link: it needs anhref, and its text is its name.- The card has no role and is not tied to the link, so a screen reader is not told that it opened. Treat it as a visual preview and make sure the link's destination says the same.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/hover-card.json