useNotification.js 7.3 KB

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