Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Hover Card for UI SDK

A card that opens while the pointer rests on a link, such as the profile behind a username. A React hover card on UI SDK, with open and close delays.

On this page9 sections

A hover card is a small preview that opens while the pointer rests on a link, such as the handle, bio and join date behind a username. It opens after a short delay you can tune and stays up while the pointer moves into it, so people can read the card and select its text.

For a few words on an icon button, use a Tooltip. When the panel has to open from a click or a tap, or holds controls people must reach, use a Popover.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Hover Card anatomy: trigger, positioner, arrow, content

Usage

import { Button } from "@/components/ui/button";
import {
  HoverCard, HoverCardContent, HoverCardTrigger,
} from "@/components/ui/hover-card";
const HoverCardDemo = () => (
  <HoverCard>
    <HoverCardTrigger
      closeDelay={100}
      delay={10}
      render={<Button variant="link" />}
    >
      Hover Here
    </HoverCardTrigger>
    <HoverCardContent className="flex w-64 max-w-full flex-col gap-0.5">
      <div className="font-semibold">@nextjs</div>
      <div>The React Framework – created and maintained by @vercel.</div>
      <div className="mt-1 text-muted-foreground text-xs">
        Joined December 2021
      </div>
    </HoverCardContent>
  </HoverCard>
);

Accessibility

UI SDK gives the card no role and the trigger no ARIA that points to it, so a screen reader says nothing when the card opens. Keyboard focus on the trigger opens it after the delay, Tab then moves into the card when it holds a link or a button, and Escape closes it. Focus leaving both the trigger and the card closes it too. Treat the card as a preview for pointer users, and keep anything people need, such as the profile link, on the page itself.

Tab
Opens the hover card once keyboard focus reaches the trigger, after the delay. While the card is open, moves focus from the trigger to the first focusable element in the card.
Escape
If open, closes the hover card. From inside the card, focus goes back to the trigger.

Demos

A link inside a sentence, this article, opens a card with the post's title, a one-line summary and its site. That is the hover card's main job: a preview for people with a pointer, while the link still goes where it says. The trigger is the link itself, an a with an href.

Preview width

Sides

Four outline buttons, left, top, bottom and right, each open a card that says which side it took. side on HoverCardContent picks it. Both delays are 100 milliseconds so you can sweep along the row. Pin a side when the card would otherwise cover the text someone is reading.

Preview width

RTL

A product preview in Arabic, wireless headphones at $99.99, in a row with dir set to rtl inside a Direction provider set to right to left. The second row uses inline-start and inline-end, which UI SDK resolves from the direction the card itself computes, so the dir set again on each portaled card makes the start of the line open on the right.

Preview width

FAQ

Should I use a Hover Card, a Tooltip or a Popover?

Use a Hover Card for a preview that takes more than a glance, such as a profile card behind a username or a summary behind a link to a document. It's extra context for pointer users, and the link itself still does the main job.

A Tooltip is a short label on an icon button, shown on hover or focus. A Popover opens on a click, works on touch screens and keyboards alike, and is the one to use when the panel holds buttons or fields.

Why does my Hover Card open slowly, or close before I can reach it?

Both are delays in milliseconds. UI SDK waits 600 before opening and 300 before closing, set with delay and closeDelay on HoverCard, or on HoverCardTrigger, which hands them to HoverCard. The default demo lowers them to 10 and 100 on the trigger.

A longer open delay stops cards from flashing as the pointer crosses a list of names. If the card closes as the pointer travels to it, raise the close delay or shrink the gap between the trigger and the card.

How do I control whether a Hover Card is open in React?

Pass open on HoverCard and update your state in onOpenChange, which receives the next state first and the event details, with the reason it changed, second.

Hover, focus and Escape still ask to change it, so keep the callback wired. Controlling it is useful for opening a card from a tour step, or for keeping it open while you style it.

How do I choose which side a Hover Card opens on?

Pass side on HoverCardContent: top, right, bottom (the default), left, or inline-start and inline-end, which follow the card's own reading direction, so they swap when the card has dir set to rtl. align lines it up with the start, center or end of the trigger, sideOffset sets the gap, 4 pixels in this file, and alignOffset nudges it along the edge.

The Sides demo shows all four. When there isn't room on the side you picked, the card flips to the opposite one.

How do I use a link or a button as the Hover Card trigger?

HoverCardTrigger renders an a already, so give it an href. For a Button or a router link, pass the element to its render prop, as the demos do.

Keep the trigger focusable, so keyboard users get the card on focus. The username or the document title makes a better trigger than an info icon, since the card previews what the link leads to, as in the Link preview demo.

Can keyboard and screen reader users reach the links inside a Hover Card?

With a keyboard, yes, but a screen reader isn't told. While the card is open, Tab from the trigger moves into it and Escape closes it again, yet UI SDK gives the trigger no ARIA that points to the card, so nothing is announced when it opens. Put every link and action in the card somewhere else on the page too, or use a Popover when the panel exists for its buttons.

Does a Hover Card work on a phone or a tablet?

Don't count on it. A touch screen has no hover, and UI SDK ignores touch pointers in its hover handling, so a tap goes through to the link or button and does what it would anyway. Keep anything a phone user needs out of the card, or open a Popover on a tap instead.

API Reference

HoverCard

PropType
open?boolean

Whether the card is open. Pass it to control the card.

defaultOpen?boolean

