Commit be2129f 1 parent f8bb0cf commit be2129f Copy full SHA for be2129f
File tree 1 file changed +21
-15
lines changed
1 file changed +21
-15
lines changed Original file line number Diff line number Diff line change 1
1
const themes = [ 'system' , 'light' , 'dark' ] as const
2
2
export type Themes = typeof themes [ number ]
3
3
4
- export const themePreference = useLocalStorage < Themes > ( 'theme_preference' , 'system' , {
5
- listenToStorageChanges : true ,
6
- serializer : {
7
- read ( raw ) : Themes {
8
- return themes . includes ( raw as any ) ? ( raw as Themes ) : 'system'
9
- } ,
10
- write ( value ) : string {
11
- return value
12
- } ,
13
- } ,
14
- } )
15
-
16
- export const isDark = computed ( ( ) =>
17
- themePreference . value === 'system' ? usePreferredDark ( ) . value : themePreference . value === 'dark' ,
18
- )
4
+ export const themePreference = ref < Themes > ( 'system' )
5
+ export const isDark = ref ( false )
19
6
20
7
export function applyTheme ( ) {
21
8
// Attention: only client has 'document' object
22
9
if ( process . client ) {
10
+ const storedPreference = useLocalStorage < Themes > ( 'theme_preference' , 'system' , {
11
+ listenToStorageChanges : true ,
12
+ serializer : {
13
+ read ( raw ) : Themes {
14
+ return themes . includes ( raw as any ) ? ( raw as Themes ) : 'system'
15
+ } ,
16
+ write ( value ) : string {
17
+ return value
18
+ } ,
19
+ } ,
20
+ } )
21
+ themePreference . value = storedPreference . value
22
+
23
+ watchEffect ( ( ) => {
24
+ isDark . value = computed ( ( ) =>
25
+ themePreference . value === 'system' ? usePreferredDark ( ) . value : themePreference . value === 'dark' ,
26
+ ) . value
27
+ } )
28
+
23
29
const html = document . documentElement
24
30
const metaTag = document . querySelector ( 'meta[name="theme-color"]' )
25
31
watchEffect ( ( ) => {
You can’t perform that action at this time.
0 commit comments