Tag Archives: XM Cloud

How to Create a New Story for the Card Component in Sitecore XM Cloud with Storybook

The Card component is created in my guide on Building Variant Card Components in Sitecore XM Cloud with Next.js.

Storybook is an excellent tool for developing UI components in isolation, and in this post, we’ll walk through creating a new Story for the Card component in your Sitecore XM Cloud Foundation Head project. We will define three different versions of the Card component: Default, Small, and Large, with varying layout sizes and mock data.

If you haven’t set up Storybook yet, check out my previous post on Integrating Storybook with Sitecore XM Cloud Foundation Head for a step-by-step guide.

Step 1: Create a Story File for the Card Component

Inside your Storybook setup, create a new file in your project, following a similar path to where your Card component exists. Typically, you’ll want to place it within a stories or components folder.

Let’s assume your project structure looks like this:

src/  
components/
Card.tsx
stories/
Card.stories.tsx

In Card.stories.tsx, we will define the stories for the Card component using mock data.

Step 2: Import Dependencies and Define Metadata

First, import the necessary modules from Storybook and Sitecore JSS to ensure the component and fields are correctly typed:

import type { Meta, StoryObj } from '@storybook/react'; 
import { CardWithImage as CardImage } from '../components/Card';
import { ImageField, LinkField } from '@sitecore-jss/sitecore-jss-nextjs';
import '../assets/main.scss';

Next, define the metadata (meta) for the Card component, which will be used by Storybook to render the component properly:

 
const meta = {
  component: CardImage,
} satisfies Meta<typeof CardImage>;

export default meta;

This block of code informs Storybook that the CardImage component is the one being used and provides the necessary type checks.

Step 3: Define Story Variants

We’ll now create different versions of the Card component (called “stories”) by passing in mock data. Each story represents a different size of the card (Default, Small, and Large), using mock heading, body, link, and image fields from Sitecore JSS.

Here’s the structure for our Default, Small, and Large stories:

 type Story = StoryObj;
// Default Card
export const Default: Story = {
args: {
params: {
styles: 'col-lg-4', // Custom class for layout
},
fields: {
heading: {
value: 'Sample Heading',
},
body: {
value: 'This is a sample content. You can replace this with your actual content from Sitecore.',
},
link: {
href: 'https://www.example.com',
text: 'Click here to learn more',
} as unknown as LinkField,
image: {
src: 'https://via.placeholder.com/300x200', // Dummy placeholder image
alt: 'Placeholder Image',
} as ImageField,
},
index: 0, // Optional index property
},
};
// Small Card
export const Small: Story = {
args: {
params: {
styles: 'col-lg-3', // Smaller layout
},
fields: {
heading: {
value: 'Sample Heading',
},
body: {
value: 'This is a sample content. You can replace this with your actual content from Sitecore.',
},
link: {
href: 'https://www.example.com',
text: 'Click here to learn more',
} as unknown as LinkField,
image: {
src: 'https://via.placeholder.com/300x200', // Dummy placeholder image
alt: 'Placeholder Image',
} as ImageField,
},
index: 0,
},
};
// Large Card
export const Large: Story = {
args: {
params: {
styles: 'col-lg-6', // Larger layout
},
fields: {
heading: {
value: 'Sample Heading',
},
body: {
value: 'This is a sample content. You can replace this with your actual content from Sitecore.',
},
link: {
href: 'https://www.example.com',
text: 'Click here to learn more',
} as unknown as LinkField,
image: {
src: 'https://via.placeholder.com/300x200', // Dummy placeholder image
alt: 'Placeholder Image',
} as ImageField,
},
index: 0,
},
};

Step 4: Running Storybook

Once your story is defined, you can launch Storybook to visualize the different variants of the Card component. Run the following command in your terminal:

npm run storybook 

Storybook will start on http://localhost:6006. You should see the Card component rendered with three variants: Default, Small, and Large. Each will show a mock card with a heading, body, link, and image.

Integrating Storybook with Sitecore XM Cloud Foundation Head

