Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit eded04a

Browse files
authored
feat: auto detect color preference (#238)
* auto detect color preference * tweaks * enable detectSystemDarkTheme
1 parent 6fe1fc0 commit eded04a

File tree

5 files changed

+42
-13
lines changed

5 files changed

+42
-13
lines changed

src/plugins/dark-theme-toggler/DarkThemeToggler.vue

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,22 @@
3535
<script>
3636
export default {
3737
data() {
38+
const themeStore = localStorage.getItem('docute:theme')
39+
const dark =
40+
'dark' in this.$route.query
41+
? true
42+
: themeStore === 'dark'
43+
? true
44+
: themeStore === 'default'
45+
? false
46+
: this.$store.getters.config.theme === 'dark'
3847
return {
39-
dark:
40-
localStorage.getItem('docute:theme') === 'dark' ||
41-
'dark' in this.$route.query ||
42-
this.$store.getters.config.theme === 'dark'
48+
dark
4349
}
4450
},
4551
4652
created() {
47-
if (this.dark) {
48-
this.$store.commit('SET_THEME', 'dark')
49-
}
53+
this.$store.commit('SET_THEME', this.dark ? 'dark' : 'default')
5054
},
5155
5256
methods: {
@@ -57,11 +61,7 @@ export default {
5761
'SET_THEME',
5862
this.dark ? 'dark' : prevTheme === 'dark' ? 'default' : prevTheme
5963
)
60-
if (this.dark) {
61-
localStorage.setItem('docute:theme', 'dark')
62-
} else {
63-
localStorage.removeItem('docute:theme')
64-
}
64+
localStorage.setItem('docute:theme', this.dark ? 'dark' : 'default')
6565
}
6666
}
6767
}

src/store.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ Vue.use(Vuex)
1515

1616
const initialState = inBrowser && window[INITIAL_STATE_NAME]
1717

18+
const getDefaultTheme = (store, {theme, detectSystemDarkTheme}) => {
19+
if (!inBrowser || !detectSystemDarkTheme) {
20+
return theme || 'default'
21+
}
22+
23+
const mq = window.matchMedia('(prefers-color-scheme: dark)')
24+
25+
mq.addListener(() => {
26+
store.commit('SET_THEME', mq.matches ? 'dark' : 'default')
27+
})
28+
29+
return theme || (mq.matches ? 'dark' : 'default')
30+
}
31+
1832
const store = new Vuex.Store({
1933
state: {
2034
originalConfig: {},
@@ -36,7 +50,7 @@ const store = new Vuex.Store({
3650
if (config.centerContent) {
3751
config.layout = 'narrow'
3852
}
39-
config.theme = config.theme || 'default'
53+
config.theme = getDefaultTheme(store, config)
4054
state.originalConfig = config
4155
},
4256

website/docs/options.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ The text for *edit link*.
155155

156156
Site theme.
157157

158+
## detectSystemDarkTheme
159+
160+
- Type: `boolean`
161+
- Default: `undefined`
162+
163+
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.
164+
158165
## darkThemeToggler
159166

160167
- Type: `boolean`

website/docs/zh/options.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ https://github.com/USER/REPO/blob/master/docs
155155

156156
网站主题。
157157

158+
## detectSystemDarkTheme
159+
160+
- Type: `boolean`
161+
- Default: `undefined`
162+
163+
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.
164+
158165
## darkThemeToggler
159166

160167
- Type: `boolean`

website/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ new Docute({
4848
router: {
4949
mode: 'history'
5050
},
51+
detectSystemDarkTheme: true,
5152
darkThemeToggler: true,
5253
sourcePath: '/',
5354
componentMixins: [

0 commit comments

Comments
 (0)