Create Dynamic Menus in Storyblok and Gatsby.js
Storyblok is the first headless CMS that works for developers & marketers alike.
If you'd like to build a multilanguage website with Gatsby, please follow this tutorial instead. If you'd like to build a website without internationalization logic, please keep following the rest of this article and the Ultimate Tutorial articles.
In this part of the tutorial series, we'll make the menu in our header dynamic, so that can manage it directly through Storyblok!
If you’re in a hurry, have a look at our Ultimate Tutorial GitHub repo part 3 branch!
Requirements
This tutorial is part 3 of the Ultimate Tutorial Series for Gatsby.js! We recommend that you follow the previous tutorials before starting this one.
Enter the name config
{1} and choose Content type block {2}.
Now you can create a new field with the name header_menu
{1} and choose the field type Blocks {2}.
In this field, we would like to provide the possibility to add menu links as nested blocks. To accomplish that, let’s create another new block. This time it should be a Nested block {1} with the name menu_link
{2}.
Now we can add a new field called link
in this newly created block and choose Link as the field type.
Alright, our component schemas are almost done! Just one more step: to avoid that just any block could be nested in our header_menu
, we want to make sure that only specific components can be inserted {1}. Now you can choose the menu_link
block in the whitelist {2}.
With that out of the way, we can now go to the Content section of our Storyblok space. Here, we want to create a new story with the name Config {2}, using our recently created content type Config {3}.
If you open this newly created Config story, you can now nest as many menu_link
blocks in the header_menu
field as you would like. For now, let’s add our Blog and About page.
Rendering the Menu in Gatsby.js
Now, let's create the code that will render our menu in the frontend of our application. First, let’s review what our imports from the Storyblok Gatsby SDK -- storyblokEditable
and StoryblokComponent
-- do:
storyblokEditable
makes our components editable in our Real-Time Visual Editor.StoryblokComponent
sets up our page for our Storyblok components.
Then, let's set up our components: config.js
, and menuLink.js
to match with our blocks created in Storyblok.
Let's start from config.js
file.
First, we'll make sure that the Config content-type story will be generated through File System Route API from Gatsby.
After creating config.js
file, let's modify {storyblokEntry.full_slug}.js
file because Config content-type and Page content-type have different structures from draft/published JSON.
We'll conditionally render different data paths to render different content-type stories in {storyblokEntry.full_slug}.js
file.
Line 14 and 16 are conditionally filtering the names of the content-type. Page content-type returns dynamic components by mapping inside of the body
. On the other hand, Config content-type returns dynamic components without body
.
Next up, we'll create menuLink.js
file.
Let’s make sure those components render. In layout.js
, add your components:
In the previous tutorial (Render Storyblok Stories Dynamically in Gatsby), we didn't make header navigation to be dynamic. Now, we have Config content-type component and Menu Link nested component ready. We can update navigation.js
file to dynamically render header navigation items.
Now, if you go back to your Storyblok Visual Editor, you should be able to see your menu being rendered! You can add more links, remove them, or even reorder them if you like.
Continue reading and Create Custom Components in Storyblok and Gatsby.js
Resource | Link |
---|---|
Storyblok Gatsby.js Ultimate Tutorial | https://www.storyblok.com/tp/gatsby.js-headless-cms-ultimate-tutorial |
Gatsby Repo | https://github.com/storyblok/gatsby-storyblok-boilerplate |
Storyblok Technologies Hub | https://www.storyblok.com/technologies |
Gatsby.js Technology Hub | Storyblok Gatsby.js Technology Hub |
Storyblok Gatsby SDK | storyblok/gatsby-source-storyblok |