Hover Card for Astro
Astro link previews that show supporting information on hover or focus.
Installation
bunx --bun shadcn@latest add @gnostic/hover-cardnpx shadcn@latest add @gnostic/hover-cardpnpm dlx shadcn@latest add @gnostic/hover-cardInstall component packages
bun add @data-slot/core @data-slot/hover-cardnpm install @data-slot/core @data-slot/hover-cardpnpm add @data-slot/core @data-slot/hover-cardCopy the files
src/components/ui/hover-card/hover-card-content.astrosrc/components/ui/hover-card/hover-card-trigger.astrosrc/components/ui/hover-card/hover-card.astrosrc/components/ui/hover-card/index.tssrc/components/ui/hover-card/types.ts
Usage
---
import {
HoverCard,
HoverCardContent,
HoverCardTrigger,
} from "@/components/ui/hover-card"
---
<HoverCard>
<HoverCardTrigger>Hover</HoverCardTrigger>
<HoverCardContent>Preview content</HoverCardContent>
</HoverCard>
Composition
Arrange the parts as shown for a HoverCard:
HoverCard
├── HoverCardTrigger
└── HoverCardContent
Examples
Hover Card Sides
Control the position of the card with the side prop.
Hover Card in Dialog
Keep the default portal setting inside a dialog. The card moves to the page body
and appears above its parent dialog. The current data-slot position calculation
uses page coordinates, so portal={false} does not work inside a positioned dialog.
Open state
Set defaultOpen when the card should start open and continue to manage its own state. If you supply open, your script owns that state. Send hover-card:set to the root with { detail: { open: true } } to open it, or use false to close it.
API Reference
HoverCardProps
defaultOpen?boolean
Initial open state (uncontrolled mode only)
booleanopen?boolean
Controlled open state. Internal interactions do not mutate when set.
booleandelay?number
Delay before opening on hover/keyboard focus (ms).
number700skipDelayDuration?number
Duration to skip delay after closing (ms). Set to 0 to disable warm-up.
number300closeDelay?number
Delay before closing after leave/blur (ms).
number300side?HoverCardSide
The preferred side of the trigger to render against.
HoverCardSide"bottom"align?HoverCardAlign
The preferred alignment against the trigger.
HoverCardAlign"center"sideOffset?number
The distance in pixels from the trigger.
number4alignOffset?number
Offset in pixels from the alignment edge.
number0avoidCollisions?boolean
When true, flips/shifts content to avoid viewport collisions.
booleantruecollisionPadding?number
Viewport padding used when avoiding collisions.
number8portal?boolean
Portal content to body while open.
booleantruemountStrategy?MountStrategy
Detach closed content by default; eager keeps it connected and hidden.
MountStrategycloseOnClickOutside?boolean
Close when clicking outside.
booleantruecloseOnEscape?boolean
Close when pressing Escape.
booleantrueHoverCardContentProps
side?HoverCardSide
The preferred side of the trigger to render against.
HoverCardSide"bottom"align?HoverCardAlign
The preferred alignment against the trigger.
HoverCardAlign"center"sideOffset?number
The distance in pixels from the trigger.
number4alignOffset?number
Offset in pixels from the alignment edge.
number0avoidCollisions?boolean
When true, flips/shifts content to avoid viewport collisions.
booleantruecollisionPadding?number
Viewport padding used when avoiding collisions.
number8portal?boolean
Portal content to body while open.
booleantrueSee the data-slot docs for more information on interactivity.
Events
Dispatch these events on the hover card root. The root emits the outbound events.
Outbound events
| Event | Detail |
|---|---|
hover-card:change |
{ open: boolean, reason: "pointer" | "focus" | "blur" | "dismiss" | "api", trigger: HTMLElement, content: HTMLElement } |
Inbound events
| Event | Detail |
|---|---|
hover-card:set |
{ open: boolean } |
Read the installed source for the complete prop types.
When to use Hover Card
Use Hover Card for a brief preview of a destination. Keep the main link usable without opening the preview. Place its content and trigger under the same root.
Keyboard and behavior
The data-slot controller handles preview timing and positioning. Hover content must not be the only source of required information, especially on touch devices. Put essential actions on the destination page.
Theme and layout
The popup uses shared popover colors and a bounded width. Test long names and links near viewport edges. Avoid a large form inside a transient preview.