From 8c164feb5784bce4b8af9b0b2c4a4d91cb125f19 Mon Sep 17 00:00:00 2001 From: LittleSound <464388324@qq.com> Date: Sun, 18 Sep 2022 18:37:31 +0800 Subject: [PATCH 1/7] feat: add support for collapsible multi-level sidebar feat: #1360 --- docs/.vitepress/config.ts | 17 ++- .../components/VPSidebarLink.vue | 103 +++++++++++++++--- types/default-theme.d.ts | 20 +++- 3 files changed, 119 insertions(+), 21 deletions(-) diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index a24c1be1f109..4e88021ab230 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -78,8 +78,21 @@ function sidebarGuide() { items: [ { text: 'What is VitePress?', link: '/guide/what-is-vitepress' }, { text: 'Getting Started', link: '/guide/getting-started' }, - { text: 'Configuration', link: '/guide/configuration' }, - { text: 'Deploying', link: '/guide/deploying' } + { text: 'Configuration', link: '/guide/configuration', items: [ + { text: 'What is VitePress?', link: '/guide/what-is-vitepress' }, + { text: 'Getting Started', link: '/guide/getting-started' }, + { text: 'Configuration', link: '/guide/configuration' }, + { text: 'Deploying', link: '/guide/deploying', }, + ] }, + { text: 'Deploying', link: '/guide/deploying', + collapsible: true, + collapsed: true, + items: [ + { text: 'What is VitePress?', link: '/guide/what-is-vitepress' }, + { text: 'Getting Started', link: '/guide/getting-started' }, + { text: 'Configuration', link: '/guide/configuration' }, + { text: 'Deploying', link: '/guide/deploying', }, + ] }, ] }, { diff --git a/src/client/theme-default/components/VPSidebarLink.vue b/src/client/theme-default/components/VPSidebarLink.vue index 330790eb99c9..fab887ccb539 100644 --- a/src/client/theme-default/components/VPSidebarLink.vue +++ b/src/client/theme-default/components/VPSidebarLink.vue @@ -1,11 +1,13 @@ diff --git a/types/default-theme.d.ts b/types/default-theme.d.ts index 36ba6f0d68f4..e5dc5981d3c7 100644 --- a/types/default-theme.d.ts +++ b/types/default-theme.d.ts @@ -148,7 +148,25 @@ export namespace DefaultTheme { export type SidebarItem = | { text: string; link: string } - | { text: string; link?: string; items: SidebarItem[] } + | { + text: string; + link?: string; + items: SidebarItem[]; + + /** + * If `true`, toggle button is shown. + * + * @default false + */ + collapsible?: boolean + + /** + * If `true`, collapsible group is collapsed by default. + * + * @default false + */ + collapsed?: boolean + } // edit link ----------------------------------------------------------------- From 14255848d2dd8ad6973c2ffc11e351172f16eafc Mon Sep 17 00:00:00 2001 From: LittleSound <464388324@qq.com> Date: Sun, 18 Sep 2022 20:10:59 +0800 Subject: [PATCH 2/7] chore: remove the config.ts modification --- docs/.vitepress/config.ts | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 4e88021ab230..a24c1be1f109 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -78,21 +78,8 @@ function sidebarGuide() { items: [ { text: 'What is VitePress?', link: '/guide/what-is-vitepress' }, { text: 'Getting Started', link: '/guide/getting-started' }, - { text: 'Configuration', link: '/guide/configuration', items: [ - { text: 'What is VitePress?', link: '/guide/what-is-vitepress' }, - { text: 'Getting Started', link: '/guide/getting-started' }, - { text: 'Configuration', link: '/guide/configuration' }, - { text: 'Deploying', link: '/guide/deploying', }, - ] }, - { text: 'Deploying', link: '/guide/deploying', - collapsible: true, - collapsed: true, - items: [ - { text: 'What is VitePress?', link: '/guide/what-is-vitepress' }, - { text: 'Getting Started', link: '/guide/getting-started' }, - { text: 'Configuration', link: '/guide/configuration' }, - { text: 'Deploying', link: '/guide/deploying', }, - ] }, + { text: 'Configuration', link: '/guide/configuration' }, + { text: 'Deploying', link: '/guide/deploying' } ] }, { From 4386900cd45fd70dc05c6408f13f7c827244cb76 Mon Sep 17 00:00:00 2001 From: LittleSound <464388324@qq.com> Date: Sun, 18 Sep 2022 22:23:30 +0800 Subject: [PATCH 3/7] feat: adapt to more situations and improve ease of use --- .../components/VPSidebarLink.vue | 50 ++++++++++++++++--- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/src/client/theme-default/components/VPSidebarLink.vue b/src/client/theme-default/components/VPSidebarLink.vue index fab887ccb539..5ce7b74156ac 100644 --- a/src/client/theme-default/components/VPSidebarLink.vue +++ b/src/client/theme-default/components/VPSidebarLink.vue @@ -21,6 +21,12 @@ const closeSideBar = inject('close-sidebar') as () => void const collapsible = computed(() => 'items' in props.item ? !!props.item.collapsible : false) const collapsed = ref(false) +/** + * When this node is both a page and a parent node, + * a split line will be added between the collapse button and the link to distinguish the functional area. + */ +const divider = computed(() => !!props.item.link && collapsible.value) + watchEffect(() => { if ('items' in props.item) @@ -32,6 +38,15 @@ function toggle() { collapsed.value = !collapsed.value } } + +function clickLink() { + closeSideBar() + + // If there are no links to jump to, switch to expand when clicking on the text + if (!props.item.link) { + toggle() + } +}