Skip to content

Commit c489b81

Browse files
committed
refactor: clean dark mode
1 parent e991375 commit c489b81

File tree

5 files changed

+40
-102
lines changed

5 files changed

+40
-102
lines changed

src/App.vue

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
<script setup lang="ts">
2-
import { storeToRefs } from 'pinia'
3-
import useAppStore from '@/stores/modules/app'
42
import useRouteCache from '@/stores/modules/routeCache'
5-
import useAutoThemeSwitcher from '@/hooks/useAutoThemeSwitcher'
63
74
useHead({
85
title: 'Vue3 Vant Mobile',
@@ -25,32 +22,27 @@ useHead({
2522
],
2623
})
2724
28-
const appStore = useAppStore()
29-
const { mode } = storeToRefs(appStore)
30-
31-
const { initializeThemeSwitcher } = useAutoThemeSwitcher(appStore)
32-
3325
const keepAliveRouteNames = computed(() => {
3426
return useRouteCache().routeCaches as string[]
3527
})
3628
37-
onMounted(() => {
38-
initializeThemeSwitcher()
29+
const mode = computed(() => {
30+
return isDark.value ? 'dark' : 'light'
3931
})
4032
</script>
4133

4234
<template>
43-
<VanConfigProvider :theme="mode">
44-
<NavBar />
35+
<van-config-provider :theme="mode">
36+
<nav-bar />
4537
<router-view v-slot="{ Component, route }">
4638
<section class="app-wrapper">
4739
<keep-alive :include="keepAliveRouteNames">
4840
<component :is="Component" :key="route.name" />
4941
</keep-alive>
5042
</section>
5143
</router-view>
52-
<TabBar />
53-
</VanConfigProvider>
44+
<tab-bar />
45+
</van-config-provider>
5446
</template>
5547

5648
<style scoped>

src/hooks/useAutoThemeSwitcher.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/pages/index.vue

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
<script setup lang="ts">
22
import type { PickerColumn } from 'vant'
3-
import useAppStore from '@/stores/modules/app'
43
import { languageColumns, locale } from '@/utils/i18n'
54
6-
const appStore = useAppStore()
7-
const checked = ref<boolean>(isDark.value)
8-
9-
watch(
10-
() => isDark.value,
11-
(newMode) => {
12-
checked.value = newMode
13-
},
14-
{ immediate: true },
15-
)
16-
17-
function toggle() {
18-
toggleDark()
19-
appStore.switchMode(isDark.value ? 'dark' : 'light')
20-
}
21-
225
const { t } = useI18n()
236
7+
const menuItems = computed(() => ([
8+
{ title: t('menus.mockGuide'), route: 'mock' },
9+
{ title: t('menus.echartsDemo'), route: 'charts' },
10+
{ title: t('menus.unocssExample'), route: 'unocss' },
11+
{ title: t('menus.persistPiniaState'), route: 'counter' },
12+
{ title: t('menus.keepAlive'), route: 'keepalive' },
13+
{ title: t('menus.404Demo'), route: 'unknown' },
14+
]))
15+
2416
const showLanguagePicker = ref(false)
2517
const languageValues = ref<Array<string>>([locale.value])
2618
const language = computed(() => languageColumns.find(l => l.value === locale.value).text)
@@ -30,37 +22,44 @@ function onLanguageConfirm(event: { selectedOptions: PickerColumn }) {
3022
showLanguagePicker.value = false
3123
}
3224
33-
const menuItems = computed(() => ([
34-
{ title: t('menus.mockGuide'), route: 'mock' },
35-
{ title: t('menus.echartsDemo'), route: 'charts' },
36-
{ title: t('menus.unocssExample'), route: 'unocss' },
37-
{ title: t('menus.persistPiniaState'), route: 'counter' },
38-
{ title: t('menus.keepAlive'), route: 'keepalive' },
39-
{ title: t('menus.404Demo'), route: 'unknown' },
40-
]))
25+
const checked = ref<boolean>(isDark.value)
26+
27+
watchEffect(() => {
28+
checked.value = isDark.value
29+
})
30+
31+
function toggle(val: boolean) {
32+
checked.value = val
33+
toggleDark()
34+
}
4135
</script>
4236

4337
<template>
44-
<VanCellGroup :title="t('menus.basicSettings')" :border="false" :inset="true">
45-
<VanCell center :title="t('menus.darkMode')">
38+
<van-cell-group :title="t('menus.basicSettings')" :border="false" :inset="true">
39+
<van-cell center :title="t('menus.darkMode')">
4640
<template #right-icon>
47-
<VanSwitch v-model="checked" size="20px" aria-label="on/off Dark Mode" @click="toggle()" />
41+
<van-switch
42+
v-model="checked"
43+
size="20px"
44+
aria-label="on/off Dark Mode"
45+
@change="toggle"
46+
/>
4847
</template>
49-
</VanCell>
48+
</van-cell>
5049

51-
<VanCell
50+
<van-cell
5251
is-link
5352
:title="t('menus.language')"
5453
:value="language"
5554
@click="showLanguagePicker = true"
5655
/>
57-
</VanCellGroup>
56+
</van-cell-group>
5857

59-
<VanCellGroup :title="t('menus.exampleComponents')" :border="false" :inset="true">
58+
<van-cell-group :title="t('menus.exampleComponents')" :border="false" :inset="true">
6059
<template v-for="item in menuItems" :key="item.route">
61-
<VanCell :title="item.title" :to="item.route" is-link />
60+
<van-cell :title="item.title" :to="item.route" is-link />
6261
</template>
63-
</VanCellGroup>
62+
</van-cell-group>
6463

6564
<van-popup v-model:show="showLanguagePicker" position="bottom">
6665
<van-picker

src/stores/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { createPinia } from 'pinia'
22
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
33

4-
import useAppStore from './modules/app'
54
import useUserStore from './modules/user'
65

76
const pinia = createPinia()
87
pinia.use(piniaPluginPersistedstate)
98

10-
export { useAppStore, useUserStore }
9+
export { useUserStore }
1110
export default pinia

src/stores/modules/app.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)