UNPKG

8.63 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
4
5var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.default = void 0;
11
12var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
13
14var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
15
16var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
17
18var React = _interopRequireWildcard(require("react"));
19
20var _rcAlign = _interopRequireDefault(require("rc-align"));
21
22var _useLayoutEffect = _interopRequireDefault(require("rc-util/lib/hooks/useLayoutEffect"));
23
24var _rcMotion = _interopRequireDefault(require("rc-motion"));
25
26var _classnames = _interopRequireDefault(require("classnames"));
27
28var _useVisibleStatus3 = _interopRequireDefault(require("./useVisibleStatus"));
29
30var _legacyUtil = require("../utils/legacyUtil");
31
32var _useStretchStyle3 = _interopRequireDefault(require("./useStretchStyle"));
33
34var PopupInner = /*#__PURE__*/React.forwardRef(function (props, ref) {
35 var visible = props.visible,
36 prefixCls = props.prefixCls,
37 className = props.className,
38 style = props.style,
39 children = props.children,
40 zIndex = props.zIndex,
41 stretch = props.stretch,
42 destroyPopupOnHide = props.destroyPopupOnHide,
43 forceRender = props.forceRender,
44 align = props.align,
45 point = props.point,
46 getRootDomNode = props.getRootDomNode,
47 getClassNameFromAlign = props.getClassNameFromAlign,
48 onAlign = props.onAlign,
49 onMouseEnter = props.onMouseEnter,
50 onMouseLeave = props.onMouseLeave,
51 onMouseDown = props.onMouseDown,
52 onTouchStart = props.onTouchStart,
53 onClick = props.onClick;
54 var alignRef = (0, React.useRef)();
55 var elementRef = (0, React.useRef)();
56
57 var _useState = (0, React.useState)(),
58 _useState2 = (0, _slicedToArray2.default)(_useState, 2),
59 alignedClassName = _useState2[0],
60 setAlignedClassName = _useState2[1]; // ======================= Measure ========================
61
62
63 var _useStretchStyle = (0, _useStretchStyle3.default)(stretch),
64 _useStretchStyle2 = (0, _slicedToArray2.default)(_useStretchStyle, 2),
65 stretchStyle = _useStretchStyle2[0],
66 measureStretchStyle = _useStretchStyle2[1];
67
68 function doMeasure() {
69 if (stretch) {
70 measureStretchStyle(getRootDomNode());
71 }
72 } // ======================== Status ========================
73
74
75 var _useVisibleStatus = (0, _useVisibleStatus3.default)(visible, doMeasure),
76 _useVisibleStatus2 = (0, _slicedToArray2.default)(_useVisibleStatus, 2),
77 status = _useVisibleStatus2[0],
78 goNextStatus = _useVisibleStatus2[1]; // ======================== Aligns ========================
79
80 /**
81 * `alignedClassName` may modify `source` size,
82 * which means one time align may not move to the correct position at once.
83 *
84 * We will reset `alignTimes` for each status switch to `alignPre`
85 * and let `rc-align` to align for multiple times to ensure get final stable place.
86 * Currently we mark `alignTimes < 2` repeat align, it will increase if user report for align issue.
87 *
88 * Update:
89 * In React 18. `rc-align` effect of align may faster than ref called trigger `forceAlign`.
90 * We adjust this to `alignTimes < 2`.
91 * We need refactor `rc-align` to support mark of `forceAlign` call if this still happen.
92 */
93
94
95 var _useState3 = (0, React.useState)(0),
96 _useState4 = (0, _slicedToArray2.default)(_useState3, 2),
97 alignTimes = _useState4[0],
98 setAlignTimes = _useState4[1];
99
100 var prepareResolveRef = (0, React.useRef)();
101 (0, _useLayoutEffect.default)(function () {
102 if (status === 'alignPre') {
103 setAlignTimes(0);
104 }
105 }, [status]); // `target` on `rc-align` can accept as a function to get the bind element or a point.
106 // ref: https://www.npmjs.com/package/rc-align
107
108 function getAlignTarget() {
109 if (point) {
110 return point;
111 }
112
113 return getRootDomNode;
114 }
115
116 function forceAlign() {
117 var _alignRef$current;
118
119 (_alignRef$current = alignRef.current) === null || _alignRef$current === void 0 ? void 0 : _alignRef$current.forceAlign();
120 }
121
122 function onInternalAlign(popupDomNode, matchAlign) {
123 var nextAlignedClassName = getClassNameFromAlign(matchAlign);
124
125 if (alignedClassName !== nextAlignedClassName) {
126 setAlignedClassName(nextAlignedClassName);
127 } // We will retry multi times to make sure that the element has been align in the right position.
128
129
130 setAlignTimes(function (val) {
131 return val + 1;
132 });
133
134 if (status === 'align') {
135 onAlign === null || onAlign === void 0 ? void 0 : onAlign(popupDomNode, matchAlign);
136 }
137 } // Delay to go to next status
138
139
140 (0, _useLayoutEffect.default)(function () {
141 if (status === 'align') {
142 // Repeat until not more align needed
143 if (alignTimes < 3) {
144 forceAlign();
145 } else {
146 goNextStatus(function () {
147 var _prepareResolveRef$cu;
148
149 (_prepareResolveRef$cu = prepareResolveRef.current) === null || _prepareResolveRef$cu === void 0 ? void 0 : _prepareResolveRef$cu.call(prepareResolveRef);
150 });
151 }
152 }
153 }, [alignTimes]); // ======================== Motion ========================
154
155 var motion = (0, _objectSpread2.default)({}, (0, _legacyUtil.getMotion)(props));
156 ['onAppearEnd', 'onEnterEnd', 'onLeaveEnd'].forEach(function (eventName) {
157 var originHandler = motion[eventName];
158
159 motion[eventName] = function (element, event) {
160 goNextStatus();
161 return originHandler === null || originHandler === void 0 ? void 0 : originHandler(element, event);
162 };
163 });
164
165 function onShowPrepare() {
166 return new Promise(function (resolve) {
167 prepareResolveRef.current = resolve;
168 });
169 } // Go to stable directly when motion not provided
170
171
172 React.useEffect(function () {
173 if (!motion.motionName && status === 'motion') {
174 goNextStatus();
175 }
176 }, [motion.motionName, status]); // ========================= Refs =========================
177
178 React.useImperativeHandle(ref, function () {
179 return {
180 forceAlign: forceAlign,
181 getElement: function getElement() {
182 return elementRef.current;
183 }
184 };
185 }); // ======================== Render ========================
186
187 var mergedStyle = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, stretchStyle), {}, {
188 zIndex: zIndex,
189 opacity: status === 'motion' || status === 'stable' || !visible ? undefined : 0,
190 // Cannot interact with disappearing elements
191 // https://github.com/ant-design/ant-design/issues/35051#issuecomment-1101340714
192 pointerEvents: !visible && status !== 'stable' ? 'none' : undefined
193 }, style); // Align status
194
195 var alignDisabled = true;
196
197 if (align !== null && align !== void 0 && align.points && (status === 'align' || status === 'stable')) {
198 alignDisabled = false;
199 }
200
201 var childNode = children; // Wrapper when multiple children
202
203 if (React.Children.count(children) > 1) {
204 childNode = /*#__PURE__*/React.createElement("div", {
205 className: "".concat(prefixCls, "-content")
206 }, children);
207 }
208
209 return /*#__PURE__*/React.createElement(_rcMotion.default, (0, _extends2.default)({
210 visible: visible,
211 ref: elementRef,
212 leavedClassName: "".concat(prefixCls, "-hidden")
213 }, motion, {
214 onAppearPrepare: onShowPrepare,
215 onEnterPrepare: onShowPrepare,
216 removeOnLeave: destroyPopupOnHide,
217 forceRender: forceRender
218 }), function (_ref, motionRef) {
219 var motionClassName = _ref.className,
220 motionStyle = _ref.style;
221 var mergedClassName = (0, _classnames.default)(prefixCls, className, alignedClassName, motionClassName);
222 return /*#__PURE__*/React.createElement(_rcAlign.default, {
223 target: getAlignTarget(),
224 key: "popup",
225 ref: alignRef,
226 monitorWindowResize: true,
227 disabled: alignDisabled,
228 align: align,
229 onAlign: onInternalAlign
230 }, /*#__PURE__*/React.createElement("div", {
231 ref: motionRef,
232 className: mergedClassName,
233 onMouseEnter: onMouseEnter,
234 onMouseLeave: onMouseLeave,
235 onMouseDownCapture: onMouseDown,
236 onTouchStartCapture: onTouchStart,
237 onClick: onClick,
238 style: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, motionStyle), mergedStyle)
239 }, childNode));
240 });
241});
242PopupInner.displayName = 'PopupInner';
243var _default = PopupInner;
244exports.default = _default;
\No newline at end of file