generated from arvinxx/npm-template
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
52 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,66 @@ | ||
import { ConfigProvider, message, Modal, notification, theme, ThemeConfig } from 'antd'; | ||
import { memo, useEffect, useMemo, type FC } from 'react'; | ||
import { ConfigProvider, theme, ThemeConfig } from 'antd'; | ||
import { memo, useLayoutEffect, useMemo, type FC } from 'react'; | ||
|
||
import { useThemeMode } from '@/hooks'; | ||
import type { ThemeProviderProps } from './type'; | ||
|
||
type AntdProviderProps = Pick< | ||
ThemeProviderProps<any>, | ||
'theme' | 'prefixCls' | 'getStaticInstance' | 'children' | 'staticInstanceConfig' | ||
>; | ||
type AntdProviderProps = Pick<ThemeProviderProps<any>, 'theme' | 'prefixCls' | 'children'>; | ||
|
||
const AntdProvider: FC<AntdProviderProps> = memo( | ||
({ children, theme: themeProp, prefixCls, getStaticInstance, staticInstanceConfig }) => { | ||
const { appearance, isDarkMode } = useThemeMode(); | ||
const Provider: FC<AntdProviderProps> = memo(({ children, theme: themeProp, prefixCls }) => { | ||
const { appearance, isDarkMode } = useThemeMode(); | ||
|
||
const [messageInstance, messageContextHolder] = message.useMessage( | ||
staticInstanceConfig?.message, | ||
); | ||
const [notificationInstance, notificationContextHolder] = notification.useNotification( | ||
staticInstanceConfig?.notification, | ||
); | ||
const [modalInstance, modalContextHolder] = Modal.useModal(); | ||
// 获取 antd 主题 | ||
const antdTheme = useMemo<ThemeConfig>(() => { | ||
const baseAlgorithm = isDarkMode ? theme.darkAlgorithm : theme.defaultAlgorithm; | ||
|
||
useEffect(() => { | ||
getStaticInstance?.({ | ||
message: messageInstance, | ||
modal: modalInstance, | ||
notification: notificationInstance, | ||
}); | ||
}, []); | ||
let antdTheme = themeProp as ThemeConfig | undefined; | ||
|
||
// 获取 antd 主题 | ||
const antdTheme = useMemo<ThemeConfig>(() => { | ||
const baseAlgorithm = isDarkMode ? theme.darkAlgorithm : theme.defaultAlgorithm; | ||
if (typeof themeProp === 'function') { | ||
antdTheme = themeProp(appearance); | ||
} | ||
|
||
let antdTheme = themeProp as ThemeConfig | undefined; | ||
if (!antdTheme) { | ||
return { algorithm: baseAlgorithm }; | ||
} | ||
|
||
if (typeof themeProp === 'function') { | ||
antdTheme = themeProp(appearance); | ||
} | ||
// 如果有 themeProp 说明是外部传入的 theme,需要对算法做一个合并处理,因此先把 themeProp 的算法规整为一个数组 | ||
const algoProp = !antdTheme.algorithm | ||
? [] | ||
: antdTheme.algorithm instanceof Array | ||
? antdTheme.algorithm | ||
: [antdTheme.algorithm]; | ||
|
||
if (!antdTheme) { | ||
return { algorithm: baseAlgorithm }; | ||
} | ||
return { | ||
...antdTheme, | ||
algorithm: !antdTheme.algorithm ? baseAlgorithm : [baseAlgorithm, ...algoProp], | ||
}; | ||
}, [themeProp, isDarkMode]); | ||
|
||
// 如果有 themeProp 说明是外部传入的 theme,需要对算法做一个合并处理,因此先把 themeProp 的算法规整为一个数组 | ||
const algoProp = !antdTheme.algorithm | ||
? [] | ||
: antdTheme.algorithm instanceof Array | ||
? antdTheme.algorithm | ||
: [antdTheme.algorithm]; | ||
return ( | ||
<ConfigProvider prefixCls={prefixCls} theme={antdTheme}> | ||
{children} | ||
</ConfigProvider> | ||
); | ||
}); | ||
|
||
return { | ||
...antdTheme, | ||
algorithm: !antdTheme.algorithm ? baseAlgorithm : [baseAlgorithm, ...algoProp], | ||
}; | ||
}, [themeProp, isDarkMode]); | ||
Provider.displayName = 'AntdProvider'; | ||
|
||
return ( | ||
<ConfigProvider prefixCls={prefixCls} theme={antdTheme}> | ||
{messageContextHolder} | ||
{notificationContextHolder} | ||
{modalContextHolder} | ||
{children} | ||
</ConfigProvider> | ||
); | ||
}, | ||
); | ||
const AntdProvider = memo<AntdProviderProps>(({ children, theme, prefixCls }) => { | ||
useLayoutEffect(() => { | ||
ConfigProvider.config({ | ||
holderRender: (children) => ( | ||
<Provider theme={theme} prefixCls={prefixCls}> | ||
{children} | ||
</Provider> | ||
), | ||
}); | ||
}, [prefixCls, theme]); | ||
|
||
AntdProvider.displayName = 'AntdProvider'; | ||
return ( | ||
<Provider theme={theme} prefixCls={prefixCls}> | ||
{children} | ||
</Provider> | ||
); | ||
}); | ||
|
||
export default AntdProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters