Skip to content

Commit 22e99cc

Browse files
committed
fix: If system set colorMode preference in storage to use in SSR
1 parent 795c7ad commit 22e99cc

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Diff for: src/DarkMode.vue

+6-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export default {
7878
7979
computed: {
8080
getPrefersColorScheme () {
81-
if (this.$isServer) return false
81+
if (this.$isServer) return this.getStorage.getItem('colorModePrefer')
8282
const colorSchemeTypes = ['dark', 'light']
8383
let colorScheme = null
8484
colorSchemeTypes.forEach(type => {
@@ -142,7 +142,10 @@ export default {
142142
methods: {
143143
setMode (chosenMode) {
144144
this.chosenMode = chosenMode
145-
if (this.getStorage) this.getStorage.setItem('colorMode', this.chosenMode)
145+
if (this.getStorage) {
146+
this.getStorage.setItem('colorMode', this.chosenMode)
147+
if (this.chosenMode === 'system') this.getStorage.setItem('colorModePrefer', this.getPrefersColorScheme)
148+
}
146149
this.handleColorModeClass('add')
147150
if (Object.keys(this.metaThemeColor).length) this.setMetaThemeColor(this.metaThemeColor[this.currentMode] || this.metaThemeColor[this.getPrefersColorScheme])
148151
this.$emit('change-mode', this.chosenMode)
@@ -171,7 +174,7 @@ export default {
171174
handleColorModeClass (action) {
172175
const className = `${this.className.replace(/%cm/g, this.currentMode)}`
173176
if (!this.$isServer) return document.documentElement.classList[action](className)
174-
this.$ssrContext.colorModeClass = this.currentMode === 'system' ? '' : className // Adds the className in the ssr context for the user to insert as they wish in the HTML tag
177+
this.$ssrContext.colorModeClass = this.currentMode === 'system' ? `${this.className.replace(/%cm/g, this.getPrefersColorScheme)}` : className // Adds the className in the ssr context for the user to insert as they wish in the HTML tag
175178
},
176179
177180
handlePreferColorScheme (e) {

0 commit comments

Comments
 (0)