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

Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
pull/896/head
灵谦 2 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"> <script setup lang="ts">
import { provide } from 'vue' import { provide, watch } from 'vue'
import { useRoute } from 'vitepress'
import { useSidebar, useCloseSidebarOnEscape } from './composables/sidebar' import { useSidebar, useCloseSidebarOnEscape } from './composables/sidebar'
import VPSkipLink from './components/VPSkipLink.vue' import VPSkipLink from './components/VPSkipLink.vue'
import VPBackdrop from './components/VPBackdrop.vue' import VPBackdrop from './components/VPBackdrop.vue'
@ -15,6 +16,9 @@ const {
close: closeSidebar close: closeSidebar
} = useSidebar() } = useSidebar()
const route = useRoute()
watch(() => route.path, closeSidebar)
useCloseSidebarOnEscape(isSidebarOpen, closeSidebar) useCloseSidebarOnEscape(isSidebarOpen, closeSidebar)
provide('close-sidebar', closeSidebar) provide('close-sidebar', closeSidebar)

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

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

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

Loading…
Cancel
Save