Skip to content

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

Components

Free

Hover Card for Radix UI

A card that opens while the pointer rests on a link, such as the profile behind a username. A React hover card on Radix UI, 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";
<HoverCard>
  <HoverCardTrigger asChild>
    <Button variant="link">Hover Here</Button>
  </HoverCardTrigger>
  <HoverCardContent className="flex w-64 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

Radix UI gives the card no role and the trigger no ARIA that points to it, so a screen reader doesn't announce the card. Focus on the trigger opens it after the open delay, and Radix UI takes every link and button inside the card out of the tab order. 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 focus reaches the trigger, after the open delay. Closes it after the close delay when focus moves on.
Escape
If open, closes the hover card.

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, inside a wrapper with dir set to rtl. The card is portaled out of that wrapper, so it sets dir again itself. side only takes physical sides, so the second row maps the start of the line to right and its end to left.

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. Radix UI waits 700 before opening and 300 before closing, set with openDelay and closeDelay on HoverCard. The default demo lowers them to 10 and 100.

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 as a boolean.

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) or left. align lines it up with the start, center or end of the trigger, and sideOffset sets the gap, 4 pixels in this file.

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?

Set asChild on HoverCardTrigger and put one link or Button inside, which then takes the trigger's handlers. Without it, the trigger renders an a of its own, which needs an href to take focus.

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?

No. Focus on the trigger opens the card, but Radix UI sets tabindex to -1 on everything focusable inside it, and the card isn't announced to a screen reader. 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 Radix UI 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

data-slot="hover-card"

PropType
open?boolean

Whether it's open. Pair with onOpenChange to control it.

defaultOpen?boolean

Whether it starts open, when you don't control open.

onOpenChange?{ bivarianceHack(open: boolean): void; }["bivarianceHack"]

Called when it opens or closes.

Type
{ bivarianceHack(open: boolean): void; }["bivarianceHack"]
closeDelay?number

How long it waits to close after the pointer leaves, in milliseconds.

Default
300
openDelay?number

How long the pointer rests on the trigger before it opens, in milliseconds.

Default
600

HoverCardTrigger

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

PropType
asChild?boolean

Merge props onto the child element instead of rendering a host.

HoverCardContent

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

PropType
align?(typeof ALIGN_OPTIONS)[number]

How it lines up with the trigger: start, center or end.

Default
"center"
alignOffset?number

How far it shifts from its align position, in px.

arrowPadding?number

The space kept between the arrow and the content's corners, in px.

avoidCollisions?boolean

Whether it flips and shifts to stay in view.

collisionBoundary?Boundary | Boundary[]

The element or elements it stays inside. The viewport when unset.

Type
Boundary | Boundary[]
collisionPadding?number | Partial<Record<Side, number>>

The distance it keeps from the boundary's edges, in px, or one per side.

Type
number | Partial<Record<Side, number>>
forceMount?true

Used to force mounting when more control is needed. Useful when controlling animation with React animation libraries.

hideWhenDetached?boolean

Whether it hides when its trigger is entirely out of view.

onEscapeKeyDown?{ bivarianceHack(event: KeyboardEvent): void; }["bivarianceHack"]

Event handler called when the escape key is down. Can be prevented.

Type
{ bivarianceHack(event: KeyboardEvent): void; }["bivarianceHack"]
onFocusOutside?{ bivarianceHack(event: FocusOutsideEvent): void; }["bivarianceHack"]

Event handler called when the focus moves outside of the HoverCard. Can be prevented.

Type
{ bivarianceHack(event: FocusOutsideEvent): void; }["bivarianceHack"]
onInteractOutside?{ bivarianceHack(event: PointerDownOutsideEvent | FocusOutsideEvent): void; }["bivarianceHack"]

Event handler called when an interaction happens outside the HoverCard. Specifically, when a pointerdown event happens outside or focus moves outside of it. Can be prevented.

Type
{ bivarianceHack(event: PointerDownOutsideEvent | FocusOutsideEvent): void; }["bivarianceHack"]
onPointerDownOutside?{ bivarianceHack(event: PointerDownOutsideEvent): void; }["bivarianceHack"]

Event handler called when the a pointerdown event happens outside of the HoverCard. Can be prevented.

Type
{ bivarianceHack(event: PointerDownOutsideEvent): void; }["bivarianceHack"]
side?(typeof SIDE_OPTIONS)[number]

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

sideOffset?number

The distance from the trigger, in px.

Default
4
sticky?(typeof Sticky)[keyof typeof Sticky]

How it stays in view along its trigger: partial while the trigger is partly visible, always whatever the trigger does.

Type
(typeof Sticky)[keyof typeof Sticky]
updatePositionStrategy?(typeof UpdatePositionStrategy)[keyof typeof UpdatePositionStrategy]

How often it updates its position: optimized, on layout changes, or always, every animation frame.

Type
(typeof UpdatePositionStrategy)[keyof typeof UpdatePositionStrategy]
asChild?boolean

Merge props onto the child element instead of rendering a host.

className?string

Merged with the part's classes.

AttributeType
data-placementstring

The content's placement on its trigger, such as bottom-start.

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

The side of the trigger the content sits on.

Type
"top" | "right" | "bottom" | "left"
data-state"open" | "closed"

"open" or "closed".

Type
"open" | "closed"
CSS variableType
--radix-hover-card-content-transform-originstring