Whether the card starts open, when uncontrolled.

Default
false
onOpenChange?(open: boolean, details: ChangeDetails<HoverCardOpenChangeReason>) → void

Runs when the card wants to open or close. details.reason says why, and details.cancel() stops it.

Type
(open: boolean, details: ChangeDetails<HoverCardOpenChangeReason>) → void
Parameters

open – boolean

details – ChangeDetails<HoverCardOpenChangeReason>

Returns
void
closeDelay?number

Milliseconds before the card closes once the pointer leaves the trigger and the card. UI SDK's default: 300.

Default
trigger.closeDelay
container?DocumentFragment | Element | null

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type
DocumentFragment | Element | null
controller?PopupController<any, HoverCardOpenChangeReason, {}, string, HoverCardPayloadChangeReason>

Opens, closes and inspects the card from anywhere, and connects triggers outside it. Create one with createHoverCardController().

Type
PopupController<any, HoverCardOpenChangeReason, {}, string, HoverCardPayloadChangeReason>
debug?boolean

Logs every state change with its reason to the console, in development builds.

delay?number

Milliseconds the pointer rests on the trigger, or keyboard focus stays on it, before the card opens. UI SDK's default: 600.

Default
trigger.delay
disabled?boolean

Stops the card from opening. The trigger keeps working as a link.

Default
false
onOpenChangeComplete?(open: boolean) → void

Runs once the card has finished opening or closing, after any animation.

Parameters

open – boolean

Returns
void
onPayloadChange?(payload: unknown, details: ChangeDetails<HoverCardPayloadChangeReason>) → void

Runs when the payload the content shows changes: a trigger with another payload opens the card, or takes the open card over, which fires no onOpenChange, and so does a controller's open({ payload }) on the open card. details.cancel() keeps the previous payload.

Type
(payload: unknown, details: ChangeDetails<HoverCardPayloadChangeReason>) → void
Parameters

payload – unknown

details – ChangeDetails<HoverCardPayloadChangeReason>

Returns
void

HoverCardTrigger

<a> data-slot="hover-card-trigger"

PropType
closeDelay?number

Milliseconds before the card closes once the pointer leaves it and the trigger.

controller?PopupController<any, HoverCardOpenChangeReason, {}, string, HoverCardPayloadChangeReason>

Connects a trigger outside the card, like every author link in a feed, to the card with this controller.

Type
PopupController<any, HoverCardOpenChangeReason, {}, string, HoverCardPayloadChangeReason>
delay?number

Milliseconds the pointer rests on the trigger before the card opens.

payload?unknown

Data for the content to render when this trigger opens the card, like the profile it links to.

render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
AttributeType
data-closed?string

Present while the card is closed, including while it animates out.

data-open?string

Present while the card is open. On a trigger, while it's open from that trigger.

HoverCardContent

<div> data-slot="hover-card-content"

PropType
align?Align

How the popup lines up with the anchor along that side. start and end follow the reading direction. How the card lines up with the trigger along that side.

Default
"center"
alignOffset?number

Shift along the anchor, in pixels. Shift along the trigger, in pixels.

Default
0
anchor?string | Anchor | null

Positions the popup against this instead of its trigger: an element, a CSS selector, a point like { x, y }, or anything with getBoundingClientRect(). It's read on every check, so it can move. A controller's open({ anchor }) wins for that open.

Type
string | Anchor | null
arrowPadding?number

Space to keep between the arrow and the popup's corners, in pixels.

Default
6
collisionPadding?number

Space to keep from the viewport edges, in pixels.

Default
8
container?DocumentFragment | Element | null

Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.

Type
DocumentFragment | Element | null
keepMounted?boolean

Whether the card stays in the page, hidden, while closed. Its content keeps its state between opens.

Default
false
side?HoverCardSide

The side of the trigger it opens on. It flips when there's no room, unless avoidCollisions is off.

Default
"bottom"
sideOffset?number

Gap between the anchor and the popup, in pixels. Gap between the trigger and the card, in pixels.

Default
4
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-align"start" | "center" | "end"

How the card lines up with its trigger.

Type
"start" | "center" | "end"
data-closed?string

Present while the card is closed, including while it animates out.

data-ending-style?string

Present while the card animates out. Style the state you animate to.

data-open?string

Present while the card is open. On a trigger, while it's open from that trigger.

data-side"top" | "right" | "bottom" | "left"

Where the card ended up, after flipping away from edges. Set on the arrow too.

Type
"top" | "right" | "bottom" | "left"
data-starting-style?string

Present for the first frame. Style the state you animate from.

CSS variableType
--anchor-heightstring

Height of the anchor.

--anchor-widthstring

Width of the anchor, usually the trigger. Use it to match widths.

--arrow-offsetstring

Where the anchor's center falls along the popup's edge, kept arrowPadding from the corners. Position a ::before arrow with it.

--available-heightstring

Space between the anchor and the viewport edge. Use it for max-height. Not inherited, so a child that needs it copies it on the popup, like [--list-height:var(--available-height)], and reads max-h-(--list-height). The first popup registers both names for the whole page with CSS.registerProperty, so they don't inherit anywhere. A page that needs them to inherit calls it first, with inherits: true. An @property rule can't, since a registration in script wins.

--available-widthstring

Space beside the popup, up to the viewport edge. Not inherited either.

--transform-originstring

Where the content scales from, on its trigger's side.