-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathindex.tsx
127 lines (111 loc) · 4.02 KB
/
index.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
import React from 'react';
import {AnchorRef, PopoverContextProps, PopoverContextValue} from './types';
const PopoverContext = React.createContext<PopoverContextValue>({
onOpen: () => {},
popover: {},
close: () => {},
isOpen: false,
});
function PopoverContextProvider(props: PopoverContextProps) {
const [isOpen, setIsOpen] = React.useState(false);
const activePopoverRef = React.useRef<AnchorRef | null>(null);
const closePopover = React.useCallback((anchorRef?: React.RefObject<HTMLElement>) => {
if (!activePopoverRef.current || (anchorRef && anchorRef !== activePopoverRef.current.anchorRef)) {
return;
}
activePopoverRef.current.close();
if (activePopoverRef.current.onCloseCallback) {
activePopoverRef.current.onCloseCallback();
}
activePopoverRef.current = null;
setIsOpen(false);
}, []);
React.useEffect(() => {
const listener = (e: Event) => {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (activePopoverRef.current?.ref?.current?.contains(e.target as Node) || activePopoverRef.current?.anchorRef?.current?.contains(e.target as Node)) {
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: Event) => {
if (activePopoverRef.current?.ref?.current?.contains(e.target as Node)) {
return;
}
closePopover();
};
document.addEventListener('contextmenu', listener);
return () => {
document.removeEventListener('contextmenu', listener);
};
}, [closePopover]);
React.useEffect(() => {
const listener = (e: KeyboardEvent) => {
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: Event) => {
if (activePopoverRef.current?.ref?.current?.contains(e.target as Node)) {
return;
}
closePopover();
};
document.addEventListener('scroll', listener, true);
return () => {
document.removeEventListener('scroll', listener, true);
};
}, [closePopover]);
const onOpen = React.useCallback(
(popoverParams: AnchorRef) => {
if (activePopoverRef.current && activePopoverRef.current.ref !== popoverParams?.ref) {
closePopover(activePopoverRef.current.anchorRef);
}
activePopoverRef.current = popoverParams;
if (popoverParams?.onOpenCallback) {
popoverParams.onOpenCallback();
}
setIsOpen(true);
},
[closePopover],
);
const contextValue = React.useMemo(
() => ({
onOpen,
close: closePopover,
popover: activePopoverRef.current,
isOpen,
}),
[onOpen, closePopover, isOpen],
);
return <PopoverContext.Provider value={contextValue}>{props.children}</PopoverContext.Provider>;
}
PopoverContextProvider.displayName = 'PopoverContextProvider';
export default PopoverContextProvider;
export {PopoverContext};