Storybook is a powerful tool for developing, documenting, and testing UI components in isolation. It can be incredibly useful when working with Sitecore XM Cloud projects. In this guide, I will walk you through integrating Storybook into the Sitecore XM Cloud Foundation Head project.

Prerequisites

Make sure you have the following installed:

  • Node.js (We’ll use NVM to manage Node.js versions)
  • GitHub CLI: Install it using the command below:
  • winget install --id github.cli

After installation, restart your terminal.

Step 1: Manage Node Versions with NVM (Optional)

To ensure you’re using the correct Node.js version, install NVM (Node Version Manager) for Windows.

  1. Download the NVM for Windows installer.
  2. Install the latest Node.js version by running:
  3. nvm install latest
  4. Verify the installation with
  5. node -v

Step 2: Clone the Sitecore Foundation Head Repository

Now, authenticate with GitHub using the CLI and clone the Sitecore XM Cloud Foundation Head repository:

gh auth login
gh repo clone sitecorelabs/xmcloud-foundation-head-dev
cd xmcloud-foundation-head-dev

Step 3: Install Dependencies

In the project directory, install the necessary dependencies:

npm install 

Once dependencies are installed, build and start the project:

npm run build 
npm start

Step 4: Set Up Storybook

Install Storybook CLI

If you don’t already have npx installed globally, do so by running:

npm i -g npx 

Now, initialize Storybook in your project:

npx storybook init 

Step 5: Add Additional Packages

To extend Storybook functionality with additional features, install the following packages:

npm install --save-dev @storybook/addon-actions @storybook/addon-links @storybook/nextjs 

Step 6: Update package.json Scripts

To streamline Storybook’s usage in your project, update the scripts section in your package.json file to include the following commands:

"scripts": {   
"prestorybook": "",
    "storybook": "storybook dev -p 6006",
    "build-storybook": "storybook build"
}

Step 7: Configure TypeScript (If Applicable)

If your project is using TypeScript, you may need to adjust the tsconfig.json file to ensure that Storybook resolves the correct type definitions.

  1. Open tsconfig.json.
  2. Update the compilerOptions section with the following entry:
"compilerOptions": {   
"paths": {
"react": ["node_modules/@types/react"]
}
}

This ensures that Storybook uses the correct React type definitions, which is critical for TypeScript-based projects.

Step 8: Run Storybook

To start Storybook and view your components in an isolated environment, simply run:

npm run storybook 

This will launch Storybook on http://localhost:6006, where you can view and interact with your components in real-time.

My experience preparing for XM Cloud Developer certification exam

I recently took the XM Cloud Developer Certification exam, and I think sharing my experience to the public will help them cover the exam

About the Exam

50 questions for 100 minutes to finish it.

To pass and obtain certification, minimum score of 80% is required

Pre-requisites

This exam is intended for Sitecore XM Cloud developers. You should be familiar with Sitecore Cloud portal and XM Cloud application. It will be good to have some basic experience with Sitecore JSS as well.

Exam Competencies

The exam covers a wide range of topics, including:

  1. XM Cloud Architecture and Developer Workflow – 12%
  2. Deployment of XM Cloud Projects – 16%
  3. Renderings and Layout – 14%
  4. Sitecore Content Serialization – 14%
  5. Sitecore APIs & Webhooks – 10%
  6. XM Cloud Pages – 10%
  7. Security for Developers – 10%
  8. Data Modeling – 14%

How to Prepare for the Exam

  • Sitecore e-learning platform, is where you should start.
  •  XM Cloud documentation for developers contains detailed information for each topic.
  • If possible, spend time on XM Cloud portal to navigate trhough different sections, to understand each topic in real exams
  • Sitecore Videos are in a great help.
  • I’ve generated 81 different questions with possible answers and created Google Forms with them. I’ve divided the questions by competency so each colleague can test specific areas, rather than answering all the questions. If you would like to test your competencies, send me a message, and I will provide you with the link to the form.