Skip to content

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

Free

Link Preview Hover Card for React

Inside a sentence, the words this article open a card with the article title, a summary, and example.com.

The sentence is Check out this article for more information. The card titles Getting Started with React Server Components, adds a short summary, and shows example.com.

It fits posts and docs where readers should judge a link first. If the preview must open on a tap, use a Popover. In React, Base UI does not fetch the page. This preview is written in the file.

Preview

Preview width

Check out this article for more information.

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

<p className="text-muted-foreground text-sm">
  Check out{" "}
  <HoverCard>
    <HoverCardTrigger
      className="font-medium text-foreground underline underline-offset-4"
      closeDelay={100}
      delay={10}
      href="#"
    >
      this article
    </HoverCardTrigger>
    <HoverCardContent className="w-80 max-w-full">
      <div className="flex flex-col gap-2">
        <h4 className="font-semibold text-sm">
          Getting Started with React Server Components
        </h4>
        <p className="text-muted-foreground text-sm">
          Learn how to build modern React applications with server components,
          streaming, and Suspense.
        </p>
        <div className="flex items-center gap-2 text-muted-foreground text-xs">
          <ExternalLinkIcon aria-hidden="true" className="size-3" />
          example.com
        </div>
      </div>
    </HoverCardContent>
  </HoverCard>{" "}
  for more information.
</p>

FAQ

What is in the card?

The article's title, a muted summary and a row with an external-link icon and example.com.

How is the inline link made the trigger?

HoverCardTrigger renders an anchor itself, so the inline link is the trigger with href and its classes set on it.

Where does the preview come from?

In the demo it is written in the file. In an app, load the page's title and description, for example from its Open Graph tags, and render them in the card.

How wide is the card?

w-80 on the card, for the extra content. The class is on HoverCardContent. max-w-full keeps it inside narrow screens.

How do I change how fast the card opens and closes?

The card opens a moment after the pointer rests on the trigger and stays up long enough for the pointer to move into it. Both delays are in milliseconds. The demo sets delay 10 and closeDelay 100 on HoverCardTrigger. side and align on HoverCardContent place it.

Does the card open from the keyboard and on touch screens?

Focusing the link opens the card, moving focus away or pressing Escape closes it. Touch screens have no hover, so keep what matters reachable elsewhere, or use a Popover when the card has to open on a tap.

API Reference

Hover Card

Sets closeDelay={100}, delay={10} and href="#". The rest is on the Hover Card page.