fix(theme): close menu on route change (#887)

Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
pull/896/head
灵谦 3 years ago committed by GitHub
parent 9d9db6227d
commit fcd7642924
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,6 @@
<script setup lang="ts">
import { provide } from 'vue'
import { provide, watch } from 'vue'
import { useRoute } from 'vitepress'
import { useSidebar, useCloseSidebarOnEscape } from './composables/sidebar'
import VPSkipLink from './components/VPSkipLink.vue'
import VPBackdrop from './components/VPBackdrop.vue'
@ -15,6 +16,9 @@ const {
close: closeSidebar
} = useSidebar()
const route = useRoute()
watch(() => route.path, closeSidebar)
useCloseSidebarOnEscape(isSidebarOpen, closeSidebar)
provide('close-sidebar', closeSidebar)

@ -1,13 +1,16 @@
<script setup lang="ts">
import { computed } from 'vue'
import { useRoute } from 'vitepress'
import { useSidebar } from '../composables/sidebar'
import VPDocAside from './VPDocAside.vue'
import VPDocFooter from './VPDocFooter.vue'
const { path } = useRoute()
const route = useRoute()
const { hasSidebar } = useSidebar()
const pageName = path.replace(/[./]+/g, '_').replace(/_html$/, '')
const pageName = computed(() =>
route.path.replace(/[./]+/g, '_').replace(/_html$/, '')
)
</script>
<template>

@ -1,5 +1,5 @@
import type { DefaultTheme } from 'vitepress/theme'
import { ref, computed } from 'vue'
import { ref, computed, watch } from 'vue'
import { useData, useRoute } from 'vitepress'
export function useNav() {
@ -26,6 +26,9 @@ export function useNav() {
window.outerWidth >= 768 && closeScreen()
}
const route = useRoute()
watch(() => route.path, closeScreen)
return {
isScreenOpen,
openScreen,

@ -1,5 +1,5 @@
import { Ref, ref, computed, watchEffect, onMounted, onUnmounted } from 'vue'
import { useRoute, useData } from 'vitepress'
import { computed, onMounted, onUnmounted, Ref, ref, watchEffect } from 'vue'
import { useData, useRoute } from 'vitepress'
import { getSidebar } from '../support/sidebar'
export function useSidebar() {
@ -10,9 +10,7 @@ export function useSidebar() {
const sidebar = computed(() => {
const sidebarConfig = theme.value.sidebar
const relativePath = route.data.relativePath
return sidebarConfig ? getSidebar(sidebarConfig, relativePath) : []
return sidebarConfig ? getSidebar(sidebarConfig, route.path) : []
})
const hasSidebar = computed(() => {

Loading…
Cancel
Save