Skip to content

Commit 3daabdc

Browse files
zam157brc-dd
andauthored
fix(theme): prevent body scrolling when sidebar has opened on small screens (vuejs#1391)
Co-authored-by: Divyansh Singh <[email protected]>
1 parent 55688a8 commit 3daabdc

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/client/theme-default/components/VPSidebar.vue

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts" setup>
2-
import { ref, watchPostEffect, nextTick } from 'vue'
2+
import { ref, watchPostEffect } from 'vue'
3+
import { disableBodyScroll, clearAllBodyScrollLocks } from 'body-scroll-lock'
34
import { useSidebar } from '../composables/sidebar.js'
45
import VPSidebarGroup from './VPSidebarGroup.vue'
56
@@ -10,12 +11,22 @@ const props = defineProps<{
1011
}>()
1112
1213
// a11y: focus Nav element when menu has opened
13-
let navEl = ref<(Element & { focus(): void }) | null>(null)
14+
let navEl = ref<HTMLElement | null>(null)
15+
16+
function lockBodyScroll() {
17+
disableBodyScroll(navEl.value!, { reserveScrollBarGap: true })
18+
}
19+
20+
function unlockBodyScroll() {
21+
clearAllBodyScrollLocks()
22+
}
1423
1524
watchPostEffect(async () => {
1625
if (props.open) {
17-
await nextTick()
26+
lockBodyScroll()
1827
navEl.value?.focus()
28+
} else {
29+
unlockBodyScroll()
1930
}
2031
})
2132
</script>

0 commit comments

Comments
 (0)