Skip to the content

Controls and surfaces

hover-card

A preview that appears beside a link while the pointer rests on it or the keyboard focuses it.

Primitives

When to use it

A look ahead at where a link goes: a person, a page, a record. It does not open on touch and a screen reader does not announce it, so it only ever repeats what following the link shows. For a hint of a few words use tooltip; for anything a person must be able to reach or act on use popover.

See it working

The demonstration the browser checks drive, with the keyboard as well as the pointer. More of it is on the Primitives page.

Alert dialog

The project is kept.

Collapsible

The options are closed.

Hover card

The first published program was written by Ada Lovelace.

Menubar

Command: none yet. Ruler: hidden. Zoom: 100.

Navigation menu

Toolbar

Styles: none. Saved 0 times.

One-time code

Typed: nothing yet. Sent: nothing yet.

Password

At least twelve characters.

Words for screen readers

Add it

npx shadcn@latest add @rivier-ui/hover-card

The registry is named once in components.json: see Installation.

Example

Import it from @/components/ui/hover-card.

<HoverCard>
  <HoverCardTrigger asChild><a href="/people/ada">Ada Lovelace</a></HoverCardTrigger>
  <HoverCardContent>
    <p className="font-medium">Ada Lovelace</p>
    <p className="text-muted-foreground">Wrote the first published program.</p>
  </HoverCardContent>
</HoverCard>

What it needs

A client component: it uses state, effects or browser behaviour. Packages: radix-ui.

It imports

Source

The whole file. Once copied it is yours: the registry item holds the same text.

src/components/ui/hover-card.tsx
"use client";
/**
 * @name hover-card
 * @category ui
 * @description A preview that appears beside a link while the pointer rests on it or the keyboard focuses it.
 * @when A look ahead at where a link goes: a person, a page, a record. It does not open on touch and a screen reader does not announce it, so it only ever repeats what following the link shows. For a hint of a few words use tooltip; for anything a person must be able to reach or act on use popover.
 * @example
 * <HoverCard>
 *   <HoverCardTrigger asChild><a href="/people/ada">Ada Lovelace</a></HoverCardTrigger>
 *   <HoverCardContent>
 *     <p className="font-medium">Ada Lovelace</p>
 *     <p className="text-muted-foreground">Wrote the first published program.</p>
 *   </HoverCardContent>
 * </HoverCard>
 */
import { HoverCard as HoverCardPrimitive } from "radix-ui";
import type { ComponentProps } from "react";
import { cn } from "@/lib/cn";

/** `openDelay` and `closeDelay` are in milliseconds: long enough that passing over a link does not open it. */
export function HoverCard({ openDelay = 400, closeDelay = 150, ...props }: ComponentProps<typeof HoverCardPrimitive.Root>) {
  return <HoverCardPrimitive.Root openDelay={openDelay} closeDelay={closeDelay} {...props} />;
}

/** The link. Renders an `a`; use asChild for a framework's link component. */
export function HoverCardTrigger(props: ComponentProps<typeof HoverCardPrimitive.Trigger>) {
  return <HoverCardPrimitive.Trigger data-slot="hover-card-trigger" {...props} />;
}

export function HoverCardContent({ className, align = "start", sideOffset = 6, collisionPadding = 8, ...props }: ComponentProps<typeof HoverCardPrimitive.Content>) {
  return (
    <HoverCardPrimitive.Portal>
      <HoverCardPrimitive.Content
        data-slot="hover-card-content"
        align={align}
        sideOffset={sideOffset}
        collisionPadding={collisionPadding}
        className={cn(
          "z-50 max-h-(--radix-hover-card-content-available-height) w-72 max-w-(--radix-hover-card-content-available-width) origin-(--radix-hover-card-content-transform-origin) overflow-y-auto rounded-surface border bg-popover p-4 text-sm text-popover-foreground ease-out animation-duration-(--duration-base) fade-in-0 fade-out-0 zoom-in-95 zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 data-[state=closed]:animate-out data-[state=open]:animate-in motion-reduce:animate-none!",
          className,
        )}
        {...props}
      />
    </HoverCardPrimitive.Portal>
  );
}