File tree Expand file tree Collapse file tree 4 files changed +45
-0
lines changed Expand file tree Collapse file tree 4 files changed +45
-0
lines changed Original file line number Diff line number Diff line change 22import { storeToRefs } from ' pinia'
33import useAppStore from ' @/stores/modules/app'
44import useRouteTransitionNameStore from ' @/stores/modules/routeTransitionName'
5+ import useAutoThemeSwitcher from ' @/hooks/useAutoThemeSwitcher'
56
67useHead ({
78 title: ' Vue3 Vant Mobile' ,
@@ -22,6 +23,12 @@ const { mode } = storeToRefs(appStore)
2223
2324const routeTransitionNameStore = useRouteTransitionNameStore ()
2425const { routeTransitionName } = storeToRefs (routeTransitionNameStore )
26+
27+ const { initializeThemeSwitcher } = useAutoThemeSwitcher (appStore )
28+
29+ onMounted (() => {
30+ initializeThemeSwitcher ()
31+ })
2532 </script >
2633
2734<template >
Original file line number Diff line number Diff line change 1+ import type { AppStore } from '@/stores/modules/app'
2+
3+ export default function useAutoThemeSwitcher ( appStore : AppStore ) {
4+ const handleAttributeChange = ( ) => {
5+ const rootElement = document . documentElement
6+ if ( rootElement . classList . contains ( 'dark' ) )
7+ appStore . swithMode ( 'dark' )
8+ else
9+ appStore . swithMode ( 'light' )
10+ }
11+
12+ const observerOptions = {
13+ attributes : true ,
14+ attributeFilter : [ 'class' ] ,
15+ }
16+
17+ const observer = new MutationObserver ( handleAttributeChange )
18+
19+ const targetElement = document . querySelector ( 'html' )
20+
21+ const initializeThemeSwitcher = ( ) => {
22+ observer . observe ( targetElement , observerOptions )
23+ }
24+
25+ return { initializeThemeSwitcher }
26+ }
Original file line number Diff line number Diff line change 11import { defineStore } from 'pinia'
22import type { ConfigProviderTheme } from 'vant'
33
4+ export interface AppStore {
5+ swithMode : ( val : ConfigProviderTheme ) => void
6+ }
7+
48const prefersDark
59= window . matchMedia
610&& window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches
Original file line number Diff line number Diff line change @@ -11,6 +11,14 @@ definePage({
1111const appStore = useAppStore ()
1212const checked = ref <boolean >(isDark .value )
1313
14+ watch (
15+ () => isDark .value ,
16+ (newMode ) => {
17+ checked .value = newMode
18+ },
19+ { immediate: true },
20+ )
21+
1422function toggle() {
1523 toggleDark ()
1624 appStore .swithMode (isDark .value ? ' dark' : ' light' )
You can’t perform that action at this time.
0 commit comments