hover-card.tsx
Overview
The hover-card.tsx file defines a React component wrapper around the Radix UI Hover Card primitive. It provides a customizable hover card UI element that displays additional content when a user hovers over a trigger element. This file enhances the base Radix Hover Card primitive by applying consistent styling, sensible defaults, and forwarding refs for better integration within React applications.
The hover card pattern is commonly used in UI designs to show contextual information, previews, or actions related to an item without requiring a click or navigation.
Components
1. HoverCard
Type:
React.ComponentDescription: The root container component for the hover card. It manages the hover state and coordinates trigger and content display.
Source: Directly re-exported from
HoverCardPrimitive.Root.Usage: Wraps the trigger and content components.
Example Usage
<HoverCard>
<HoverCardTrigger>Hover over me!</HoverCardTrigger>
<HoverCardContent>
This is the content shown on hover.
</HoverCardContent>
</HoverCard>
2. HoverCardTrigger
Type:
React.ComponentDescription: The element that the user interacts with (typically hovers over) to reveal the hover card content.
Source: Re-exported from
HoverCardPrimitive.Trigger.Usage: Should be nested inside
HoverCard.
Example Usage
<HoverCardTrigger>
<button>Hover here</button>
</HoverCardTrigger>
3. HoverCardContent
Type:
React.ForwardRefExoticComponentDescription: The content panel that appears when the trigger is hovered. This component is enhanced with custom styling and animations.
Props:
Inherits all props from
HoverCardPrimitive.Content.className?: string- Additional CSS classes to customize styling.align?: 'center' | 'start' | 'end'- Controls horizontal alignment relative to the trigger. Defaults to'center'.sideOffset?: number- Distance in pixels between the trigger and content. Defaults to4.
Ref: Forwards a React ref to the underlying DOM element for imperative access.
Implementation Details
Uses
React.forwardRefto support refs.Applies complex CSS classes for:
Styling (rounded borders, shadows, background and text colors).
Animations tied to Radix UI's internal state attributes (
data-[state=open],data-[side=bottom], etc).Responsive max width and overflow handling.
Uses a utility
cnfunction (likely a classnames merge utility) to combine default and user-provided classes.
Example Usage
<HoverCardContent align="start" sideOffset={8} className="custom-class">
<p>Additional hover details here.</p>
</HoverCardContent>
Important Implementation Details
The file exclusively composes and re-exports Radix UI Hover Card primitives with minimal additional logic.
Styling is tightly integrated with utility-first CSS classes (likely Tailwind CSS) for rapid UI development.
Animations leverage Radix's data attributes to trigger smooth fade, zoom, and slide effects based on hover card state and position.
React.forwardRefusage inHoverCardContentensures compatibility with parent components needing direct DOM access.
Interactions with Other Parts of the System
Imports
cnfrom@/lib/utils, a utility function for combining CSS class names conditionally.Utilizes Radix UI's
@radix-ui/react-hover-cardprimitives for accessible and robust hover card functionality.Intended to be used as part of a React component library or UI framework, providing a standardized hover card experience across the application.
The styling assumes design tokens or CSS variables like
bg-popoverandtext-popover-foregroundare defined globally for consistent theming.
Visual Diagram
componentDiagram
component HoverCard
component HoverCardTrigger
component HoverCardContent
HoverCard <|-- HoverCardTrigger : contains
HoverCard <|-- HoverCardContent : contains
note right of HoverCardContent
- forwards ref
- applies styling & animations
- customizable via props
end note
HoverCardTrigger ..> HoverCardContent : triggers display
Summary
hover-card.tsx acts as a thin, styled wrapper around Radix UI's Hover Card primitives, exposing three main components:
HoverCard(root container),HoverCardTrigger(hover target),HoverCardContent(hover panel with styling and animation).
By abstracting the complexity of Radix primitives and applying consistent UI styling, this file simplifies the integration of hover cards into React applications while maintaining accessibility and flexibility. It fits within a larger UI library ecosystem, leveraging utility CSS and design tokens for theming consistency.