useNotification.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _extends from "@babel/runtime/helpers/esm/extends";
  3. import { createVNode as _createVNode } from "vue";
  4. var __rest = this && this.__rest || function (s, e) {
  5. var t = {};
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  7. if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
  8. if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  9. }
  10. return t;
  11. };
  12. import { shallowRef, computed, defineComponent } from 'vue';
  13. import { useNotification as useVcNotification } from '../vc-notification';
  14. import useStyle from './style';
  15. import { getCloseIcon, PureContent } from './PurePanel';
  16. import { getMotion, getPlacementStyle } from './util';
  17. import useConfigInject from '../config-provider/hooks/useConfigInject';
  18. import classNames from '../_util/classNames';
  19. const DEFAULT_OFFSET = 24;
  20. const DEFAULT_DURATION = 4.5;
  21. const Holder = defineComponent({
  22. name: 'Holder',
  23. inheritAttrs: false,
  24. props: ['prefixCls', 'class', 'type', 'icon', 'content', 'onAllRemoved'],
  25. setup(props, _ref) {
  26. let {
  27. expose
  28. } = _ref;
  29. const {
  30. getPrefixCls,
  31. getPopupContainer
  32. } = useConfigInject('notification', props);
  33. const prefixCls = computed(() => props.prefixCls || getPrefixCls('notification'));
  34. // =============================== Style ===============================
  35. const getStyles = placement => {
  36. var _a, _b;
  37. return getPlacementStyle(placement, (_a = props.top) !== null && _a !== void 0 ? _a : DEFAULT_OFFSET, (_b = props.bottom) !== null && _b !== void 0 ? _b : DEFAULT_OFFSET);
  38. };
  39. // Style
  40. const [, hashId] = useStyle(prefixCls);
  41. const getClassName = () => classNames(hashId.value, {
  42. [`${prefixCls.value}-rtl`]: props.rtl
  43. });
  44. // ============================== Motion ===============================
  45. const getNotificationMotion = () => getMotion(prefixCls.value);
  46. // ============================== Origin ===============================
  47. const [api, holder] = useVcNotification({
  48. prefixCls: prefixCls.value,
  49. getStyles,
  50. getClassName,
  51. motion: getNotificationMotion,
  52. closable: true,
  53. closeIcon: getCloseIcon(prefixCls.value),
  54. duration: DEFAULT_DURATION,
  55. getContainer: () => {
  56. var _a, _b;
  57. return ((_a = props.getPopupContainer) === null || _a === void 0 ? void 0 : _a.call(props)) || ((_b = getPopupContainer.value) === null || _b === void 0 ? void 0 : _b.call(getPopupContainer)) || document.body;
  58. },
  59. maxCount: props.maxCount,
  60. hashId: hashId.value,
  61. onAllRemoved: props.onAllRemoved
  62. });
  63. // ================================ Ref ================================
  64. expose(_extends(_extends({}, api), {
  65. prefixCls: prefixCls.value,
  66. hashId
  67. }));
  68. return holder;
  69. }
  70. });
  71. // ==============================================================================
  72. // == Hook ==
  73. // ==============================================================================
  74. export function useInternalNotification(notificationConfig) {
  75. const holderRef = shallowRef(null);
  76. const holderKey = Symbol('notificationHolderKey');
  77. // ================================ API ================================
  78. // Wrap with notification content
  79. // >>> Open
  80. const open = config => {
  81. if (!holderRef.value) {
  82. return;
  83. }
  84. const {
  85. open: originOpen,
  86. prefixCls,
  87. hashId
  88. } = holderRef.value;
  89. const noticePrefixCls = `${prefixCls}-notice`;
  90. const {
  91. message,
  92. description,
  93. icon,
  94. type,
  95. btn,
  96. class: className
  97. } = config,
  98. restConfig = __rest(config, ["message", "description", "icon", "type", "btn", "class"]);
  99. return originOpen(_extends(_extends({
  100. placement: 'topRight'
  101. }, restConfig), {
  102. content: () => _createVNode(PureContent, {
  103. "prefixCls": noticePrefixCls,
  104. "icon": typeof icon === 'function' ? icon() : icon,
  105. "type": type,
  106. "message": typeof message === 'function' ? message() : message,
  107. "description": typeof description === 'function' ? description() : description,
  108. "btn": typeof btn === 'function' ? btn() : btn
  109. }, null),
  110. // @ts-ignore
  111. class: classNames(type && `${noticePrefixCls}-${type}`, hashId, className)
  112. }));
  113. };
  114. // >>> destroy
  115. const destroy = key => {
  116. var _a, _b;
  117. if (key !== undefined) {
  118. (_a = holderRef.value) === null || _a === void 0 ? void 0 : _a.close(key);
  119. } else {
  120. (_b = holderRef.value) === null || _b === void 0 ? void 0 : _b.destroy();
  121. }
  122. };
  123. const wrapAPI = {
  124. open,
  125. destroy
  126. };
  127. const keys = ['success', 'info', 'warning', 'error'];
  128. keys.forEach(type => {
  129. wrapAPI[type] = config => open(_extends(_extends({}, config), {
  130. type
  131. }));
  132. });
  133. // ============================== Return ===============================
  134. return [wrapAPI, () => _createVNode(Holder, _objectSpread(_objectSpread({
  135. "key": holderKey
  136. }, notificationConfig), {}, {
  137. "ref": holderRef
  138. }), null)];
  139. }
  140. export default function useNotification(notificationConfig) {
  141. return useInternalNotification(notificationConfig);
  142. }