Skip to content

Commit

Permalink
fix(theme): close menu on route change (#887)
Browse files Browse the repository at this point in the history
Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
  • Loading branch information
zonemeen and brc-dd committed Jul 2, 2022
1 parent 9d9db62 commit fcd7642
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/client/theme-default/Layout.vue
@@ -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'
Expand All @@ -15,6 +16,9 @@ const {
close: closeSidebar
} = useSidebar()
const route = useRoute()
watch(() => route.path, closeSidebar)
useCloseSidebarOnEscape(isSidebarOpen, closeSidebar)
provide('close-sidebar', closeSidebar)
Expand Down
7 changes: 5 additions & 2 deletions src/client/theme-default/components/VPDoc.vue
@@ -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>
Expand Down
5 changes: 4 additions & 1 deletion src/client/theme-default/composables/nav.ts
@@ -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() {
Expand All @@ -26,6 +26,9 @@ export function useNav() {
window.outerWidth >= 768 && closeScreen()
}

const route = useRoute()
watch(() => route.path, closeScreen)

return {
isScreenOpen,
openScreen,
Expand Down
8 changes: 3 additions & 5 deletions src/client/theme-default/composables/sidebar.ts
@@ -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() {
Expand All @@ -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(() => {
Expand Down

0 comments on commit fcd7642

Please sign in to comment.