useHover
Track mouse hover state on DOM elements with precise event detection. Perfect for React applications requiring hover interactions with Next.js integration and TypeScript support.
Installation
npx shadcn@latest add https://www.shadcn.io/registry/use-hover.json
npx shadcn@latest add https://www.shadcn.io/registry/use-hover.json
pnpm dlx shadcn@latest add https://www.shadcn.io/registry/use-hover.json
bunx shadcn@latest add https://www.shadcn.io/registry/use-hover.json
Features
- Precise hover detection with mouseenter and mouseleave event handling
- TypeScript support with generic element types and complete type definitions
- Ref-based targeting allowing hover tracking on any DOM element
- Performance optimized using event delegation through useEventListener
- shadcn/ui integration compatible with Tailwind CSS hover utilities
- Zero dependencies beyond the internal useEventListener hook
Examples
Multiple Element Tracking
Track hover states across multiple elements simultaneously with individual state management.
Use Cases
This free open source React component works well for:
- Interactive tooltips - Show/hide content on hover with Next.js and TypeScript
- Button states - Visual feedback for hover interactions using Tailwind CSS
- Image galleries - Preview functionality triggered by mouse hover
- Navigation menus - Dropdown and submenu visibility with shadcn/ui components
API Reference
useHover
Parameter | Type | Description |
---|---|---|
elementRef | RefObject<T> | required - Ref object for the target DOM element |
Return Value
Type | Description |
---|---|
boolean | Current hover state - true when hovering, false otherwise |
Type Parameters
Parameter | Type | Default | Description |
---|---|---|---|
T | extends HTMLElement | HTMLElement | DOM element type for TypeScript inference |
Implementation Notes
- Uses mouseenter/mouseleave for precise hover detection without bubbling
- Requires a valid ref object pointing to a DOM element
- Returns false immediately if ref is null or undefined
- Compatible with React.useRef and React.createRef patterns
- Integrates with useEventListener for optimal performance
useEventListener
React hook that attaches event listeners to DOM elements, the window, or media query lists with automatic cleanup and type safety.
useIntersectionObserver
Track element visibility and viewport intersection with configurable thresholds. Perfect for React applications requiring scroll-triggered animations with Next.js integration and TypeScript support.