useNotification.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  2. import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
  3. import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
  4. var _excluded = ["getContainer", "motion", "prefixCls", "maxCount", "className", "style", "onAllRemoved", "stack", "renderNotifications"];
  5. import * as React from 'react';
  6. import Notifications from "../Notifications";
  7. import { useEvent } from 'rc-util';
  8. var defaultGetContainer = function defaultGetContainer() {
  9. return document.body;
  10. };
  11. var uniqueKey = 0;
  12. function mergeConfig() {
  13. var clone = {};
  14. for (var _len = arguments.length, objList = new Array(_len), _key = 0; _key < _len; _key++) {
  15. objList[_key] = arguments[_key];
  16. }
  17. objList.forEach(function (obj) {
  18. if (obj) {
  19. Object.keys(obj).forEach(function (key) {
  20. var val = obj[key];
  21. if (val !== undefined) {
  22. clone[key] = val;
  23. }
  24. });
  25. }
  26. });
  27. return clone;
  28. }
  29. export default function useNotification() {
  30. var rootConfig = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  31. var _rootConfig$getContai = rootConfig.getContainer,
  32. getContainer = _rootConfig$getContai === void 0 ? defaultGetContainer : _rootConfig$getContai,
  33. motion = rootConfig.motion,
  34. prefixCls = rootConfig.prefixCls,
  35. maxCount = rootConfig.maxCount,
  36. className = rootConfig.className,
  37. style = rootConfig.style,
  38. onAllRemoved = rootConfig.onAllRemoved,
  39. stack = rootConfig.stack,
  40. renderNotifications = rootConfig.renderNotifications,
  41. shareConfig = _objectWithoutProperties(rootConfig, _excluded);
  42. var _React$useState = React.useState(),
  43. _React$useState2 = _slicedToArray(_React$useState, 2),
  44. container = _React$useState2[0],
  45. setContainer = _React$useState2[1];
  46. var notificationsRef = React.useRef();
  47. var contextHolder = /*#__PURE__*/React.createElement(Notifications, {
  48. container: container,
  49. ref: notificationsRef,
  50. prefixCls: prefixCls,
  51. motion: motion,
  52. maxCount: maxCount,
  53. className: className,
  54. style: style,
  55. onAllRemoved: onAllRemoved,
  56. stack: stack,
  57. renderNotifications: renderNotifications
  58. });
  59. var _React$useState3 = React.useState([]),
  60. _React$useState4 = _slicedToArray(_React$useState3, 2),
  61. taskQueue = _React$useState4[0],
  62. setTaskQueue = _React$useState4[1];
  63. var open = useEvent(function (config) {
  64. var mergedConfig = mergeConfig(shareConfig, config);
  65. if (mergedConfig.key === null || mergedConfig.key === undefined) {
  66. mergedConfig.key = "rc-notification-".concat(uniqueKey);
  67. uniqueKey += 1;
  68. }
  69. setTaskQueue(function (queue) {
  70. return [].concat(_toConsumableArray(queue), [{
  71. type: 'open',
  72. config: mergedConfig
  73. }]);
  74. });
  75. });
  76. // ========================= Refs =========================
  77. var api = React.useMemo(function () {
  78. return {
  79. open: open,
  80. close: function close(key) {
  81. setTaskQueue(function (queue) {
  82. return [].concat(_toConsumableArray(queue), [{
  83. type: 'close',
  84. key: key
  85. }]);
  86. });
  87. },
  88. destroy: function destroy() {
  89. setTaskQueue(function (queue) {
  90. return [].concat(_toConsumableArray(queue), [{
  91. type: 'destroy'
  92. }]);
  93. });
  94. }
  95. };
  96. }, []);
  97. // ======================= Container ======================
  98. // React 18 should all in effect that we will check container in each render
  99. // Which means getContainer should be stable.
  100. React.useEffect(function () {
  101. setContainer(getContainer());
  102. });
  103. // ======================== Effect ========================
  104. React.useEffect(function () {
  105. // Flush task when node ready
  106. if (notificationsRef.current && taskQueue.length) {
  107. taskQueue.forEach(function (task) {
  108. switch (task.type) {
  109. case 'open':
  110. notificationsRef.current.open(task.config);
  111. break;
  112. case 'close':
  113. notificationsRef.current.close(task.key);
  114. break;
  115. case 'destroy':
  116. notificationsRef.current.destroy();
  117. break;
  118. }
  119. });
  120. // https://github.com/ant-design/ant-design/issues/52590
  121. // React `startTransition` will run once `useEffect` but many times `setState`,
  122. // So `setTaskQueue` with filtered array will cause infinite loop.
  123. // We cache the first match queue instead.
  124. var oriTaskQueue;
  125. var tgtTaskQueue;
  126. // React 17 will mix order of effect & setState in async
  127. // - open: setState[0]
  128. // - effect[0]
  129. // - open: setState[1]
  130. // - effect setState([]) * here will clean up [0, 1] in React 17
  131. setTaskQueue(function (oriQueue) {
  132. if (oriTaskQueue !== oriQueue || !tgtTaskQueue) {
  133. oriTaskQueue = oriQueue;
  134. tgtTaskQueue = oriQueue.filter(function (task) {
  135. return !taskQueue.includes(task);
  136. });
  137. }
  138. return tgtTaskQueue;
  139. });
  140. }
  141. }, [taskQueue]);
  142. // ======================== Return ========================
  143. return [api, contextHolder];
  144. }