File tree Expand file tree Collapse file tree 2 files changed +56
-1
lines changed Expand file tree Collapse file tree 2 files changed +56
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,8 @@ const vue3VantMobile: DefaultTheme.NavItemWithLink[] = [
1010 { text : '目录结构' , link : '/vue3-vant-mobile/directory' } ,
1111 { text : '图标' , link : '/vue3-vant-mobile/icons' } ,
1212 { text : '浏览器适配' , link : '/vue3-vant-mobile/browser-adaptation' } ,
13- { text : '路由' , link : '/vue3-vant-mobile/routing' }
13+ { text : '路由' , link : '/vue3-vant-mobile/routing' } ,
14+ { text : '暗黑模式' , link : '/vue3-vant-mobile/dark-mode' } ,
1415]
1516
1617const vue3VarletMobile : DefaultTheme . NavItemWithLink [ ] = [
Original file line number Diff line number Diff line change 1+ ---
2+ title : 暗黑模式
3+ description : 如何只使用某种模式
4+ outline : deep
5+ ---
6+
7+ # :last_quarter_moon : 暗黑模式
8+
9+ ### 初始化模式 {#initialization-mode}
10+
11+ 根据本地存储的主题设置和系统偏好,自动为页面添加或移除 dark 类,实现暗黑模式的切换。
12+
13+ ``` ts
14+ <script >
15+ ;(function () {
16+ const prefersDark = window .matchMedia && window .matchMedia (' (prefers-color-scheme: dark)' ).matches
17+ const setting = localStorage .getItem (' vueuse-color-scheme' ) || ' auto'
18+ if (setting === ' dark' || (prefersDark && setting !== ' light' ))
19+ document .documentElement .classList .toggle (' dark' , true )
20+ })()
21+ < / script >
22+ ```
23+
24+ ### 切换模式 {#switch-mode}
25+
26+ 统一管理暗黑模式的状态、切换和用户偏好,方便在 Vue 组件中直接使用。
27+
28+ ``` text
29+ src/composables/
30+ └── composables/
31+ └── dark.ts
32+ ```
33+
34+ ``` ts
35+ // these APIs are auto-imported from @vueuse/core
36+ export const isDark = useDark ()
37+ export const toggleDark = useToggle (isDark )
38+ export const preferredDark = usePreferredDark ()
39+ ```
40+
41+
42+ ## 常见问题 {#FAQ}
43+
44+ ### 固定模式 {#fixed-mode}
45+
46+ 固定使用某种模式,并且不受设备、浏览器影响。例如固定` light ` 模式,只需在` dark.ts ` 文件添加一行代码即可。
47+
48+ ``` ts{2}
49+ // these APIs are auto-imported from @vueuse/core
50+ useDark().value = false
51+ export const isDark = useDark()
52+ export const toggleDark = useToggle(isDark)
53+ export const preferredDark = usePreferredDark()
54+ ```
You can’t perform that action at this time.
0 commit comments