Skip to content

Commit 02fc51a

Browse files
authored
docs(fix): mobile menu dark mode in home (#815)
Co-authored-by: mdong1909 <mdong1909@users.noreply.github.com>
1 parent acbdb44 commit 02fc51a

1 file changed

Lines changed: 43 additions & 1 deletion

File tree

docs/.vitepress/theme/Layout.vue

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,47 @@
22
import OSSHeader from '@components/oss/Header.vue';
33
import BaseTheme from '@voidzero-dev/vitepress-theme/src/viteplus';
44
import { useData } from 'vitepress';
5-
import { nextTick, watch } from 'vue';
5+
import { nextTick, onUnmounted, watch } from 'vue';
66
77
import Footer from './components/Footer.vue';
88
import Home from './layouts/Home.vue';
99
// import Error404 from "./layouts/Error404.vue";
1010
1111
const { frontmatter, isDark } = useData();
1212
const { Layout: BaseLayout } = BaseTheme;
13+
let homeHeaderObserver: MutationObserver | null = null;
14+
15+
const syncHeaderMobileMenuTheme = (header: HTMLElement | null, isHome: boolean) => {
16+
const mobileMenu = header?.querySelector<HTMLElement>('#mobile-menu');
17+
18+
if (!mobileMenu) {
19+
return;
20+
}
21+
22+
if (isHome) {
23+
mobileMenu.setAttribute('data-theme', 'light');
24+
} else {
25+
mobileMenu.removeAttribute('data-theme');
26+
}
27+
};
28+
29+
const setupHomeHeaderObserver = (header: HTMLElement | null, isHome: boolean) => {
30+
homeHeaderObserver?.disconnect();
31+
homeHeaderObserver = null;
32+
33+
if (!header || !isHome || typeof MutationObserver === 'undefined') {
34+
return;
35+
}
36+
37+
homeHeaderObserver = new MutationObserver(() => {
38+
syncHeaderMobileMenuTheme(header, isHome);
39+
});
40+
41+
homeHeaderObserver.observe(header, {
42+
childList: true,
43+
subtree: true,
44+
});
45+
};
1346
1447
const syncHomeThemeOverride = async () => {
1548
if (typeof document === 'undefined') {
@@ -29,6 +62,8 @@ const syncHomeThemeOverride = async () => {
2962
3063
const header = document.querySelector<HTMLElement>('.home-header');
3164
65+
setupHomeHeaderObserver(header, isHome);
66+
3267
if (!header) {
3368
return;
3469
}
@@ -38,6 +73,8 @@ const syncHomeThemeOverride = async () => {
3873
} else {
3974
header.removeAttribute('data-theme');
4075
}
76+
77+
syncHeaderMobileMenuTheme(header, isHome);
4178
};
4279
4380
watch(
@@ -47,6 +84,11 @@ watch(
4784
},
4885
{ immediate: true },
4986
);
87+
88+
onUnmounted(() => {
89+
homeHeaderObserver?.disconnect();
90+
homeHeaderObserver = null;
91+
});
5092
</script>
5193

5294
<template>

0 commit comments

Comments
 (0)