-
Notifications
You must be signed in to change notification settings - Fork 0
/
useHover.ts
40 lines (32 loc) · 1.36 KB
/
useHover.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { useState, useEffect, MutableRefObject } from 'react';
/**
* Tracks whether the mouse pointer is hovering over a specified element.
* @param elementRef Ref object containing the target element.
* @returns Boolean value indicating whether the element is hovered.
*/
const useHover = (elementRef: MutableRefObject<HTMLElement | null>): boolean => {
const [isHovered, setIsHovered] = useState<boolean>(false);
const handleMouseEnter = () => setIsHovered(true);
const handleMouseLeave = () => setIsHovered(false);
useEffect(() => {
const element = elementRef.current;
if (!element) return;
element.addEventListener('mouseenter', handleMouseEnter);
element.addEventListener('mouseleave', handleMouseLeave);
return () => {
element.removeEventListener('mouseenter', handleMouseEnter);
element.removeEventListener('mouseleave', handleMouseLeave);
};
}, [elementRef]);
return isHovered;
};
export default useHover;
// Example usage
// const containerRef = useRef<HTMLDivElement>(null);
// Use the useHover hook to track whether the mouse is over the container
// const isHovered = useHover(containerRef);
// return (
// <div ref={containerRef} style={{ width: '200px', height: '200px', border: '1px solid black' }}>
// {isHovered ? 'Mouse is over the container!' : 'Mouse is not over the container!'}
// </div>
// );