-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sidebarDepth for sidebar groups
- Loading branch information
Showing
6 changed files
with
126 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/@vuepress/theme-default/components/SidebarLinks.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<template> | ||
<ul v-if="items.length"> | ||
<li v-for="(item, i) in items" :key="i"> | ||
<SidebarGroup | ||
v-if="item.type === 'group'" | ||
:item="item" | ||
:first="i === 0" | ||
:open="i === openGroupIndex" | ||
:collapsable="item.collapsable || item.collapsible" | ||
@toggle="toggleGroup(i)" | ||
/> | ||
<SidebarLink | ||
v-else | ||
:sidebarDepth="sidebarDepth" | ||
:item="item" | ||
/> | ||
</li> | ||
</ul> | ||
</template> | ||
|
||
<script> | ||
import SidebarGroup from './SidebarGroup.vue' | ||
import SidebarLink from './SidebarLink.vue' | ||
import { isActive } from '../util' | ||
export default { | ||
name: 'SidebarLinks', | ||
components: { SidebarGroup, SidebarLink }, | ||
props: ['items', 'sidebarDepth'], | ||
data () { | ||
return { | ||
openGroupIndex: 0 | ||
} | ||
}, | ||
created () { | ||
this.refreshIndex() | ||
}, | ||
watch: { | ||
'$route' () { | ||
this.refreshIndex() | ||
} | ||
}, | ||
methods: { | ||
refreshIndex () { | ||
const index = resolveOpenGroupIndex( | ||
this.$route, | ||
this.items | ||
) | ||
if (index > -1) { | ||
this.openGroupIndex = index | ||
} | ||
}, | ||
toggleGroup (index) { | ||
this.openGroupIndex = index === this.openGroupIndex ? -1 : index | ||
}, | ||
isActive (page) { | ||
return isActive(this.$route, page.regularPath) | ||
} | ||
} | ||
} | ||
function resolveOpenGroupIndex (route, items) { | ||
for (let i = 0; i < items.length; i++) { | ||
const item = items[i] | ||
if (item.type === 'group' && item.children.some(c => c.type === 'page' && isActive(route, c.path))) { | ||
return i | ||
} | ||
} | ||
return -1 | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters