Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from Oct 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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