Next.js with styled-components and Snipcart
Storyblok is the first headless CMS that works for developers & marketers alike.
Introduction
This tutorial will show you how to use styled components with Next.js and Storyblok. Styled components and other libraries will bring more flexibility in customizing Blok. You will see how to create a component and give the flexibility of managing responsive design or more power for your user from Storyblok.
It's essential to find the balance between what a User from Storyblok can do and your code.
As for the bonus, I will show you how to implement Snipcart, a shopping cart that you can easily integrate into any website in minutes.
Prerequisites
We will start with the tutorial from Add a headless CMS to Next.js in 5 minutes. It shows how to create components in Storyblok and Next.js. We recommend that tutorial before starting this one. You can find the tutorial GitHub repository.
Next, we will install styled-components with the code block below:
Then, create a next.config.js
file at the root with this code :
If you are missing something you can always check this repository, each step of the tutorial has its own branch.
Create a card component with styled-components
Let's now add a block named sbCard which will be a Nested type block. It should have the following fields :
- image of type asset {1}: similar to the way we added in this tutorial.
- title of type text {2}
- text of type textarea {3}
You can get the asset I used in my storyblok-next-demo repository.
Let’s start creating a new file called SbCard.js
in the components folder and add the following code to it.
The container component will be an <li>
element because we will use it as an item on the list. It will be wrapped in a grid in the next step. Here you can see a simple use of styled-components, on the next step we will see how to create a styled-components based on props.
Now we can create a sbCard
, it should display like this:
Wrap sbCard in a Grid layout
In this part, we will create a Grid Layout to wrap our sbCard. We will pass some parameters from Storyblok to the component in order to choose how many columns we want to display in a different formats (mobile, tablet, desktop) and optionally add a padding-top.
Let's create a block named sbGrid which will be a Nested type block. It should have the following fields :
- content of type Blocks
- nbColumnMobile of type number and 1 as default value.
- nbColumnTablet of type number and 2 as default value.
- nbColumnDesktop of type number and 4 as default value.
- paddingTop of type number and 0 as default value.
You should have something like this :
Let’s create a new file called SbGrid.js in the components folder and add the following code to it :
Now let’s modify our content in Storyblok. Create a sbGrid and move the created sbCard inside and add 3 new sbCard components.
You should have something like this :
So what we have just done ? We use the Grid CSS property and media queries to define the display. Storyblok give the number of columns in each format and with styled-components we passed props in order to have a dynamic grid.
For example, you can reuse sbGrid and have different column in the same format.
Here is an example, I create two sbGrid with 48px of padding-top, the first one with 3 columns and the second with 2 columns.
And that's it! Now you can use sbGrid and sbCard as you want, or create more flexible and customizable components with styled-components from Storyblok.
You can find the final result of this part on the branch feat/final-result.
Now, let’s start with Snipcart!
How it works
Unlike many e-commerce solutions, Snipcart lives on your site, in its HTML client-side code.
Add the shopping cart to your site with a simple JavaScript snippet inclusion. It's similar to adding a third-party script like Google Analytics.
You create products by adding simple HTML attributes—product name, price, description, etc.—to elements on your site. Usually, developers add these attributes to a “buy” button.
You manage orders within a user-friendly merchant dashboard. Unlike the cart, this dashboard doesn't live on your site, it's hosted on our own servers. You can access it at any time at this URL:
https://app.snipcart.com/dashboard
Source: https://docs.snipcart.com/v3/#how-it-works
Requirements
We follow the tutorial we have just finished.
You can find the final result in this branch GitHub repository.
First, create an account on Snipcart.
Add Snipcart script
Check your Snipcart account and go to the credentials page. You should see this image below :
In your Next.js project, create a _document.js and replace <your-public-api-token> by your API key from Snipcart.
It's important to note that if you have an attribute lang in your tag <Html>, the script will use the language you have in your Html. So if you have <Html lang="fr">, you will have the cart in French :
Create a ShoppingCartButton
Let’s make the ShoppingCartButton first. The button will show the current state of your cart. And on click, it will open the Cart.
Create a SbShoppingCartButton.js, you don’t need to create a component from Storyblok.
Here is the code below:
Then, add in index.js the button :
You should have:
On clicking the button, a cart should be opened :
Add Snipcart Buy Button
Add parameters price to the sbCard from :
Let’s add the buy button. It will help us to add your product in the cart. Below the code, we add a button and change the layout card in order to make the card more consistent.
Let explained a bit the code of the button, here what you got in the DOM :
The important thing is to add the class snipcart-add-item, if you don’t, the script won’t recognize the button, and you won’t add anything to your cart. After the other attributes help to describe your product. More information here.
In the cart, it shows like this :
To conclude you should have now :
And when you click on the Add to cart button, the cart should open and get your product.
Then you can click on Checkout, but to put it in production you need to put a payment gateway in your Snipcart account and everything should work without doing anything.
Wrapping Up
In this tutorial, you see how to use styled-components in Next.js and how to create flexible components in different format and bring more power and customization to your user.
With Snipcart, you see how is easy to set up and can be plugged to any frontend you have. If you leave the website and come again, Snipcart keeps your last cart state. And also on the payment of the product, it will browse your e-commerce website and check the products on your cart to get the same information (price, id etc…) as your website for the security.
Resources
Inspired by:
https://snipcart.com/blog/next-js-ecommerce-tutorial-example
To customize the cart part:
https://snipcart.com/blog/customizing-snipcart-to-reflect-the-style-of-your-websi