index.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. "use client";
  2. var __rest = this && this.__rest || function (s, e) {
  3. var t = {};
  4. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  5. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  6. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  7. }
  8. return t;
  9. };
  10. import * as React from 'react';
  11. import classNames from 'classnames';
  12. import RcDrawer from 'rc-drawer';
  13. import { composeRef } from "rc-util/es/ref";
  14. import ContextIsolator from '../_util/ContextIsolator';
  15. import { useZIndex } from '../_util/hooks/useZIndex';
  16. import { getTransitionName } from '../_util/motion';
  17. import { devUseWarning } from '../_util/warning';
  18. import zIndexContext from '../_util/zindexContext';
  19. import { ConfigContext } from '../config-provider';
  20. import { useComponentConfig } from '../config-provider/context';
  21. import { usePanelRef } from '../watermark/context';
  22. import DrawerPanel from './DrawerPanel';
  23. import useStyle from './style';
  24. const _SizeTypes = ['default', 'large'];
  25. const defaultPushState = {
  26. distance: 180
  27. };
  28. const Drawer = props => {
  29. var _a;
  30. const {
  31. rootClassName,
  32. width,
  33. height,
  34. size = 'default',
  35. mask = true,
  36. push = defaultPushState,
  37. open,
  38. afterOpenChange,
  39. onClose,
  40. prefixCls: customizePrefixCls,
  41. getContainer: customizeGetContainer,
  42. panelRef = null,
  43. style,
  44. className,
  45. // Deprecated
  46. visible,
  47. afterVisibleChange,
  48. maskStyle,
  49. drawerStyle,
  50. contentWrapperStyle,
  51. destroyOnClose,
  52. destroyOnHidden
  53. } = props,
  54. rest = __rest(props, ["rootClassName", "width", "height", "size", "mask", "push", "open", "afterOpenChange", "onClose", "prefixCls", "getContainer", "panelRef", "style", "className", "visible", "afterVisibleChange", "maskStyle", "drawerStyle", "contentWrapperStyle", "destroyOnClose", "destroyOnHidden"]);
  55. const {
  56. getPopupContainer,
  57. getPrefixCls,
  58. direction,
  59. className: contextClassName,
  60. style: contextStyle,
  61. classNames: contextClassNames,
  62. styles: contextStyles
  63. } = useComponentConfig('drawer');
  64. const prefixCls = getPrefixCls('drawer', customizePrefixCls);
  65. const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
  66. const getContainer =
  67. // 有可能为 false,所以不能直接判断
  68. customizeGetContainer === undefined && getPopupContainer ? () => getPopupContainer(document.body) : customizeGetContainer;
  69. const drawerClassName = classNames({
  70. 'no-mask': !mask,
  71. [`${prefixCls}-rtl`]: direction === 'rtl'
  72. }, rootClassName, hashId, cssVarCls);
  73. // ========================== Warning ===========================
  74. if (process.env.NODE_ENV !== 'production') {
  75. const warning = devUseWarning('Drawer');
  76. [['visible', 'open'], ['afterVisibleChange', 'afterOpenChange'], ['headerStyle', 'styles.header'], ['bodyStyle', 'styles.body'], ['footerStyle', 'styles.footer'], ['contentWrapperStyle', 'styles.wrapper'], ['maskStyle', 'styles.mask'], ['drawerStyle', 'styles.content'], ['destroyInactivePanel', 'destroyOnHidden']].forEach(([deprecatedName, newName]) => {
  77. warning.deprecated(!(deprecatedName in props), deprecatedName, newName);
  78. });
  79. if (getContainer !== undefined && ((_a = props.style) === null || _a === void 0 ? void 0 : _a.position) === 'absolute') {
  80. process.env.NODE_ENV !== "production" ? warning(false, 'breaking', '`style` is replaced by `rootStyle` in v5. Please check that `position: absolute` is necessary.') : void 0;
  81. }
  82. }
  83. // ============================ Size ============================
  84. const mergedWidth = React.useMemo(() => width !== null && width !== void 0 ? width : size === 'large' ? 736 : 378, [width, size]);
  85. const mergedHeight = React.useMemo(() => height !== null && height !== void 0 ? height : size === 'large' ? 736 : 378, [height, size]);
  86. // =========================== Motion ===========================
  87. const maskMotion = {
  88. motionName: getTransitionName(prefixCls, 'mask-motion'),
  89. motionAppear: true,
  90. motionEnter: true,
  91. motionLeave: true,
  92. motionDeadline: 500
  93. };
  94. const panelMotion = motionPlacement => ({
  95. motionName: getTransitionName(prefixCls, `panel-motion-${motionPlacement}`),
  96. motionAppear: true,
  97. motionEnter: true,
  98. motionLeave: true,
  99. motionDeadline: 500
  100. });
  101. // ============================ Refs ============================
  102. // Select `ant-drawer-content` by `panelRef`
  103. const innerPanelRef = usePanelRef();
  104. const mergedPanelRef = composeRef(panelRef, innerPanelRef);
  105. // ============================ zIndex ============================
  106. const [zIndex, contextZIndex] = useZIndex('Drawer', rest.zIndex);
  107. // =========================== Render ===========================
  108. const {
  109. classNames: propClassNames = {},
  110. styles: propStyles = {}
  111. } = rest;
  112. return wrapCSSVar(/*#__PURE__*/React.createElement(ContextIsolator, {
  113. form: true,
  114. space: true
  115. }, /*#__PURE__*/React.createElement(zIndexContext.Provider, {
  116. value: contextZIndex
  117. }, /*#__PURE__*/React.createElement(RcDrawer, Object.assign({
  118. prefixCls: prefixCls,
  119. onClose: onClose,
  120. maskMotion: maskMotion,
  121. motion: panelMotion
  122. }, rest, {
  123. classNames: {
  124. mask: classNames(propClassNames.mask, contextClassNames.mask),
  125. content: classNames(propClassNames.content, contextClassNames.content),
  126. wrapper: classNames(propClassNames.wrapper, contextClassNames.wrapper)
  127. },
  128. styles: {
  129. mask: Object.assign(Object.assign(Object.assign({}, propStyles.mask), maskStyle), contextStyles.mask),
  130. content: Object.assign(Object.assign(Object.assign({}, propStyles.content), drawerStyle), contextStyles.content),
  131. wrapper: Object.assign(Object.assign(Object.assign({}, propStyles.wrapper), contentWrapperStyle), contextStyles.wrapper)
  132. },
  133. open: open !== null && open !== void 0 ? open : visible,
  134. mask: mask,
  135. push: push,
  136. width: mergedWidth,
  137. height: mergedHeight,
  138. style: Object.assign(Object.assign({}, contextStyle), style),
  139. className: classNames(contextClassName, className),
  140. rootClassName: drawerClassName,
  141. getContainer: getContainer,
  142. afterOpenChange: afterOpenChange !== null && afterOpenChange !== void 0 ? afterOpenChange : afterVisibleChange,
  143. panelRef: mergedPanelRef,
  144. zIndex: zIndex,
  145. // TODO: In the future, destroyOnClose in rc-drawer needs to be upgrade to destroyOnHidden
  146. destroyOnClose: destroyOnHidden !== null && destroyOnHidden !== void 0 ? destroyOnHidden : destroyOnClose
  147. }), /*#__PURE__*/React.createElement(DrawerPanel, Object.assign({
  148. prefixCls: prefixCls
  149. }, rest, {
  150. onClose: onClose
  151. }))))));
  152. };
  153. /** @private Internal Component. Do not use in your production. */
  154. const PurePanel = props => {
  155. const {
  156. prefixCls: customizePrefixCls,
  157. style,
  158. className,
  159. placement = 'right'
  160. } = props,
  161. restProps = __rest(props, ["prefixCls", "style", "className", "placement"]);
  162. const {
  163. getPrefixCls
  164. } = React.useContext(ConfigContext);
  165. const prefixCls = getPrefixCls('drawer', customizePrefixCls);
  166. const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
  167. const cls = classNames(prefixCls, `${prefixCls}-pure`, `${prefixCls}-${placement}`, hashId, cssVarCls, className);
  168. return wrapCSSVar(/*#__PURE__*/React.createElement("div", {
  169. className: cls,
  170. style: style
  171. }, /*#__PURE__*/React.createElement(DrawerPanel, Object.assign({
  172. prefixCls: prefixCls
  173. }, restProps))));
  174. };
  175. Drawer._InternalPanelDoNotUseOrYouWillBeFired = PurePanel;
  176. if (process.env.NODE_ENV !== 'production') {
  177. Drawer.displayName = 'Drawer';
  178. }
  179. export default Drawer;