PopupInner.js
6.01 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
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import * as React from 'react';
import { useRef, useState } from 'react';
import Align from 'rc-align';
import CSSMotion from 'rc-motion';
import classNames from 'classnames';
import useVisibleStatus from './useVisibleStatus';
import { getMotion } from '../utils/legacyUtil';
import useStretchStyle from './useStretchStyle';
var PopupInner = React.forwardRef(function (props, ref) {
var visible = props.visible,
prefixCls = props.prefixCls,
className = props.className,
style = props.style,
children = props.children,
zIndex = props.zIndex,
stretch = props.stretch,
destroyPopupOnHide = props.destroyPopupOnHide,
align = props.align,
point = props.point,
getRootDomNode = props.getRootDomNode,
getClassNameFromAlign = props.getClassNameFromAlign,
onAlign = props.onAlign,
onMouseEnter = props.onMouseEnter,
onMouseLeave = props.onMouseLeave,
onMouseDown = props.onMouseDown,
onTouchStart = props.onTouchStart;
var alignRef = useRef();
var elementRef = useRef();
var _useState = useState(),
_useState2 = _slicedToArray(_useState, 2),
alignedClassName = _useState2[0],
setAlignedClassName = _useState2[1]; // ======================= Measure ========================
var _useStretchStyle = useStretchStyle(stretch),
_useStretchStyle2 = _slicedToArray(_useStretchStyle, 2),
stretchStyle = _useStretchStyle2[0],
measureStretchStyle = _useStretchStyle2[1];
function doMeasure() {
if (stretch) {
measureStretchStyle(getRootDomNode());
}
} // ======================== Status ========================
var _useVisibleStatus = useVisibleStatus(visible, doMeasure),
_useVisibleStatus2 = _slicedToArray(_useVisibleStatus, 2),
status = _useVisibleStatus2[0],
goNextStatus = _useVisibleStatus2[1]; // ======================== Aligns ========================
var prepareResolveRef = useRef(); // `target` on `rc-align` can accept as a function to get the bind element or a point.
// ref: https://www.npmjs.com/package/rc-align
function getAlignTarget() {
if (point) {
return point;
}
return getRootDomNode;
}
function forceAlign() {
var _alignRef$current;
(_alignRef$current = alignRef.current) === null || _alignRef$current === void 0 ? void 0 : _alignRef$current.forceAlign();
}
function onInternalAlign(popupDomNode, matchAlign) {
if (status === 'align') {
var nextAlignedClassName = getClassNameFromAlign(matchAlign);
setAlignedClassName(nextAlignedClassName); // Repeat until not more align needed
if (alignedClassName !== nextAlignedClassName) {
Promise.resolve().then(function () {
forceAlign();
});
} else {
goNextStatus(function () {
var _prepareResolveRef$cu;
(_prepareResolveRef$cu = prepareResolveRef.current) === null || _prepareResolveRef$cu === void 0 ? void 0 : _prepareResolveRef$cu.call(prepareResolveRef);
});
}
onAlign === null || onAlign === void 0 ? void 0 : onAlign(popupDomNode, matchAlign);
}
} // ======================== Motion ========================
var motion = _objectSpread({}, getMotion(props));
['onAppearEnd', 'onEnterEnd', 'onLeaveEnd'].forEach(function (eventName) {
var originHandler = motion[eventName];
motion[eventName] = function (element, event) {
goNextStatus();
return originHandler === null || originHandler === void 0 ? void 0 : originHandler(element, event);
};
});
function onShowPrepare() {
return new Promise(function (resolve) {
prepareResolveRef.current = resolve;
});
} // Go to stable directly when motion not provided
React.useEffect(function () {
if (!motion.motionName && status === 'motion') {
goNextStatus();
}
}, [motion.motionName, status]); // ========================= Refs =========================
React.useImperativeHandle(ref, function () {
return {
forceAlign: forceAlign,
getElement: function getElement() {
return elementRef.current;
}
};
}); // ======================== Render ========================
var mergedStyle = _objectSpread(_objectSpread(_objectSpread({}, stretchStyle), {}, {
zIndex: zIndex
}, style), {}, {
opacity: status === 'motion' || status === 'stable' || !visible ? undefined : 0,
pointerEvents: status === 'stable' ? undefined : 'none'
}); // Align status
var alignDisabled = true;
if ((align === null || align === void 0 ? void 0 : align.points) && (status === 'align' || status === 'stable')) {
alignDisabled = false;
}
var childNode = children; // Wrapper when multiple children
if (React.Children.count(children) > 1) {
childNode = React.createElement("div", {
className: "".concat(prefixCls, "-content")
}, children);
}
return React.createElement(CSSMotion, Object.assign({
visible: visible,
ref: elementRef,
leavedClassName: "".concat(prefixCls, "-hidden")
}, motion, {
onAppearPrepare: onShowPrepare,
onEnterPrepare: onShowPrepare,
removeOnLeave: destroyPopupOnHide
}), function (_ref, motionRef) {
var motionClassName = _ref.className,
motionStyle = _ref.style;
var mergedClassName = classNames(prefixCls, className, alignedClassName, motionClassName);
return React.createElement(Align, {
target: getAlignTarget(),
key: "popup",
ref: alignRef,
monitorWindowResize: true,
disabled: alignDisabled,
align: align,
onAlign: onInternalAlign
}, React.createElement("div", {
ref: motionRef,
className: mergedClassName,
onMouseEnter: onMouseEnter,
onMouseLeave: onMouseLeave,
onMouseDown: onMouseDown,
onTouchStart: onTouchStart,
style: _objectSpread(_objectSpread({}, motionStyle), mergedStyle)
}, childNode));
});
});
PopupInner.displayName = 'PopupInner';
export default PopupInner;