Skip to content

Commit bb9f1aa

Browse files
committed
fix: resolve SSR hydration and UI flashing (#1)
1 parent d635854 commit bb9f1aa

File tree

11 files changed

+64
-36
lines changed

11 files changed

+64
-36
lines changed

app/app.vue

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ const keepAliveRouteNames = computed(() => {
2020

2121
<template>
2222
<VanConfigProvider :theme="mode">
23-
<ColorScheme tag="div">
24-
<NuxtLoadingIndicator color="repeating-linear-gradient(to right,var(--c-primary) 0%,var(--c-primary-active) 100%)" />
25-
<NuxtLayout>
26-
<NuxtPage :keepalive="{ include: keepAliveRouteNames }" />
27-
</NuxtLayout>
28-
</ColorScheme>
23+
<NuxtLoadingIndicator color="repeating-linear-gradient(to right,var(--c-primary) 0%,var(--c-primary-active) 100%)" />
24+
<NuxtLayout>
25+
<NuxtPage :keepalive="{ include: keepAliveRouteNames }" />
26+
</NuxtLayout>
2927
</VanConfigProvider>
3028
</template>

app/components/AppFooter.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ const route = useRoute()
55
66
const active = ref(0)
77
8-
const display = computed(() => {
8+
const show = computed(() => {
99
if (route.name && names.includes(route.name))
1010
return true
1111
return false
1212
})
1313
</script>
1414

1515
<template>
16-
<van-tabbar v-show="display" v-model="active" route>
16+
<van-tabbar v-if="show" v-model="active" route placeholder fixed>
1717
<van-tabbar-item replace to="/">
1818
<span>{{ $t('tabbar.home') }}</span>
1919
<template #icon>

app/components/AppHeader.vue

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<script setup lang="ts">
2+
import { useAppFooterRouteNames as routeWhiteList } from '~/config'
3+
24
const route = useRoute()
35
const router = useRouter()
46
@@ -16,14 +18,15 @@ const title = computed(() => {
1618
return ''
1719
return route.meta.i18n ? t(route.meta.i18n) : (route.meta.title || '')
1820
})
21+
22+
const showLeftArrow = computed(() => route.name && routeWhiteList.includes(route.name))
1923
</script>
2024

2125
<template>
2226
<VanNavBar
23-
v-show="title"
2427
:title="title"
25-
:fixed="true"
26-
clickable left-arrow
28+
:left-arrow="!showLeftArrow"
29+
placeholder clickable fixed
2730
@click-left="onBack"
2831
/>
2932
</template>

app/layouts/default.vue

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
1-
<script setup lang="ts">
2-
import { useAppFooterRouteNames as names } from '~/config'
3-
4-
const route = useRoute()
5-
6-
const classNames = computed(() => {
7-
return route.name && names.includes(route.name) ? 'py-20' : 'px-10 pt-60'
8-
})
9-
</script>
10-
111
<template>
12-
<main>
13-
<AppHeader />
2+
<main class="flex flex-col min-h-svh">
3+
<AppHeader class="h-[var(--van-nav-bar-height)]" />
144

15-
<div :class="classNames">
5+
<div class="flex-1 p-16 pb-[var(--van-nav-bar-height)]">
166
<slot />
177
</div>
188

app/pages/index.vue

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import type { PickerColumn } from 'vant'
44
import type { ComputedRef } from 'vue'
55
import { Locale } from 'vant'
66
7+
definePageMeta({
8+
layout: 'default',
9+
title: '主页',
10+
i18n: 'menu.home',
11+
})
12+
713
const color = useColorMode()
814
915
useHead({
@@ -17,14 +23,10 @@ useHead({
1723
const checked = computed({
1824
get: () => color.value === 'dark',
1925
set: (val: boolean) => {
20-
return val
26+
color.preference = val ? 'dark' : 'light'
2127
},
2228
})
2329
24-
function toggleDark() {
25-
color.preference = color.value === 'dark' ? 'light' : 'dark'
26-
}
27-
2830
const { setLocale, t } = useI18n()
2931
const i18n = useNuxtApp().$i18n
3032
@@ -57,12 +59,13 @@ function onLanguageConfirm(event: { selectedOptions: PickerColumn }) {
5759
<VanCellGroup inset>
5860
<VanCell :title="$t('menu.darkMode')" center>
5961
<template #right-icon>
60-
<VanSwitch
61-
v-model="checked"
62-
size="20px"
63-
aria-label="on/off Dark Mode"
64-
@click="toggleDark"
65-
/>
62+
<ClientOnly>
63+
<VanSwitch
64+
v-model="checked"
65+
size="20px"
66+
aria-label="on/off Dark Mode"
67+
/>
68+
</ClientOnly>
6669
</template>
6770
</VanCell>
6871

app/pages/profile/index.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
<script setup lang="ts">
2+
definePageMeta({
3+
layout: 'default',
4+
title: '我的',
5+
i18n: 'menu.profile',
6+
})
7+
</script>
8+
19
<template>
210
<div mx-auto mb-60 pt-15 text-center text-16 text-dark dark:text-white>
311
{{ $t('profile_page.txt') }}

app/styles/default-theme.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
:root:root {
22
--van-primary-color: var(--c-primary);
3+
--van-cell-group-inset-padding: 0;
34
}

app/utils/preload.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default function preload() {
2+
return `
3+
;(function() {
4+
const prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
5+
const setting = localStorage.getItem('nuxt-color-mode') || 'auto';
6+
if (setting === 'dark' || (prefersDark && setting !== 'light'))
7+
document.documentElement.classList.toggle('van-theme-dark', true);
8+
})()
9+
`
10+
}

i18n/locales/en-US.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"menu": {
3+
"home": "Home",
4+
"profile": "Profile",
35
"darkMode": "🌗 Dark Mode",
46
"language": "📚 Language",
57
"404Demo": "🙅 Page 404 Demo",

i18n/locales/zh-CN.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"menu": {
3+
"home": "主页",
4+
"profile": "我的",
35
"darkMode": "🌗 暗黑模式",
46
"language": "📚 语言",
57
"404Demo": "🙅 404页 演示",

0 commit comments

Comments
 (0)