@@ -29,7 +29,11 @@ function hasThemeStorage() {
2929}
3030
3131function getSystemDark ( ) {
32- return typeof window !== "undefined" && window . matchMedia ( MEDIA_QUERY ) . matches ;
32+ return (
33+ typeof window !== "undefined" &&
34+ typeof window . matchMedia === "function" &&
35+ window . matchMedia ( MEDIA_QUERY ) . matches
36+ ) ;
3337}
3438
3539function getStored ( ) : Theme {
@@ -90,7 +94,7 @@ export function syncBrowserChromeTheme() {
9094
9195function applyTheme ( theme : Theme , suppressTransitions = false ) {
9296 if ( typeof document === "undefined" || typeof window === "undefined" ) return ;
93- const systemDark = getSystemDark ( ) ;
97+ const systemDark = theme === "system" ? getSystemDark ( ) : false ;
9498 if ( lastAppliedTheme ?. theme === theme && lastAppliedTheme . systemDark === systemDark ) {
9599 syncDesktopTheme ( theme ) ;
96100 return ;
@@ -156,12 +160,12 @@ function subscribe(listener: () => void): () => void {
156160 listeners . push ( listener ) ;
157161
158162 // Listen for system preference changes
159- const mq = window . matchMedia ( MEDIA_QUERY ) ;
163+ const mq = typeof window . matchMedia === "function" ? window . matchMedia ( MEDIA_QUERY ) : null ;
160164 const handleChange = ( ) => {
161165 if ( getStored ( ) === "system" ) applyTheme ( "system" , true ) ;
162166 emitChange ( ) ;
163167 } ;
164- mq . addEventListener ( "change" , handleChange ) ;
168+ mq ? .addEventListener ( "change" , handleChange ) ;
165169
166170 // Listen for storage changes from other tabs
167171 const handleStorage = ( e : StorageEvent ) => {
@@ -174,7 +178,7 @@ function subscribe(listener: () => void): () => void {
174178
175179 return ( ) => {
176180 listeners = listeners . filter ( ( l ) => l !== listener ) ;
177- mq . removeEventListener ( "change" , handleChange ) ;
181+ mq ? .removeEventListener ( "change" , handleChange ) ;
178182 window . removeEventListener ( "storage" , handleStorage ) ;
179183 } ;
180184}
0 commit comments