15
15
</template >
16
16
17
17
<script >
18
+ import {
19
+ storage ,
20
+ getMediaQueryList
21
+ } from ' ./utils'
22
+
18
23
export default {
19
24
name: ' DarkMode' ,
20
25
@@ -34,7 +39,7 @@ export default {
34
39
default: ' %cm-mode'
35
40
},
36
41
storage: {
37
- type: String ,
42
+ type: [ String , Object ] ,
38
43
default: ' localStorage'
39
44
},
40
45
metaThemeColor: {
@@ -70,7 +75,7 @@ export default {
70
75
const colorSchemeTypes = [' dark' , ' light' ]
71
76
let colorScheme = null
72
77
colorSchemeTypes .forEach (type => {
73
- if (this . getMediaQueryList (type).matches ) {
78
+ if (getMediaQueryList (type).matches ) {
74
79
colorScheme = type
75
80
}
76
81
})
@@ -90,8 +95,12 @@ export default {
90
95
return this .modes [currentIndex === (this .modes .length - 1 ) ? 0 : currentIndex + 1 ]
91
96
},
92
97
98
+ getStorage () {
99
+ return storage (this .storage )
100
+ },
101
+
93
102
getStorageColorMode () {
94
- return window [ this .storage ] .getItem (' colorMode' )
103
+ return this .getStorage .getItem (' colorMode' )
95
104
},
96
105
97
106
isSystem () {
@@ -111,7 +120,7 @@ export default {
111
120
112
121
mounted () {
113
122
this .metaThemeColorElement = document .querySelector (' meta[name="theme-color"]' )
114
- this .listenerDark = this . getMediaQueryList (' dark' )
123
+ this .listenerDark = getMediaQueryList (' dark' )
115
124
this .listenerDark .addListener (this .handlePreferColorScheme )
116
125
this .toggleFavicon (this .getPrefersColorScheme )
117
126
},
@@ -123,16 +132,12 @@ export default {
123
132
methods: {
124
133
setMode (chosenMode ) {
125
134
this .chosenMode = chosenMode
126
- window [ this .storage ] .setItem (' colorMode' , this .chosenMode )
135
+ this .getStorage .setItem (' colorMode' , this .chosenMode )
127
136
this .handleColorModeClass (' add' )
128
137
if (Object .keys (this .metaThemeColor ).length ) this .setMetaThemeColor (this .metaThemeColor [this .currentMode ] || this .metaThemeColor [this .getPrefersColorScheme ])
129
138
this .$emit (' change-mode' , this .chosenMode )
130
139
},
131
140
132
- getMediaQueryList (type ) {
133
- return window .matchMedia (` (prefers-color-scheme: ${ type} )` )
134
- },
135
-
136
141
setMetaThemeColor (color ) {
137
142
if (color) {
138
143
this .$nextTick (() => {
0 commit comments