Skip to content

Commit ca941eb

Browse files
committed
awful banner hack
1 parent 140a788 commit ca941eb

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

.vitepress/theme/components/AnnouncementBanner.vue

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import { onMounted, onUnmounted, ref, watch, nextTick } from 'vue'
44
55
const { frontmatter } = useData()
66
const banner = ref(null)
7+
const isMenuOpen = ref(false)
8+
let observer = null
79
810
const updateHeight = () => {
9-
if (banner.value) {
11+
if (banner.value && !isMenuOpen.value) {
1012
const height = banner.value.offsetHeight
1113
if (window.innerWidth >= 960) {
1214
document.documentElement.style.setProperty('--vp-layout-top-height', height + 'px')
@@ -23,19 +25,46 @@ watch(frontmatter, async () => {
2325
updateHeight()
2426
})
2527
28+
const setupObserver = () => {
29+
const navbar = document.querySelector('.VPNavBar')
30+
if (navbar) {
31+
observer = new MutationObserver((mutations) => {
32+
mutations.forEach((mutation) => {
33+
if (mutation.attributeName === 'class') {
34+
isMenuOpen.value = navbar.classList.contains('screen-open')
35+
updateHeight()
36+
}
37+
})
38+
})
39+
observer.observe(navbar, { attributes: true, attributeFilter: ['class'] })
40+
isMenuOpen.value = navbar.classList.contains('screen-open')
41+
updateHeight()
42+
return true
43+
}
44+
return false
45+
}
46+
2647
onMounted(() => {
2748
updateHeight()
2849
window.addEventListener('resize', updateHeight)
50+
51+
if (!setupObserver()) {
52+
const interval = setInterval(() => {
53+
if (setupObserver()) clearInterval(interval)
54+
}, 100)
55+
setTimeout(() => clearInterval(interval), 5000)
56+
}
2957
})
3058
3159
onUnmounted(() => {
3260
window.removeEventListener('resize', updateHeight)
61+
if (observer) observer.disconnect()
3362
document.documentElement.style.removeProperty('--vp-layout-top-height')
3463
})
3564
</script>
3665

3766
<template>
38-
<div v-if="frontmatter.layout === 'home'" ref="banner" class="announcement-banner">
67+
<div v-if="frontmatter.layout === 'home' && !isMenuOpen" ref="banner" class="announcement-banner">
3968
This website has been completely redone! Check out the new design and features.
4069
</div>
4170
</template>

0 commit comments

Comments
 (0)