useNotification.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. "use strict";
  2. "use client";
  3. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  4. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
  5. Object.defineProperty(exports, "__esModule", {
  6. value: true
  7. });
  8. exports.default = useNotification;
  9. exports.useInternalNotification = useInternalNotification;
  10. var _react = _interopRequireWildcard(require("react"));
  11. var _classnames = _interopRequireDefault(require("classnames"));
  12. var _rcNotification = require("rc-notification");
  13. var _warning = require("../_util/warning");
  14. var _configProvider = require("../config-provider");
  15. var _useCSSVarCls = _interopRequireDefault(require("../config-provider/hooks/useCSSVarCls"));
  16. var _internal = require("../theme/internal");
  17. var _PurePanel = require("./PurePanel");
  18. var _style = _interopRequireDefault(require("./style"));
  19. var _util = require("./util");
  20. var __rest = void 0 && (void 0).__rest || function (s, e) {
  21. var t = {};
  22. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  23. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  24. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  25. }
  26. return t;
  27. };
  28. const DEFAULT_OFFSET = 24;
  29. const DEFAULT_DURATION = 4.5;
  30. const DEFAULT_PLACEMENT = 'topRight';
  31. const Wrapper = ({
  32. children,
  33. prefixCls
  34. }) => {
  35. const rootCls = (0, _useCSSVarCls.default)(prefixCls);
  36. const [wrapCSSVar, hashId, cssVarCls] = (0, _style.default)(prefixCls, rootCls);
  37. return wrapCSSVar(/*#__PURE__*/_react.default.createElement(_rcNotification.NotificationProvider, {
  38. classNames: {
  39. list: (0, _classnames.default)(hashId, cssVarCls, rootCls)
  40. }
  41. }, children));
  42. };
  43. const renderNotifications = (node, {
  44. prefixCls,
  45. key
  46. }) => (/*#__PURE__*/_react.default.createElement(Wrapper, {
  47. prefixCls: prefixCls,
  48. key: key
  49. }, node));
  50. const Holder = /*#__PURE__*/_react.default.forwardRef((props, ref) => {
  51. const {
  52. top,
  53. bottom,
  54. prefixCls: staticPrefixCls,
  55. getContainer: staticGetContainer,
  56. maxCount,
  57. rtl,
  58. onAllRemoved,
  59. stack,
  60. duration,
  61. pauseOnHover = true,
  62. showProgress
  63. } = props;
  64. const {
  65. getPrefixCls,
  66. getPopupContainer,
  67. notification,
  68. direction
  69. } = (0, _react.useContext)(_configProvider.ConfigContext);
  70. const [, token] = (0, _internal.useToken)();
  71. const prefixCls = staticPrefixCls || getPrefixCls('notification');
  72. // =============================== Style ===============================
  73. const getStyle = placement => (0, _util.getPlacementStyle)(placement, top !== null && top !== void 0 ? top : DEFAULT_OFFSET, bottom !== null && bottom !== void 0 ? bottom : DEFAULT_OFFSET);
  74. const getClassName = () => (0, _classnames.default)({
  75. [`${prefixCls}-rtl`]: rtl !== null && rtl !== void 0 ? rtl : direction === 'rtl'
  76. });
  77. // ============================== Motion ===============================
  78. const getNotificationMotion = () => (0, _util.getMotion)(prefixCls);
  79. // ============================== Origin ===============================
  80. const [api, holder] = (0, _rcNotification.useNotification)({
  81. prefixCls,
  82. style: getStyle,
  83. className: getClassName,
  84. motion: getNotificationMotion,
  85. closable: true,
  86. closeIcon: (0, _PurePanel.getCloseIcon)(prefixCls),
  87. duration: duration !== null && duration !== void 0 ? duration : DEFAULT_DURATION,
  88. getContainer: () => (staticGetContainer === null || staticGetContainer === void 0 ? void 0 : staticGetContainer()) || (getPopupContainer === null || getPopupContainer === void 0 ? void 0 : getPopupContainer()) || document.body,
  89. maxCount,
  90. pauseOnHover,
  91. showProgress,
  92. onAllRemoved,
  93. renderNotifications,
  94. stack: stack === false ? false : {
  95. threshold: typeof stack === 'object' ? stack === null || stack === void 0 ? void 0 : stack.threshold : undefined,
  96. offset: 8,
  97. gap: token.margin
  98. }
  99. });
  100. // ================================ Ref ================================
  101. _react.default.useImperativeHandle(ref, () => Object.assign(Object.assign({}, api), {
  102. prefixCls,
  103. notification
  104. }));
  105. return holder;
  106. });
  107. // ==============================================================================
  108. // == Hook ==
  109. // ==============================================================================
  110. function useInternalNotification(notificationConfig) {
  111. const holderRef = _react.default.useRef(null);
  112. const warning = (0, _warning.devUseWarning)('Notification');
  113. // ================================ API ================================
  114. const wrapAPI = _react.default.useMemo(() => {
  115. // Wrap with notification content
  116. // >>> Open
  117. const open = config => {
  118. var _a;
  119. if (!holderRef.current) {
  120. process.env.NODE_ENV !== "production" ? warning(false, 'usage', 'You are calling notice in render which will break in React 18 concurrent mode. Please trigger in effect instead.') : void 0;
  121. return;
  122. }
  123. const {
  124. open: originOpen,
  125. prefixCls,
  126. notification
  127. } = holderRef.current;
  128. const noticePrefixCls = `${prefixCls}-notice`;
  129. const {
  130. message,
  131. description,
  132. icon,
  133. type,
  134. btn,
  135. actions,
  136. className,
  137. style,
  138. role = 'alert',
  139. closeIcon,
  140. closable
  141. } = config,
  142. restConfig = __rest(config, ["message", "description", "icon", "type", "btn", "actions", "className", "style", "role", "closeIcon", "closable"]);
  143. if (process.env.NODE_ENV !== 'production') {
  144. warning.deprecated(!btn, 'btn', 'actions');
  145. }
  146. const mergedActions = actions !== null && actions !== void 0 ? actions : btn;
  147. const realCloseIcon = (0, _PurePanel.getCloseIcon)(noticePrefixCls, (0, _util.getCloseIconConfig)(closeIcon, notificationConfig, notification));
  148. return originOpen(Object.assign(Object.assign({
  149. // use placement from props instead of hard-coding "topRight"
  150. placement: (_a = notificationConfig === null || notificationConfig === void 0 ? void 0 : notificationConfig.placement) !== null && _a !== void 0 ? _a : DEFAULT_PLACEMENT
  151. }, restConfig), {
  152. content: (/*#__PURE__*/_react.default.createElement(_PurePanel.PureContent, {
  153. prefixCls: noticePrefixCls,
  154. icon: icon,
  155. type: type,
  156. message: message,
  157. description: description,
  158. actions: mergedActions,
  159. role: role
  160. })),
  161. className: (0, _classnames.default)(type && `${noticePrefixCls}-${type}`, className, notification === null || notification === void 0 ? void 0 : notification.className),
  162. style: Object.assign(Object.assign({}, notification === null || notification === void 0 ? void 0 : notification.style), style),
  163. closeIcon: realCloseIcon,
  164. closable: closable !== null && closable !== void 0 ? closable : !!realCloseIcon
  165. }));
  166. };
  167. // >>> destroy
  168. const destroy = key => {
  169. var _a, _b;
  170. if (key !== undefined) {
  171. (_a = holderRef.current) === null || _a === void 0 ? void 0 : _a.close(key);
  172. } else {
  173. (_b = holderRef.current) === null || _b === void 0 ? void 0 : _b.destroy();
  174. }
  175. };
  176. const clone = {
  177. open,
  178. destroy
  179. };
  180. const keys = ['success', 'info', 'warning', 'error'];
  181. keys.forEach(type => {
  182. clone[type] = config => open(Object.assign(Object.assign({}, config), {
  183. type
  184. }));
  185. });
  186. return clone;
  187. }, []);
  188. // ============================== Return ===============================
  189. return [wrapAPI, /*#__PURE__*/_react.default.createElement(Holder, Object.assign({
  190. key: "notification-holder"
  191. }, notificationConfig, {
  192. ref: holderRef
  193. }))];
  194. }
  195. function useNotification(notificationConfig) {
  196. return useInternalNotification(notificationConfig);
  197. }