Create Custom Components in Storyblok and Next.js
Storyblok is the first headless CMS that works for developers & marketers alike.
In this short tutorial, we will see how to start making our own components and extend the existing ones with Next.js and Storyblok. We will add a new Hero
component with two Layout
options in Storyblok, along with the code in our frontend. We will also extend our existing Feature
component by including images and better styling.
If you’re in a hurry, have a look at our live demo in Stackblitz! Alternatively, you can explore or fork the code from the Next Ultimate Tutorial GitHub Repository.
Requirements
This is a part of the Ultimate Tutorial Guide for Next.js. You can find the previous part of the series here, which shows how to render Storyblok stories dynamically in Next.js. We recommend you to take a look at that tutorial before starting this one.
You can also take a look at the optional tutorial of the series - Create Dynamic Menus in Storyblok and Next.js.
We will use the code from the previous tutorial as a starting point. You can find the Stackblitz demo for it here.
Now, let’s edit the Feature
’s schema, and add image
as a new field {1}.
This image field should be of type asset. To change the field type and view other options related to the field, click on the field image.
Change the field type to Asset
{1} and select Images
in the Filetypes
{2}.
We will have to change the code in our Next.js app with the updated fields and styles.
Replace the code of Feature.js
to the following:
Let’s go ahead and add images to the features of a grid on the about
page. To do that, we need to click on the Grid
and then click on one of the features inside it. We will see that we have a field named image
already present there. We can now add an image to a feature. Let’s add images for other features as well. It should look something like this -
Creating a Hero Component
Before creating the schema for this new block, let’s first of all consider what we would like it to look like and what options we want to provide. I would say that a headline
, a subheadline
and a background_image
field would be a great place to start. However, let’s kick it up a notch and provide the option to make this hero component use the full width of the screen.
First, go to the Block Library {1} and create a New Block {2}.
It should be a Nested block {1} with the name hero
{2}.
Now we can create our first three fields:
headline
: field type Textsubheadline
: field type Textbackground_image
: field type Image
The required steps for this are exactly the same as we have taken to add an image
field to the Feature component earlier in this tutorial.
Once these fields are ready, we can create the layout
field to make it possible to choose between two different layouts. Let’s add the field and choose Single-Option as its type {1}.
Let's add two key-value pairs which represent the possible choices {1}, hide the empty option {2}, and set the default value to constrained
{3}:
Finally, save the component and add it to our Home story, right above the Teaser. You can already add some sample content to the fields. Of course, nothing will be shown in our frontend just yet. So let’s take care of that next, shall we?
As we added a new component, we will also need to add the component to the frontend code of our project. Create a Hero.js
file inside the components
folder, and add the following code to it.
Note that we are changing the styles of the Hero
section depending on the selected layout. This is being done conditionally on line 13.
The only thing left to do is to add this component to our list of dynamic components in _app.js
.
Save and go back to our Home
story in Storyblok. We will see something like this -
We also have an option to change the layout here. We can choose the other one from the dropdown and we will see the changes.
Wrapping Up
In this tutorial, we saw how to extend and create new components from scratch with different types of fields, along with the integration of those components into the frontend of our application. Congratulations!
In the next part of this series, we will see how to create and render blog articles in Storyblok and Next.js. You can find it here.
Resource | Link |
---|---|
Storyblok Next.js Ultimate Tutorial | https://www.storyblok.com/tp/nextjs-headless-cms-ultimate-tutorial |
Stackblitz Demo | https://stackblitz.com/edit/create-custom-components-in-storyblok-and-next-js |
Storyblok Technologies Hub | https://www.storyblok.com/technologies |
Next.js Technology Hub | Storyblok Next.js Technology Hub |
Storyblok React SDK | storyblok/storyblok-react |