Skip to content

Commit

Permalink
fix(theme): prevent body scrolling when sidebar has opened on small s…
Browse files Browse the repository at this point in the history
…creens (#1391)

Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
  • Loading branch information
zam157 and brc-dd committed Oct 13, 2022
1 parent 55688a8 commit 3daabdc
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/client/theme-default/components/VPSidebar.vue
@@ -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'
Expand All @@ -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>
Expand Down

0 comments on commit 3daabdc

Please sign in to comment.