fix(theme): prevent body scrolling when sidebar has opened on small screens (#1391)

Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
pull/1479/head
WT 2 years ago committed by GitHub
parent 55688a87e3
commit 3daabdc480
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,6 @@
<script lang="ts" setup>
import { ref, watchPostEffect, nextTick } from 'vue'
import { ref, watchPostEffect } from 'vue'
import { disableBodyScroll, clearAllBodyScrollLocks } from 'body-scroll-lock'
import { useSidebar } from '../composables/sidebar.js'
import VPSidebarGroup from './VPSidebarGroup.vue'
@ -10,12 +11,22 @@ const props = defineProps<{
}>()
// a11y: focus Nav element when menu has opened
let navEl = ref<(Element & { focus(): void }) | null>(null)
let navEl = ref<HTMLElement | null>(null)
function lockBodyScroll() {
disableBodyScroll(navEl.value!, { reserveScrollBarGap: true })
}
function unlockBodyScroll() {
clearAllBodyScrollLocks()
}
watchPostEffect(async () => {
if (props.open) {
await nextTick()
lockBodyScroll()
navEl.value?.focus()
} else {
unlockBodyScroll()
}
})
</script>

Loading…
Cancel
Save