1- import { createTheme , ThemeProvider } from '@mui/material/styles' ;
21import React , { useCallback , useEffect , useMemo , useState } from 'react' ;
32import { translate } from 'react-polyglot' ;
43import { connect } from 'react-redux' ;
@@ -20,18 +19,21 @@ import { currentBackend } from '@staticcms/core/backend';
2019import { changeTheme } from '../actions/globalUI' ;
2120import { invokeEvent } from '../lib/registry' ;
2221import { getDefaultPath } from '../lib/util/collection.util' ;
22+ import { isNotNullish } from '../lib/util/null.util' ;
23+ import { isEmpty } from '../lib/util/string.util' ;
2324import { generateClassNames } from '../lib/util/theming.util' ;
24- import { selectTheme } from '../reducers/selectors/globalUI ' ;
25- import { useAppDispatch , useAppSelector } from '../store/hooks ' ;
25+ import { useAppDispatch } from '../store/hooks ' ;
26+ import NotFoundPage from './NotFoundPage ' ;
2627import CollectionRoute from './collections/CollectionRoute' ;
2728import { Alert } from './common/alert/Alert' ;
2829import { Confirm } from './common/confirm/Confirm' ;
2930import Loader from './common/progress/Loader' ;
3031import EditorRoute from './entry-editor/EditorRoute' ;
3132import MediaPage from './media-library/MediaPage' ;
32- import NotFoundPage from './NotFoundPage' ;
3333import Page from './page/Page' ;
3434import Snackbars from './snackbar/Snackbars' ;
35+ import ThemeManager from './theme/ThemeManager' ;
36+ import useTheme from './theme/hooks/useTheme' ;
3537
3638import type { Credentials , TranslatedProps } from '@staticcms/core/interface' ;
3739import type { RootState } from '@staticcms/core/store' ;
@@ -78,25 +80,7 @@ const App = ({
7880 const navigate = useNavigate ( ) ;
7981 const dispatch = useAppDispatch ( ) ;
8082
81- const mode = useAppSelector ( selectTheme ) ;
82-
83- const theme = React . useMemo (
84- ( ) =>
85- createTheme ( {
86- palette : {
87- mode,
88- primary : {
89- main : 'rgb(37 99 235)' ,
90- } ,
91- ...( mode === 'dark' && {
92- background : {
93- paper : 'rgb(15 23 42)' ,
94- } ,
95- } ) ,
96- } ,
97- } ) ,
98- [ mode ] ,
99- ) ;
83+ const theme = useTheme ( ) ;
10084
10185 const configError = useCallback (
10286 ( error ?: string ) => {
@@ -175,21 +159,6 @@ const App = ({
175159 dispatch ( discardDraft ( ) ) ;
176160 } , [ dispatch , pathname , searchParams ] ) ;
177161
178- useEffect ( ( ) => {
179- // On page load or when changing themes, best to add inline in `head` to avoid FOUC
180- if (
181- localStorage . getItem ( 'color-theme' ) === 'dark' ||
182- ( ! ( 'color-theme' in localStorage ) &&
183- window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches )
184- ) {
185- document . documentElement . classList . add ( 'dark' ) ;
186- dispatch ( changeTheme ( 'dark' ) ) ;
187- } else {
188- document . documentElement . classList . remove ( 'dark' ) ;
189- dispatch ( changeTheme ( 'light' ) ) ;
190- }
191- } , [ dispatch ] ) ;
192-
193162 const [ prevUser , setPrevUser ] = useState ( user ) ;
194163 useEffect ( ( ) => {
195164 if ( ! prevUser && user ) {
@@ -255,6 +224,20 @@ const App = ({
255224 } ) ;
256225 } , [ ] ) ;
257226
227+ useEffect ( ( ) => {
228+ const defaultTheme = config . config ?. theme ?. default_theme ;
229+ if ( isEmpty ( defaultTheme ) ) {
230+ return ;
231+ }
232+
233+ const themeName = localStorage . getItem ( 'color-theme' ) ;
234+ if ( isNotNullish ( themeName ) ) {
235+ return ;
236+ }
237+
238+ dispatch ( changeTheme ( defaultTheme ) ) ;
239+ } , [ config . config ?. theme ?. default_theme , dispatch ] ) ;
240+
258241 if ( ! config . config ) {
259242 return configError ( t ( 'app.app.configNotFound' ) ) ;
260243 }
@@ -268,7 +251,7 @@ const App = ({
268251 }
269252
270253 return (
271- < ThemeProvider theme = { theme } >
254+ < ThemeManager theme = { theme } element = { document . documentElement } >
272255 < ScrollSync key = "scroll-sync" enabled = { scrollSyncEnabled } >
273256 < >
274257 < div key = "back-to-top-anchor" id = "back-to-top-anchor" />
@@ -282,7 +265,7 @@ const App = ({
282265 </ div >
283266 </ >
284267 </ ScrollSync >
285- </ ThemeProvider >
268+ </ ThemeManager >
286269 ) ;
287270} ;
288271
0 commit comments