Creating a new section
In this guide, you will learn how to create a new section for your storefront and make it available in the Headless CMS. This solution is useful when your project requires a section not natively available in FastStore.
For this guide, we will create two sections:
  • Call to Action Gets a specific action from the user.
  • OurStores: Indicates the closest store for the user.
    Sections are page components that wrap up other components, enabling you to create cohesive and functional content for your store. For example, the Hero section is a native section that includes the following components: Hero, HeroImage, and HeroHeader.

Before you begin

1. Integrate the store with the Headless CMS

All sections must be available in the Headless CMS so they can be added and managed on your store's pages. To integrate your FastStore project, please refer to the Headless CMS integration track.

2. Knowledge of how sections and content types work on Headless CMS

During the creation of a new section, you will create files such as sections.json and content-types.json, which follow a structure established for the Headless CMS.

3. Install the FastStore CLI

Make sure to install the FastStore CLI to use its commands locally. Refer to the FastStore CLI guide for more information.

Call to Action

  1. In the FastStore root directory, create a folder named cms.
  2. Inside cms, create the faststore folder.
  3. Within the cms/faststore folder, create sections.json file.
  4. In the sections.json file, add the new section that you want to display in the Headless CMS. The schema below defines how the Headless CMS renders a section:
sections.json
[
{
"name": "CallToAction",
"schema": {
"title": "Call To Action",
"description": "Get your 20% off on the first purchase!",
"type": "object",
"required": [
"title",
"link"
],
"properties": {
"title": {
"title": "Title",
"type": "string"
},
"link": {
"title": "Link Path",
"type": "object",
"required": [
"text",
"url"
],
"properties": {
"text": {
"title": "Text",
"type": "string"
},
"url": {
"title": "URL",
"type": "string"
}
}
}
}
}
}
]
This new section receives a title and a link pointing to a specific location.

Step 2: Creating the new section

To render the CallToAction section you created in the previous step, you need to create this section component.
  1. If you don't already have it, create a folder named components inside the src directory.
  2. Inside the components folder, create a file named CallToAction.tsx and add the following code:
src/components/CallToAction.tsx
import React from "react";

export interface CallToActionProps {
title: string;
link: {
text: string;
url: string;
};
}

export default function CallToAction(props: CallToActionProps) {
return (
<section>
<h2>{props.title}</h2>
<a href={props.link.url}>{props.link.text}</a>
</section>
);
}
This section will receive the link and title defined previously in the sections.json file.
  1. Create a file named index.tsx inside the components folder.
The index.tsx file provides an entry point for importing and using the CallToAction component. It acts as a container for all the components within the components folder and allows for easier organization and reusability of code.
Open the index.tsx file and add the following code:
src/components/index.tsx
import CallToAction from './CallToAction'

export default { CallToAction }

