Hover Card

For sighted users to preview content available behind a link.

Installation

  1. Install the @radix-ui/react-hover-card component from radix-ui:
npm install @radix-ui/react-hover-card
  1. Copy and paste the following code into your project.
"use client"
 
import * as React from "react"
import * as HoverCardPrimitive from "@radix-ui/react-hover-card"
 
import { cn } from "@/lib/utils"
 
const HoverCard = HoverCardPrimitive.Root
 
const HoverCardTrigger = HoverCardPrimitive.Trigger
 
const HoverCardContent = React.forwardRef<
  React.ElementRef<typeof HoverCardPrimitive.Content>,
  React.ComponentPropsWithoutRef<typeof HoverCardPrimitive.Content>
>(({ className, align = "center", sideOffset = 4, ...props }, ref) => (
  <HoverCardPrimitive.Content
    ref={ref}
    align={align}
    sideOffset={sideOffset}
    className={cn(
      "animate-in zoom-in-90 z-50 w-64 rounded-md border border-slate-100 bg-white p-4 shadow-md outline-none dark:border-slate-800 dark:bg-slate-800",
      className
    )}
    {...props}
  />
))
HoverCardContent.displayName = HoverCardPrimitive.Content.displayName
 
export { HoverCard, HoverCardTrigger, HoverCardContent }

This is the <HoverCard /> primitive. You can place it in a file at components/ui/hover-card.tsx.

Usage

import {
  HoverCard,
  HoverCardContent,
  HoverCardTrigger,
} from "@/components/ui/hover-card"
<HoverCard>
  <HoverCardTrigger>Hover</HoverCardTrigger>
  <HoverCardContent>
    <div className="space-y-2">
      <h4 className="text-sm font-semibold">@nextjs</h4>
      <p className="text-sm">
        The React Framework – created and maintained by @vercel.
      </p>
    </div>
  </HoverCardContent>
</HoverCard>