@@ -883,7 +883,7 @@ function getFinalTheme(baseTheme) {
883883}
884884
885885/* Get the actual system theme is user selected system theme in preferences */
886- function getSystemTheme ( selectedTheme ) {
886+ function parseSystemTheme ( selectedTheme ) {
887887 if ( selectedTheme === 'system' ) {
888888 const systemMatchMedia = matchMedia ( '(prefers-color-scheme: dark)' ) ;
889889 return {
@@ -907,7 +907,7 @@ export default function Theme({children}) {
907907 'light' ;
908908
909909 // Initialize theme state
910- const [ theme , setTheme ] = useState ( getSystemTheme ( selectedTheme ) . theme ) ;
910+ const [ theme , setTheme ] = useState ( parseSystemTheme ( selectedTheme ) . theme ) ;
911911
912912 // Memoize the theme object
913913 const themeObj = useMemo ( ( ) => {
@@ -925,22 +925,17 @@ export default function Theme({children}) {
925925
926926 // Handle theme updates
927927 useEffect ( ( ) => {
928- if ( selectedTheme !== 'system' ) {
929- setTheme ( selectedTheme ) ;
930- return ;
931- }
932-
933- let { theme, systemMatchMedia} = getSystemTheme ( selectedTheme ) ;
928+ let { theme, systemMatchMedia} = parseSystemTheme ( selectedTheme ) ;
934929 setTheme ( theme ) ;
935930
936931 const updateTheme = ( event ) => {
937932 const newTheme = event . matches ? 'dark' : 'light' ;
938933 setTheme ( newTheme ) ;
939934 } ;
940- systemMatchMedia . addEventListener ( 'change' , updateTheme ) ;
935+ systemMatchMedia ? .addEventListener ( 'change' , updateTheme ) ;
941936
942937 return ( ) => {
943- systemMatchMedia . removeEventListener ( 'change' , updateTheme ) ;
938+ systemMatchMedia ? .removeEventListener ( 'change' , updateTheme ) ;
944939 } ;
945940 } , [ selectedTheme ] ) ;
946941
0 commit comments