forked from react-component/trigger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
265 lines (236 loc) · 7.5 KB
/
index.tsx
File metadata and controls
265 lines (236 loc) · 7.5 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import * as React from 'react';
import Portal, { type PortalProps } from '@rc-component/portal';
import TriggerContext, {
UniqueContext,
type UniqueContextProps,
type TriggerContextProps,
type UniqueShowOptions,
} from '../context';
import useDelay from '../hooks/useDelay';
import useAlign from '../hooks/useAlign';
import Popup from '../Popup';
import { useEvent } from '@rc-component/util';
import useTargetState from './useTargetState';
import { isDOM } from '@rc-component/util/lib/Dom/findDOMNode';
import UniqueContainer from './UniqueContainer';
import { clsx } from 'clsx';
import { getAlignPopupClassName } from '../util';
export interface UniqueProviderProps {
children: React.ReactNode;
onKeyDown?: (event: KeyboardEvent) => void;
/** Additional handle options data to do the customize info */
postTriggerProps?: (options: UniqueShowOptions) => UniqueShowOptions;
}
const UniqueProvider = ({
children,
postTriggerProps,
onKeyDown,
}: UniqueProviderProps) => {
const [trigger, open, options, onTargetVisibleChanged] = useTargetState();
// ========================== Options ===========================
const mergedOptions = React.useMemo(() => {
if (!options || !postTriggerProps) {
return options;
}
return postTriggerProps(options);
}, [options, postTriggerProps]);
// =========================== Popup ============================
const [popupEle, setPopupEle] = React.useState<HTMLDivElement>(null);
const [popupSize, setPopupSize] = React.useState<{
width: number;
height: number;
}>(null);
// Used for forwardRef popup. Not use internal
const externalPopupRef = React.useRef<HTMLDivElement>(null);
const setPopupRef = useEvent((node: HTMLDivElement) => {
externalPopupRef.current = node;
if (isDOM(node) && popupEle !== node) {
setPopupEle(node);
}
});
// ========================== Register ==========================
// Store the isOpen function from the latest show call
const isOpenRef = React.useRef<(() => boolean) | null>(null);
const delayInvoke = useDelay();
const show = useEvent(
(showOptions: UniqueShowOptions, isOpen: () => boolean) => {
// Store the isOpen function for later use in hide
isOpenRef.current = isOpen;
delayInvoke(() => {
trigger(showOptions);
}, showOptions.delay);
},
);
const hide = (delay: number) => {
delayInvoke(() => {
// Check if we should still hide by calling the isOpen function
// If isOpen returns true, it means another trigger wants to keep it open
if (isOpenRef.current?.()) {
return; // Don't hide if something else wants it open
}
trigger(false);
// Don't clear target, currentNode, options immediately, wait until animation completes
}, delay);
};
// Callback after animation completes
const onVisibleChanged = useEvent((visible: boolean) => {
// Call useTargetState callback to handle animation state
onTargetVisibleChanged(visible);
});
const onEsc: PortalProps['onEsc'] = ({ top, event }) => {
if (top) {
trigger(false);
}
onKeyDown?.(event);
};
// =========================== Align ============================
const [
ready,
offsetX,
offsetY,
offsetR,
offsetB,
arrowX,
arrowY, // scaleX - not used in UniqueProvider
,
,
// scaleY - not used in UniqueProvider
alignInfo,
onAlign,
] = useAlign(
open,
popupEle,
mergedOptions?.target,
mergedOptions?.popupPlacement,
mergedOptions?.builtinPlacements || {},
mergedOptions?.popupAlign,
undefined, // onPopupAlign
false, // isMobile
);
const alignedClassName = React.useMemo(() => {
if (!mergedOptions) {
return '';
}
const baseClassName = getAlignPopupClassName(
mergedOptions.builtinPlacements || {},
mergedOptions.prefixCls || '',
alignInfo,
false, // alignPoint is false for UniqueProvider
);
return clsx(
baseClassName,
mergedOptions.getPopupClassNameFromAlign?.(alignInfo),
);
}, [
alignInfo,
mergedOptions?.getPopupClassNameFromAlign,
mergedOptions?.builtinPlacements,
mergedOptions?.prefixCls,
]);
const contextValue = React.useMemo<UniqueContextProps>(
() => ({
show,
hide,
}),
[],
);
// =========================== Align ============================
React.useEffect(() => {
onAlign();
}, [mergedOptions?.target]);
// =========================== Motion ===========================
const onPrepare = useEvent(() => {
onAlign();
return Promise.resolve();
});
// ======================== Trigger Context =====================
const subPopupElements = React.useRef<Record<string, HTMLElement>>({});
const parentContext = React.useContext(TriggerContext);
const triggerContextValue = React.useMemo<TriggerContextProps>(
() => ({
registerSubPopup: (id, subPopupEle) => {
subPopupElements.current[id] = subPopupEle;
parentContext?.registerSubPopup(id, subPopupEle);
},
}),
[parentContext],
);
// =========================== Render ===========================
const prefixCls = mergedOptions?.prefixCls;
return (
<UniqueContext.Provider value={contextValue}>
{children}
{mergedOptions && (
<TriggerContext.Provider value={triggerContextValue}>
<Popup
ref={setPopupRef}
portal={Portal}
onEsc={onEsc}
prefixCls={prefixCls}
popup={mergedOptions.popup}
className={clsx(
mergedOptions.popupClassName,
alignedClassName,
`${prefixCls}-unique-controlled`,
)}
style={mergedOptions.popupStyle}
target={mergedOptions.target}
open={open}
keepDom={true}
fresh={true}
autoDestroy={false}
onVisibleChanged={onVisibleChanged}
ready={ready}
offsetX={offsetX}
offsetY={offsetY}
offsetR={offsetR}
offsetB={offsetB}
onAlign={onAlign}
onPrepare={onPrepare}
onResize={(size) =>
setPopupSize({
width: size.offsetWidth,
height: size.offsetHeight,
})
}
arrowPos={{
x: arrowX,
y: arrowY,
}}
align={alignInfo}
zIndex={mergedOptions.zIndex}
mask={mergedOptions.mask}
arrow={mergedOptions.arrow}
motion={mergedOptions.popupMotion}
maskMotion={mergedOptions.maskMotion}
getPopupContainer={mergedOptions.getPopupContainer}
>
<UniqueContainer
prefixCls={prefixCls}
isMobile={false}
ready={ready}
open={open}
align={alignInfo}
offsetR={offsetR}
offsetB={offsetB}
offsetX={offsetX}
offsetY={offsetY}
arrowPos={{
x: arrowX,
y: arrowY,
}}
popupSize={popupSize}
motion={mergedOptions.popupMotion}
uniqueContainerClassName={clsx(
mergedOptions.uniqueContainerClassName,
alignedClassName,
)}
uniqueContainerStyle={mergedOptions.uniqueContainerStyle}
/>
</Popup>
</TriggerContext.Provider>
)}
</UniqueContext.Provider>
);
};
export default UniqueProvider;