1515</template >
1616
1717<script >
18+ import {
19+ storage ,
20+ getMediaQueryList
21+ } from ' ./utils'
22+
1823export default {
1924 name: ' DarkMode' ,
2025
@@ -34,7 +39,7 @@ export default {
3439 default: ' %cm-mode'
3540 },
3641 storage: {
37- type: String ,
42+ type: [ String , Object ] ,
3843 default: ' localStorage'
3944 },
4045 metaThemeColor: {
@@ -70,7 +75,7 @@ export default {
7075 const colorSchemeTypes = [' dark' , ' light' ]
7176 let colorScheme = null
7277 colorSchemeTypes .forEach (type => {
73- if (this . getMediaQueryList (type).matches ) {
78+ if (getMediaQueryList (type).matches ) {
7479 colorScheme = type
7580 }
7681 })
@@ -90,8 +95,12 @@ export default {
9095 return this .modes [currentIndex === (this .modes .length - 1 ) ? 0 : currentIndex + 1 ]
9196 },
9297
98+ getStorage () {
99+ return storage (this .storage )
100+ },
101+
93102 getStorageColorMode () {
94- return window [ this .storage ] .getItem (' colorMode' )
103+ return this .getStorage .getItem (' colorMode' )
95104 },
96105
97106 isSystem () {
@@ -111,7 +120,7 @@ export default {
111120
112121 mounted () {
113122 this .metaThemeColorElement = document .querySelector (' meta[name="theme-color"]' )
114- this .listenerDark = this . getMediaQueryList (' dark' )
123+ this .listenerDark = getMediaQueryList (' dark' )
115124 this .listenerDark .addListener (this .handlePreferColorScheme )
116125 this .toggleFavicon (this .getPrefersColorScheme )
117126 },
@@ -123,16 +132,12 @@ export default {
123132 methods: {
124133 setMode (chosenMode ) {
125134 this .chosenMode = chosenMode
126- window [ this .storage ] .setItem (' colorMode' , this .chosenMode )
135+ this .getStorage .setItem (' colorMode' , this .chosenMode )
127136 this .handleColorModeClass (' add' )
128137 if (Object .keys (this .metaThemeColor ).length ) this .setMetaThemeColor (this .metaThemeColor [this .currentMode ] || this .metaThemeColor [this .getPrefersColorScheme ])
129138 this .$emit (' change-mode' , this .chosenMode )
130139 },
131140
132- getMediaQueryList (type ) {
133- return window .matchMedia (` (prefers-color-scheme: ${ type} )` )
134- },
135-
136141 setMetaThemeColor (color ) {
137142 if (color) {
138143 this .$nextTick (() => {
0 commit comments