-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathActiveHoverable.tsx
155 lines (127 loc) · 5.61 KB
/
ActiveHoverable.tsx
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import type {Ref} from 'react';
import {cloneElement, forwardRef, useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {DeviceEventEmitter} from 'react-native';
import mergeRefs from '@libs/mergeRefs';
import {getReturnValue} from '@libs/ValueUtils';
import CONST from '@src/CONST';
import type HoverableProps from './types';
type ActiveHoverableProps = Omit<HoverableProps, 'disabled'>;
type MouseEvents = 'onMouseEnter' | 'onMouseLeave' | 'onMouseMove' | 'onBlur';
type OnMouseEvents = Record<MouseEvents, (e: MouseEvent) => void>;
function ActiveHoverable({onHoverIn, onHoverOut, shouldHandleScroll, shouldFreezeCapture, children}: ActiveHoverableProps, outerRef: Ref<HTMLElement>) {
const [isHovered, setIsHovered] = useState(false);
const elementRef = useRef<HTMLElement | null>(null);
const isScrollingRef = useRef(false);
const isHoveredRef = useRef(false);
const isVisibiltyHidden = useRef(false);
const updateIsHovered = useCallback(
(hovered: boolean) => {
isHoveredRef.current = hovered;
// Nullish coalescing operator (`??`) wouldn't be appropriate here because
// it's not a matter of providing a default when encountering `null` or `undefined`
// but rather making a decision based on the truthy nature of the complete expressions.
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if ((shouldHandleScroll && isScrollingRef.current) || shouldFreezeCapture) {
return;
}
setIsHovered(hovered);
},
[shouldHandleScroll, shouldFreezeCapture],
);
useEffect(() => {
if (isHovered) {
onHoverIn?.();
} else {
onHoverOut?.();
}
}, [isHovered, onHoverIn, onHoverOut]);
useEffect(() => {
if (!shouldHandleScroll) {
return;
}
const scrollingListener = DeviceEventEmitter.addListener(CONST.EVENTS.SCROLLING, (scrolling: boolean) => {
isScrollingRef.current = scrolling;
if (!isScrollingRef.current) {
setIsHovered(isHoveredRef.current);
}
});
return () => scrollingListener.remove();
}, [shouldHandleScroll]);
useEffect(() => {
// Do not mount a listener if the component is not hovered
if (!isHovered) {
return;
}
/**
* Checks the hover state of a component and updates it based on the event target.
* This is necessary to handle cases where the hover state might get stuck due to an unreliable mouseleave trigger,
* such as when an element is removed before the mouseleave event is triggered.
* @param event The hover event object.
*/
const unsetHoveredIfOutside = (event: MouseEvent) => {
// We're also returning early if shouldFreezeCapture is true in order
// to not update the hover state but keep it frozen.
if (!elementRef.current || elementRef.current.contains(event.target as Node) || shouldFreezeCapture) {
return;
}
setIsHovered(false);
};
document.addEventListener('mouseover', unsetHoveredIfOutside);
return () => document.removeEventListener('mouseover', unsetHoveredIfOutside);
}, [isHovered, elementRef, shouldFreezeCapture]);
useEffect(() => {
const unsetHoveredWhenDocumentIsHidden = () => {
if (document.visibilityState !== 'hidden') {
return;
}
isVisibiltyHidden.current = true;
setIsHovered(false);
};
document.addEventListener('visibilitychange', unsetHoveredWhenDocumentIsHidden);
return () => document.removeEventListener('visibilitychange', unsetHoveredWhenDocumentIsHidden);
}, []);
const child = useMemo(() => getReturnValue(children, !isScrollingRef.current && isHovered), [children, isHovered]);
const {onMouseEnter, onMouseLeave, onMouseMove, onBlur} = child.props as OnMouseEvents;
const hoverAndForwardOnMouseEnter = useCallback(
(e: MouseEvent) => {
isVisibiltyHidden.current = false;
updateIsHovered(true);
onMouseEnter?.(e);
},
[updateIsHovered, onMouseEnter],
);
const unhoverAndForwardOnMouseLeave = useCallback(
(e: MouseEvent) => {
updateIsHovered(false);
onMouseLeave?.(e);
},
[updateIsHovered, onMouseLeave],
);
const unhoverAndForwardOnBlur = useCallback(
(event: MouseEvent) => {
// Check if the blur event occurred due to clicking outside the element
// and the wrapperView contains the element that caused the blur and reset isHovered
if (!elementRef.current?.contains(event.target as Node) && !elementRef.current?.contains(event.relatedTarget as Node)) {
setIsHovered(false);
}
onBlur?.(event);
},
[onBlur],
);
const handleAndForwardOnMouseMove = useCallback(
(e: MouseEvent) => {
isVisibiltyHidden.current = false;
updateIsHovered(true);
onMouseMove?.(e);
},
[updateIsHovered, onMouseMove],
);
return cloneElement(child, {
ref: mergeRefs(elementRef, outerRef, child.ref),
onMouseEnter: hoverAndForwardOnMouseEnter,
onMouseLeave: unhoverAndForwardOnMouseLeave,
onBlur: unhoverAndForwardOnBlur,
...(isVisibiltyHidden.current ? {onMouseMove: handleAndForwardOnMouseMove} : {}),
});
}
export default forwardRef(ActiveHoverable);