Sidebar
Creating a sidebar is useful to:
- Group multiple related documents
- Display a sidebar on each of those documents
- Provide a paginated navigation, with next/previous button
To use sidebars on your Docusaurus site:
- Define a file that exports a sidebar object.
- Pass this object into the
@docusaurus/plugin-docs
plugin directly or via@docusaurus/preset-classic
.
#
Default sidebarBy default, Docusaurus automatically generates a sidebar for you, by using the filesystem structure of the docs
folder:
You can also define your sidebars explicitly.
#
Sidebar objectA sidebar is a tree of sidebar items.
A sidebars file can contain multiple sidebar objects.
Example:
Notice the following:
- There is a single sidebar
mySidebar
, containing 5 sidebar items Getting Started
andDocusaurus
are sidebar categoriesdoc1
,doc2
anddoc3
are sidebar documents
tip
Use the shorthand syntax to express this sidebar more concisely:
#
Using multiple sidebarsYou can create a sidebar for each set of markdown files that you want to group together.
Example:
note
The keys tutorialSidebar
and apiSidebar
are sidebar technical ids and do not matter much.
When browsing:
doc1
ordoc2
: thetutorialSidebar
will be displayeddoc3
ordoc4
: theapiSidebar
will be displayed
A paginated navigation link documents inside the same sidebar with next and previous buttons.
#
Understanding sidebar itemsSidebarItem
is an item defined in a Sidebar tree.
There are different types of sidebar items:
- Doc: link to a doc page, assigning it to the sidebar
- Ref: link to a doc page, without assigning it to the sidebar
- Link: link to any internal or external page
- Category: create a hierarchy of sidebar items
- Autogenerated: generate a sidebar slice automatically
#
Doc: link to a docUse the doc
type to link to a doc page and assign that doc to a sidebar:
Example:
The sidebar_label
markdown frontmatter has a higher precedence over the label
key in SidebarItemDoc
.
note
Don't assign the same doc to multiple sidebars: use a ref instead.
#
Ref: link to a doc, without sidebarUse the ref
type to link to a doc page without assigning it to a sidebar.
Example:
When browsing doc1
, Docusaurus will not display the mySidebar
sidebar.
#
Link: link to any pageUse the link
type to link to any page (internal or external) that is not a doc.
Example:
#
Category: create a hierarchyUse the category
type to create a hierarchy of sidebar items.
Example:
tip
Use the shorthand syntax when you don't need category options:
#
Collapsible categoriesFor sites with a sizable amount of content, we support the option to expand/collapse a category to toggle the display of its contents. Categories are collapsible by default. If you want them to be always expanded, set themeConfig.sidebarCollapsible
to false
:
#
Expanded categories by defaultFor docs that have collapsible categories, you may want more fine-grain control over certain categories. If you want specific categories to be always expanded, you can set collapsed
to false
:
#
Autogenerated: generate a sidebarDocusaurus can create a sidebar automatically from your filesystem structure: each folder creates a sidebar category.
An autogenerated
item is converted by Docusaurus to a sidebar slice: a list of items of type doc
and category
.
Docusaurus can generate a sidebar from your docs folder:
You can also use multiple autogenerated
items in a sidebar, and interleave them with regular sidebar items:
#
Autogenerated sidebar metadatasBy default, the sidebar slice will be generated in alphabetical order (using files and folders names).
If the generated sidebar does not look good, you can assign additional metadatas to docs and categories.
For docs: use additional frontmatter:
For categories: add a _category_.json
or _category_.yml
file in the appropriate folder:
info
The position metadata is only used inside a sidebar slice: Docusaurus does not re-order other items of your sidebar.
#
Using number prefixesA simple way to order an autogenerated sidebar is to prefix docs and folders by number prefixes:
To make it easier to adopt, Docusaurus supports multiple number prefix patterns.
By default, Docusaurus will remove the number prefix from the doc id, title, label and url paths.
caution
Prefer using additional metadatas.
Updating a number prefix can be annoying, as it can require updating multiple existing markdown links:
#
Customize the sidebar items generatorYou can provide a custom sidebarItemsGenerator
function in the docs plugin (or preset) config:
tip
Re-use and enhance the default generator instead of writing a generator from scratch.
Add, update, filter, re-order the sidebar items according to your use-case:
#
Hideable sidebarUsing the enabled themeConfig.hideableSidebar
option, you can make the entire sidebar hidden, allowing you to better focus your users on the content. This is especially useful when content consumption on medium screens (e.g. on tablets).
#
Passing custom propsTo pass in custom props to a swizzled sidebar item, add the optional customProps
object to any of the items:
#
Complex sidebars exampleReal-world example from the Docusaurus site: