1 | import { useEffect, useState } from 'react';
|
---|
2 | import { off, on } from './misc/util';
|
---|
3 | // kudos: https://usehooks.com/
|
---|
4 | var useHoverDirty = function (ref, enabled) {
|
---|
5 | if (enabled === void 0) { enabled = true; }
|
---|
6 | if (process.env.NODE_ENV === 'development') {
|
---|
7 | if (typeof ref !== 'object' || typeof ref.current === 'undefined') {
|
---|
8 | console.error('useHoverDirty expects a single ref argument.');
|
---|
9 | }
|
---|
10 | }
|
---|
11 | var _a = useState(false), value = _a[0], setValue = _a[1];
|
---|
12 | useEffect(function () {
|
---|
13 | var onMouseOver = function () { return setValue(true); };
|
---|
14 | var onMouseOut = function () { return setValue(false); };
|
---|
15 | if (enabled && ref && ref.current) {
|
---|
16 | on(ref.current, 'mouseover', onMouseOver);
|
---|
17 | on(ref.current, 'mouseout', onMouseOut);
|
---|
18 | }
|
---|
19 | // fixes react-hooks/exhaustive-deps warning about stale ref elements
|
---|
20 | var current = ref.current;
|
---|
21 | return function () {
|
---|
22 | if (enabled && current) {
|
---|
23 | off(current, 'mouseover', onMouseOver);
|
---|
24 | off(current, 'mouseout', onMouseOut);
|
---|
25 | }
|
---|
26 | };
|
---|
27 | }, [enabled, ref]);
|
---|
28 | return value;
|
---|
29 | };
|
---|
30 | export default useHoverDirty;
|
---|