In the previous post in this series — AI Code Generation — From prompt to XM Cloud component matrix — I focused on requirements, not code. Starting from a manually curated component matrix or Figma design system, I used coding agents to produce one Sitecore-ready requirement markdown file per component under something like ai/prompts/component_specs/Accordion.md.
This post is about what happens next: how I take those requirement files and let coding agents generate a local-first component library with Storybook, staying close to the specs while keeping humans firmly in charge.
For this phase I usually work inside a Next.js + TypeScript repo (often the same one that will later host the XM Cloud head) and lean on:
- React + Next.js App Router for implementation,
- Storybook as the local UI workbench where I develop components in isolation, see different states, and let designers / POs review them without touching the real site,
- OpenAI ChatGPT‑5 Codex as the primary coding agent,
- Claude Sonnet 4 as a strong alternative for analysis and refactors,
- and Google CLI with Gemini 2.5 as a cheaper, but less capable, option when budget is tight.
Going “local‑first” with this setup has helped me in two very practical ways on real projects:
- I can get the component library moving before anyone provisions a Sitecore XM Cloud environment or Experience Edge keys.
- I can make better use of the frontend team when they are available earlier than the Sitecore team — UI engineers build and refine components in Storybook, while Sitecore specialists step in later to wire those components into XM Cloud (BYOC, data contracts, templates).
At a high level, the flow I use looks like this:
The input to this post is only the requirement files from the previous one; Storybook and code show up here for the first time.
From here, the rest of the series builds on this library:
- Wiring these components into XM Cloud BYOC: AI Code Generation — Promoting Storybook components into XM Cloud (BYOC)
- Reusing the same contracts in a Flutter head: AI Code Generation — Flutter head for XM Cloud
- Zooming into the Figma-first variant of this pipeline: AI Code Generation — Figma to Next.js components with Figma MCP and Claude Code
Starting point: requirement files, not spreadsheets
By now, the component matrix work is done. For each component you have a markdown file that looks roughly like this (shortened here for space):
# Requirements
## Name
Accordion
## Description
Expandable FAQ container used to present grouped questions and answers. Authors can supply an optional intro with subtitle, then curate individual accordion items that reveal detailed answers on click. Component supports lazy loading and can reveal additional items with a “Load more” control.
## Fields
### Title
- **Field Type**: Single-Line Text
- **Required**: Yes
...
### Accordion Items
- **Field Type**: Treelist (Accordion Item)
- **Required**: Yes
- **Repeating**: Yes
...
#### Accordion Item Fields:
- **Question**: Single-Line Text - Title displayed on the accordion trigger.
- **Answer**: Rich Text - Content revealed when the panel expands; supports lists and inline links.
- **Anchor ID**: Single-Line Text - Optional anchor for deep-linking directly to the FAQ entry.
## Example Reference
* **URL**: https://www.cws.com/en/workwear/products
* **Element**: `<div class="paragraph paragraph--type--faq lazy-load workwear lazy-inst-1">`
## Notes
- Component lazy-loads content to improve performance; ensure important FAQs appear in the first three slots.
...
My goal for this post is to treat that folder of requirement files as the only input and have agents:
- read each file,
- understand the fields and behavior,
- and generate a small, Storybook‑ready component package for each.
Give the code repo its own AGENTS.md
Just like the requirements workspace had its own AGENTS.md, I give the code repo a dedicated one. This keeps agents aligned on tech stack, structure, and what they are allowed to generate.
Here is a trimmed‑down version of what I use for a Next.js + Storybook component library that will later be wired into XM Cloud:
# AGENTS.md — Local-first component library for XM Cloud
## Project scope
- Next.js + TypeScript component library built Storybook-first.
- Components will later be wired into Sitecore XM Cloud (BYOC).
- Input: requirement markdown files under `ai/prompts/component_specs/`.
## Preferred models
- Primary: OpenAI ChatGPT-5 Codex for coding tasks.
- Secondary: Claude Sonnet 4 for analysis-heavy refactors and reviews.
- Budget: Google CLI with Gemini 2.5 (expect more manual review and corrections).
## Tech stack
- Next.js App Router (e.g., `src/app`).
- React + TypeScript with strict type checking.
- Storybook for component development.
- Styling: CSS Modules or Sass Modules (as configured in this repo).
## File structure
- Components live under `src/components/<ComponentName>/`.
- Each component folder usually contains:
- `<ComponentName>.tsx` — implementation.
- `<ComponentName>.stories.tsx` — Storybook stories.
- `<ComponentName>.props.ts` — typed props / Sitecore mapping (optional).
- `<ComponentName>.module.(css|scss)` — styling.
## Conventions
- Component names are PascalCase (e.g., `Accordion`, `IndustryCard`).
- Props map 1:1 to fields defined in requirement files.
- Components should be pure and data-agnostic: no direct HTTP calls or CMS access.
## What you should generate
- New components and stories based on existing requirement files.
- Supporting types and mock data as needed.
- Do NOT change project configuration or CI scripts unless explicitly asked.
I reference this file from my prompts so agents understand that the requirements repo and the code repo have different roles.
Batch task: generate components from all spec files
Once the repo is agent‑friendly, I set up a batch prompt that tells a coding agent how to transform every requirements file in ai/prompts/component_specs/ into a component folder under src/components/.
Here is a version of that task, adapted to the way I usually run this phase.
# Component generation from requirement files
## Overview
You are an AI coding agent working inside a Next.js + TypeScript project.
Your goal is to read all component requirement markdown files and, for each one, generate a Storybook-ready React component package using the project’s conventions.
Preferred models:
- OpenAI ChatGPT-5 Codex for code generation.
- Claude Sonnet 4 if Codex is unavailable.
- Google CLI with Gemini 2.5 only if necessary and with extra human review.
## Input files
- **Requirements directory**: `ai/prompts/component_specs/` (all `.md` files).
- **Component template prompt**: `ai/prompts/create_component.md` (describes file structure and naming).
## Component name extraction
For each requirements file:
1. **Filename**: start from the filename without `.md` (e.g., `Accordion.md` → `Accordion`).
2. **Content**: if the filename is generic, read the `## Name` section inside the file.
3. **Sanitize**: convert the result to PascalCase for the component name (e.g., `industry-card` → `IndustryCard`).
## Processing strategy
- Scan `ai/prompts/component_specs/` and build a list of all `.md` files.
- Validate that each file can be read and parsed; build a processing queue.
- Process components in batches (for example 5–8 per batch) to keep context manageable.
- For each batch:
- Read `ai/prompts/create_component.md` once.
- For each spec in the batch:
- Extract the component name.
- Read the requirements content.
- Generate or update the component files (`.tsx`, `.stories.tsx`, styles, props) under `src/components/<ComponentName>/`.
- Report which components succeeded or failed.
## Output requirements
For every requirement file, generate a component package that:
- Lives under `src/components/<ComponentName>/`.
- Includes at least:
- `<ComponentName>.tsx` — React component with strictly typed props that map to fields in the requirements file.
- `<ComponentName>.stories.tsx` — Storybook stories showcasing default and key variant states using realistic mock data.
- `<ComponentName>.module.(css|scss)` — styling consistent with the repo.
- Uses existing utilities, design tokens, and shared primitives when available (do not reinvent button or typography primitives).
- Does **not** make any direct calls to Sitecore XM Cloud or Experience Edge; props should be passed in from the outside.
## Logging
Maintain a markdown log at `ai/prompts/component_generation_log.md` with:
```md
# Component generation log
## Summary
- Total specs found: [count]
- Successfully generated: [count]
- Failed: [count]
- Skipped: [count]
## Detailed results
| Component Name | Status | Notes |
|----------------|--------|-------|
| Accordion | ✓ Success | - |
| IndustryCard | ✓ Success | - |
| BrokenSpec | ✗ Failed | Missing required fields section |
Pre-execution checks
Before you start, verify that:
-
ai/prompts/component_specs/exists and contains.mdfiles. -
ai/prompts/create_component.mdis readable and contains guidance on file structure. -
src/components/exists and is writable. - You understand the project conventions from
AGENTS.md.
I generally paste this prompt into the agent, attach or reference the `AGENTS.md`, `create_component.md`, and a handful of requirement files, then let it generate a first batch while I review the output.
---
## Single-component prompt: Accordion as a Storybook-first build
For components that deserve more attention—or when the batch output needs refinement—I use a **single‑component prompt**. Here is a version tailored to the Accordion requirements and a Storybook‑first approach.
```md
You are working inside a Next.js + TypeScript repo that will later be wired into Sitecore XM Cloud.
I need you to build a new component “Storybook-first” based on an existing requirement document.
## Inputs
- Requirement file: `ai/prompts/component_specs/Accordion.md`
- This file defines fields, example values, and behavior for the Accordion component.
- Project conventions: see `AGENTS.md` in the repo root.
- Target location: `src/components/Accordion/`.
## Goals
Implement a fully-typed, accessible Accordion component and its Storybook stories, using the requirement file as the single source of truth.
## Files to create or update
- `src/components/Accordion/Accordion.tsx`
- React component that maps props 1:1 to the fields in the requirements file (titles, intro text, items, load-more toggle, background theme, etc.).
- No direct CMS calls; all data comes from props.
- `src/components/Accordion/Accordion.module.(css|scss)`
- Styling that matches the look and feel implied by the Example Reference and Notes.
- Use the project’s preferred styling approach (CSS Modules or Sass Modules) and design tokens where available.
- `src/components/Accordion/Accordion.props.ts` (optional)
- TypeScript interfaces for props and item types, named consistently with Sitecore field concepts (e.g., `AccordionProps`, `AccordionItem`).
- `src/components/Accordion/Accordion.stories.tsx`
- Storybook stories showing:
- A default FAQ list with 3–5 items.
- A variant with “Load more” enabled and more than 3 items.
- An edge-case variant with minimal content.
## Implementation notes
- Map each Sitecore field in the requirements document to a prop.
- For example, `Load More Enabled` becomes a boolean prop that controls whether extra items are hidden behind a “load more” button.
- For repeating items, create a typed array of item objects (`AccordionItem[]`).
- Ensure accessibility:
- Use semantic markup for accordions (e.g., buttons with `aria-expanded`, `aria-controls`, and appropriate heading levels).
- Support keyboard navigation (Enter/Space to toggle, arrow keys if appropriate for your design system).
- Implement the “only one panel open at a time” behavior described in the Notes.
- Reflect the `Background Theme` field in CSS classes (e.g., `accordion--light-grey`).
## Storybook specifics
- Stories must import the component from `src/components/Accordion/Accordion`.
- Use realistic mock data based on the Example Reference and field guidelines.
- Give the story a clear `title` in the Storybook tree (e.g., `Components/Accordion`).
- Avoid live CMS calls or network requests in stories; use local mock data only.
## Out of scope
- Do NOT register this component in XM Cloud or modify any Sitecore configuration.
- Do NOT create Playwright tests in this task; focus on the component and stories only.
I keep the prompt close to the requirements language so it is easy to verify that every field and behavior made it into the component and its stories.
Storybook-first, local-first workflow
Once a few components like Accordion are in place, the rest of the library tends to follow the same pattern:
- requirement file defines fields and behavior,
- batch prompt generates the initial implementation and stories,
- I refine key components with single‑component prompts,
- and everything is reviewable and testable without talking to XM Cloud yet.
Some habits that have helped:
- Keep components pure: props in, JSX out, with no routing or data‑fetching logic inside.
- Let stories use the same types and mock data you will later reuse in tests.
- When requirements change, update the markdown first, then ask the agent to refactor the component and stories to match.
Tests (Playwright or otherwise) still matter, but I tend to cover them in the BYOC/Integration steps rather than at this first implementation pass.
How this feeds into XM Cloud and beyond
By the end of this step you have:
- a folder of requirement markdown files,
- a growing local‑first Next.js + Storybook component library,
- and prompts and context (
AGENTS.md,create_component.md) that let agents keep generating components in a predictable way.
The next post in this theme—AI Code Generation — Promoting Storybook components into XM Cloud (BYOC)—picks up from here:
- taking the components and stories you now have,
- registering them in the XM Cloud component map,
- aligning props with templates and fields,
- and letting editors compose pages using them in XM Cloud Pages.
Later, the Flutter head post shows how the same requirements and content contracts can be reused in a Flutter-based head, keeping multi-channel experiences aligned without making the Storybook/Next.js library an afterthought.
Additional resources
- Storybook docs: https://storybook.js.org/docs
- Next.js 16 docs: https://nextjs.org/docs
- React TypeScript cheatsheet: https://react-typescript-cheatsheet.netlify.app
- Sitecore XM Cloud docs: https://doc.sitecore.com/xmc/en/developers/xm-cloud/index-en.html
- Sitecore XM Cloud BYOC docs: https://doc.sitecore.com/xmc/en/developers/xm-cloud/create-components-with-bring-your-own-components.html
- AI Code Generation series overview (this repo):
blogs/ai_code_generation_series_overview_and_roadmap.md