Step 3: Synchronizing the new section with the Headless CMS

  1. In the terminal, run faststore cms-sync. This command will synchronize the new section you created with the CMS.
  2. Go to the VTEX Admin and access Storefront > Headless CMS.
  3. Click on the Page content type.
{"base64":"  ","img":{"width":3824,"height":1360,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":321106,"url":"https://vtexhelp.vtexassets.com/assets/docs/src/create-new-section-cms___51aed56fb1be8d73c2fee14e54151c3d.png"}}
  1. In the Sections tab, click the +, search for the new Call to Action section, and add it to your page.
{"base64":"  ","img":{"width":1898,"height":736,"type":"gif","mime":"image/gif","wUnits":"px","hUnits":"px","length":928366,"url":"https://vtexhelp.vtexassets.com/assets/docs/src/create-new-section-cms-component___994587c4fa77d99a04fda72524266d4d.gif"}}

OurStore

  1. After creating the files and folders, In the sections.json file, add the new section that you want to display in the Headless CMS. The schema below defines how the Headless CMS renders the OurStores section that we will use in the example:
sections.json
[
{
"name": "OurStores",
"schema": {
"title": "Our Stores",
"description": "Search stores near to the users",
"type": "object",
"required": ["title", "description"],
"properties": {
"title": {
"title": "Title",
"type": "string",
"default": "Explore Nearby Stores"
},
"description": {
"title": "Link Path",
"type": "string",
"default": "Discover the closest store to you and pay us a visit."
}
}
}
}
]
  1. Inside the components folder, create a file named OurStores.tsx and add the following code:
src/components/OurStores.tsx
import React from 'react'
import { Button, Icon, SelectField } from '@faststore/ui'

export interface OurStoresProps {
title: string
description: string
}

export default function OurStores(props: OurStoresProps) {
return (
<section>
<h2>{props.title}</h2>
<p>{props.description}</p>
<div>
<SelectField
id="select-state-store"
label="State:"
options={{
newYork: 'New York',
arizona: 'Arizona',
massachusetts: 'Massachusetts',
hawaii: 'Hawaii',
}}
/>
<SelectField
id="select-city-store"
label="City:"
options={{
newYorkCity: 'New York City',
phoenix: 'Phoenix',
boston: 'Boston',
honolulu: 'Honolulu',
}}
/>
<Button
variant="secondary"
icon={<Icon name="ArrowRight" />}
iconPosition="right"
>
Find Store
</Button>
</div>
</section>
)
}
For this section, we will use the following components from the Faststore UI: Button, Icon and SelectField.
  1. Create a file named index.tsx inside the components folder, and add the following code:
src/components/index.tsx
import OurStores from './OurStores'

export default { OurStores }
  1. Repeat the steps in Step 3: Synchronizing the new section with the Headless CMS, and you should see the new section but without styles as in the example below:
{"base64":"  ","img":{"width":1144,"height":175,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":24240,"url":"https://vtexhelp.vtexassets.com/assets/docs/src/section-structure-without-styles___7be40088d6137bdd63cbb4940c2fa599.png"}}
To add styles to OurStore, refer to the following section Importing styles when creating a new section.

Adding styles to a new section

After creating a new section, you can style it to match your store's branding. Follow the steps below using the OurStores section as an example.

Instructions

  1. In your store code, open the components folder and create a .module.scss file for your component. For example, if your component is OurStores, name the file ourStores.module.scss. This file will contain the styles specific to this section.
  2. Import the necessary FastStore UI styles into your .module.scss file. For example, to style the OurStores component, we imported the styles for the Button and SelectField components. The Select styles are also imported since SelectField inherits them.
.ourStores {
@import '@faststore/ui/src/components/atoms/Button/styles.scss';
@import '@faststore/ui/src/components/atoms/Select/styles.scss';
@import '@faststore/ui/src/components/molecules/SelectField/styles.scss';
}
  1. Open the component's .tsx file. For example, OurStores.tsx.
  2. Import the newly created stylesheet into the component:
import React from 'react'
import { Button, Icon, SelectField } from '@faststore/ui'

import styles from './ourStores.module.scss' ```

5. Apply the styles to your components using `className={styles.<className>}`. Replace `<className>` with the appropriate class name defined in your stylesheet. For example:
```tsx mark=13
import React from 'react'
import { Button, Icon, SelectField } from '@faststore/ui'

import styles from './ourStores.module.scss'

export interface OurStoresProps {
title: string
description: string
}

export default function OurStores(props: OurStoresProps) {
return (
<section className={styles.ourStores}>
<h2>{props.title}</h2>
<p>{props.description}</p>
<div>
<SelectField
id="select-state-store"
label="State:"
options={{
newYork: 'New York',
arizona: 'Arizona',
massachusetts: 'Massachusetts',
hawaii: 'Hawaii',
}}
/>
<SelectField
id="select-city-store"
label="City:"
options={{
newYorkCity: 'New York City',
phoenix: 'Phoenix',
boston: 'Boston',
honolulu: 'Honolulu',
}}
/>
<Button
variant="secondary"
icon={<Icon name="ArrowRight" />}
iconPosition="right"
>
Find Store
</Button>
</div>
</section>
)
}
  1. Open the terminal and run yarn dev to see your changes.
{"base64":"  ","img":{"width":2552,"height":946,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":135138,"url":"https://vtexhelp.vtexassets.com/assets/docs/src/without-styles___4680d2a9a67e4175f3b73eff036ca8ac.png"}}
  1. Add more styles to the section by using design tokens and component-specific data attributes to the .module.scss file:
.ourStores {
@import '@faststore/ui/src/components/atoms/Button/styles.scss';
@import '@faststore/ui/src/components/atoms/Select/styles.scss';
@import '@faststore/ui/src/components/molecules/SelectField/styles.scss';

width: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: var(--fs-spacing-5);
margin: auto;
background-color: var(--fs-color-neutral-bkg);

[data-fs-select-field] {
flex-direction: column;
align-items: flex-start;

[data-fs-select] {
border: var(--fs-border-width) solid var(--fs-border-color);
margin-top: var(--fs-spacing-0);
width: 10rem;
height: var(--fs-control-min-height);

select {
width: 100%;
}
}
}
}

.ourStores__title {
font-size: var(--fs-text-size-title-section);
margin-bottom: var(--fs-spacing-1);
}

.ourStores__content {
display: flex;
align-items: flex-end;
column-gap: var(--fs-spacing-5);
margin: var(--fs-spacing-5);
}
  1. Update the component's file by adding the appropriate CSS classes to elements, ensuring they align with the newly defined styles in your stylesheet. For example:
import React from 'react'
import { Button, Icon, SelectField } from '@faststore/ui'

import styles from './ourStores.module.scss'

export interface OurStoresProps {
title: string
description: string
}

export default function OurStores(props: OurStoresProps) {
return (
<section className={styles.ourStores}>
<h2 className={styles.ourStores__title}>{props.title}</h2>
<p>{props.description}</p>
<div className={styles.ourStores__content}>
<SelectField
id="select-state-store"
label="State:"
options={{
newYork: 'New York',
arizona: 'Arizona',
massachusetts: 'Massachusetts',
hawaii: 'Hawaii',
}}
/>
<SelectField
id="select-city-store"
label="City:"
options={{
newYorkCity: 'New York City',
phoenix: 'Phoenix',
boston: 'Boston',
honolulu: 'Honolulu',
}}
/>
<Button
variant="secondary"
icon={<Icon name="ArrowRight" />}
iconPosition="right"
>
Find Store
</Button>
</div>
</section>
)
}
  1. Navigate to http://localhost:3000 in your browser to see the updated component with the applied styles.
{"base64":"  ","img":{"width":2552,"height":946,"type":"png","mime":"image/png","wUnits":"px","hUnits":"px","length":129326,"url":"https://vtexhelp.vtexassets.com/assets/docs/src/with-styles___4b1cd40d4dcbbba544fd051b1f2dcb36.png"}}
Contributors
2
Photo of the contributor
Photo of the contributor
Was this helpful?
Yes
No
Suggest Edits (GitHub)