From 465ae40d28081a477257c1f2e68726be67e4aaf5 Mon Sep 17 00:00:00 2001 From: Dennis Reimann Date: Mon, 3 Aug 2020 16:45:22 +0200 Subject: [PATCH] feat($theme-default): add initial open group index option (#2408) * feat($theme-default): add initial open group index option Adds an optional property for sidebar groups, so that one can initially display a group other than the first as open. Also allows to initially display all groups as closed, by setting `initialOpenGroupIndex` to `-1`. * docs: add details on `initialOpenGroupIndex` --- packages/@vuepress/theme-default/components/SidebarGroup.vue | 1 + packages/@vuepress/theme-default/components/SidebarLinks.vue | 5 +++-- packages/@vuepress/theme-default/util/index.js | 1 + packages/docs/docs/theme/default-theme-config.md | 5 ++++- packages/docs/docs/zh/theme/default-theme-config.md | 3 ++- 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/@vuepress/theme-default/components/SidebarGroup.vue b/packages/@vuepress/theme-default/components/SidebarGroup.vue index 23f8a61139..d7f192946a 100644 --- a/packages/@vuepress/theme-default/components/SidebarGroup.vue +++ b/packages/@vuepress/theme-default/components/SidebarGroup.vue @@ -47,6 +47,7 @@ class="sidebar-group-items" :items="item.children" :sidebar-depth="item.sidebarDepth" + :initial-open-group-index="item.initialOpenGroupIndex" :depth="depth + 1" /> diff --git a/packages/@vuepress/theme-default/components/SidebarLinks.vue b/packages/@vuepress/theme-default/components/SidebarLinks.vue index 7adf461b42..ea084e25c4 100644 --- a/packages/@vuepress/theme-default/components/SidebarLinks.vue +++ b/packages/@vuepress/theme-default/components/SidebarLinks.vue @@ -37,12 +37,13 @@ export default { props: [ 'items', 'depth', // depth of current sidebar links - 'sidebarDepth' // depth of headers to be extracted + 'sidebarDepth', // depth of headers to be extracted + 'initialOpenGroupIndex' ], data () { return { - openGroupIndex: 0 + openGroupIndex: this.initialOpenGroupIndex || 0 } }, diff --git a/packages/@vuepress/theme-default/util/index.js b/packages/@vuepress/theme-default/util/index.js index 4433bb3c5f..92fcd3b311 100644 --- a/packages/@vuepress/theme-default/util/index.js +++ b/packages/@vuepress/theme-default/util/index.js @@ -236,6 +236,7 @@ function resolveItem (item, pages, base, groupDepth = 1) { path: item.path, title: item.title, sidebarDepth: item.sidebarDepth, + initialOpenGroupIndex: item.initialOpenGroupIndex, children: children.map(child => resolveItem(child, pages, base, groupDepth + 1)), collapsable: item.collapsable !== false } diff --git a/packages/docs/docs/theme/default-theme-config.md b/packages/docs/docs/theme/default-theme-config.md index fe75254f05..f868c6cfcb 100644 --- a/packages/docs/docs/theme/default-theme-config.md +++ b/packages/docs/docs/theme/default-theme-config.md @@ -220,7 +220,8 @@ module.exports = { }, { title: 'Group 2', - children: [ /* ... */ ] + children: [ /* ... */ ], + initialOpenGroupIndex: -1 // optional, defaults to 0, defines the index of initially opened subgroup } ] } @@ -233,6 +234,8 @@ A sidebar group config also supports [sidebarDepth](#nested-header-links) field ::: tip    Nested sidebar group is also supported. + By default the first subgroup is opened initially. + You can change this using the `initialOpenGroupIndex`: Specify an index to open another subgroup or use `-1` for no open group. ::: ### Multiple Sidebars diff --git a/packages/docs/docs/zh/theme/default-theme-config.md b/packages/docs/docs/zh/theme/default-theme-config.md index 7dcd24f492..f230b88f9a 100644 --- a/packages/docs/docs/zh/theme/default-theme-config.md +++ b/packages/docs/docs/zh/theme/default-theme-config.md @@ -218,7 +218,8 @@ module.exports = { }, { title: 'Group 2', - children: [ /* ... */ ] + children: [ /* ... */ ], + initialOpenGroupIndex: -1 // 可选的, 默认值是 0 } ] }