Skip to content

Commit

Permalink
fix: If system set colorMode preference in storage to use in SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
ktquez committed Jun 10, 2020
1 parent 795c7ad commit 22e99cc
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/DarkMode.vue
Expand Up @@ -78,7 +78,7 @@ export default {
computed: {
getPrefersColorScheme () {
if (this.$isServer) return false
if (this.$isServer) return this.getStorage.getItem('colorModePrefer')
const colorSchemeTypes = ['dark', 'light']
let colorScheme = null
colorSchemeTypes.forEach(type => {
Expand Down Expand Up @@ -142,7 +142,10 @@ export default {
methods: {
setMode (chosenMode) {
this.chosenMode = chosenMode
if (this.getStorage) this.getStorage.setItem('colorMode', this.chosenMode)
if (this.getStorage) {
this.getStorage.setItem('colorMode', this.chosenMode)
if (this.chosenMode === 'system') this.getStorage.setItem('colorModePrefer', this.getPrefersColorScheme)
}
this.handleColorModeClass('add')
if (Object.keys(this.metaThemeColor).length) this.setMetaThemeColor(this.metaThemeColor[this.currentMode] || this.metaThemeColor[this.getPrefersColorScheme])
this.$emit('change-mode', this.chosenMode)
Expand Down Expand Up @@ -171,7 +174,7 @@ export default {
handleColorModeClass (action) {
const className = `${this.className.replace(/%cm/g, this.currentMode)}`
if (!this.$isServer) return document.documentElement.classList[action](className)
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
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
},
handlePreferColorScheme (e) {
Expand Down

0 comments on commit 22e99cc

Please sign in to comment.