Skip to content

feat: allow collapsible sidebar items #663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function sidebarGuide() {
return [
{
text: 'Introduction',
collapsible: true,
items: [
{ text: 'What is VitePress?', link: '/guide/what-is-vitepress' },
{ text: 'Getting Started', link: '/guide/getting-started' },
Expand All @@ -71,6 +72,7 @@ function sidebarGuide() {
},
{
text: 'Theme',
collapsible: true,
items: [
{ text: 'Introduction', link: '/guide/theme-introduction' },
{ text: 'Layout', link: '/guide/theme-layout' },
Expand All @@ -81,6 +83,7 @@ function sidebarGuide() {
},
{
text: 'Migrations',
collapsible: true,
items: [
{
text: 'Migration from VuePress',
Expand Down
7 changes: 6 additions & 1 deletion src/client/theme-default/components/VPSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ watchPostEffect(async () => {
</span>

<div v-for="group in sidebar" :key="group.text" class="group">
<VPSidebarGroup :text="group.text" :items="group.items" />
<VPSidebarGroup
:text="group.text"
:items="group.items"
:collapsible="group.collapsible"
:collapsed="group.collapsed"
/>
</div>
</nav>
</aside>
Expand Down
89 changes: 80 additions & 9 deletions src/client/theme-default/components/VPSidebarGroup.vue
Original file line number Diff line number Diff line change
@@ -1,38 +1,109 @@
<script lang="ts" setup>
import { useData } from 'vitepress'
import { ref, onMounted } from 'vue'
import { DefaultTheme } from '../config'
import VPIconPlusSquare from './icons/VPIconPlusSquare.vue'
import VPIconMinusSquare from './icons/VPIconMinusSquare.vue'
import VPSidebarLink from './VPSidebarLink.vue'

const props = defineProps<{
text: string
items: DefaultTheme.SidebarItem[]
collapsible?: boolean
collapsed?: boolean
}>()

const { page } = useData()
const collapsed = ref(!!props.collapsed)

function toggle() {
collapsed.value = !collapsed.value
}
</script>

<template>
<section class="VPSidebarGroup">
<div class="title">
<section class="VPSidebarGroup" :class="{ collapsible, collapsed }">
<div class="title" role="button" @click="toggle">
<h2 class="title-text">{{ text }}</h2>
<div class="action">
<VPIconMinusSquare class="icon minus" />
<VPIconPlusSquare class="icon plus" />
</div>
</div>

<template v-for="item in items" :key="item.link">
<VPSidebarLink :item="item" />
</template>
<div class="items">
<template v-for="item in items" :key="item.link">
<VPSidebarLink :item="item" />
</template>
</div>
</section>
</template>

<style scoped>
.title {
padding: 6px 0;
display: flex;
justify-content: space-between;
align-items: flex-start;
z-index: 2;
}

.VPSidebarGroup.collapsible .title {
cursor: pointer;
}

.title-text {
padding-top: 6px;
padding-bottom: 6px;
line-height: 20px;
font-size: 14px;
font-weight: 700;
color: var(--vp-c-text-1);
transition: color 0.5s;
}

.action {
display: none;
position: relative;
margin-right: -8px;
border-radius: 4px;
width: 32px;
height: 32px;
color: var(--vp-c-text-3);
transition: color 0.25s;
}

.VPSidebarGroup.collapsible .action {
display: block;
}

.title:hover .action {
color: var(--vp-c-text-2);
}

.icon {
position: absolute;
top: 8px;
left: 8px;
width: 16px;
height: 16px;
fill: currentColor;
}

.icon.minus { opacity: 1; }
.icon.plus { opacity: 0; }

.VPSidebarGroup.collapsed .icon.minus { opacity: 0; }
.VPSidebarGroup.collapsed .icon.plus { opacity: 1; }

.items {
overflow: hidden;
}

.VPSidebarGroup.collapsed .items {
margin-bottom: -22px;
max-height: 0;
}

@media (min-width: 960px) {
.VPSidebarGroup.collapsed .items {
margin-bottom: -14px;
}
}
</style>
5 changes: 5 additions & 0 deletions src/client/theme-default/components/icons/VPIconMinus.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path d="M22,13H2a1,1,0,0,1,0-2H22a1,1,0,0,1,0,2Z" />
</svg>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<template>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24">
<path d="M19,2H5C3.3,2,2,3.3,2,5v14c0,1.7,1.3,3,3,3h14c1.7,0,3-1.3,3-3V5C22,3.3,20.7,2,19,2zM20,19c0,0.6-0.4,1-1,1H5c-0.6,0-1-0.4-1-1V5c0-0.6,0.4-1,1-1h14c0.6,0,1,0.4,1,1V19z" />
<path d="M16,11H8c-0.6,0-1,0.4-1,1s0.4,1,1,1h8c0.6,0,1-0.4,1-1S16.6,11,16,11z" />
</svg>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<template>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path d="M19,2H5C3.3,2,2,3.3,2,5v14c0,1.7,1.3,3,3,3h14c1.7,0,3-1.3,3-3V5C22,3.3,20.7,2,19,2z M20,19c0,0.6-0.4,1-1,1H5c-0.6,0-1-0.4-1-1V5c0-0.6,0.4-1,1-1h14c0.6,0,1,0.4,1,1V19z" />
<path d="M16,11h-3V8c0-0.6-0.4-1-1-1s-1,0.4-1,1v3H8c-0.6,0-1,0.4-1,1s0.4,1,1,1h3v3c0,0.6,0.4,1,1,1s1-0.4,1-1v-3h3c0.6,0,1-0.4,1-1S16.6,11,16,11z" />
</svg>
</template>
4 changes: 4 additions & 0 deletions src/client/theme-default/styles/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ html {
-webkit-text-size-adjust: 100%;
}

html.dark {
color-scheme: dark;
}

body {
margin: 0;
width: 100%;
Expand Down
14 changes: 14 additions & 0 deletions types/default-theme.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ export namespace DefaultTheme {
export interface SidebarGroup {
text: 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
}

export interface SidebarItem {
Expand Down