Skip to content
Browse documentation

A preview of what a link points to: it rises off the link after a rest, stays open while you move onto it, and closes after a short grace.

Used forMentions and usernamesIssue and PR referencesDocs linksTables

Install with the shadcn CLI

$ npx shadcn@latest add @hairline/hover-card

Older shadcn CLI, or a network that blocks ui.shadcn.com? Install by URL: shadcn add https://hairlineui.com/r/hover-card.json. More

Templates

Need the whole page?

65 Next.js landing pages, each designed from scratch. Try any of them live; one is $39, all of them $159, and 8 are free.

Where it’s used

  • Mentions and usernames

    A profile with a Follow button on @ada.

  • Issue and PR references

    Status and title behind #482.

  • Docs links

    A summary of the page before you leave.

  • Tables

    A customer's details behind their name.

What it handles for you

The gap between a link and its card is where most hover cards close by accident; a grace period and shared hover fix it.

Instead of wiring upRadix HoverCardlink previewprofile popover
  1. Travel-safe

    Pointer on the link or the card keeps it open; 300ms grace after leaving both.

  2. Rests before opening

    600ms, so passing over links doesn't flash cards.

  3. Keyboard

    Focus opens it, Escape closes it.

  4. Touch-honest

    No hover on touch: the link just works.

Overview

Hover previews usually close the moment the pointer leaves the link, so nothing inside them can be used, or they stay stuck open. This one rises off the link after 600ms, stays open while you travel onto it (so its buttons work), and closes 300ms after you leave both. Keyboard focus opens it at once; touch never does, so a tap still follows the link.

States

rest
After 600ms on the link the card rises 6px from its side out of a 4px blur (320ms).
travel
Moving onto the card keeps it open.
leave
300ms after leaving both, it fades (160ms).

Usage

usage.tsx
import { HoverCard } from "@/components/hover-card";<HoverCard content={<ProfilePreview user={ada} />}>  <a href="/people/ada">@ada</a></HoverCard>

Depends on lucide-react, installed for you by the CLI along with the keyframes it needs.

Props

children
ReactElement
The link it previews.
content
ReactNode
The card.
side / align= "bottom" / "start"
Side / Align
Where it sits.
openDelay / closeDelay= 600 / 300
number
Rest and grace in ms.
width= 300
number
Card width.

Accessibility and motion

  • Placement, presence and dismissal come from floating.ts, shared with tooltip, popover and the menus.
  • Nothing in the card should be the only way to reach something: touch and screen readers may never see it.
  • Reduced motion: it fades.

Requirements

React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

Source

"use client";import * as React from "react";import { createPortal } from "react-dom";import { enterStyle, themeVars, useFloating, usePresence, type Align, type Side } from "./floating";import { useInteractionReducedMotion } from "./ui-interaction-motion";/* ───────────────────────────────────────────────────────── * HOVER CARD: a preview of what a link points to * *   rest     after resting on the link (600ms) the card rises off *            it out of a blur, from the point nearest to it *   travel   moving from the link onto the card keeps it open, so *            its buttons can be used; leaving both closes it after *            a short grace (300ms), so a diagonal path doesn't *   keys     focusing the link from the keyboard opens it at once; *            Escape closes it * * Touch has no hover: a tap follows the link, as people expect. * The card adds to the link, so nothing in it should be the only * way to reach something. * ───────────────────────────────────────────────────────── */export interface HoverCardProps {  /** The link or name it previews. */  children: React.ReactElement<{ "aria-describedby"?: string }>;  content: React.ReactNode;  side?: Side;  align?: Align;  /** Rest before it opens (ms). */  openDelay?: number;  /** Grace before it closes once the pointer has left both (ms). */  closeDelay?: number;  width?: number;  className?: string;}export function HoverCard({ children, content, side = "bottom", align = "start", openDelay = 600, closeDelay = 300, width = 300, className = "" }: HoverCardProps) {  const reduced = useInteractionReducedMotion();  const anchor = React.useRef<HTMLSpanElement>(null);  const surface = React.useRef<HTMLDivElement>(null);  const [open, setOpen] = React.useState(false);  const timer = React.useRef(0);  const id = React.useId();  const { mounted, shown } = usePresence(open, 160);  const place = useFloating({ open: mounted, anchor, floating: surface, side, align, offset: 8 });  const [vars, setVars] = React.useState<React.CSSProperties>({});  const later = (next: boolean, ms: number) => {    window.clearTimeout(timer.current);    timer.current = window.setTimeout(() => setOpen(next), ms);  };  const enter = (e: React.PointerEvent) => e.pointerType === "mouse" && later(true, open ? 0 : openDelay);  const leave = (e: React.PointerEvent) => e.pointerType === "mouse" && later(false, closeDelay);  React.useEffect(() => () => window.clearTimeout(timer.current), []);  React.useEffect(() => {    if (!open) return;    const onKey = (e: KeyboardEvent) => e.key === "Escape" && setOpen(false);    document.addEventListener("keydown", onKey);    return () => document.removeEventListener("keydown", onKey);  }, [open]);  React.useLayoutEffect(() => {    if (mounted) setVars(themeVars(anchor.current));  }, [mounted]);  return (    <>      <span        ref={anchor}        className="inline"        onPointerEnter={enter}        onPointerLeave={leave}        onFocus={(e) => {          if ((e.target as HTMLElement).matches(":focus-visible")) {            window.clearTimeout(timer.current);            setOpen(true);          }        }}        onBlur={(e) => {          if (!surface.current?.contains(e.relatedTarget as Node)) later(false, 0);        }}      >        {React.cloneElement(children, { "aria-describedby": open ? id : children.props["aria-describedby"] })}      </span>      {mounted &&        createPortal(          <div            ref={surface}            id={id}            role="tooltip"            onPointerEnter={enter}            onPointerLeave={leave}            className={`fixed left-0 top-0 z-[55] max-w-[calc(100vw_-_16px)] rounded-[16px] bg-popover p-4 text-[13.5px] text-popover-foreground shadow-[0_0_0_1px_var(--border)] ${className}`}            style={{ ...vars, width, left: place.x, top: place.y, transformOrigin: place.origin, ...enterStyle(shown, place.side, reduced) }}          >            {content}          </div>,          document.body,        )}    </>  );}export default HoverCard;

Registry item: /r/hover-card.json