-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathindex.js
134 lines (118 loc) · 4.11 KB
/
index.js
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
import React from 'react';
import PropTypes from 'prop-types';
const propTypes = {
children: PropTypes.node.isRequired,
};
const defaultProps = {};
const PopoverContext = React.createContext({
onOpen: () => {},
popover: {},
close: () => {},
isOpen: false,
});
function PopoverContextProvider(props) {
const [isOpen, setIsOpen] = React.useState(false);
const activePopoverRef = React.useRef(null);
const closePopover = React.useCallback((anchorRef) => {
if (!activePopoverRef.current || (anchorRef && anchorRef !== activePopoverRef.current.anchorRef)) {
return;
}
activePopoverRef.current.close();
activePopoverRef.current = null;
setIsOpen(false);
}, []);
React.useEffect(() => {
const listener = (e) => {
if (
!activePopoverRef.current ||
!activePopoverRef.current.ref ||
!activePopoverRef.current.ref.current ||
activePopoverRef.current.ref.current.contains(e.target) ||
(activePopoverRef.current.anchorRef && activePopoverRef.current.anchorRef.current && activePopoverRef.current.anchorRef.current.contains(e.target))
) {
return;
}
const ref = activePopoverRef.current.anchorRef;
closePopover(ref);
};
document.addEventListener('click', listener, true);
return () => {
document.removeEventListener('click', listener, true);
};
}, [closePopover]);
React.useEffect(() => {
const listener = (e) => {
if (!activePopoverRef.current || !activePopoverRef.current.ref || !activePopoverRef.current.ref.current || activePopoverRef.current.ref.current.contains(e.target)) {
return;
}
closePopover();
};
document.addEventListener('contextmenu', listener);
return () => {
document.removeEventListener('contextmenu', listener);
};
}, [closePopover]);
React.useEffect(() => {
const listener = (e) => {
if (e.key !== 'Escape') {
return;
}
closePopover();
};
document.addEventListener('keydown', listener, true);
return () => {
document.removeEventListener('keydown', listener, true);
};
}, [closePopover]);
React.useEffect(() => {
const listener = () => {
if (document.hasFocus()) {
return;
}
closePopover();
};
document.addEventListener('visibilitychange', listener);
return () => {
document.removeEventListener('visibilitychange', listener);
};
}, [closePopover]);
React.useEffect(() => {
const listener = (e) => {
if (activePopoverRef.current && activePopoverRef.current.ref && activePopoverRef.current.ref.current && activePopoverRef.current.ref.current.contains(e.target)) {
return;
}
closePopover();
};
document.addEventListener('scroll', listener, true);
return () => {
document.removeEventListener('scroll', listener, true);
};
}, [closePopover]);
const onOpen = React.useCallback(
(popoverParams) => {
if (activePopoverRef.current && activePopoverRef.current.ref !== popoverParams.ref) {
closePopover(activePopoverRef.current.anchorRef);
}
activePopoverRef.current = popoverParams;
setIsOpen(true);
},
[closePopover],
);
return (
<PopoverContext.Provider
value={{
onOpen,
close: closePopover,
popover: activePopoverRef.current,
isOpen,
}}
>
{props.children}
</PopoverContext.Provider>
);
}
PopoverContextProvider.defaultProps = defaultProps;
PopoverContextProvider.propTypes = propTypes;
PopoverContextProvider.displayName = 'PopoverContextProvider';
export default PopoverContextProvider;
export {PopoverContext};