File tree Expand file tree Collapse file tree 5 files changed +73
-59
lines changed
packages/vuepress-theme-reco/components Expand file tree Collapse file tree 5 files changed +73
-59
lines changed Original file line number Diff line number Diff line change 1313</template >
1414
1515<script >
16- import setMode from ' ./setMode '
16+ import applyMode from ' ./applyMode '
1717export default {
1818 name: ' ModeOptions' ,
1919
@@ -29,18 +29,27 @@ export default {
2929 },
3030
3131 mounted () {
32- const mode = localStorage .getItem (' mode' )
33- const { mode: customMode , modePicker } = this .$themeConfig
34- const themeMode = customMode || ' auto'
35- this .currentMode = modePicker === false ? themeMode : mode || themeMode
36- setMode (this .currentMode )
32+ // modePicker 开启时默认使用用户主动设置的模式
33+ this .currentMode = localStorage .getItem (' mode' ) || this .$themeConfig .mode || ' auto'
34+
35+ // Dark and Light autoswitches
36+ // 为了避免在 server-side 被执行,故在 Vue 组件中设置监听器
37+ var that = this
38+ window .matchMedia (' (prefers-color-scheme: dark)' ).addListener (() => {
39+ that .$data .currentMode === ' auto' && applyMode (that .$data .currentMode )
40+ })
41+ window .matchMedia (' (prefers-color-scheme: light)' ).addListener (() => {
42+ that .$data .currentMode === ' auto' && applyMode (that .$data .currentMode )
43+ })
44+
45+ applyMode (this .currentMode )
3746 },
3847
3948 methods: {
4049 selectMode (mode ) {
4150 if (mode !== this .currentMode ) {
4251 this .currentMode = mode
43- setMode (mode)
52+ applyMode (mode)
4453 localStorage .setItem (' mode' , mode)
4554 }
4655 },
Original file line number Diff line number Diff line change 1+ import modeOptions from './modeOptions'
2+
3+ function render ( mode ) {
4+ const rootElement = document . querySelector ( ':root' )
5+ const options = modeOptions [ mode ]
6+
7+ for ( const k in options ) {
8+ rootElement . style . setProperty ( k , options [ k ] )
9+ }
10+ }
11+
12+ /**
13+ * Sets a color scheme for the website.
14+ * If browser supports "prefers-color-scheme", 'auto' mode will respect the setting for light or dark mode
15+ * otherwise it will set a dark theme during night time
16+ */
17+ export default function applyMode ( mode ) {
18+ if ( mode !== 'auto' ) {
19+ render ( mode )
20+ return
21+ }
22+
23+ const isDarkMode = window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches
24+ const isLightMode = window . matchMedia ( '(prefers-color-scheme: light)' ) . matches
25+
26+ if ( isDarkMode ) render ( 'dark' )
27+ if ( isLightMode ) render ( 'light' )
28+
29+ if ( ! isDarkMode && ! isLightMode ) {
30+ console . log ( 'You specified no preference for a color scheme or your browser does not support it. I schedule dark mode during night time.' )
31+ const hour = new Date ( ) . getHours ( )
32+ if ( hour < 6 || hour >= 18 ) render ( 'dark' )
33+ else render ( 'light' )
34+ }
35+ }
Original file line number Diff line number Diff line change 11<template >
2- <div v-click-outside =" hideMenu" class =" color-picker" >
2+ <div v-click-outside =" hideMenu" class =" color-picker" v-if = " $themeConfig.modePicker !== false " >
33 <a class =" color-button" @click.prevent =" showMenu = !showMenu" >
44 <i class =" iconfont reco-color" ></i >
55 </a >
1414<script >
1515import ClickOutside from ' vue-click-outside'
1616import ModePicker from ' ./ModePicker'
17+ import applyMode from ' ./applyMode'
1718
1819export default {
1920 name: ' UserSettings' ,
@@ -32,6 +33,25 @@ export default {
3233 }
3334 },
3435
36+ // 为了在保证 modePicker 在 SSR 中正确开关,并实现管理,Mode 组件将负责 modePicker 关闭的情况
37+ mounted () {
38+ // modePicker 关闭时默认使用主题设置的模式
39+ const themeMode = this .$themeConfig .mode || ' auto'
40+ const { modePicker } = this .$themeConfig
41+ if (modePicker === false ) {
42+ // 为 'auto' 模式设置监听器
43+ if (themeMode === ' auto' ) {
44+ window .matchMedia (' (prefers-color-scheme: dark)' ).addListener (() => {
45+ applyMode (themeMode)
46+ })
47+ window .matchMedia (' (prefers-color-scheme: light)' ).addListener (() => {
48+ applyMode (themeMode)
49+ })
50+ }
51+ applyMode (themeMode)
52+ }
53+ },
54+
3555 methods: {
3656 hideMenu () {
3757 this .showMenu = false
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 2222 'max-width': linksWrapMaxWidth + 'px'
2323 } : {}" >
2424
25- <Mode v-if = " $themeConfig.modePicker !== false " />
25+ <Mode />
2626 <AlgoliaSearchBox
2727 v-if =" isAlgoliaSearch"
2828 :options =" algolia" />
You can’t perform that action at this time.
0 commit comments