ant-design/components/notification/index.tsx

253 lines
5.9 KiB
TypeScript
Raw Normal View History

feat: prepend use-client directive for with Next.js App Router (#43573) * fix: prepend use-client directive for with Next.js App Router * Update components/affix/index.tsx Co-authored-by: MadCcc <1075746765@qq.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/badge/index.tsx Co-authored-by: MadCcc <1075746765@qq.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/divider/index.tsx Co-authored-by: MadCcc <1075746765@qq.com> Signed-off-by: lijianan <574980606@qq.com> * Update components/cascader/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/list/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/mentions/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/mentions/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/mentions/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/mentions/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/qrcode/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/spin/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/select/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/spin/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/spin/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/steps/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/time-picker/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/transfer/index.tsx Signed-off-by: lijianan <574980606@qq.com> * Update components/tree-select/index.tsx Signed-off-by: lijianan <574980606@qq.com> --------- Signed-off-by: lijianan <574980606@qq.com> Co-authored-by: MadCcc <1075746765@qq.com>
2023-07-15 12:57:03 +08:00
'use client';
import * as React from 'react';
import { render } from 'rc-util/lib/React/render';
import ConfigProvider, { globalConfig, warnContext } from '../config-provider';
import type { ArgsProps, GlobalConfigProps, NotificationInstance } from './interface';
import PurePanel from './PurePanel';
import useNotification, { useInternalNotification } from './useNotification';
let notification: GlobalNotification | null = null;
let act: (callback: VoidFunction) => Promise<void> | void = (callback: VoidFunction) => callback();
interface GlobalNotification {
fragment: DocumentFragment;
instance?: NotificationInstance | null;
sync?: VoidFunction;
}
2019-01-05 21:33:07 +08:00
type Task =
| {
type: 'open';
config: ArgsProps;
}
| {
type: 'destroy';
key: React.Key;
};
let taskQueue: Task[] = [];
let defaultGlobalConfig: GlobalConfigProps = {};
function getGlobalContext() {
const {
prefixCls: globalPrefixCls,
getContainer: globalGetContainer,
rtl,
maxCount,
top,
bottom,
} = defaultGlobalConfig;
const mergedPrefixCls = globalPrefixCls ?? globalConfig().getPrefixCls('notification');
const mergedContainer = globalGetContainer?.() || document.body;
return {
prefixCls: mergedPrefixCls,
getContainer: () => mergedContainer!,
rtl,
maxCount,
top,
bottom,
};
}
interface GlobalHolderRef {
instance: NotificationInstance;
sync: () => void;
}
2015-08-03 18:00:22 +08:00
const GlobalHolder = React.forwardRef<GlobalHolderRef, {}>((_, ref) => {
const [notificationConfig, setNotificationConfig] =
React.useState<GlobalConfigProps>(getGlobalContext);
const [api, holder] = useInternalNotification(notificationConfig);
const global = globalConfig();
const rootPrefixCls = global.getRootPrefixCls();
const rootIconPrefixCls = global.getIconPrefixCls();
const theme = global.getTheme();
const sync = () => {
setNotificationConfig(getGlobalContext);
};
React.useEffect(sync, []);
React.useImperativeHandle(ref, () => {
2023-02-23 16:38:06 +08:00
const instance: NotificationInstance = { ...api };
2023-02-23 16:38:06 +08:00
Object.keys(instance).forEach((method: keyof NotificationInstance) => {
instance[method] = (...args: any[]) => {
sync();
return (api as any)[method](...args);
};
});
return {
instance,
sync,
};
});
return (
<ConfigProvider prefixCls={rootPrefixCls} iconPrefixCls={rootIconPrefixCls} theme={theme}>
{holder}
</ConfigProvider>
);
});
function flushNotice() {
if (!notification) {
const holderFragment = document.createDocumentFragment();
const newNotification: GlobalNotification = {
fragment: holderFragment,
};
notification = newNotification;
// Delay render to avoid sync issue
act(() => {
render(
<GlobalHolder
2022-11-18 23:34:07 +08:00
ref={(node) => {
const { instance, sync } = node || {};
Promise.resolve().then(() => {
if (!newNotification.instance && instance) {
newNotification.instance = instance;
newNotification.sync = sync;
flushNotice();
}
});
}}
/>,
holderFragment,
);
});
return;
}
// Notification not ready
2022-11-18 23:34:07 +08:00
if (!notification.instance) {
return;
}
// >>> Execute task
2022-11-18 23:34:07 +08:00
taskQueue.forEach((task) => {
// eslint-disable-next-line default-case
switch (task.type) {
case 'open': {
act(() => {
notification!.instance!.open({
...defaultGlobalConfig,
...task.config,
});
});
break;
}
case 'destroy':
act(() => {
notification?.instance!.destroy(task.key);
});
break;
}
});
// Clean up
taskQueue = [];
}
2019-01-05 21:33:07 +08:00
// ==============================================================================
// == Export ==
// ==============================================================================
function setNotificationGlobalConfig(config: GlobalConfigProps) {
defaultGlobalConfig = {
...defaultGlobalConfig,
...config,
};
// Trigger sync for it
act(() => {
notification?.sync?.();
});
}
function open(config: ArgsProps) {
// Warning if exist theme
if (process.env.NODE_ENV !== 'production') {
warnContext('notification');
}
taskQueue.push({
type: 'open',
config,
});
flushNotice();
2015-07-31 18:13:32 +08:00
}
2015-07-28 15:40:28 +08:00
function destroy(key: React.Key) {
taskQueue.push({
type: 'destroy',
key,
});
flushNotice();
}
interface BaseMethods {
open: (config: ArgsProps) => void;
destroy: (key?: React.Key) => void;
config: any;
useNotification: typeof useNotification;
/** @private Internal Component. Do not use in your production. */
_InternalPanelDoNotUseOrYouWillBeFired: typeof PurePanel;
}
type StaticFn = (config: ArgsProps) => void;
interface NoticeMethods {
success: StaticFn;
info: StaticFn;
warning: StaticFn;
error: StaticFn;
}
const methods: (keyof NoticeMethods)[] = ['success', 'info', 'warning', 'error'];
const baseStaticMethods: BaseMethods = {
open,
destroy,
config: setNotificationGlobalConfig,
useNotification,
_InternalPanelDoNotUseOrYouWillBeFired: PurePanel,
2015-07-31 18:13:32 +08:00
};
2015-08-04 15:16:10 +08:00
const staticMethods = baseStaticMethods as NoticeMethods & BaseMethods;
methods.forEach((type: keyof NoticeMethods) => {
staticMethods[type] = (config) => open({ ...config, type });
2015-08-04 15:16:10 +08:00
});
// ==============================================================================
// == Test ==
// ==============================================================================
const noop = () => {};
2016-03-28 18:03:00 +08:00
/** @internal Only Work in test env */
// eslint-disable-next-line import/no-mutable-exports
export let actWrapper: (wrapper: any) => void = noop;
if (process.env.NODE_ENV === 'test') {
2022-11-18 23:34:07 +08:00
actWrapper = (wrapper) => {
act = wrapper;
};
}
2019-01-05 21:33:07 +08:00
export default staticMethods;