Sidebar items
We have introduced three types of item types in the example in the previous section: doc
, category
, and link
, whose usages are fairly intuitive. We will formally introduce their APIs. There's also a fourth type: autogenerated
, which we will explain in detail later.
- Doc: link to a doc page, associating it with the sidebar
- Link: link to any internal or external page
- Category: creates a dropdown of sidebar items
- Autogenerated: generate a sidebar slice automatically
- HTML: renders pure HTML in the item's position
- *Ref: link to a doc page, without making the item take part in navigation generation
Doc: link to a doc
Use the doc
type to link to a doc page and assign that doc to a sidebar:
type SidebarItemDoc =
// Normal syntax
| {
type: 'doc';
id: string;
label: string; // Sidebar label text
className?: string; // Class name for sidebar label
customProps?: Record<string, unknown>; // Custom props
}
// Shorthand syntax
| string; // docId shortcut
Example:
export default {
mySidebar: [
// Normal syntax:
{
type: 'doc',
id: 'doc1', // document ID
label: 'Getting started', // sidebar label
},
// Shorthand syntax:
'doc2', // document ID
],
};
If you use the doc shorthand or autogenerated sidebar, you would lose the ability to customize the sidebar label through item definition. You can, however, use the sidebar_label
Markdown front matter within that doc, which has higher precedence over the label
key in the sidebar item. Similarly, you can use sidebar_custom_props
to declare custom metadata for a doc page.
A doc
item sets an implicit sidebar association. Don't assign the same doc to multiple sidebars: change the type to ref
instead.
Sidebar custom props is a useful way to propagate arbitrary doc metadata to the client side, so you can get additional information when using any doc-related hook that fetches a doc object.
Link: link to any page
Use the link
type to link to any page (internal or external) that is not a doc.
type SidebarItemLink = {
type: 'link';
label: string;
href: string;
className?: string;
description?: string;
};
Example:
export default {
myLinksSidebar: [
// External link
{
type: 'link',
label: 'Facebook', // The link label
href: 'https://facebook.com', // The external URL
},
// Internal link
{
type: 'link',
label: 'Home', // The link label
href: '/', // The internal path
},
],
};
HTML: render custom markup
Use the html
type to render custom HTML within the item's <li>
tag.
This can be useful for inserting custom items such as dividers, section titles, ads, and images.
type SidebarItemHtml = {
type: 'html';
value: string;
defaultStyle?: boolean; // Use default menu item styles
className?: string;
};
Example:
export default {
myHtmlSidebar: [
{
type: 'html',
value: '<img src="sponsor.png" alt="Sponsor" />', // The HTML to be rendered
defaultStyle: true, // Use the default menu item styling
},
],
};
The menu item is already wrapped in an <li>
tag, so if your custom item is simple, such as a title, just supply a string as the value and use the className
property to style it:
export default {
myHtmlSidebar: [
{
type: 'html',
value: 'Core concepts',
className: 'sidebar-title',
},
],
};
Category: create a hierarchy
Use the category
type to create a hierarchy of sidebar items.
type SidebarItemCategory = {
type: 'category';
label: string; // Sidebar label text.
items: SidebarItem[]; // Array of sidebar items.
className?: string;
description?: string;
// Category options:
collapsible: boolean; // Set the category to be collapsible
collapsed: boolean; // Set the category to be initially collapsed or open by default
link: SidebarItemCategoryLinkDoc | SidebarItemCategoryLinkGeneratedIndex;
};
Example:
export default {
docs: [
{
type: 'category',
label: 'Guides',
collapsible: true,
collapsed: false,
items: [
'creating-pages',
{
type: 'category',
label: 'Docs',
items: ['introduction', 'sidebar', 'markdown-features', 'versioning'],
},
],
},
],
};
Use the shorthand syntax when you don't need customizations:
export default {
docs: {
Guides: [
'creating-pages',
{
Docs: ['introduction', 'sidebar', 'markdown-features', 'versioning'],
},
],
},
};
Category links
With category links, clicking on a category can navigate you to another page.
Use category links to introduce a category of documents.
Autogenerated categories can use the _category_.yml
file to declare the link.
Generated index page
You can auto-generate an index page that displays all the direct children of this category. The slug
allows you to customize the generated page's route, which defaults to /category/[categoryName]
.
export default {
docs: [
{
type: 'category',
label: 'Guides',
link: {
type: 'generated-index',
title: 'Docusaurus Guides',
description: 'Learn about the most important Docusaurus concepts!',
slug: '/category/docusaurus-guides',
keywords: ['guides'],
image: '/img/docusaurus.png',
},
items: ['pages', 'docs', 'blog', 'search'],
},
],
};
See it in action on the Docusaurus Guides page.
Use generated-index
links as a quick way to get an introductory document.
Doc link
A category can link to an existing document.
export default {
docs: [
{
type: 'category',
label: 'Guides',
link: {type: 'doc', id: 'introduction'},
items: ['pages', 'docs', 'blog', 'search'],
},
],
};
See it in action on the i18n introduction page.
Embedding generated index in doc page
You can embed the generated cards list in a normal doc page as well with the DocCardList
component. It will display all the sidebar items of the parent category of the current document.
import DocCardList from '@theme/DocCardList';
<DocCardList />
📄️ Sidebar items
We have introduced three types of item types in the example in the previous section autogenerated, which we will explain in detail later.
📄️ Autogenerated
Docusaurus can create a sidebar automatically from your filesystem structure: each folder creates a sidebar category, and each file creates a doc link.