oakoss/ui
Components

Hover Card

A preview of a link's content, shown on hover, keyboard focus or long press.

SourceReact Aria
Built on .

Installation

npx shadcn@latest add @oakoss/hover-card

Hover Card needs react-aria-components 1.20 or later, which added PreviewTrigger. It also installs Popover, whose styles and arrow it shares.

Usage

import {
  HoverCard,
  HoverCardTrigger,
} from '#/components/ui/overlays/hover-card';
import { Link } from 'react-aria-components';

<HoverCardTrigger>
  <Link href="/ada">@ada</Link>
  <HoverCard aria-label="Ada Lovelace">Wrote the first program.</HoverCard>
</HoverCardTrigger>;

Examples

Arrow

showArrow adds an arrow that points at the trigger.

Accessibility

React Aria's PreviewTrigger opens the card 600ms after the pointer or keyboard focus reaches the trigger and closes it 200ms after both leave, with room to move the pointer from the trigger to the card. On touch, a long press opens it, and React Aria adds a translated "Long press to open preview" hint to the trigger. The card is a dialog, so aria-label or aria-labelledby is required.

While the card is open, the trigger is described by the card's whole content, so keep it short. Tab moves from the trigger into the card, Shift + Tab out of the card moves to the element before the trigger, and Escape closes it and returns focus to the trigger. The card may hold links and buttons, unlike a Tooltip, but the page must not depend on it: use it for a preview of where a link goes. Inside an open Dialog or Popover it stays out of the content React Aria hides behind them, so it stays reachable.

API

HoverCard

Renders PopoverContent, 16rem wide. className applies to the card, and a z-* class overrides its z-(--z-popover).

Props

Prop

Type

Also accepts React Aria Popover props except isNonModal, trigger and slot, which PreviewTrigger sets, and the open state props, which go on HoverCardTrigger; children and className take plain values. placement defaults to bottom, and offset to 8, or 12 with an arrow.

Data attributes

AttributePresent when
data-slotAlways: hover-card-content; popover-arrow with showArrow
data-placementAlways; the side it opened on (top, bottom, left, right)
data-enteringThe card is fading in
data-exitingThe card is fading out

HoverCardTrigger

Renders React Aria's PreviewTrigger. Its first child is the trigger, usually a React Aria Link. Open state lives here, not on HoverCard, so the trigger and card stay in step.

Accepts all React Aria PreviewTrigger props: delay (600ms) and closeDelay (200ms), isOpen, defaultOpen and onOpenChange, and isDisabled. A disabled trigger keeps the card closed on hover, keyboard focus and long press, and disabling an open card closes it and calls onOpenChange(false); React Aria's own isDisabled stops only hover and long press, and leaves an open card open.