Sidebar
To generate a sidebar to 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
.
#
Sidebar objectA sidebar object contains sidebar items and it is defined like this:
For example:
In this example, notice the following:
- The key
docs
is the id of the sidebar. The id can be any value, not necessarilydocs
. Getting Started
is a category within the sidebar.greeting
anddoc1
are both sidebar item.
Shorthand notation can also be used:
note
Shorthand notation relies on the iteration order of JavaScript object keys for the category name. When using this notation, keep in mind that EcmaScript does not guarantee Object.keys({a,b}) === ['a','b']
, yet this is generally true.
#
Using multiple sidebarsYou can have multiple sidebars for different Markdown files by adding more top-level keys to the exported object.
Example:
By default, the doc page the user is reading will display the sidebar that it is part of. This can be customized with the sidebar type.
For example, with the above example, when displaying the doc2
page, the sidebar will contain the items of secondSidebar
only. Another example of multiple sidebars is the API
and Docs
sections on the Docusaurus website.
For more information about sidebars and how they relate to doc pages, see Navbar doc link.
#
Understanding sidebar itemsAs the name implies, SidebarItem
is an item defined in a Sidebar. A sibarItem as a type
that defines what the item links to.
type
supports the following values
#
Linking to a doc pageSet type
to doc
to link to a documentation page. This is the default type.
Example:
Using just the Document ID is also valid, the following is equivalent to the above:
Using this type will bind the linked doc to current sidebar. This means that if you access the doc1
page, the sidebar displayed will be the sidebar that contains this doc page.
In the example below, doc1
is bound to firstSidebar
.
#
Creating a generic linkSet type
to link
to link to a non-documentation page. For example, to create an external link.
Example:
#
Creating a link to page without sidebarSet type
to ref
to link to a documentation page without binding it to a sidebar. This means the sidebar disappears when the user displays the linked page.
Example:
#
Creating a hierachyThe Sidebar item type that creates a hierarchy in the sidebar. Set type
to category
.
Example:
Note: it's possible to use the shorthand syntax to create nested categories:
#
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
:
#
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: