123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- "use client";
- import React, { useContext } from 'react';
- import { AppConfigContext } from '../app/context';
- import ConfigProvider, { ConfigContext, globalConfig, warnContext } from '../config-provider';
- import { unstableSetRender } from '../config-provider/UnstableContext';
- import PurePanel from './PurePanel';
- import useNotification, { useInternalNotification } from './useNotification';
- let notification = null;
- let act = callback => callback();
- let taskQueue = [];
- let defaultGlobalConfig = {};
- function getGlobalContext() {
- const {
- getContainer,
- rtl,
- maxCount,
- top,
- bottom,
- showProgress,
- pauseOnHover
- } = defaultGlobalConfig;
- const mergedContainer = (getContainer === null || getContainer === void 0 ? void 0 : getContainer()) || document.body;
- return {
- getContainer: () => mergedContainer,
- rtl,
- maxCount,
- top,
- bottom,
- showProgress,
- pauseOnHover
- };
- }
- const GlobalHolder = /*#__PURE__*/React.forwardRef((props, ref) => {
- const {
- notificationConfig,
- sync
- } = props;
- const {
- getPrefixCls
- } = useContext(ConfigContext);
- const prefixCls = defaultGlobalConfig.prefixCls || getPrefixCls('notification');
- const appConfig = useContext(AppConfigContext);
- const [api, holder] = useInternalNotification(Object.assign(Object.assign(Object.assign({}, notificationConfig), {
- prefixCls
- }), appConfig.notification));
- React.useEffect(sync, []);
- React.useImperativeHandle(ref, () => {
- const instance = Object.assign({}, api);
- Object.keys(instance).forEach(method => {
- instance[method] = (...args) => {
- sync();
- return api[method].apply(api, args);
- };
- });
- return {
- instance,
- sync
- };
- });
- return holder;
- });
- const GlobalHolderWrapper = /*#__PURE__*/React.forwardRef((_, ref) => {
- const [notificationConfig, setNotificationConfig] = React.useState(getGlobalContext);
- const sync = () => {
- setNotificationConfig(getGlobalContext);
- };
- React.useEffect(sync, []);
- const global = globalConfig();
- const rootPrefixCls = global.getRootPrefixCls();
- const rootIconPrefixCls = global.getIconPrefixCls();
- const theme = global.getTheme();
- const dom = /*#__PURE__*/React.createElement(GlobalHolder, {
- ref: ref,
- sync: sync,
- notificationConfig: notificationConfig
- });
- return /*#__PURE__*/React.createElement(ConfigProvider, {
- prefixCls: rootPrefixCls,
- iconPrefixCls: rootIconPrefixCls,
- theme: theme
- }, global.holderRender ? global.holderRender(dom) : dom);
- });
- const flushNotificationQueue = () => {
- if (!notification) {
- const holderFragment = document.createDocumentFragment();
- const newNotification = {
- fragment: holderFragment
- };
- notification = newNotification;
- // Delay render to avoid sync issue
- act(() => {
- const reactRender = unstableSetRender();
- reactRender(/*#__PURE__*/React.createElement(GlobalHolderWrapper, {
- ref: node => {
- const {
- instance,
- sync
- } = node || {};
- Promise.resolve().then(() => {
- if (!newNotification.instance && instance) {
- newNotification.instance = instance;
- newNotification.sync = sync;
- flushNotificationQueue();
- }
- });
- }
- }), holderFragment);
- });
- return;
- }
- // Notification not ready
- if (!notification.instance) {
- return;
- }
- // >>> Execute task
- taskQueue.forEach(task => {
- switch (task.type) {
- case 'open':
- {
- act(() => {
- notification.instance.open(Object.assign(Object.assign({}, defaultGlobalConfig), task.config));
- });
- break;
- }
- case 'destroy':
- act(() => {
- var _a;
- (_a = notification === null || notification === void 0 ? void 0 : notification.instance) === null || _a === void 0 ? void 0 : _a.destroy(task.key);
- });
- break;
- }
- });
- // Clean up
- taskQueue = [];
- };
- // ==============================================================================
- // == Export ==
- // ==============================================================================
- function setNotificationGlobalConfig(config) {
- defaultGlobalConfig = Object.assign(Object.assign({}, defaultGlobalConfig), config);
- // Trigger sync for it
- act(() => {
- var _a;
- (_a = notification === null || notification === void 0 ? void 0 : notification.sync) === null || _a === void 0 ? void 0 : _a.call(notification);
- });
- }
- function open(config) {
- const global = globalConfig();
- if (process.env.NODE_ENV !== 'production' && !global.holderRender) {
- warnContext('notification');
- }
- taskQueue.push({
- type: 'open',
- config
- });
- flushNotificationQueue();
- }
- const destroy = key => {
- taskQueue.push({
- type: 'destroy',
- key
- });
- flushNotificationQueue();
- };
- const methods = ['success', 'info', 'warning', 'error'];
- const baseStaticMethods = {
- open,
- destroy,
- config: setNotificationGlobalConfig,
- useNotification,
- _InternalPanelDoNotUseOrYouWillBeFired: PurePanel
- };
- const staticMethods = baseStaticMethods;
- methods.forEach(type => {
- staticMethods[type] = config => open(Object.assign(Object.assign({}, config), {
- type
- }));
- });
- // ==============================================================================
- // == Test ==
- // ==============================================================================
- const noop = () => {};
- let _actWrapper = noop;
- if (process.env.NODE_ENV === 'test') {
- _actWrapper = wrapper => {
- act = wrapper;
- };
- }
- const actWrapper = _actWrapper;
- export { actWrapper };
- let _actDestroy = noop;
- if (process.env.NODE_ENV === 'test') {
- _actDestroy = () => {
- notification = null;
- };
- }
- const actDestroy = _actDestroy;
- export { actDestroy };
- export default staticMethods;
|