"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2")); var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _portal = _interopRequireDefault(require("@rc-component/portal")); var _useLayoutEffect = _interopRequireDefault(require("rc-util/lib/hooks/useLayoutEffect")); var React = _interopRequireWildcard(require("react")); var _context = require("./context"); var _DrawerPopup = _interopRequireDefault(require("./DrawerPopup")); var _util = require("./util"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } var Drawer = function Drawer(props) { var _props$open = props.open, open = _props$open === void 0 ? false : _props$open, _props$prefixCls = props.prefixCls, prefixCls = _props$prefixCls === void 0 ? 'rc-drawer' : _props$prefixCls, _props$placement = props.placement, placement = _props$placement === void 0 ? 'right' : _props$placement, _props$autoFocus = props.autoFocus, autoFocus = _props$autoFocus === void 0 ? true : _props$autoFocus, _props$keyboard = props.keyboard, keyboard = _props$keyboard === void 0 ? true : _props$keyboard, _props$width = props.width, width = _props$width === void 0 ? 378 : _props$width, _props$mask = props.mask, mask = _props$mask === void 0 ? true : _props$mask, _props$maskClosable = props.maskClosable, maskClosable = _props$maskClosable === void 0 ? true : _props$maskClosable, getContainer = props.getContainer, forceRender = props.forceRender, afterOpenChange = props.afterOpenChange, destroyOnClose = props.destroyOnClose, onMouseEnter = props.onMouseEnter, onMouseOver = props.onMouseOver, onMouseLeave = props.onMouseLeave, onClick = props.onClick, onKeyDown = props.onKeyDown, onKeyUp = props.onKeyUp, panelRef = props.panelRef; var _React$useState = React.useState(false), _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2), animatedVisible = _React$useState2[0], setAnimatedVisible = _React$useState2[1]; // ============================= Warn ============================= if (process.env.NODE_ENV !== 'production') { (0, _util.warnCheck)(props); } // ============================= Open ============================= var _React$useState3 = React.useState(false), _React$useState4 = (0, _slicedToArray2.default)(_React$useState3, 2), mounted = _React$useState4[0], setMounted = _React$useState4[1]; (0, _useLayoutEffect.default)(function () { setMounted(true); }, []); var mergedOpen = mounted ? open : false; // ============================ Focus ============================= var popupRef = React.useRef(); var lastActiveRef = React.useRef(); (0, _useLayoutEffect.default)(function () { if (mergedOpen) { lastActiveRef.current = document.activeElement; } }, [mergedOpen]); // ============================= Open ============================= var internalAfterOpenChange = function internalAfterOpenChange(nextVisible) { var _popupRef$current; setAnimatedVisible(nextVisible); afterOpenChange === null || afterOpenChange === void 0 ? void 0 : afterOpenChange(nextVisible); if (!nextVisible && lastActiveRef.current && !((_popupRef$current = popupRef.current) !== null && _popupRef$current !== void 0 && _popupRef$current.contains(lastActiveRef.current))) { var _lastActiveRef$curren; (_lastActiveRef$curren = lastActiveRef.current) === null || _lastActiveRef$curren === void 0 ? void 0 : _lastActiveRef$curren.focus({ preventScroll: true }); } }; // =========================== Context ============================ var refContext = React.useMemo(function () { return { panel: panelRef }; }, [panelRef]); // ============================ Render ============================ if (!forceRender && !animatedVisible && !mergedOpen && destroyOnClose) { return null; } var eventHandlers = { onMouseEnter: onMouseEnter, onMouseOver: onMouseOver, onMouseLeave: onMouseLeave, onClick: onClick, onKeyDown: onKeyDown, onKeyUp: onKeyUp }; var drawerPopupProps = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, { open: mergedOpen, prefixCls: prefixCls, placement: placement, autoFocus: autoFocus, keyboard: keyboard, width: width, mask: mask, maskClosable: maskClosable, inline: getContainer === false, afterOpenChange: internalAfterOpenChange, ref: popupRef }, eventHandlers); return /*#__PURE__*/React.createElement(_context.RefContext.Provider, { value: refContext }, /*#__PURE__*/React.createElement(_portal.default, { open: mergedOpen || forceRender || animatedVisible, autoDestroy: false, getContainer: getContainer, autoLock: mask && (mergedOpen || animatedVisible) }, /*#__PURE__*/React.createElement(_DrawerPopup.default, drawerPopupProps))); }; if (process.env.NODE_ENV !== 'production') { Drawer.displayName = 'Drawer'; } var _default = Drawer; exports.default = _default;