|
1 |
| -import { ConfigProvider, message, Modal, notification, theme, ThemeConfig } from 'antd'; |
2 |
| -import { memo, useEffect, useMemo, type FC } from 'react'; |
| 1 | +import { ConfigProvider, theme, ThemeConfig } from 'antd'; |
| 2 | +import { memo, useLayoutEffect, useMemo, type FC } from 'react'; |
3 | 3 |
|
4 | 4 | import { useThemeMode } from '@/hooks';
|
5 | 5 | import type { ThemeProviderProps } from './type';
|
6 | 6 |
|
7 |
| -type AntdProviderProps = Pick< |
8 |
| - ThemeProviderProps<any>, |
9 |
| - 'theme' | 'prefixCls' | 'getStaticInstance' | 'children' | 'staticInstanceConfig' |
10 |
| ->; |
| 7 | +type AntdProviderProps = Pick<ThemeProviderProps<any>, 'theme' | 'prefixCls' | 'children'>; |
11 | 8 |
|
12 |
| -const AntdProvider: FC<AntdProviderProps> = memo( |
13 |
| - ({ children, theme: themeProp, prefixCls, getStaticInstance, staticInstanceConfig }) => { |
14 |
| - const { appearance, isDarkMode } = useThemeMode(); |
| 9 | +const Provider: FC<AntdProviderProps> = memo(({ children, theme: themeProp, prefixCls }) => { |
| 10 | + const { appearance, isDarkMode } = useThemeMode(); |
15 | 11 |
|
16 |
| - const [messageInstance, messageContextHolder] = message.useMessage( |
17 |
| - staticInstanceConfig?.message, |
18 |
| - ); |
19 |
| - const [notificationInstance, notificationContextHolder] = notification.useNotification( |
20 |
| - staticInstanceConfig?.notification, |
21 |
| - ); |
22 |
| - const [modalInstance, modalContextHolder] = Modal.useModal(); |
| 12 | + // 获取 antd 主题 |
| 13 | + const antdTheme = useMemo<ThemeConfig>(() => { |
| 14 | + const baseAlgorithm = isDarkMode ? theme.darkAlgorithm : theme.defaultAlgorithm; |
23 | 15 |
|
24 |
| - useEffect(() => { |
25 |
| - getStaticInstance?.({ |
26 |
| - message: messageInstance, |
27 |
| - modal: modalInstance, |
28 |
| - notification: notificationInstance, |
29 |
| - }); |
30 |
| - }, []); |
| 16 | + let antdTheme = themeProp as ThemeConfig | undefined; |
31 | 17 |
|
32 |
| - // 获取 antd 主题 |
33 |
| - const antdTheme = useMemo<ThemeConfig>(() => { |
34 |
| - const baseAlgorithm = isDarkMode ? theme.darkAlgorithm : theme.defaultAlgorithm; |
| 18 | + if (typeof themeProp === 'function') { |
| 19 | + antdTheme = themeProp(appearance); |
| 20 | + } |
35 | 21 |
|
36 |
| - let antdTheme = themeProp as ThemeConfig | undefined; |
| 22 | + if (!antdTheme) { |
| 23 | + return { algorithm: baseAlgorithm }; |
| 24 | + } |
37 | 25 |
|
38 |
| - if (typeof themeProp === 'function') { |
39 |
| - antdTheme = themeProp(appearance); |
40 |
| - } |
| 26 | + // 如果有 themeProp 说明是外部传入的 theme,需要对算法做一个合并处理,因此先把 themeProp 的算法规整为一个数组 |
| 27 | + const algoProp = !antdTheme.algorithm |
| 28 | + ? [] |
| 29 | + : antdTheme.algorithm instanceof Array |
| 30 | + ? antdTheme.algorithm |
| 31 | + : [antdTheme.algorithm]; |
41 | 32 |
|
42 |
| - if (!antdTheme) { |
43 |
| - return { algorithm: baseAlgorithm }; |
44 |
| - } |
| 33 | + return { |
| 34 | + ...antdTheme, |
| 35 | + algorithm: !antdTheme.algorithm ? baseAlgorithm : [baseAlgorithm, ...algoProp], |
| 36 | + }; |
| 37 | + }, [themeProp, isDarkMode]); |
45 | 38 |
|
46 |
| - // 如果有 themeProp 说明是外部传入的 theme,需要对算法做一个合并处理,因此先把 themeProp 的算法规整为一个数组 |
47 |
| - const algoProp = !antdTheme.algorithm |
48 |
| - ? [] |
49 |
| - : antdTheme.algorithm instanceof Array |
50 |
| - ? antdTheme.algorithm |
51 |
| - : [antdTheme.algorithm]; |
| 39 | + return ( |
| 40 | + <ConfigProvider prefixCls={prefixCls} theme={antdTheme}> |
| 41 | + {children} |
| 42 | + </ConfigProvider> |
| 43 | + ); |
| 44 | +}); |
52 | 45 |
|
53 |
| - return { |
54 |
| - ...antdTheme, |
55 |
| - algorithm: !antdTheme.algorithm ? baseAlgorithm : [baseAlgorithm, ...algoProp], |
56 |
| - }; |
57 |
| - }, [themeProp, isDarkMode]); |
| 46 | +Provider.displayName = 'AntdProvider'; |
58 | 47 |
|
59 |
| - return ( |
60 |
| - <ConfigProvider prefixCls={prefixCls} theme={antdTheme}> |
61 |
| - {messageContextHolder} |
62 |
| - {notificationContextHolder} |
63 |
| - {modalContextHolder} |
64 |
| - {children} |
65 |
| - </ConfigProvider> |
66 |
| - ); |
67 |
| - }, |
68 |
| -); |
| 48 | +const AntdProvider = memo<AntdProviderProps>(({ children, theme, prefixCls }) => { |
| 49 | + useLayoutEffect(() => { |
| 50 | + ConfigProvider.config({ |
| 51 | + holderRender: (children) => ( |
| 52 | + <Provider theme={theme} prefixCls={prefixCls}> |
| 53 | + {children} |
| 54 | + </Provider> |
| 55 | + ), |
| 56 | + }); |
| 57 | + }, [prefixCls, theme]); |
69 | 58 |
|
70 |
| -AntdProvider.displayName = 'AntdProvider'; |
| 59 | + return ( |
| 60 | + <Provider theme={theme} prefixCls={prefixCls}> |
| 61 | + {children} |
| 62 | + </Provider> |
| 63 | + ); |
| 64 | +}); |
71 | 65 |
|
72 | 66 | export default AntdProvider;
|
0 commit comments