import _extends from "@babel/runtime/helpers/esm/extends"; 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 = /*#__PURE__*/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, forceRender = props.forceRender, 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) { var nextAlignedClassName = getClassNameFromAlign(matchAlign); if (alignedClassName !== nextAlignedClassName) { setAlignedClassName(nextAlignedClassName); } if (status === 'align') { // 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({}, stretchStyle), {}, { zIndex: zIndex, opacity: status === 'motion' || status === 'stable' || !visible ? undefined : 0, pointerEvents: status === 'stable' ? undefined : 'none' }, style); // 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 = /*#__PURE__*/React.createElement("div", { className: "".concat(prefixCls, "-content") }, children); } return /*#__PURE__*/React.createElement(CSSMotion, _extends({ visible: visible, ref: elementRef, leavedClassName: "".concat(prefixCls, "-hidden") }, motion, { onAppearPrepare: onShowPrepare, onEnterPrepare: onShowPrepare, removeOnLeave: destroyPopupOnHide, forceRender: forceRender }), function (_ref, motionRef) { var motionClassName = _ref.className, motionStyle = _ref.style; var mergedClassName = classNames(prefixCls, className, alignedClassName, motionClassName); return /*#__PURE__*/React.createElement(Align, { target: getAlignTarget(), key: "popup", ref: alignRef, monitorWindowResize: true, disabled: alignDisabled, align: align, onAlign: onInternalAlign }, /*#__PURE__*/React.createElement("div", { ref: motionRef, className: mergedClassName, onMouseEnter: onMouseEnter, onMouseLeave: onMouseLeave, onMouseDownCapture: onMouseDown, onTouchStartCapture: onTouchStart, style: _objectSpread(_objectSpread({}, motionStyle), mergedStyle) }, childNode)); }); }); PopupInner.displayName = 'PopupInner'; export default PopupInner;