lbry-desktop/ui/effects/use-hover.js

24 lines
658 B
JavaScript
Raw Permalink Normal View History

import { useEffect, useState } from 'react';
export default function useHover(ref) {
const [isHovering, setIsHovering] = useState(false);
2020-04-23 18:31:42 +02:00
const hoverFunc = () => setIsHovering(true);
const unHoverFunc = () => setIsHovering(false);
2020-04-23 18:31:42 +02:00
useEffect(() => {
const refElement = ref.current;
if (refElement) {
2020-04-23 18:31:42 +02:00
refElement.addEventListener('mouseenter', hoverFunc);
refElement.addEventListener('mouseleave', unHoverFunc);
return () => {
2020-04-23 18:31:42 +02:00
refElement.removeEventListener('mouseenter', hoverFunc);
refElement.removeEventListener('mouseleave', unHoverFunc);
};
}
}, [ref, isHovering]);
return isHovering;
}