Join our Discord Community

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.

Loading component...

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

Loading component...

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

ParameterTypeDescription
elementRefRefObject<T>required - Ref object for the target DOM element

Return Value

TypeDescription
booleanCurrent hover state - true when hovering, false otherwise

Type Parameters

ParameterTypeDefaultDescription
Textends HTMLElementHTMLElementDOM 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