suiss/ui
  • Docs
  • Components
  • Patterns
  • Interactions
  • Charts
  • Blocks
  • AI
GitHubCreate
DocsComponentsPatternsInteractionsChartsBlocksAICreate
Components84
  • Overview
  • Accordion
  • Alert
  • Alert Dialog
  • Alert Sheet
  • Amount Input
  • Aspect Ratio
  • Attachment
  • Avatar
  • Badge
  • Bottom Bar
  • Breadcrumb
  • Bubble
  • Button
  • Button Group
  • Calendar
  • Card
  • Carousel
  • Chart
  • Checkbox
  • Chip
  • Code Block
  • Collapsible
  • Combobox
  • Command
  • Context Menu
  • Dialog
  • Direction
  • Drawer
  • Dropdown Menu
  • Empty
  • FAB
  • Field
  • Fullscreen Menu
  • Haptics
  • Header
  • Hover Card
  • Icon
  • Input
  • Input Accessory Bar
  • Input Group
  • Input OTP
  • Item
  • Kbd
  • Keyboard Scroll View
  • Keypad
  • Label
  • Marker
  • Menubar
  • Message
  • Message Scroller
  • Native Select
  • Navigation Menu
  • Page Indicator
  • Pagination
  • Pin Pad
  • Popover
  • Progress
  • QR Scanner
  • Questionnaire
  • Radio Group
  • Resizable
  • Screen
  • Scroll Area
  • Select
  • Separator
  • Sheet
  • Sidebar
  • Signature Pad
  • Skeleton
  • Slider
  • Spinner
  • Switch
  • Tab Bar
  • Table
  • Tabs
  • Text
  • Textarea
  • Toast
  • Toggle
  • Toggle Group
  • Tooltip
  • Trace
  • Wheel Picker
  • Widget

suiss/ui · Component

Hover Card

A card that previews content behind a link when people hover over or focus it.

Hover Card is an open-source component from suiss/ui for React. The @esuiss/ui CLI copies its source into your project, so you own the code.

import { CalendarBlankIcon } from "@phosphor-icons/react"
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
import { buttonVariants } from "@/components/ui/button"
import {
  HoverCard,
  HoverCardContent,
  HoverCardTrigger,
} from "@/components/ui/hover-card"

export default function HoverCardDemo() {
  return (
    <HoverCard>
      <HoverCardTrigger
        href="#"
        className={buttonVariants({ variant: "link" })}
      >

HoverCard wraps the hover card primitive of the chosen base. The HoverCard root manages the open state, HoverCardTrigger renders the link that opens it, and HoverCardContent renders the floating card in a portal, positioned next to the trigger.

Use a hover card for supplementary, non-essential previews, such as a profile summary behind a username. Because it only opens on hover or focus, do not put content there that people need to complete a task. For interactive content opened with a click, use a popover.

Installation

$ pnpm dlx shadcn@latest add @esuiss/hover-card

Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:

$ pnpm dlx @esuiss/ui@latest add hover-card
  1. Install the following dependencies and add the cn helper:
$ pnpm add @base-ui/react
  1. Copy and paste the following code into your project.
components/ui/hover-card.tsx
"use client"

import { PreviewCard as PreviewCardPrimitive } from "@base-ui/react/preview-card"
import { cn } from "@/lib/utils"

function HoverCard({ ...props }: PreviewCardPrimitive.Root.Props) {
  return <PreviewCardPrimitive.Root data-slot="hover-card" {...props} />
}

function HoverCardTrigger({ ...props }: PreviewCardPrimitive.Trigger.Props) {
  return (
    <PreviewCardPrimitive.Trigger data-slot="hover-card-trigger" {...props} />
  )
}

function HoverCardContent({
  1. Update the import paths to match your project setup.

Usage

import { HoverCard, HoverCardTrigger, HoverCardContent } from "@/components/ui/hover-card"
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
import { buttonVariants } from "@/components/ui/button"
<HoverCard>
  <HoverCardTrigger
    href="#"
    className={buttonVariants({ variant: "link" })}
  >
    @nextjs
  </HoverCardTrigger>
  <HoverCardContent className="w-80">
    <div className="flex gap-4">
      <Avatar size="lg">
        <AvatarImage src="https://github.com/vercel.png" alt="@nextjs" />
        <AvatarFallback>NJ</AvatarFallback>
      </Avatar>
      <div className="flex flex-col gap-1">
        <h4 className="text-sm font-semibold">@nextjs</h4>
        <p className="text-sm">
          The React framework, created and maintained by @vercel.
        </p>
        <div className="text-muted-foreground mt-1 flex items-center gap-1.5 text-xs">
          <CalendarBlankIcon />
          Joined December 2021
        </div>
      </div>
    </div>
  </HoverCardContent>
</HoverCard>

Composition

Use the following composition to build a HoverCard:

HoverCard
├── HoverCardTrigger
└── HoverCardContent

Examples

Sides

Use the side prop on HoverCardContent to open the card on the top, right, bottom or left of its trigger.

import { buttonVariants } from "@/components/ui/button"
import {
  HoverCard,
  HoverCardContent,
  HoverCardTrigger,
} from "@/components/ui/hover-card"

export default function HoverCardSides() {
  return (
    <div className="flex flex-wrap items-center gap-3 p-24">
      <HoverCard>
        <HoverCardTrigger
          href="#"
          className={buttonVariants({ variant: "outline" })}
        >
          Top

Accessibility

  • The trigger renders as a link and is reachable with Tab.
  • The card opens when the pointer rests on the trigger or when the trigger receives keyboard focus, and closes when the pointer leaves.
  • The card is a visual preview; make sure the same information is available elsewhere, for example on the linked page.

API Reference

HoverCard

The root that manages the card's open state. It renders no element of its own.

Also accepts the props of the primitive part it wraps.

Set defaultOpen to start open, or control it with open and onOpenChange.

Set defaultOpen to start open, or control it with isOpen and onOpenChange.

HoverCardTrigger

The link that opens the card on hover or focus.

Also accepts the props of the primitive part it wraps.

The trigger renders an anchor, so give it an href.

HoverCardContent

The floating card, rendered in a portal and positioned relative to the trigger.

PropTypeDefault
side"top" | "right" | "bottom" | "left" | "inline-start" | "inline-end""bottom"
sideOffsetnumber4
align"start" | "center" | "end""center"
alignOffsetnumber4
PropTypeDefault
side"top" | "right" | "bottom" | "left""bottom"
sideOffsetnumber4
align"start" | "center" | "end""center"
alignOffsetnumber4

Also accepts the props of the primitive parts it wraps.

side picks the side of the trigger the card opens on, and align how it lines up along that side. sideOffset is the gap in pixels between the trigger and the card, and alignOffset the offset in pixels along the alignment axis. The card is w-72 by default; override it with className. It slides in from the side it opens on, and fades and zooms as it opens and closes.

Each part sets a data-slot, such as hover-card-trigger or hover-card-content, for styling.

NextIconPreviousHeader

On this page

  1. 01Installation
  2. 02Usage
  3. 03Composition
  4. 04Examples
    • Sides
  5. 05Accessibility
  6. 06API Reference
    • HoverCard
    • HoverCardTrigger
    • HoverCardContent