Create top navigation menu in Docusaurus
5 minute read
The goal is to add the top navigation menu items each having their own sidebar menu. The sidebar must use automatic folder structure. See the API top navbar item in Docusaurus site as an example of the result.
Prerequisites
To add the top navigation menu to Docusaurus, make sure you have Docusaurus installed:
- To check that you have Node.js installed:
node --version
. If you don’t see the version, download and install Node.js. - To check that you have Docusaurus installed:
npx docusaurus --version
. If you don’t see the version, follow the installation instructions here.
I assume that you’ve installed your Docusaurus. Open the project folder in VS Code or any other IDE of your liking. For these instructions, I’m using my test Docusaurus project:
-
repository: https://github.com/ivancheban/my-site
Add top navigation menu items
By default, a Docusaurus site doesn’t have the top navigation menu other than Docs
.
To add another top navigation menu item with its own sidebar, your docs folder must include a separate folder for each sidebar. Make sure you have separate folders with .md files in the docs
folder.
-
Copy the existing folders and files in the
docs
folder to a separate folder, for example,default-docs
. Now, the path to the existing Docusaurus files isdocs/default-docs
. -
Add another folder to the docs folder, for example,
docs/test
. -
Add your .md files and folders to the
docs/test
folder. -
Add two items to the navbar in the
docusaurus.config.js
file:where:
-
type
is the type of the navigation menu item:docSidebar
. -
sidebarId
is a unique ID of your sidebar, for example,defaultSidebar
ornewSidebar
. You can create your own ID. -
position
is the position to the left (default) or to the right in the top navbar. -
label
is the actual name displayed in the top navbar:Docs
,Test
, you name it.
Click here to view the entire
docusaurus.config.js
file.
-
-
Add your sidebars to the
sidebars.js
file, using thesidebarId
value for each of your sidebars.where:
-
type
isautogenerated
meaning that folder structure is used for generating the sidebar from .md files. You control the hierarchy using the front mattersidebar_position
value. -
dirName
is the name of the folder with .md files, for example,default-docs
ortest
folder.
Click here to view the entire
sidebars.js
file.
-
Note
Don’t forget to change the paths relative to the folders in thedocs
folder. For example, I had to change the link in the Start here
button on the main page in the src/pages/index.js
and in the footer of docusaurus.config.js
from /docs/intro
to /docs/default-docs/intro
.
As the result, the top navigation bar now has two items: Docs
and Test
. Each of these items has its own sidebar that’s built automatically from the .md files in the separate folders of the docs
folder.
Each sidebar may have subsections based on the subfolders with the _category_.json
file. See the default Docusaurus tutorial-basics
and tutorial-extras
folders.
View the real-world example here:
-
repository: https://github.com/ivancheban/my-site
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.