feat: add collapsible option to sidebar (#640) (#663)

close #640 

Co-authored-by: Kia King Ishii <kia.king.08@gmail.com>
pull/665/head
Divyansh Singh 2 years ago committed by GitHub
parent 7d89b61b92
commit e32936b6ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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' },
@ -71,6 +72,7 @@ function sidebarGuide() {
},
{
text: 'Theme',
collapsible: true,
items: [
{ text: 'Introduction', link: '/guide/theme-introduction' },
{ text: 'Layout', link: '/guide/theme-layout' },
@ -81,6 +83,7 @@ function sidebarGuide() {
},
{
text: 'Migrations',
collapsible: true,
items: [
{
text: 'Migration from VuePress',

@ -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>

@ -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>

@ -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>

@ -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>

@ -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>

@ -10,6 +10,10 @@ html {
-webkit-text-size-adjust: 100%;
}
html.dark {
color-scheme: dark;
}
body {
margin: 0;
width: 100%;

@ -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 {

Loading…
Cancel
Save