feat(theme): use inert to avoid traverse menus and content with keyboard (#2932)

pull/2933/head
Joaquín Sánchez 1 year ago committed by GitHub
parent c0ce7f723e
commit 070fc0a56d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,13 +9,16 @@ import VPNav from './components/VPNav.vue'
import VPSidebar from './components/VPSidebar.vue' import VPSidebar from './components/VPSidebar.vue'
import VPSkipLink from './components/VPSkipLink.vue' import VPSkipLink from './components/VPSkipLink.vue'
import { useData } from './composables/data' import { useData } from './composables/data'
import { useNav } from './composables/nav'
import { useCloseSidebarOnEscape, useSidebar } from './composables/sidebar' import { useCloseSidebarOnEscape, useSidebar } from './composables/sidebar'
const { const {
isOpen: isSidebarOpen, isOpen: isSidebarOpen,
open: openSidebar, open: openSidebar,
close: closeSidebar close: closeSidebar,
isSidebarEnabled,
} = useSidebar() } = useSidebar()
const { isScreenOpen } = useNav()
const route = useRoute() const route = useRoute()
watch(() => route.path, closeSidebar) watch(() => route.path, closeSidebar)
@ -33,9 +36,9 @@ provide('hero-image-slot-exists', heroImageSlotExists)
<template> <template>
<div v-if="frontmatter.layout !== false" class="Layout" :class="frontmatter.pageClass" > <div v-if="frontmatter.layout !== false" class="Layout" :class="frontmatter.pageClass" >
<slot name="layout-top" /> <slot name="layout-top" />
<VPSkipLink /> <VPSkipLink :inert="isSidebarOpen || isScreenOpen" />
<VPBackdrop class="backdrop" :show="isSidebarOpen" @click="closeSidebar" /> <VPBackdrop class="backdrop" :show="isSidebarOpen" @click="closeSidebar" />
<VPNav v-if="frontmatter.navbar !== false"> <VPNav v-if="frontmatter.navbar !== false" :inert="isSidebarOpen">
<template #nav-bar-title-before><slot name="nav-bar-title-before" /></template> <template #nav-bar-title-before><slot name="nav-bar-title-before" /></template>
<template #nav-bar-title-after><slot name="nav-bar-title-after" /></template> <template #nav-bar-title-after><slot name="nav-bar-title-after" /></template>
<template #nav-bar-content-before><slot name="nav-bar-content-before" /></template> <template #nav-bar-content-before><slot name="nav-bar-content-before" /></template>
@ -43,14 +46,14 @@ provide('hero-image-slot-exists', heroImageSlotExists)
<template #nav-screen-content-before><slot name="nav-screen-content-before" /></template> <template #nav-screen-content-before><slot name="nav-screen-content-before" /></template>
<template #nav-screen-content-after><slot name="nav-screen-content-after" /></template> <template #nav-screen-content-after><slot name="nav-screen-content-after" /></template>
</VPNav> </VPNav>
<VPLocalNav :open="isSidebarOpen" @open-menu="openSidebar" /> <VPLocalNav :inert="isSidebarOpen || isScreenOpen" :open="isSidebarOpen" @open-menu="openSidebar" />
<VPSidebar :open="isSidebarOpen"> <VPSidebar :inert="!isSidebarEnabled && (!isSidebarOpen || isScreenOpen)" :open="isSidebarOpen">
<template #sidebar-nav-before><slot name="sidebar-nav-before" /></template> <template #sidebar-nav-before><slot name="sidebar-nav-before" /></template>
<template #sidebar-nav-after><slot name="sidebar-nav-after" /></template> <template #sidebar-nav-after><slot name="sidebar-nav-after" /></template>
</VPSidebar> </VPSidebar>
<VPContent> <VPContent :inert="isSidebarOpen || isScreenOpen">
<template #page-top><slot name="page-top" /></template> <template #page-top><slot name="page-top" /></template>
<template #page-bottom><slot name="page-bottom" /></template> <template #page-bottom><slot name="page-bottom" /></template>
@ -76,7 +79,7 @@ provide('hero-image-slot-exists', heroImageSlotExists)
<template #aside-ads-after><slot name="aside-ads-after" /></template> <template #aside-ads-after><slot name="aside-ads-after" /></template>
</VPContent> </VPContent>
<VPFooter /> <VPFooter :inert="isSidebarOpen || isScreenOpen" />
<slot name="layout-bottom" /> <slot name="layout-bottom" />
</div> </div>
<Content v-else /> <Content v-else />

@ -27,6 +27,7 @@ function focusOnTargetAnchor({ target }: Event) {
</script> </script>
<template> <template>
<div>
<span ref="backToTop" tabindex="-1" /> <span ref="backToTop" tabindex="-1" />
<a <a
href="#VPContent" href="#VPContent"
@ -35,6 +36,7 @@ function focusOnTargetAnchor({ target }: Event) {
> >
Skip to content Skip to content
</a> </a>
</div>
</template> </template>
<style scoped> <style scoped>

@ -1,9 +1,9 @@
import { ref, watch } from 'vue' import { ref, watch } from 'vue'
import { useRoute } from 'vitepress' import { useRoute } from 'vitepress'
export function useNav() { const isScreenOpen = ref(false)
const isScreenOpen = ref(false)
export function useNav() {
function openScreen() { function openScreen() {
isScreenOpen.value = true isScreenOpen.value = true
window.addEventListener('resize', closeScreenOnTabletWindow) window.addEventListener('resize', closeScreenOnTabletWindow)

Loading…
Cancel
Save