Skip to content

Commit

Permalink
feat: auto detect color preference (#238)
Browse files Browse the repository at this point in the history
* auto detect color preference

* tweaks

* enable detectSystemDarkTheme
  • Loading branch information
egoist authored Jun 10, 2019
1 parent 6fe1fc0 commit eded04a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
24 changes: 12 additions & 12 deletions src/plugins/dark-theme-toggler/DarkThemeToggler.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,22 @@
<script>
export default {
data() {
const themeStore = localStorage.getItem('docute:theme')
const dark =
'dark' in this.$route.query
? true
: themeStore === 'dark'
? true
: themeStore === 'default'
? false
: this.$store.getters.config.theme === 'dark'
return {
dark:
localStorage.getItem('docute:theme') === 'dark' ||
'dark' in this.$route.query ||
this.$store.getters.config.theme === 'dark'
dark
}
},
created() {
if (this.dark) {
this.$store.commit('SET_THEME', 'dark')
}
this.$store.commit('SET_THEME', this.dark ? 'dark' : 'default')
},
methods: {
Expand All @@ -57,11 +61,7 @@ export default {
'SET_THEME',
this.dark ? 'dark' : prevTheme === 'dark' ? 'default' : prevTheme
)
if (this.dark) {
localStorage.setItem('docute:theme', 'dark')
} else {
localStorage.removeItem('docute:theme')
}
localStorage.setItem('docute:theme', this.dark ? 'dark' : 'default')
}
}
}
Expand Down
16 changes: 15 additions & 1 deletion src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ Vue.use(Vuex)

const initialState = inBrowser && window[INITIAL_STATE_NAME]

const getDefaultTheme = (store, {theme, detectSystemDarkTheme}) => {
if (!inBrowser || !detectSystemDarkTheme) {
return theme || 'default'
}

const mq = window.matchMedia('(prefers-color-scheme: dark)')

mq.addListener(() => {
store.commit('SET_THEME', mq.matches ? 'dark' : 'default')
})

return theme || (mq.matches ? 'dark' : 'default')
}

const store = new Vuex.Store({
state: {
originalConfig: {},
Expand All @@ -36,7 +50,7 @@ const store = new Vuex.Store({
if (config.centerContent) {
config.layout = 'narrow'
}
config.theme = config.theme || 'default'
config.theme = getDefaultTheme(store, config)
state.originalConfig = config
},

Expand Down
7 changes: 7 additions & 0 deletions website/docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ The text for *edit link*.

Site theme.

## detectSystemDarkTheme

- Type: `boolean`
- Default: `undefined`

In recent versions of macOS (Mojave) and Windows 10, users have been able to enable a system level dark mode. Set this option to `true` so that Docute will use the dark theme by default if your system has it enabled.

## darkThemeToggler

- Type: `boolean`
Expand Down
7 changes: 7 additions & 0 deletions website/docs/zh/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ https://github.com/USER/REPO/blob/master/docs

网站主题。

## detectSystemDarkTheme

- Type: `boolean`
- Default: `undefined`

In recent versions of macOS (Mojave) and Windows 10, users have been able to enable a system level dark mode. Set this option to `true` so that Docute will use the dark theme by default if your system has it enabled.

## darkThemeToggler

- Type: `boolean`
Expand Down
1 change: 1 addition & 0 deletions website/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ new Docute({
router: {
mode: 'history'
},
detectSystemDarkTheme: true,
darkThemeToggler: true,
sourcePath: '/',
componentMixins: [
Expand Down

0 comments on commit eded04a

Please sign in to comment.