Skip to content

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

Free

Document Preview Hover Card for React

Project Proposal.pdf opens a card with a file icon, 2.4 MB, PDF Document, a short summary, and the last modified date.

The link reads Project Proposal.pdf. The card shows a file icon in the destructive color, 2.4 MB, PDF Document, a summary of the Q1 proposal, and Last modified: December 15, 2023.

Use it in a file list so people can confirm the document before opening it. If the card has to open on a tap, use a Popover. In React, Base UI opens the card after a short pause.

Preview

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

<HoverCard>
  <HoverCardTrigger
    className="flex items-center gap-2 font-medium text-sm underline underline-offset-4"
    closeDelay={100}
    delay={10}
    href="#"
  >
    <FileTextIcon aria-hidden="true" className="size-4" />
    Project Proposal.pdf
  </HoverCardTrigger>
  <HoverCardContent className="w-80 max-w-full">
    <div className="flex flex-col gap-3">
      <div className="flex items-start gap-3">
        <div className="flex size-10 shrink-0 items-center justify-center rounded-md bg-destructive/10">
          <FileTextIcon
            aria-hidden="true"
            className="size-5 text-destructive"
          />
        </div>
        <div className="flex flex-1 flex-col gap-1">
          <h4 className="font-semibold text-sm">Project Proposal.pdf</h4>
          <p className="text-muted-foreground text-xs">
            2.4 MB · PDF Document
          </p>
        </div>
      </div>
      <p className="text-muted-foreground text-sm">
        Comprehensive project proposal including timeline, budget, and
        deliverables for Q1 2024.
      </p>
      <p className="text-muted-foreground text-xs">
{/* The rest of the file is under Installation. */}

FAQ

What is in the card?

A file icon on a bg-destructive/10 tile, the name, 2.4 MB · PDF Document, the summary and Last modified: December 15, 2023.

How is the file link made the trigger?

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

Why is the file icon red?

Red is the usual color for PDF files. The tile uses the destructive token, bg-destructive/10 with a text-destructive icon, so it follows the theme. Pick a token per file type.

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.