11<script setup>
2- import { computed , onUnmounted , watchEffect } from ' vue'
2+ import { computed , nextTick , onMounted , onUnmounted , ref , watch } from ' vue'
33import { useData } from ' vitepress'
44
55const { theme } = useData ()
@@ -42,28 +42,64 @@ const linkText = computed(() => {
4242 return ' View the latest version.'
4343})
4444
45- const activeClassName = ' has-version-banner'
45+ const bannerElement = ref (null )
46+ let resizeObserver = null
4647
47- function toggleActiveClass ( isActive ) {
48+ function setLayoutTopHeight ( height ) {
4849 if (typeof document === ' undefined' ) {
4950 return
5051 }
5152
52- document .body . classList . toggle (activeClassName, isActive )
53+ document .documentElement . style . setProperty ( ' --vp-layout-top-height ' , height )
5354}
5455
55- watchEffect (() => {
56- toggleActiveClass (isVisible .value )
56+ async function syncLayoutTopHeight () {
57+ if (! isVisible .value ) {
58+ setLayoutTopHeight (' 0px' )
59+ return
60+ }
61+
62+ await nextTick ()
63+
64+ const bannerHeight = bannerElement .value ? .offsetHeight || 0
65+ setLayoutTopHeight (` ${ bannerHeight} px` )
66+ }
67+
68+ watch (isVisible, () => {
69+ syncLayoutTopHeight ()
70+ }, { immediate: true })
71+
72+ onMounted (() => {
73+ if (typeof window === ' undefined' || ! window .ResizeObserver ) {
74+ return
75+ }
76+
77+ resizeObserver = new ResizeObserver (() => {
78+ syncLayoutTopHeight ()
79+ })
80+
81+ if (bannerElement .value ) {
82+ resizeObserver .observe (bannerElement .value )
83+ }
84+
85+ syncLayoutTopHeight ()
5786})
5887
5988onUnmounted (() => {
60- toggleActiveClass (false )
89+ if (resizeObserver) {
90+ resizeObserver .disconnect ()
91+ resizeObserver = null
92+ }
93+
94+ setLayoutTopHeight (' 0px' )
6195})
96+
6297< / script>
6398
6499< template>
65100 < div
66101 v- if = " isVisible"
102+ ref= " bannerElement"
67103 class = " version-banner"
68104 role= " status"
69105 aria- live= " polite"
@@ -77,14 +113,17 @@ onUnmounted(() => {
77113< style scoped>
78114 .version - banner {
79115 position: fixed;
80- z-index : 1 ;
81- top : var (--vp-nav-height );
116+ top: 0 ;
117+ left: 0 ;
118+ right: 0 ;
119+ z- index: calc (var (-- vp- z- index- nav) + 1 );
82120 background: #fff7e8;
83121 color: #734a00 ;
84122 padding: 12px 16px ;
85123 width: 100 % ;
86124 text- align: center;
87125 user- select: none;
126+ border- bottom: 1px solid #e8c892;
88127 }
89128
90129 .version - banner a {
@@ -93,8 +132,8 @@ onUnmounted(() => {
93132 }
94133
95134 .dark .version - banner {
96- border-color : #9b6a1f ;
97135 background: #2d2417 ;
98136 color: #f3c372;
137+ border- bottom- color: #9b6a1f ;
99138 }
100139< / style>
0 commit comments