Hover Card
The shadcn/ui Hover Card that grows out of its trigger with snappier delays and an optional arrow — plus two animated React hover card blocks built with Motion and Radix: a user profile preview that opens from the trigger, and a link preview that rises and follows the cursor.
- Drop-in shadcn API
- Radix positioning
- Motion blocks
- prefers-reduced-motion
"use client";
import * as React from "react";
import { Check, UserPlus } from "lucide-react";
import { Button } from "@/components/ui/button";
import { ProfileHoverCard, type ProfileUser } from "@/components/dusk/hover-card/profile-hover-card";
const ada: ProfileUser = {
name: "Ada Kim",
handle: "adakim",
avatarUrl: "https://i.pravatar.cc/150?img=47",
verified: true,
bio: "Design engineer. Spring physics, variable fonts and very small details.",
location: "Berlin",
joined: "Joined March 2021",
stats: [
{ label: "Followers", value: "12.4k" },
{ label: "Following", value: "318" },
{ label: "Components", value: "64" },
],
};
const leo: ProfileUser = {
name: "Leo Park",
handle: "leopark",
// No avatar: the initials show instead.
bio: "Frontend at a small studio. Writes about accessibility and motion.",
location: "Seoul",
stats: [
{ label: "Followers", value: "2,091" },
{ label: "Following", value: "77" },
],
};
function FollowButton() {
const [following, setFollowing] = React.useState(false);
return (
<Button
size="sm"
variant={following ? "outline" : "default"}
aria-pressed={following}
onClick={() => setFollowing((f) => !f)}
>
{following ? <Check /> : <UserPlus />}
{following ? "Following" : "Follow"}
</Button>
);
}
const mention =
"rounded-sm font-medium text-primary underline-offset-4 outline-none hover:underline focus-visible:ring-[3px] focus-visible:ring-ring/50";
export default function ProfileHoverCardDemo() {
return (
<p className="max-w-md text-center text-sm leading-7 text-muted-foreground">
Reviewed by{" "}
<ProfileHoverCard user={ada} action={<FollowButton />}>
<a href="#" className={mention}>
@adakim
</a>
</ProfileHoverCard>{" "}
and{" "}
<ProfileHoverCard user={leo} action={<FollowButton />}>
<a href="#" className={mention}>
@leopark
</a>
</ProfileHoverCard>
. Hover or focus a name to see their profile.
</p>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/hover-card.json1. Install the dependencies.
npm install @radix-ui/react-hover-card2. Copy the source into
components/ui/hover-card.tsx.hover-card.tsx "use client"; import * as React from "react"; import * as HoverCardPrimitive from "@radix-ui/react-hover-card"; import { cn } from "@/lib/utils"; /** * DuskUI Hover Card — a drop-in upgrade of the shadcn/ui Hover Card. * * Same parts, props and data-slot attributes as shadcn, plus: * - Snappier defaults: opens after 400ms and closes 200ms after the pointer * leaves (Radix defaults are 700 / 300). Override with `openDelay` / `closeDelay`. * - Grows out of its trigger: the scale origin follows Radix's transform * origin and it shifts in slightly from the side it opens on (150ms * ease-out), then closes quicker than it opened. * - A neutral shadow on the surface, and an optional arrow. * - Under `prefers-reduced-motion` it only fades. * CSS only (tw-animate-css). Radix handles hover intent, focus, Escape, * collision avoidance and positioning. */ function HoverCard({ openDelay = 400, closeDelay = 200, ...props }: React.ComponentProps<typeof HoverCardPrimitive.Root>) { return ( <HoverCardPrimitive.Root data-slot="hover-card" openDelay={openDelay} closeDelay={closeDelay} {...props} /> ); } /** The element that opens the card on hover or keyboard focus. Usually a link (`asChild`). */ function HoverCardTrigger({ ...props }: React.ComponentProps<typeof HoverCardPrimitive.Trigger>) { return <HoverCardPrimitive.Trigger data-slot="hover-card-trigger" {...props} />; } /** The floating card. Portalled and positioned next to the trigger. */ function HoverCardContent({ className, align = "center", sideOffset = 6, showArrow = false, children, ...props }: React.ComponentProps<typeof HoverCardPrimitive.Content> & { /** Draw a small arrow pointing at the trigger. */ showArrow?: boolean; }) { return ( <HoverCardPrimitive.Portal data-slot="hover-card-portal"> <HoverCardPrimitive.Content data-slot="hover-card-content" align={align} sideOffset={sideOffset} className={cn( "z-50 w-64 rounded-xl border bg-popover p-4 text-popover-foreground outline-hidden", "origin-(--radix-hover-card-content-transform-origin)", "shadow-md ease-out", "data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95 data-[state=open]:duration-150", "data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[state=closed]:duration-100", "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", "motion-reduce:data-[state=open]:zoom-in-100 motion-reduce:data-[state=closed]:zoom-out-100", "motion-reduce:data-[side=bottom]:slide-in-from-top-0 motion-reduce:data-[side=left]:slide-in-from-right-0 motion-reduce:data-[side=right]:slide-in-from-left-0 motion-reduce:data-[side=top]:slide-in-from-bottom-0", className )} {...props} > {children} {showArrow && ( <HoverCardPrimitive.Arrow asChild width={14} height={7}> {/* Nudged 1px into the surface so its base covers the border line. */} <svg viewBox="0 0 14 7" className="block -translate-y-px fill-popover stroke-border" aria-hidden> <path d="M0 0 L7 6.5 L14 0" strokeWidth={1} /> </svg> </HoverCardPrimitive.Arrow> )} </HoverCardPrimitive.Content> </HoverCardPrimitive.Portal> ); } export { HoverCard, HoverCardTrigger, HoverCardContent };
Same exports as shadcn/ui (HoverCard, HoverCardTrigger, HoverCardContent) plus showArrow. It opens after 400ms and closes 200ms after the pointer leaves; pass openDelay / closeDelay to change that. Animations are CSS (tw-animate-css), so the base needs no Motion.
Usage
import { HoverCard, HoverCardContent, HoverCardTrigger } from "@/components/ui/hover-card"
export function Mention() {
return (
<HoverCard>
<HoverCardTrigger asChild>
<a href="/u/duskui">@duskui</a>
</HoverCardTrigger>
<HoverCardContent className="w-80">
Animated React components for shadcn/ui.
</HoverCardContent>
</HoverCard>
)
}Examples
Default
Scales and slides out of the trigger from Radix's transform origin, and closes quicker than it opened. showArrow draws a small arrow that blends into the border. Opens on hover and on keyboard focus of the trigger.
import { CalendarDays } from "lucide-react";
import { HoverCard, HoverCardContent, HoverCardTrigger } from "@/components/ui/hover-card";
export default function HoverCardBasicDemo() {
return (
<HoverCard>
<HoverCardTrigger asChild>
<a href="#" className="rounded-sm font-medium text-primary underline-offset-4 outline-none hover:underline focus-visible:ring-[3px] focus-visible:ring-ring/50">
@duskui
</a>
</HoverCardTrigger>
<HoverCardContent className="w-80" showArrow>
<div className="flex gap-4">
<span
aria-hidden
className="grid size-11 shrink-0 place-items-center rounded-full bg-muted font-semibold text-foreground"
>
D
</span>
<div className="grid gap-1">
<h4 className="text-sm font-semibold">@duskui</h4>
<p className="text-sm text-muted-foreground">
Animated React components for shadcn/ui, built with Motion.
</p>
<p className="mt-1 flex items-center gap-1.5 text-xs text-muted-foreground">
<CalendarDays className="size-3.5" aria-hidden />
Joined November 2025
</p>
</div>
</div>
</HoverCardContent>
</HoverCard>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Profile Hover Card
A user preview for @mentions and avatars. It opens out of the trigger, then the avatar, name, bio, meta and stats fade up in sequence. The avatar is a plain <img> layered over the user's initials, so a missing or broken image still looks intentional (hover @leopark). Put a Follow button in action.
"use client";
import * as React from "react";
import { Check, UserPlus } from "lucide-react";
import { Button } from "@/components/ui/button";
import { ProfileHoverCard, type ProfileUser } from "@/components/dusk/hover-card/profile-hover-card";
const ada: ProfileUser = {
name: "Ada Kim",
handle: "adakim",
avatarUrl: "https://i.pravatar.cc/150?img=47",
verified: true,
bio: "Design engineer. Spring physics, variable fonts and very small details.",
location: "Berlin",
joined: "Joined March 2021",
stats: [
{ label: "Followers", value: "12.4k" },
{ label: "Following", value: "318" },
{ label: "Components", value: "64" },
],
};
const leo: ProfileUser = {
name: "Leo Park",
handle: "leopark",
// No avatar: the initials show instead.
bio: "Frontend at a small studio. Writes about accessibility and motion.",
location: "Seoul",
stats: [
{ label: "Followers", value: "2,091" },
{ label: "Following", value: "77" },
],
};
function FollowButton() {
const [following, setFollowing] = React.useState(false);
return (
<Button
size="sm"
variant={following ? "outline" : "default"}
aria-pressed={following}
onClick={() => setFollowing((f) => !f)}
>
{following ? <Check /> : <UserPlus />}
{following ? "Following" : "Follow"}
</Button>
);
}
const mention =
"rounded-sm font-medium text-primary underline-offset-4 outline-none hover:underline focus-visible:ring-[3px] focus-visible:ring-ring/50";
export default function ProfileHoverCardDemo() {
return (
<p className="max-w-md text-center text-sm leading-7 text-muted-foreground">
Reviewed by{" "}
<ProfileHoverCard user={ada} action={<FollowButton />}>
<a href="#" className={mention}>
@adakim
</a>
</ProfileHoverCard>{" "}
and{" "}
<ProfileHoverCard user={leo} action={<FollowButton />}>
<a href="#" className={mention}>
@leopark
</a>
</ProfileHoverCard>
. Hover or focus a name to see their profile.
</p>
);
}$ npx shadcn@latest add https://duskui.com/r/profile-hover-card.json1. Install the dependencies.
npm install motion @radix-ui/react-hover-card lucide-react2. Copy the source into
components/dusk/hover-card/profile-hover-card.tsx.profile-hover-card.tsx "use client"; import * as React from "react"; import * as HoverCardPrimitive from "@radix-ui/react-hover-card"; import { AnimatePresence, motion, useReducedMotion, type Variants } from "motion/react"; import { BadgeCheck, CalendarDays, MapPin } from "lucide-react"; import { HoverCard, HoverCardTrigger } from "@/components/ui/hover-card"; import { cn } from "@/lib/utils"; export interface ProfileUser { name: string; /** Shown as @handle. */ handle?: string; /** Avatar URL. Falls back to initials if it's missing or fails to load. */ avatarUrl?: string; bio?: React.ReactNode; location?: string; /** e.g. "Joined March 2021". */ joined?: string; verified?: boolean; /** Short stats under the bio, e.g. [{ label: "Followers", value: "12.4k" }]. */ stats?: { label: string; value: React.ReactNode }[]; /** Cover color or image behind the avatar. Any CSS background. */ cover?: string; } export interface ProfileHoverCardProps extends Omit<React.ComponentProps<typeof HoverCardPrimitive.Content>, "forceMount" | "asChild" | "children"> { user: ProfileUser; /** The trigger, usually a link to the profile (e.g. an @mention or avatar). */ children: React.ReactElement; /** Buttons in the top-right corner, e.g. a Follow button. */ action?: React.ReactNode; open?: boolean; defaultOpen?: boolean; onOpenChange?: (open: boolean) => void; /** ms before opening on hover. */ openDelay?: number; /** ms before closing after the pointer leaves. */ closeDelay?: number; } /** * A user preview card that opens out of the trigger. It scales up from * Radix's transform origin (the edge nearest the trigger, even after a flip), * then the avatar and the details fade up in sequence. The avatar is a * plain <img> layered over the user's initials, so a missing or broken image * still looks intentional. Opens on hover and on keyboard focus of the * trigger; Escape closes it. Fades only under reduced motion. */ export function ProfileHoverCard({ user, children, action, open: openProp, defaultOpen = false, onOpenChange, openDelay, closeDelay, side = "bottom", align = "start", sideOffset = 8, className, ...props }: ProfileHoverCardProps) { const reduceMotion = useReducedMotion(); const [internalOpen, setInternalOpen] = React.useState(defaultOpen); const open = openProp ?? internalOpen; const setOpen = (next: boolean) => { if (openProp === undefined) setInternalOpen(next); onOpenChange?.(next); }; const surface: Variants = reduceMotion ? { closed: { opacity: 0, transition: { duration: 0.1 } }, open: { opacity: 1, transition: { duration: 0.15 } } } : { closed: { opacity: 0, scale: 0.96, transition: { duration: 0.12, ease: "easeIn" } }, open: { opacity: 1, scale: 1, transition: { duration: 0.18, ease: "easeOut", staggerChildren: 0.04, delayChildren: 0.05 }, }, }; const item: Variants = reduceMotion ? { closed: { opacity: 1 }, open: { opacity: 1 } } : { closed: { opacity: 0, y: 4, transition: { duration: 0.08 } }, open: { opacity: 1, y: 0, transition: { duration: 0.2, ease: "easeOut" } }, }; const pop: Variants = reduceMotion ? item : { closed: { opacity: 0, scale: 0.96, transition: { duration: 0.08 } }, open: { opacity: 1, scale: 1, transition: { duration: 0.2, ease: "easeOut" } }, }; return ( <HoverCard open={open} onOpenChange={setOpen} openDelay={openDelay} closeDelay={closeDelay}> <HoverCardTrigger asChild>{children}</HoverCardTrigger> <AnimatePresence> {open && ( // forceMount hands mounting to AnimatePresence so the exit animation can play. <HoverCardPrimitive.Portal key="profile" forceMount> <HoverCardPrimitive.Content forceMount asChild side={side} align={align} sideOffset={sideOffset} {...props} > <motion.div data-slot="profile-hover-card" className={cn( "z-50 w-80 overflow-hidden rounded-xl border bg-popover text-popover-foreground outline-none", "origin-(--radix-hover-card-content-transform-origin)", "shadow-lg", className )} variants={surface} initial="closed" animate="open" exit="closed" > <div aria-hidden className="h-16" style={{ background: user.cover ?? "var(--muted)", }} /> <div className="relative px-4 pb-4"> <div className="-mt-8 flex items-end justify-between gap-3"> <motion.div variants={pop}> <ProfileAvatar name={user.name} src={user.avatarUrl} className="size-16 border-4 border-popover text-lg" /> </motion.div> {action && ( <motion.div variants={item} className="flex gap-2 pb-1"> {action} </motion.div> )} </div> <motion.div variants={item} className="mt-3"> <p className="flex items-center gap-1.5 leading-tight font-semibold"> {user.name} {user.verified && ( <BadgeCheck className="size-4 shrink-0 text-muted-foreground" aria-label="Verified" role="img" /> )} </p> {user.handle && <p className="text-sm text-muted-foreground">@{user.handle}</p>} </motion.div> {user.bio && ( <motion.p variants={item} className="mt-3 text-sm leading-relaxed text-foreground/85"> {user.bio} </motion.p> )} {(user.location || user.joined) && ( <motion.div variants={item} className="mt-3 flex flex-wrap gap-x-4 gap-y-1 text-xs text-muted-foreground"> {user.location && ( <span className="inline-flex items-center gap-1"> <MapPin className="size-3.5" aria-hidden /> {user.location} </span> )} {user.joined && ( <span className="inline-flex items-center gap-1"> <CalendarDays className="size-3.5" aria-hidden /> {user.joined} </span> )} </motion.div> )} {user.stats?.length ? ( <motion.dl variants={item} className="mt-4 flex gap-5 border-t pt-3 text-sm"> {user.stats.map((stat) => ( <div key={stat.label} className="flex flex-col-reverse"> <dt className="text-xs text-muted-foreground">{stat.label}</dt> <dd className="font-semibold tabular-nums">{stat.value}</dd> </div> ))} </motion.dl> ) : null} </div> </motion.div> </HoverCardPrimitive.Content> </HoverCardPrimitive.Portal> )} </AnimatePresence> </HoverCard> ); } /** Up to two initials from a name: "Ada Kim" → "AK". */ function initialsOf(name: string) { const parts = name.trim().split(/\s+/).filter(Boolean); return ((parts[0]?.[0] ?? "") + (parts.length > 1 ? parts[parts.length - 1][0] : "")).toUpperCase(); } /** * A plain <img> over the user's initials. If the image is missing or fails, * the initials simply show through, with no layout shift. Decorative: the * name is always written next to it. */ export function ProfileAvatar({ name, src, className }: { name: string; src?: string; className?: string }) { const [failedSrc, setFailedSrc] = React.useState<string>(); const showImage = Boolean(src) && failedSrc !== src; return ( <span aria-hidden className={cn( "relative grid shrink-0 place-items-center overflow-hidden rounded-full bg-muted font-semibold text-foreground select-none", className )} > {initialsOf(name)} {showImage && ( // eslint-disable-next-line @next/next/no-img-element -- plain <img> keeps the block framework-agnostic. <img key={src} src={src} alt="" loading="lazy" decoding="async" referrerPolicy="no-referrer" onError={() => setFailedSrc(src)} className="absolute inset-0 size-full object-cover" /> )} </span> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| user | ProfileUser | — | { name, handle?, avatarUrl?, bio?, location?, joined?, verified?, stats?, cover? }. Required. |
| children | ReactElement | — | The trigger, usually a link to the profile. Required. |
| action | ReactNode | — | Top-right buttons, e.g. Follow. |
| open / defaultOpen / onOpenChange | boolean / boolean / (open: boolean) => void | — | Controlled or uncontrolled state. |
| openDelay / closeDelay | number | 400 / 200 | Hover delays in ms. |
| side / align / sideOffset | Radix content props | "bottom" / "start" / 8 | Placement next to the trigger. |
Link Preview
An inline link that previews its destination. The card rises out of the link, then leans toward the cursor as it moves along the text. The image shimmers with the DuskUI Skeleton until it loads, then fades in; with no image (or a broken one) it shows a plain panel with the site's initial. Pass Open Graph data you fetched on the server as preview.
import { LinkPreview } from "@/components/dusk/hover-card/link-preview";
export default function LinkPreviewDemo() {
return (
<p className="max-w-md text-sm leading-7 text-muted-foreground">
DuskUI builds on{" "}
<LinkPreview
href="https://ui.shadcn.com"
preview={{
title: "shadcn/ui",
description: "Beautifully designed components that you can copy and paste into your apps.",
image: "https://picsum.photos/seed/duskui-shadcn/600/314",
}}
>
shadcn/ui
</LinkPreview>{" "}
and animates everything with{" "}
<LinkPreview
href="https://motion.dev"
preview={{
title: "Motion — JavaScript & React animation library",
description: "A production-grade animation library for React, JavaScript and Vue.",
siteName: "motion.dev",
}}
>
Motion
</LinkPreview>
. Move your cursor along a link and the card follows.
</p>
);
}$ npx shadcn@latest add https://duskui.com/r/link-preview.json1. Install the dependencies.
npm install motion @radix-ui/react-hover-card lucide-react2. Copy the source into
components/dusk/hover-card/link-preview.tsx.link-preview.tsx "use client"; import * as React from "react"; import * as HoverCardPrimitive from "@radix-ui/react-hover-card"; import { AnimatePresence, motion, useMotionValue, useReducedMotion, useSpring } from "motion/react"; import { ArrowUpRight } from "lucide-react"; import { HoverCard, HoverCardTrigger } from "@/components/ui/hover-card"; import { Skeleton } from "@/components/ui/skeleton"; import { cn } from "@/lib/utils"; export interface LinkPreviewData { title: string; description?: string; /** Preview image (an Open Graph image works well, 1.91:1). */ image?: string; /** Shown above the title. Defaults to the link's hostname. */ siteName?: string; } export interface LinkPreviewProps extends Omit<React.ComponentProps<"a">, "children" | "href"> { href: string; /** The link text. */ children: React.ReactNode; /** What the card shows. Fetch Open Graph data on your server and pass it in. */ preview: LinkPreviewData; side?: "top" | "bottom"; /** Let the card drift toward the cursor as it moves along the link. */ followCursor?: boolean; open?: boolean; onOpenChange?: (open: boolean) => void; openDelay?: number; closeDelay?: number; /** Classes for the floating card. */ contentClassName?: string; } function hostnameOf(href: string) { try { return new URL(href).hostname.replace(/^www\./, ""); } catch { return href; } } /** * An inline link that previews its destination on hover. The card rises * out of the link, then drifts along with the * cursor as it moves across the text. The image shimmers (DuskUI Skeleton) * until it has loaded, then fades in; a broken image falls back to a plain * panel with the site name. The link stays a normal link: keyboard focus * opens the preview too, and the card is extra, never the only way to see * where the link goes. Static positioning and fades only under reduced motion. */ export function LinkPreview({ href, children, preview, side = "top", followCursor = true, open: openProp, onOpenChange, openDelay = 300, closeDelay, className, contentClassName, onPointerMove, ...props }: LinkPreviewProps) { const reduceMotion = useReducedMotion(); const [internalOpen, setInternalOpen] = React.useState(false); const open = openProp ?? internalOpen; const site = preview.siteName ?? hostnameOf(href); const offset = useMotionValue(0); const x = useSpring(offset, { stiffness: 220, damping: 26, mass: 0.6 }); const setOpen = (next: boolean) => { if (openProp === undefined) setInternalOpen(next); onOpenChange?.(next); if (next) offset.jump(0); }; return ( <HoverCard open={open} onOpenChange={setOpen} openDelay={openDelay} closeDelay={closeDelay}> <HoverCardTrigger asChild> <a href={href} className={cn( "rounded-sm font-medium text-primary outline-none", "bg-[linear-gradient(currentColor,currentColor),linear-gradient(color-mix(in_oklab,currentColor_30%,transparent),color-mix(in_oklab,currentColor_30%,transparent))]", "bg-size-[0%_1px,100%_1px] bg-position-[0_100%,0_100%] bg-no-repeat pb-px", "transition-[background-size] duration-300 ease-out hover:bg-size-[100%_1px,100%_1px] focus-visible:bg-size-[100%_1px,100%_1px]", "focus-visible:ring-[3px] focus-visible:ring-ring/50 motion-reduce:transition-none", className )} onPointerMove={(event) => { onPointerMove?.(event); if (!followCursor || reduceMotion) return; const rect = event.currentTarget.getBoundingClientRect(); // Follow at 40% of the distance from the link's center, so it leans rather than chases. offset.set((event.clientX - (rect.left + rect.width / 2)) * 0.4); }} {...props} > {children} </a> </HoverCardTrigger> <AnimatePresence> {open && ( <HoverCardPrimitive.Portal key="preview" forceMount> <HoverCardPrimitive.Content forceMount asChild side={side} align="center" sideOffset={10} collisionPadding={12}> <motion.div data-slot="link-preview" className="z-50 [perspective:800px]" style={{ x }} > <motion.a href={href} tabIndex={-1} aria-hidden className={cn( "block w-72 overflow-hidden rounded-xl border bg-popover p-2 text-popover-foreground outline-none", "origin-(--radix-hover-card-content-transform-origin)", "shadow-lg", contentClassName )} initial={ reduceMotion ? { opacity: 0 } : { opacity: 0, scale: 0.96, y: side === "top" ? 4 : -4 } } animate={{ opacity: 1, scale: 1, y: 0 }} exit={ reduceMotion ? { opacity: 0, transition: { duration: 0.1 } } : { opacity: 0, scale: 0.95, y: side === "top" ? 4 : -4, transition: { duration: 0.12, ease: "easeIn" } } } transition={{ duration: 0.18, ease: "easeOut" }} > <PreviewImage src={preview.image} site={site} /> <div className="px-1.5 pt-2.5 pb-1"> <p className="flex items-center gap-1 text-xs text-muted-foreground"> {site} <ArrowUpRight className="size-3" /> </p> <p className="mt-1 line-clamp-2 text-sm leading-snug font-semibold">{preview.title}</p> {preview.description && ( <p className="mt-1 line-clamp-2 text-xs leading-relaxed text-muted-foreground">{preview.description}</p> )} </div> </motion.a> </motion.div> </HoverCardPrimitive.Content> </HoverCardPrimitive.Portal> )} </AnimatePresence> </HoverCard> ); } /** The preview image: a shimmer until it loads, then a fade-in. Falls back to a plain panel. */ function PreviewImage({ src, site }: { src?: string; site: string }) { const [status, setStatus] = React.useState<"loading" | "loaded" | "error">(src ? "loading" : "error"); return ( <div className="relative aspect-[1.91/1] overflow-hidden rounded-lg bg-muted"> {status === "loading" && <Skeleton className="absolute inset-0 rounded-none" />} {status === "error" ? ( <div className="absolute inset-0 grid place-items-center"> <span className="text-2xl font-semibold tracking-tight text-muted-foreground">{site.charAt(0).toUpperCase()}</span> </div> ) : ( // eslint-disable-next-line @next/next/no-img-element -- plain <img> keeps the block framework-agnostic. <img src={src} alt="" decoding="async" referrerPolicy="no-referrer" onLoad={() => setStatus("loaded")} onError={() => setStatus("error")} className={cn( "absolute inset-0 size-full object-cover transition-opacity duration-300", status === "loaded" ? "opacity-100" : "opacity-0" )} /> )} </div> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| href | string | — | Link destination. Required. |
| children | ReactNode | — | The link text. Required. |
| preview | { title, description?, image?, siteName? } | — | What the card shows. siteName defaults to the hostname. Required. |
| side | "top" | "bottom" | "top" | Which side of the link the card opens on. |
| followCursor | boolean | true | Lean toward the cursor along the link. |
| open / onOpenChange | boolean / (open: boolean) => void | — | Controlled state. |
| openDelay / closeDelay | number | 300 / 200 | Hover delays in ms. |
| contentClassName | string | — | Classes for the card. |
API Reference
Same parts as shadcn/ui: HoverCard, HoverCardTrigger and HoverCardContent. Each forwards the Radix Hover Card props.
| Prop | Type | Default | Description |
|---|---|---|---|
| openDelay / closeDelay | number | 400 / 200 | Hover delays in ms (on HoverCard). |
| open / onOpenChange | boolean / (open: boolean) => void | — | Controlled state (on HoverCard). |
| side / align / sideOffset | Radix content props | "bottom" / "center" / 6 | Placement (on HoverCardContent). |
| showArrow | boolean | false | Draw an arrow pointing at the trigger (on HoverCardContent). |
Accessibility
- A hover card is a visual extra: screen readers don't announce it, so the trigger itself must make sense on its own (a real link with clear text).
- It also opens when the trigger gets keyboard focus and closes on Escape or blur, so keyboard users get the same preview.
- Don't put actions that exist only inside the card; the Follow button in the demo is a convenience, not the only way to follow.
- Under prefers-reduced-motion the cards only fade: no scale, stagger or cursor following.