@@ -6,15 +6,17 @@ import type { ThemeProviderType } from './useThemeConfigProvider';
66import { storage } from '../Utils' ;
77import { getThemeConfig } from './getThemeConfig' ;
88import { useCustomToken } from './useCustomToken' ;
9+ import { useTheme , useLocale } from '../HomePage/Hooks' ;
910
11+ type AlgorithmType = 'defaultAlgorithm' | 'darkAlgorithm' ;
1012type IThemeProvider = {
1113 locales : {
1214 'zh-CN' : Record < string , string > ;
1315 'en-US' : Record < string , string > ;
1416 } ;
1517 children : React . ReactNode ;
1618 locale ?: 'zh-CN' | 'en-US' ;
17- algorithm ?: 'defaultAlgorithm' | 'darkAlgorithm' ;
19+ algorithm ?: AlgorithmType ;
1820} ;
1921
2022const Provider : React . FC < IThemeProvider > = props => {
@@ -47,13 +49,39 @@ const Provider: React.FC<IThemeProvider> = props => {
4749 const { componentsConfig, tokenConfig } = getThemeConfig ( algorithm ) ;
4850 const colorConfig = useCustomToken ( ) ;
4951 const isLight = algorithm === 'defaultAlgorithm' ;
52+ const currentTheme = useTheme ( ) ;
53+ const currentLocale = useLocale ( ) ;
5054
51- const handleThemeOrLocale = ( themeConfig : Partial < ThemeProviderType > ) => {
55+ useEffect ( ( ) => {
56+ setState ( preState => {
57+ return {
58+ ...preState ,
59+ algorithm : currentTheme as AlgorithmType ,
60+ locale : currentLocale as 'zh-CN' | 'en-US' ,
61+ } ;
62+ } ) ;
63+ } , [ currentTheme , currentLocale ] ) ;
64+
65+ useEffect ( ( ) => {
66+ const theme = storage . get < AlgorithmType > ( 'algorithm' ) ;
67+ const locale = storage . get < 'zh-CN' | 'en-US' > ( 'locale' ) ;
68+ document . documentElement . setAttribute ( 'data-theme' , theme || 'defaultAlgorithm' ) ;
69+ document . documentElement . setAttribute ( 'data-locale' , locale || 'en-US' ) ;
70+ } , [ ] ) ;
71+
72+ const updateStudio = ( themeConfig : Partial < ThemeProviderType > ) => {
73+ console . log ( 'themeConfig::: ' , themeConfig ) ;
5274 const { components, token } = themeConfig ;
5375 Object . keys ( themeConfig ) . forEach ( key => {
5476 storage . set ( key , themeConfig [ key ] ) ;
55- } ) ;
77+ if ( key === 'algorithm' ) {
78+ document . documentElement . setAttribute ( 'data-theme' , themeConfig . algorithm || 'defaultAlgorithm' ) ;
79+ }
5680
81+ if ( key === 'locale' ) {
82+ document . documentElement . setAttribute ( 'data-locale' , themeConfig . locale || 'en-US' ) ;
83+ }
84+ } ) ;
5785 setState ( preState => {
5886 // 特殊化处理,切token数据需初始化数据做基础
5987 storage . set ( 'token' , { ...preState . token , ...token } ) ;
@@ -74,7 +102,7 @@ const Provider: React.FC<IThemeProvider> = props => {
74102 value = { {
75103 token : { ...tokenConfig , ...token } ,
76104 components : { ...componentsConfig , ...components } ,
77- handleThemeOrLocale ,
105+ updateStudio ,
78106 algorithm,
79107 locale : locale ,
80108 isLight,
0 commit comments