Skill 20 · Webflow Code Component:local Dev Setup
Subchapter 20.1
references/EXAMPLES.mdMarkdown10 KBView on GitHub
Detailed setup examples for different scenarios.
🛠️ Local Dev Setup: New Code Components Project
I'll set up a complete code components project for you.
---
## Step 1: Initialize Project
**Create project directory and initialize npm:**
```bash
mkdir my-webflow-components
cd my-webflow-components
npm init -yCore dependencies:
npm install react react-dom
npm install -D typescript @types/react @types/react-domWebflow dependencies:
npm install -D @webflow/webflow-cli @webflow/data-types @webflow/reactCreate tsconfig.json:
{
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"moduleResolution": "bundler",
"jsx": "react-jsx",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"declaration": true,
"declarationMap": true,
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
}Create webflow.json in project root:
{
"library": {
"name": "My Component Library",
"components": ["./src/**/*.webflow.tsx"]
}
}mkdir -p src/components/ButtonProject structure:
my-webflow-components/
├── src/
│ ├── components/
│ │ └── Button/
│ │ ├── Button.tsx
│ │ ├── Button.webflow.tsx
│ │ └── Button.module.css
│ └── declarations.d.ts
├── package.json
├── tsconfig.json
├── webflow.json
└── .gitignoreCreate src/declarations.d.ts for CSS module support:
declare module "*.module.css" {
const classes: { [key: string]: string };
export default classes;
}
// For non-module CSS imports (e.g., global CSS files like globals.css)
declare module "*.css" {
const content: string;
export default content;
}This file tells TypeScript how to handle CSS module imports.
src/components/Button/Button.tsx:
import React from "react";
import styles from "./Button.module.css";
export interface ButtonProps {
label: string;
variant: "primary" | "secondary" | "outline";
disabled?: boolean;
}
export const Button: React.FC<ButtonProps> = ({
label,
variant = "primary",
disabled = false,
}) => {
return (
<button
className={`${styles.button} ${styles[variant]}`}
disabled={disabled}
>
{label}
</button>
);
};src/components/Button/Button.webflow.tsx:
import { declareComponent } from "@webflow/react";
import { props } from "@webflow/data-types";
import { Button } from "./Button";
import "./Button.module.css";
export default declareComponent(Button, {
name: "Button",
description: "A customizable button component",
group: "Interactive",
props: {
label: props.Text({
name: "Label",
defaultValue: "Click me",
}),
variant: props.Variant({
name: "Style",
options: ["primary", "secondary", "outline"],
defaultValue: "primary",
}),
disabled: props.Boolean({
name: "Disabled",
defaultValue: false,
}),
},
});src/components/Button/Button.module.css:
.button {
padding: 12px 24px;
border-radius: 8px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
border: 2px solid transparent;
transition: all 0.2s ease;
}
.button:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.primary {
background-color: #3b82f6;
color: white;
}
.primary:hover:not(:disabled) {
background-color: #2563eb;
}
.secondary {
background-color: #6b7280;
color: white;
}
.secondary:hover:not(:disabled) {
background-color: #4b5563;
}
.outline {
background-color: transparent;
border-color: #3b82f6;
color: #3b82f6;
}
.outline:hover:not(:disabled) {
background-color: #3b82f6;
color: white;
}.gitignore:
node_modules/ # Dependencies (installed via npm install)
dist/ # Build output
.env # API tokens and secrets
*.log # Log files from CLI and bundlerVerify everything works:
npx webflow library bundle --public-path http://localhost:4000/Expected output:
✓ Bundling components...
✓ Found 1 component(s)
✓ Build completeYour project is ready. Here’s what you can do next:
mkdir src/components/CardThen create Card.tsx, Card.webflow.tsx, and Card.module.css
npx webflow library sharenpx webflow library bundle --public-path http://localhost:4000/npx webflow library share💡 Tips:
webflow-code-component:component-scaffold skill to quickly create new componentswebflow-code-component:pre-deploy-check skill before deploying
---
## Example 2: Add to Existing React Project
🛠️ Adding Code Components to Existing Project
I see you have an existing React project. Let me add Webflow Code Components support.
npm install -D @webflow/webflow-cli @webflow/data-types @webflow/reactAdd to your project root:
{
"library": {
"name": "My Component Library",
"components": ["./src/webflow-components/**/*.webflow.tsx"]
}
}I’m putting components in a separate folder to keep them organized.
mkdir -p src/webflow-componentsUpdated project structure:
existing-react-project/
├── src/
│ ├── components/ # Your existing components
│ │ └── Button/
│ ├── webflow-components/ # Webflow definition files go here
│ │ └── Button.webflow.tsx
│ └── ...
├── package.json
└── webflow.json # New — Webflow configFor each component you want to export to Webflow, create a .webflow.tsx file:
src/webflow-components/Button.webflow.tsx:
import { declareComponent } from "@webflow/react";
import { props } from "@webflow/data-types";
// Import your existing component
import { Button } from "../components/Button";
export default declareComponent(Button, {
name: "Button",
description: "Your existing button component",
props: {
// Map your component props to Webflow props
children: props.Text({
name: "Label",
defaultValue: "Click me"
}),
variant: props.Variant({
name: "Variant",
options: ["primary", "secondary"],
defaultValue: "primary"
})
}
});If your existing components use:
CSS Modules: Import in .webflow.tsx file
import "../components/Button/Button.module.css";styled-components: Add decorator
// src/webflow-components/globals.ts
import { styledComponentsShadowDomDecorator } from "@webflow/styled-components-utils";
export const decorators = [styledComponentsShadowDomDecorator];Update webflow.json:
{
"library": {
"globals": "./src/webflow-components/globals.ts"
}
}Add:
.envnpx webflow library bundle --public-path http://localhost:4000/✅ Integration complete!
Your existing components can now be deployed to Webflow.
---
## Example 3: With Tailwind CSS
🛠️ Setup with Tailwind CSS
Setting up Code Components with Tailwind CSS support.
mkdir my-tailwind-components
cd my-tailwind-components
npm init -y# Core
npm install react react-dom
npm install -D typescript @types/react @types/react-dom
# Webflow
npm install -D @webflow/webflow-cli @webflow/data-types @webflow/react
# Tailwind
npm install -D tailwindcss @tailwindcss/postcss postcssCreate postcss.config.mjs:
export default {
plugins: {
"@tailwindcss/postcss": {},
},
};src/globals.css:
@import "tailwindcss";src/globals.ts:
import "./globals.css";{
"library": {
"name": "My Tailwind Library",
"components": ["./src/**/*.webflow.tsx"],
"globals": "./src/globals.ts"
}
}src/components/Card/Card.tsx:
import React from "react";
export interface CardProps {
title: string;
description: string;
featured?: boolean;
}
export const Card: React.FC<CardProps> = ({
title,
description,
featured = false,
}) => {
return (
<div className={`
p-6 rounded-lg shadow-md
${featured ? "bg-blue-50 border-2 border-blue-500" : "bg-white"}
`}>
<h2 className="text-xl font-bold text-gray-900 mb-2">{title}</h2>
<p className="text-gray-600">{description}</p>
{featured && (
<span className="inline-block mt-4 px-3 py-1 bg-blue-500 text-white text-sm rounded-full">
Featured
</span>
)}
</div>
);
};src/components/Card/Card.webflow.tsx:
import { declareComponent } from "@webflow/react";
import { props } from "@webflow/data-types";
import { Card } from "./Card";
export default declareComponent(Card, {
name: "Card",
description: "A card with Tailwind styling",
props: {
title: props.Text({
name: "Title",
defaultValue: "Card Title",
}),
description: props.Text({
name: "Description",
defaultValue: "Card description goes here.",
}),
featured: props.Boolean({
name: "Featured",
defaultValue: false,
}),
},
});npx webflow library bundle --public-path http://localhost:4000/✅ Tailwind setup complete!
All Tailwind utility classes are now available in your components.