-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
FloatingDelayGroup.tsx
167 lines (145 loc) · 3.99 KB
/
FloatingDelayGroup.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
156
157
158
159
160
161
162
163
164
165
166
167
import * as React from 'react';
import useModernLayoutEffect from 'use-isomorphic-layout-effect';
import {getDelay} from '../hooks/useHover';
import type {FloatingContext} from '../types';
type Delay = number | Partial<{open: number; close: number}>;
interface GroupState {
delay: Delay;
initialDelay: Delay;
currentId: any;
timeoutMs: number;
isInstantPhase: boolean;
}
interface GroupContext extends GroupState {
setCurrentId: React.Dispatch<React.SetStateAction<any>>;
setState: React.Dispatch<Partial<GroupState>>;
}
const FloatingDelayGroupContext = React.createContext<
GroupState & {
setCurrentId: (currentId: any) => void;
setState: React.Dispatch<Partial<GroupState>>;
}
>({
delay: 0,
initialDelay: 0,
timeoutMs: 0,
currentId: null,
setCurrentId: () => {},
setState: () => {},
isInstantPhase: false,
});
export const useDelayGroupContext = (): GroupContext =>
React.useContext(FloatingDelayGroupContext);
interface FloatingDelayGroupProps {
children?: React.ReactNode;
/**
* The delay to use for the group.
*/
delay: Delay;
/**
* An optional explicit timeout to use for the group, which represents when
* grouping logic will no longer be active after the close delay completes.
* This is useful if you want grouping to “last” longer than the close delay,
* for example if there is no close delay at all.
*/
timeoutMs?: number;
}
/**
* Provides context for a group of floating elements that should share a
* `delay`.
* @see https://floating-ui.com/docs/FloatingDelayGroup
*/
export const FloatingDelayGroup = (
props: FloatingDelayGroupProps,
): React.JSX.Element => {
const {children, delay, timeoutMs = 0} = props;
const [state, setState] = React.useReducer(
(prev: GroupState, next: Partial<GroupState>): GroupState => ({
...prev,
...next,
}),
{
delay,
timeoutMs,
initialDelay: delay,
currentId: null,
isInstantPhase: false,
},
);
const initialCurrentIdRef = React.useRef<any>(null);
const setCurrentId = React.useCallback((currentId: any) => {
setState({currentId});
}, []);
useModernLayoutEffect(() => {
if (state.currentId) {
if (initialCurrentIdRef.current === null) {
initialCurrentIdRef.current = state.currentId;
} else {
setState({isInstantPhase: true});
}
} else {
setState({isInstantPhase: false});
initialCurrentIdRef.current = null;
}
}, [state.currentId]);
return (
<FloatingDelayGroupContext.Provider
value={React.useMemo(
() => ({...state, setState, setCurrentId}),
[state, setCurrentId],
)}
>
{children}
</FloatingDelayGroupContext.Provider>
);
};
interface UseGroupOptions {
id: any;
}
/**
* Enables grouping when called inside a component that's a child of a
* `FloatingDelayGroup`.
* @see https://floating-ui.com/docs/FloatingDelayGroup
*/
export function useDelayGroup(
context: FloatingContext,
options: UseGroupOptions,
) {
const {open, onOpenChange} = context;
const {id} = options;
const {currentId, setCurrentId, initialDelay, setState, timeoutMs} =
useDelayGroupContext();
useModernLayoutEffect(() => {
if (currentId) {
setState({
delay: {
open: 1,
close: getDelay(initialDelay, 'close'),
},
});
if (currentId !== id) {
onOpenChange(false);
}
}
}, [id, onOpenChange, setState, currentId, initialDelay]);
useModernLayoutEffect(() => {
function unset() {
onOpenChange(false);
setState({delay: initialDelay, currentId: null});
}
if (!open && currentId === id) {
if (timeoutMs) {
const timeout = window.setTimeout(unset, timeoutMs);
return () => {
clearTimeout(timeout);
};
}
unset();
}
}, [open, setState, currentId, id, onOpenChange, initialDelay, timeoutMs]);
useModernLayoutEffect(() => {
if (open) {
setCurrentId(id);
}
}, [open, setCurrentId, id]);
}