Skip to content
Thrum

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.json

Adds 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

PropTypeDefaultDescription
HoverCard: openboolean–Whether the card shows, when the parent holds the state. Use it with onOpenChange.
HoverCard: defaultOpenbooleanDefault falseWhether the card shows from the start.
HoverCard: onOpenChange(open: boolean) => void–Called when the card shows or hides.
HoverCardTrigger: hrefstringRequiredWhere the link goes. The trigger is a real link and works with no card.
HoverCardTrigger: delaynumberDefault 600How long the pointer rests on the link, or the focus stays on it, before the card shows, in ms.
HoverCardTrigger: closeDelaynumberDefault 300How long the card stays after the pointer leaves, in ms. It gives the pointer time to reach the card.
HoverCardTrigger: classNamestring–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: sideOffsetnumberDefault 6The gap between the link and the card, in pixels.
HoverCardContent: alignOffsetnumberDefault 0Moves the card along the link's side, in pixels.
HoverCardContent: collisionPaddingnumberDefault 8The space kept between the card and the edge of the screen, in pixels.
HoverCardContent: classNamestring–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.
KeysAction
TabFocus the link. The card shows after 600 ms.
EnterFollow the link.
EscHide the card. Focus stays on the link.

Source

components/ui/hover-card.tsx
"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. 1.0.0 · 2026-12-03First release.

Loading index…

Keyboard shortcuts

/Ctrl KSearch components, docs and actions
?Show this list
AAppearance studio
SSound on or off
MFull or reduced motion
TTheme: auto, light, dark
GSurface: solid or frosted
BBackdrop photograph (under glass)
CCorner radius: square to round
RReplay the demo, on a component page
DDraw new data, on a component page
←→HomeEndMove the cursor in a focused chart
EscClose a dialog, or leave a chart cursor