Hover card
A preview of what a link leads to: a person, a page, a record. It opens when the pointer rests on the link or the keyboard focuses it. The link must make sense without it, because touch screens never show the card. For a hint on a control use a tooltip; for content a reader acts on, a popover.
Install
npx shadcn@latest add https://thrumui.dev/r/hover-card.jsonAdds components/ui/hover-card.tsx and installs @base-ui/react@1.9.0. Nothing else in your project changes.
import { HoverCard } from "@/components/ui/hover-card";Props
| Prop | Type | Default | Description |
|---|---|---|---|
HoverCard: open | boolean | – | Whether the card shows, when the parent holds the state. Use it with onOpenChange. |
HoverCard: defaultOpen | boolean | Default false | Whether the card shows from the start. |
HoverCard: onOpenChange | (open: boolean) => void | – | Called when the card shows or hides. |
HoverCardTrigger: href | string | Required | Where the link goes. The trigger is a real link and works with no card. |
HoverCardTrigger: delay | number | Default 600 | How long the pointer rests on the link, or the focus stays on it, before the card shows, in ms. |
HoverCardTrigger: closeDelay | number | Default 300 | How long the card stays after the pointer leaves, in ms. It gives the pointer time to reach the card. |
HoverCardTrigger: className | string | – | Extra classes for the link. The default is an underlined link in the text colour. |
HoverCardContent: side | "top" | "right" | "bottom" | "left" | Default "bottom" | The side of the link the card opens on. It flips to the opposite side when there is no room. |
HoverCardContent: align | "start" | "center" | "end" | Default "center" | How the card lines up with the link along that side. |
HoverCardContent: sideOffset | number | Default 6 | The gap between the link and the card, in pixels. |
HoverCardContent: alignOffset | number | Default 0 | Moves the card along the link's side, in pixels. |
HoverCardContent: collisionPadding | number | Default 8 | The space kept between the card and the edge of the screen, in pixels. |
HoverCardContent: className | string | – | Extra classes for the card. w-80 makes it wider; the default is 256 pixels. |
Motion and sound
Motion
- The card fades in and grows from 96% to full size over 150 ms, from the edge that faces the link.
- Hiding plays the same change backwards.
- Reduced motion: the card appears and leaves at once.
Sound
Silent. This component makes no sound.
Accessibility
- The trigger is a plain link: a screen reader reads its text and follows its href. The card adds no role and no announcement.
- The card is a visual extra for pointer and keyboard users. Everything in it must also be on the page the link opens.
- Do not put the only copy of an action in the card. Touch screens have no hover and go straight to the link.
- The pointer can travel from the link onto the card without losing it, so text in the card can be selected.
- The link shows the focus ring, and is underlined so it does not rest on colour.
- Under a glass material the card is translucent. Its tint keeps the text at 4.5 to 1 over any page.
| Keys | Action |
|---|---|
| Tab | Focus the link. The card shows after 600 ms. |
| Enter | Follow the link. |
| Esc | Hide the card. Focus stays on the link. |
Source
"use client";
import { PreviewCard as PreviewCardPrimitive } from "@base-ui/react/preview-card";
import { cn } from "@/lib/utils";
export type HoverCardSide = "top" | "right" | "bottom" | "left";
export type HoverCardAlign = "start" | "center" | "end";
export type HoverCardProps = PreviewCardPrimitive.Root.Props;
/**
* A preview of what a link leads to, shown when the pointer rests on the link or the keyboard focuses it.
* The card is an extra: everything in it must also be on the page the link opens, because touch screens never show it.
*/
export function HoverCard(props: HoverCardProps) {
return <PreviewCardPrimitive.Root {...props} />;
}
/** The link. It is a real `<a>` and needs an `href`; the card opens after the pointer rests on it for 600 ms. */
export function HoverCardTrigger({ className, ...props }: PreviewCardPrimitive.Trigger.Props) {
return (
<PreviewCardPrimitive.Trigger
data-slot="hover-card-trigger"
className={cn(
"rounded-sm font-medium text-foreground underline underline-offset-4 hover:decoration-2 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring",
className,
)}
{...props}
/>
);
}
export type HoverCardContentProps = PreviewCardPrimitive.Popup.Props &
Pick<PreviewCardPrimitive.Positioner.Props, "sideOffset" | "alignOffset" | "collisionPadding"> & {
/** The side of the link the card opens on. It flips to the opposite side when there is no room. */
side?: HoverCardSide;
/** How the card lines up with the link along that side. */
align?: HoverCardAlign;
};
/**
* The card. It is placed against the link, kept inside the screen, and is 256 pixels wide unless `className` says otherwise.
* It is a floating surface, so it turns to glass when the page's material asks for it.
*/
export function HoverCardContent({ side = "bottom", align = "center", sideOffset = 6, alignOffset, collisionPadding = 8, className, ...props }: HoverCardContentProps) {
return (
<PreviewCardPrimitive.Portal>
<PreviewCardPrimitive.Positioner side={side} align={align} sideOffset={sideOffset} alignOffset={alignOffset} collisionPadding={collisionPadding} className="z-50">
<PreviewCardPrimitive.Popup
data-slot="hover-card"
data-surface="floating"
className={cn(
"w-64 max-w-(--available-width) origin-(--transform-origin) rounded-lg border bg-popover p-4 text-sm text-popover-foreground shadow-md outline-none",
"transition-[opacity,scale] duration-150 ease-out data-ending-style:scale-[0.96] data-ending-style:opacity-0 data-starting-style:scale-[0.96] data-starting-style:opacity-0 motion-reduce:transition-none",
className,
)}
{...props}
/>
</PreviewCardPrimitive.Positioner>
</PreviewCardPrimitive.Portal>
);
}Files and dependencies
- Adds
components/ui/hover-card.tsx- Packages
@base-ui/react@1.9.0- Brings along
- Nothing
Changelog
- 1.0.0 · 2026-12-03First release.