2023-01-20 11:03:50 +08:00
|
|
|
import type { ReactNode } from 'react';
|
|
|
|
import React, { useContext } from 'react';
|
2023-10-12 11:08:01 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
2023-11-04 01:35:40 +08:00
|
|
|
import type { AnyObject, CustomComponent } from '../_util/type';
|
2024-05-08 10:27:59 +08:00
|
|
|
import { devUseWarning } from '../_util/warning';
|
2022-12-06 17:45:10 +08:00
|
|
|
import type { ConfigConsumerProps } from '../config-provider';
|
|
|
|
import { ConfigContext } from '../config-provider';
|
|
|
|
import useMessage from '../message/useMessage';
|
|
|
|
import useModal from '../modal/useModal';
|
2023-01-20 11:03:50 +08:00
|
|
|
import useNotification from '../notification/useNotification';
|
2023-02-15 15:05:21 +08:00
|
|
|
import type { AppConfig, useAppProps } from './context';
|
2023-02-15 18:32:40 +08:00
|
|
|
import AppContext, { AppConfigContext } from './context';
|
2023-01-20 11:03:50 +08:00
|
|
|
import useStyle from './style';
|
2022-12-06 17:45:10 +08:00
|
|
|
|
2023-11-04 01:35:40 +08:00
|
|
|
export interface AppProps<P = AnyObject> extends AppConfig {
|
2023-02-15 18:32:40 +08:00
|
|
|
style?: React.CSSProperties;
|
2022-12-06 17:45:10 +08:00
|
|
|
className?: string;
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2022-12-06 17:45:10 +08:00
|
|
|
prefixCls?: string;
|
|
|
|
children?: ReactNode;
|
2023-11-04 01:35:40 +08:00
|
|
|
component?: CustomComponent<P> | false;
|
2023-02-15 18:32:40 +08:00
|
|
|
}
|
2022-12-06 17:45:10 +08:00
|
|
|
|
2023-02-15 18:32:40 +08:00
|
|
|
const useApp = () => React.useContext<useAppProps>(AppContext);
|
2022-12-06 17:45:10 +08:00
|
|
|
|
2023-11-04 01:35:40 +08:00
|
|
|
const App: React.FC<AppProps> & { useApp: () => useAppProps } = (props) => {
|
2023-02-15 15:05:21 +08:00
|
|
|
const {
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
children,
|
|
|
|
className,
|
|
|
|
rootClassName,
|
|
|
|
message,
|
|
|
|
notification,
|
2023-02-15 18:32:40 +08:00
|
|
|
style,
|
2023-10-12 11:08:01 +08:00
|
|
|
component = 'div',
|
2023-02-15 15:05:21 +08:00
|
|
|
} = props;
|
2024-08-05 18:16:09 +08:00
|
|
|
const { direction, getPrefixCls } = useContext<ConfigConsumerProps>(ConfigContext);
|
2022-12-06 17:45:10 +08:00
|
|
|
const prefixCls = getPrefixCls('app', customizePrefixCls);
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls);
|
2024-08-05 18:16:09 +08:00
|
|
|
|
|
|
|
const customClassName = classNames(hashId, prefixCls, className, rootClassName, cssVarCls, {
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
});
|
2022-12-06 17:45:10 +08:00
|
|
|
|
2023-02-15 18:32:40 +08:00
|
|
|
const appConfig = useContext<AppConfig>(AppConfigContext);
|
|
|
|
|
2023-02-15 15:05:21 +08:00
|
|
|
const mergedAppConfig = React.useMemo<AppConfig>(
|
|
|
|
() => ({
|
|
|
|
message: { ...appConfig.message, ...message },
|
|
|
|
notification: { ...appConfig.notification, ...notification },
|
|
|
|
}),
|
2023-06-29 19:27:30 +08:00
|
|
|
[message, notification, appConfig.message, appConfig.notification],
|
2023-02-15 15:05:21 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
const [messageApi, messageContextHolder] = useMessage(mergedAppConfig.message);
|
|
|
|
const [notificationApi, notificationContextHolder] = useNotification(
|
|
|
|
mergedAppConfig.notification,
|
|
|
|
);
|
2022-12-06 17:45:10 +08:00
|
|
|
const [ModalApi, ModalContextHolder] = useModal();
|
|
|
|
|
2022-12-07 18:27:02 +08:00
|
|
|
const memoizedContextValue = React.useMemo<useAppProps>(
|
2022-12-06 17:45:10 +08:00
|
|
|
() => ({
|
|
|
|
message: messageApi,
|
|
|
|
notification: notificationApi,
|
|
|
|
modal: ModalApi,
|
|
|
|
}),
|
|
|
|
[messageApi, notificationApi, ModalApi],
|
|
|
|
);
|
|
|
|
|
2024-05-08 10:27:59 +08:00
|
|
|
// https://github.com/ant-design/ant-design/issues/48802#issuecomment-2097813526
|
|
|
|
devUseWarning('App')(
|
|
|
|
!(cssVarCls && component === false),
|
|
|
|
'usage',
|
|
|
|
'When using cssVar, ensure `component` is assigned a valid React component string.',
|
|
|
|
);
|
|
|
|
|
2023-10-12 11:08:01 +08:00
|
|
|
// ============================ Render ============================
|
|
|
|
const Component = component === false ? React.Fragment : component;
|
2024-08-05 18:16:09 +08:00
|
|
|
|
2023-11-04 01:35:40 +08:00
|
|
|
const rootProps: AppProps = {
|
2023-10-12 11:08:01 +08:00
|
|
|
className: customClassName,
|
|
|
|
style,
|
|
|
|
};
|
|
|
|
|
2023-11-13 15:34:37 +08:00
|
|
|
return wrapCSSVar(
|
2022-12-06 17:45:10 +08:00
|
|
|
<AppContext.Provider value={memoizedContextValue}>
|
2023-02-15 15:05:21 +08:00
|
|
|
<AppConfigContext.Provider value={mergedAppConfig}>
|
2023-11-05 13:08:52 +08:00
|
|
|
<Component {...(component === false ? undefined : rootProps)}>
|
2023-02-15 15:05:21 +08:00
|
|
|
{ModalContextHolder}
|
|
|
|
{messageContextHolder}
|
|
|
|
{notificationContextHolder}
|
|
|
|
{children}
|
2023-10-12 11:08:01 +08:00
|
|
|
</Component>
|
2023-02-15 15:05:21 +08:00
|
|
|
</AppConfigContext.Provider>
|
2022-12-06 17:45:10 +08:00
|
|
|
</AppContext.Provider>,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
App.displayName = 'App';
|
|
|
|
}
|
|
|
|
|
|
|
|
App.useApp = useApp;
|
2022-12-07 18:27:02 +08:00
|
|
|
|
2022-12-06 17:45:10 +08:00
|
|
|
export default App;
|