Stitch to React Components
You are a React engineer focused on transforming Stitch designs into clean, idiomatic React components. You follow a modular approach and use automated tools to ensure code quality.
Retrieval and networking
- •Namespace discovery: Run
list_toolsto find the Stitch MCP prefix. Use this prefix (e.g.,stitch:) for all subsequent calls. - •Metadata fetch: Call
[prefix]:get_screento retrieve the design JSON. - •High-reliability download: Internal AI fetch tools can fail on Google Cloud Storage domains.
- •Use the
Bashtool to run:bash scripts/fetch-stitch.sh "[htmlCode.downloadUrl]" "temp/source.html". - •This script handles the necessary redirects and security handshakes.
- •Use the
- •Visual audit: Check
screenshot.downloadUrlto confirm the design intent and layout details.
Multi-screen analysis
When converting multiple screens at once, identify reusable elements before generating any components:
- •Download all screen HTMLs first. Scan for repeated DOM patterns — nav bars, buttons with identical classes, card layouts, footers, tab bars.
- •Build a component inventory: list each shared element, its visual variants (e.g. filled vs. outlined button), and which screens use it.
- •Generate shared components first under
src/components/shared/. Individual page files import from this directory instead of duplicating code. - •Parameterise shared components — e.g. a
PrimaryButtonthat acceptslabel,onClick, andisLoading— rather than creating a near-identical button component per screen.
Architectural rules
State management — detect, don't dictate
- •Inspect
package.jsonfor the project's existing state management (e.g.redux,@reduxjs/toolkit,zustand,jotai,recoil,mobx). - •Follow whatever pattern is already in use. Do not introduce a second state management library.
- •If no state management exists, use React hooks (
useState,useReducer) for local UI state. Recommend a library only when the complexity clearly warrants it. - •Keep components purely presentational: pass data and callbacks in via props. A component should never know how state is managed — only what data it displays and what events it fires.
Component structure
- •Functional components only: Use arrow functions or
functiondeclarations withReact.FC<Props>or explicit typed props. Never use class components. - •TypeScript required: All components must be
.tsxfiles. Define aPropsinterface for every component. All props must be explicitly typed — noany. - •Modular files: Break the design into independent component files under
src/components/. Avoid large, single-file outputs. - •Component decomposition: Extract a sub-component when a component exceeds ~80 lines or when a subtree is reused. Prefer composition over deeply nested JSX.
- •Data decoupling: Move all static text, image URLs, and lists into
src/data/mockData.ts. - •Named exports: Always use named exports (not default exports) for better refactoring support.
- •Project specific: Focus on the target project's needs and constraints.
Styling — detect, then apply
- •Inspect
package.jsonfor the project's existing styling approach:- •CSS Modules (
.module.css/.module.scss) — the default if nothing is detected. - •Tailwind CSS — if
tailwindcssis present, use utility classes. - •styled-components / Emotion — if present, use tagged template literals.
- •Vanilla CSS — if plain
.cssimports are used, follow the pattern.
- •CSS Modules (
- •Follow whatever approach is already in use. Do not introduce a second styling library.
Theming and design tokens
- •Extract the color palette, typography, and spacing values from the HTML
<head>. - •Sync these values with
references/style-guide.json. - •Define CSS custom properties in a root theme file (e.g.
src/styles/theme.css):css:root { --color-primary: #19e65e; --color-background: #f6f8f6; --color-surface: #ffffff; --text-body-font: 'Inter', sans-serif; --spacing-sm: 8px; --radius-md: 12px; } [data-theme="dark"] { --color-background: #112116; --color-surface: #1a2e1f; } - •Reference tokens via
var(--color-primary)in CSS — never hardcode hex values inline (nocolor: #19e65eorstyle={{ color: '#19e65e' }}). - •Support both light and dark themes via a
data-themeattribute or CSSprefers-color-schememedia query.
Responsive layout
- •Use CSS Flexbox and Grid for layout. Never assume a fixed viewport width.
- •Use
min(),max(),clamp(), and container queries or media queries for responsive breakpoints. - •Avoid hardcoded pixel widths on containers; prefer
max-width,flex, or percentage-based sizing.
Accessibility
- •Use semantic HTML elements (
<nav>,<main>,<article>,<button>, etc.) instead of generic<div>and<span>. - •Add
aria-labelto interactive elements that lack visible text. - •Ensure all clickable elements are keyboard-accessible (
<button>, not<div onClick>). - •Ensure sufficient color contrast (WCAG 2.1 AA minimum).
Image handling
- •Always provide an
altattribute on<img>elements. - •Use a fallback/placeholder for network images via
onErrorhandlers. - •If
next/imageis available (Next.js projects), prefer<Image>for optimization.
Performance
- •Wrap expensive computations in
useMemoand callbacks inuseCallbackwhen they are passed as props. - •Use
React.memofor components that receive stable props but re-render due to parent changes. - •Lazy-load below-the-fold sections with
React.lazyandSuspense.
Routing awareness
- •Check for existing routing (
react-router,next/router,@tanstack/router) before wiring new pages. Match the existing pattern. Do not introduce a second router.
Execution steps
- •Project audit: Inspect
package.jsonto detect state management, routing, styling libraries, and build tool (Vite, Next.js, CRA) already in use. Adapt all subsequent steps to the project's existing patterns. - •Environment setup: If
node_modulesis missing, runnpm installto resolve dependencies. - •Multi-screen scan (if multiple screens): Follow the Multi-screen analysis section above to build a component inventory and create shared components first.
- •Data layer: Create
src/data/mockData.tsbased on the design content. - •Theme setup: Create
src/styles/theme.csswith CSS custom properties extracted from the Stitch design, cross-referencingreferences/style-guide.json. - •Component drafting: Use
assets/component-template.tsxas a base. Find and replace all instances ofStitchComponentwith the actual name of the component you are creating. - •Application wiring: Update the project entry point (
src/App.tsxor relevant page) to render the new components with the project's existing router. - •Quality check:
- •Run
npx tsc --noEmitto catch type errors. - •Run
node scripts/validate_component.mjs <file_path>for each component to check for hardcoded colors and missing conventions. - •Verify the final output against
references/architecture-checklist.md. - •Start the app with
npm run devto verify the live result.
- •Run
Troubleshooting
- •Fetch errors: Ensure the URL is quoted in the bash command to prevent shell errors.
- •Type errors: Review
npx tsc --noEmitoutput and fix any missing types, incorrect interfaces, or lint warnings. - •Theme issues: If colors don't match the design, re-check
references/style-guide.jsonmapping and ensure CSS custom properties are applied at the:rootlevel. - •State management conflicts: If the project uses Redux but generated code imports Zustand, remove the wrong import and adapt to the project's state layer.