-
-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathindex.tsx
More file actions
220 lines (197 loc) · 6.05 KB
/
index.tsx
File metadata and controls
220 lines (197 loc) · 6.05 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
import * as React from 'react';
import Portal 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 FloatBg from './FloatBg';
import classNames from 'classnames';
import MotionContent from './MotionContent';
import { getAlignPopupClassName } from '../util';
export interface UniqueProviderProps {
children: React.ReactNode;
}
const UniqueProvider = ({ children }: UniqueProviderProps) => {
const [trigger, open, options, onTargetVisibleChanged] = useTargetState();
// =========================== 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 ==========================
const [popupId, setPopupId] = React.useState(0);
const delayInvoke = useDelay();
const show = useEvent((showOptions: UniqueShowOptions) => {
delayInvoke(() => {
if (showOptions.id !== options?.id) {
setPopupId((i) => i + 1);
}
trigger(showOptions);
}, showOptions.delay);
});
const hide = (delay: number) => {
delayInvoke(() => {
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);
});
// =========================== Align ============================
const [
ready,
offsetX,
offsetY,
offsetR,
offsetB,
arrowX,
arrowY, // scaleX - not used in UniqueProvider
,
,
// scaleY - not used in UniqueProvider
alignInfo,
onAlign,
] = useAlign(
open,
popupEle,
options?.target,
options?.popupPlacement,
options?.builtinPlacements || {},
options?.popupAlign,
undefined, // onPopupAlign
false, // isMobile
);
const alignedClassName = React.useMemo(() => {
if (!options) {
return '';
}
const baseClassName = getAlignPopupClassName(
options.builtinPlacements || {},
options.prefixCls || '',
alignInfo,
false, // alignPoint is false for UniqueProvider
);
return classNames(baseClassName, options.getPopupClassNameFromAlign?.(alignInfo));
}, [
alignInfo,
options?.getPopupClassNameFromAlign,
options?.builtinPlacements,
options?.prefixCls,
]);
const contextValue = React.useMemo<UniqueContextProps>(
() => ({
show,
hide,
}),
[],
);
// =========================== 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 = options?.prefixCls;
return (
<UniqueContext.Provider value={contextValue}>
{children}
{options && (
<TriggerContext.Provider value={triggerContextValue}>
<Popup
ref={setPopupRef}
portal={Portal}
prefixCls={prefixCls}
popup={
<MotionContent prefixCls={prefixCls} key={popupId}>
{options.popup}
</MotionContent>
}
className={classNames(
options.popupClassName,
alignedClassName,
`${prefixCls}-unique-controlled`,
)}
style={options.popupStyle}
target={options.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={options.zIndex}
mask={options.mask}
arrow={options.arrow}
motion={options.popupMotion}
maskMotion={options.maskMotion}
// getPopupContainer={options.getPopupContainer}
>
<FloatBg
prefixCls={prefixCls}
isMobile={false}
ready={ready}
open={open}
align={alignInfo}
offsetR={offsetR}
offsetB={offsetB}
offsetX={offsetX}
offsetY={offsetY}
popupSize={popupSize}
motion={options.popupMotion}
/>
</Popup>
</TriggerContext.Provider>
)}
</UniqueContext.Provider>
);
};
export default UniqueProvider;