|
1 | 1 | <script setup lang="ts"> |
2 | | -import OSSHeader from '@voidzero-dev/vitepress-theme/src/components/oss/Header.vue'; |
| 2 | +import OSSHeader from '@components/oss/Header.vue'; |
3 | 3 | import BaseTheme from '@voidzero-dev/vitepress-theme/src/viteplus'; |
4 | 4 | import { useData } from 'vitepress'; |
5 | | -import { onMounted, watch } from 'vue'; |
| 5 | +import { nextTick, watch } from 'vue'; |
6 | 6 |
|
7 | 7 | import Footer from './components/Footer.vue'; |
8 | 8 | import Home from './layouts/Home.vue'; |
9 | 9 | // import Error404 from "./layouts/Error404.vue"; |
10 | 10 |
|
11 | | -const { frontmatter } = useData(); |
| 11 | +const { frontmatter, isDark } = useData(); |
12 | 12 | const { Layout: BaseLayout } = BaseTheme; |
13 | 13 |
|
14 | | -const ensureHomeLight = () => { |
15 | | - if (frontmatter.value?.layout !== 'home' || typeof document === 'undefined') { |
| 14 | +const syncHomeThemeOverride = async () => { |
| 15 | + if (typeof document === 'undefined') { |
16 | 16 | return; |
17 | 17 | } |
18 | 18 |
|
19 | | - document.documentElement.classList.remove('dark'); |
20 | | -}; |
| 19 | + const isHome = frontmatter.value?.layout === 'home'; |
| 20 | + const root = document.documentElement; |
| 21 | +
|
| 22 | + if (isHome) { |
| 23 | + root.setAttribute('data-theme', 'light'); |
| 24 | + } else { |
| 25 | + root.removeAttribute('data-theme'); |
| 26 | + } |
| 27 | +
|
| 28 | + await nextTick(); |
| 29 | +
|
| 30 | + const header = document.querySelector<HTMLElement>('.home-header'); |
21 | 31 |
|
22 | | -onMounted(() => { |
23 | | - ensureHomeLight(); |
24 | | -}); |
| 32 | + if (!header) { |
| 33 | + return; |
| 34 | + } |
| 35 | +
|
| 36 | + if (isHome) { |
| 37 | + header.setAttribute('data-theme', 'light'); |
| 38 | + } else { |
| 39 | + header.removeAttribute('data-theme'); |
| 40 | + } |
| 41 | +}; |
25 | 42 |
|
26 | 43 | watch( |
27 | | - () => frontmatter.value?.layout, |
| 44 | + [() => frontmatter.value?.layout, () => isDark.value], |
28 | 45 | () => { |
29 | | - ensureHomeLight(); |
| 46 | + void syncHomeThemeOverride(); |
30 | 47 | }, |
| 48 | + { immediate: true }, |
31 | 49 | ); |
32 | 50 | </script> |
33 | 51 |
|
34 | 52 | <template> |
35 | 53 | <div v-if="frontmatter.layout === 'home'" class="marketing-layout"> |
36 | | - <OSSHeader /> |
| 54 | + <OSSHeader class="home-header" /> |
37 | 55 | <Home /> |
38 | 56 | <Footer /> |
39 | 57 | </div> |
|
0 commit comments