Skip to content

Hover Card

Previews the content behind a link or a name when the user rests the pointer or focus on it.

Installation

pnpm dlx shadcn@latest add @silkware/hover-card

To copy the component by hand, install its dependencies:

pnpm add react-aria-components cn

Then copy the source into components/ui/hover-card.tsx:

components/ui/hover-card.tsx
"use client"
import { cn } from "cn"import {  Popover as PopoverPrimitive,  PreviewTrigger as PreviewTriggerPrimitive,  type PopoverProps as PopoverPrimitiveProps,  type PreviewTriggerProps,} from "@animate-ui/react/primitives/react-aria"
function HoverCardTrigger({ children, ...props }: PreviewTriggerProps) {  return (    <PreviewTriggerPrimitive data-slot="hover-card-trigger" {...props}>      {children}    </PreviewTriggerPrimitive>  )}
function HoverCard({  className,  placement = "bottom",  offset = 4,  crossOffset = 0,  ...props}: Omit<PopoverPrimitiveProps, "className"> & {  className?: string}) {  return (    <PopoverPrimitive      data-slot="hover-card-content"      placement={placement}      offset={offset}      crossOffset={crossOffset}      className={cn(        "z-50 w-64 origin-(--trigger-anchor-point) rounded-lg bg-popover p-4 text-sm text-popover-foreground shadow-md ring-1 ring-foreground/10 outline-hidden",        className      )}      {...props}    />  )}
export { HoverCard, HoverCardTrigger }

Usage

import { Button } from "@/components/ui/button"import { HoverCard, HoverCardTrigger } from "@/components/ui/hover-card"
<HoverCardTrigger>  <Button variant="link">Tornival</Button>  <HoverCard>Sheet steel and aluminium profiles for the Brescia site.</HoverCard></HoverCardTrigger>

The trigger must be focusable, a Button or a link. The card opens after a delay, so it never replaces information the user needs: the same details must be available on the page the trigger leads to.

Examples

Placement

placement on HoverCard sets the side. The default is bottom, and the card flips when there is no room.

Delay

delay on HoverCardTrigger is how long the pointer rests before the card opens, in milliseconds, and closeDelay is how long it takes to close after the pointer leaves. The defaults are 600 and 200.

Accessibility

KeyAction
TabFocuses the trigger, which opens the card after the delay. Pressing Tab again moves focus into the card when it holds a link or a button, and to the next control when it does not.
EscCloses the card and returns focus to the trigger.

React Aria provides:

  • A card that opens on hover and on keyboard focus, after a warm-up delay. Once one card is open, the next opens at once, until a cool-down passes.
  • On touch devices, a long press opens the card.
  • A non-modal card: the page stays interactive, and focus can move into the card so users can reach the links inside it.

You provide:

  • A trigger with a visible name that makes sense without the card. The card is extra, not the only place for the information.
  • Content that duplicates what the trigger leads to. Many users never see the card.
  • Interactive content only when you have tested that keyboard users can reach it.

Guidelines

When to use

  • To preview a person, a supplier or an order that is named in text, so the reader can check who it is without leaving the sentence.
  • For read-only, secondary detail, such as a role or a lead time.

When not to use

  • For a description of a control or an icon. Use a Tooltip.
  • For a panel with actions or fields. Use a Popover, which opens on a press and is built to hold controls.
  • For information the user needs to finish a task. Show it on the page.
  • On a control that the user mostly reaches by touch. A long press is easy to miss.

Content

  • Lead with the name in medium weight, then one or two lines of detail.
  • Keep it to what fits in the w-64 card. Do not put a scroll area or a form in it.
  • Keep the trigger's own label specific: the name of the supplier, not "Details".

Preview or actions

Assigned to , due Friday.

Do

Show read-only detail that helps the reader recognise who or what a name is.

Assigned to , due Friday.

Don’t

Don’t put buttons in a hover card. It appears and disappears with the pointer, so actions are hard to reach and easy to trigger by mistake.

API reference

HoverCardTrigger

Wraps the trigger and the card through the React Aria PreviewTrigger. It accepts every PreviewTrigger prop.

PropTypeDefaultDescription
childrenReactNodeNoneA focusable trigger, then the HoverCard.
delay?number600Milliseconds before the card opens.
closeDelay?number200Milliseconds before the card closes.
isDisabled?booleanNoneStops the card from opening.
isOpen?booleanNoneWhether the card is open. Makes the card controlled.
defaultOpen?booleanNoneWhether the card is open on first render, when uncontrolled.
onOpenChange?(isOpen: boolean) => voidNoneCalled when the card opens or closes.

HoverCard

Renders the React Aria Popover as a card. It accepts every Popover prop and the props below.

PropTypeDefaultDescription
placement?Placement"bottom"Where the card opens relative to the trigger.
offset?number4The distance in pixels from the trigger along the main axis.
crossOffset?number0The shift in pixels along the cross axis.
className?stringNoneClasses merged onto the card. Use it to set the width or lay out its content.
children?ReactNodeNoneThe card content.