1
1
import useRGS from "r18gs" ;
2
2
import type { SetStateAction } from "r18gs/use-rgs" ;
3
3
import * as React from "react" ;
4
- import type { ColorSchemePreference , ThemeState } from "../../hooks/use-theme " ;
5
- import { DEFAULT_ID } from "../../constants" ;
4
+ import type { ColorSchemePreference , ThemeState } from "../../constants " ;
5
+ import { DEFAULT_ID , DEFAULT_THEME_STATE } from "../../constants" ;
6
6
7
7
export interface ThemeSwitcherProps {
8
8
/** id of target element to apply classes to. This is useful when you want to apply theme only to specific container. */
@@ -20,6 +20,7 @@ function useMediaQuery(setThemeState: SetStateAction<ThemeState>) {
20
20
const updateSystemColorScheme = ( ) => {
21
21
setThemeState ( state => ( { ...state , systemColorScheme : media . matches ? "dark" : "light" } ) ) ;
22
22
} ;
23
+ updateSystemColorScheme ( ) ;
23
24
media . addEventListener ( "change" , updateSystemColorScheme ) ;
24
25
return ( ) => {
25
26
media . removeEventListener ( "change" , updateSystemColorScheme ) ;
@@ -103,7 +104,7 @@ function updateDOM({ targetId, themeState, dontSync }: UpdateDOMProps) {
103
104
104
105
export function ThemeSwitcher ( { targetId, dontSync, themeTransition } : ThemeSwitcherProps ) {
105
106
if ( targetId === "" ) throw new Error ( "id can not be an empty string" ) ;
106
- const [ themeState , setThemeState ] = useRGS < ThemeState > ( targetId ?? DEFAULT_ID ) ;
107
+ const [ themeState , setThemeState ] = useRGS < ThemeState > ( targetId ?? DEFAULT_ID , DEFAULT_THEME_STATE ) ;
107
108
108
109
useMediaQuery ( setThemeState ) ;
109
110
0 commit comments