mirror of https://github.com/vuejs/vitepress
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
216 lines
4.7 KiB
216 lines
4.7 KiB
3 years ago
|
# Sidebar
|
||
|
|
||
2 years ago
|
The sidebar is the main navigation block for your documentation. You can configure the sidebar menu in [`themeConfig.sidebar`](./default-theme-config#sidebar).
|
||
3 years ago
|
|
||
|
```js
|
||
|
export default {
|
||
|
themeConfig: {
|
||
|
sidebar: [
|
||
|
{
|
||
|
text: 'Guide',
|
||
|
items: [
|
||
|
{ text: 'Introduction', link: '/introduction' },
|
||
|
{ text: 'Getting Started', link: '/getting-started' },
|
||
|
...
|
||
|
]
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
## The Basics
|
||
|
|
||
2 years ago
|
The simplest form of the sidebar menu is passing in a single array of links. The first level item defines the "section" for the sidebar. It should contain `text`, which is the title of the section, and `items` which are the actual navigation links.
|
||
3 years ago
|
|
||
|
```js
|
||
|
export default {
|
||
|
themeConfig: {
|
||
|
sidebar: [
|
||
|
{
|
||
|
text: 'Section Title A',
|
||
|
items: [
|
||
|
{ text: 'Item A', link: '/item-a' },
|
||
|
{ text: 'Item B', link: '/item-b' },
|
||
|
...
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
text: 'Section Title B',
|
||
|
items: [
|
||
|
{ text: 'Item C', link: '/item-c' },
|
||
|
{ text: 'Item D', link: '/item-d' },
|
||
|
...
|
||
|
]
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
Each `link` should specify the path to the actual file starting with `/`. If you add trailing slash to the end of link, it will show `index.md` of the corresponding directory.
|
||
|
|
||
|
```js
|
||
|
export default {
|
||
|
themeConfig: {
|
||
|
sidebar: [
|
||
|
{
|
||
|
text: 'Guide',
|
||
|
items: [
|
||
|
// This shows `/guide/index.md` page.
|
||
|
{ text: 'Introduction', link: '/guide/' }
|
||
|
]
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
You may further nest the sidebar items up to 6 level deep counting up from the root level. Note that deeper than 6 level of nested items gets ignored and will not be displayed on the sidebar.
|
||
|
|
||
|
```js
|
||
|
export default {
|
||
|
themeConfig: {
|
||
|
sidebar: [
|
||
|
{
|
||
|
text: 'Level 1',
|
||
|
items: [
|
||
|
{
|
||
|
text: 'Level 2',
|
||
|
items: [
|
||
|
{
|
||
|
text: 'Level 3',
|
||
|
items: [
|
||
|
...
|
||
|
]
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
3 years ago
|
## Multiple Sidebars
|
||
|
|
||
2 years ago
|
You may show different sidebar depending on the page path. For example, as shown on this site, you might want to create a separate sections of content in your documentation like "Guide" page and "Config" page.
|
||
3 years ago
|
|
||
|
To do so, first organize your pages into directories for each desired section:
|
||
|
|
||
|
```
|
||
|
.
|
||
|
├─ guide/
|
||
|
│ ├─ index.md
|
||
|
│ ├─ one.md
|
||
|
│ └─ two.md
|
||
|
└─ config/
|
||
|
├─ index.md
|
||
|
├─ three.md
|
||
|
└─ four.md
|
||
|
```
|
||
|
|
||
|
Then, update your configuration to define your sidebar for each section. This time, you should pass an object instead of an array.
|
||
|
|
||
|
```js
|
||
|
export default {
|
||
|
themeConfig: {
|
||
|
sidebar: {
|
||
2 years ago
|
// This sidebar gets displayed when a user
|
||
|
// is on `guide` directory.
|
||
2 years ago
|
'/guide/': [
|
||
|
{
|
||
|
text: 'Guide',
|
||
|
items: [
|
||
2 years ago
|
{ text: 'Index', link: '/guide/' },
|
||
|
{ text: 'One', link: '/guide/one' },
|
||
|
{ text: 'Two', link: '/guide/two' }
|
||
2 years ago
|
]
|
||
|
}
|
||
|
],
|
||
3 years ago
|
|
||
2 years ago
|
// This sidebar gets displayed when a user
|
||
|
// is on `config` directory.
|
||
2 years ago
|
'/config/': [
|
||
|
{
|
||
|
text: 'Config',
|
||
|
items: [
|
||
2 years ago
|
{ text: 'Index', link: '/config/' },
|
||
|
{ text: 'Three', link: '/config/three' },
|
||
|
{ text: 'Four', link: '/config/four' }
|
||
2 years ago
|
]
|
||
|
}
|
||
|
]
|
||
3 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
## Collapsible Sidebar Groups
|
||
|
|
||
2 years ago
|
By adding `collapsed` option to the sidebar group, it shows a toggle button to hide/show each section.
|
||
3 years ago
|
|
||
|
```js
|
||
|
export default {
|
||
|
themeConfig: {
|
||
|
sidebar: [
|
||
|
{
|
||
|
text: 'Section Title A',
|
||
2 years ago
|
collapsed: false,
|
||
3 years ago
|
items: [...]
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
2 years ago
|
All sections are "open" by default. If you would like them to be "closed" on initial page load, set `collapsed` option to `true`.
|
||
3 years ago
|
|
||
|
```js
|
||
|
export default {
|
||
|
themeConfig: {
|
||
|
sidebar: [
|
||
|
{
|
||
|
text: 'Section Title A',
|
||
|
collapsed: true,
|
||
|
items: [...]
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
}
|
||
|
```
|
||
1 year ago
|
|
||
|
## `useSidebar` <Badge type="info" text="composable" />
|
||
|
|
||
|
Returns sidebar-related data. The returned object has the following type:
|
||
|
|
||
|
```ts
|
||
1 year ago
|
export interface DocSidebar {
|
||
1 year ago
|
isOpen: Ref<boolean>
|
||
|
sidebar: ComputedRef<DefaultTheme.SidebarItem[]>
|
||
|
sidebarGroups: ComputedRef<DefaultTheme.SidebarItem[]>
|
||
|
hasSidebar: ComputedRef<boolean>
|
||
|
hasAside: ComputedRef<boolean>
|
||
|
leftAside: ComputedRef<boolean>
|
||
|
isSidebarEnabled: ComputedRef<boolean>
|
||
|
open: () => void
|
||
|
close: () => void
|
||
|
toggle: () => void
|
||
|
}
|
||
|
```
|
||
|
|
||
|
**Example:**
|
||
|
|
||
|
```vue
|
||
|
<script setup>
|
||
|
import { useSidebar } from 'vitepress/theme'
|
||
|
|
||
|
const { hasSidebar } = useSidebar()
|
||
|
</script>
|
||
|
|
||
|
<template>
|
||
|
<div v-if="hasSidebar">Only show when sidebar exists</div>
|
||
|
</template>
|
||
|
```
|