Frameworks and libraries

Learn how to use CSS frameworks and component libraries with code components.

Code Components work with a wide range of styling approaches, including CSS preprocessors, utility frameworks, and component/style libraries.

Because Code Components render inside a Shadow DOM, some tools that inject styles into the global document.head need additional configuration to scope styles correctly. Webflow provides utilities for popular CSS-in-JS libraries (e.g. Emotion, styled-components) so they can inject styles directly into the Shadow Root.

Most setups just require a small addition to your webpack configuration and an import in your component. For CSS-in-JS solutions, you’ll wrap your components in a Shadow DOM provider.

CSS frameworks

To use Tailwind CSS with your code components, configure PostCSS to process Tailwind classes:

1

Install Tailwind CSS

Install Tailwind CSS and its PostCSS plugin:

$npm install tailwindcss @tailwindcss/postcss postcss
2

Configure PostCSS

Add the Tailwind PostCSS plugin to your postcss.config.mjs file:

postcss.config.mjs
1const config = {
2 plugins: {
3 "@tailwindcss/postcss": {},
4 },
5};
6
7export default config;
3

Create Tailwind styles

Create a CSS file that imports Tailwind:

globals.css
1@import "tailwindcss";
4

Configure global decorators

Create a global decorators file that imports your Tailwind CSS:

globals.ts
1import "./globals.css";

Then reference it in your webflow.json:

webflow.json
1{
2 "library": {
3 "globals": "./src/globals.ts"
4 }
5}
5

Use Tailwind classes

Write your React components using Tailwind utility classes. The global styles are automatically applied to all components:

1import * as React from "react";
2
3interface BadgeProps {
4 text: string;
5 variant: 'Light' | 'Dark';
6}
7
8export const Badge = ({ text, variant }: BadgeProps) => (
9 <span
10 className={`inline-block rounded-full px-3 py-1 text-sm font-medium ${
11 variant === 'Light'
12 ? 'bg-gray-100 text-gray-800'
13 : 'bg-gray-800 text-white'
14 }`}
15 >
16 {text}
17 </span>
18);

To use styled-components with code components, install the @webflow/styled-components-utils package and configure a global decorator to inject styles into the Shadow DOM.

1

Install the styled-components utility library

Install the utility library:

$npm i @webflow/styled-components-utils

This package requires the following peer dependencies:

$npm i styled-components react react-dom
2

Configure global decorators

Create a global decorators file that exports the styled-components decorator:

globals.ts
1import { styledComponentsShadowDomDecorator } from "@webflow/styled-components-utils";
2
3export const decorators = [styledComponentsShadowDomDecorator];

Then reference it in your webflow.json:

webflow.json
1{
2 "library": {
3 "globals": "./src/globals.ts"
4 }
5}
3

Use styled-components in your component

Write your React component using styled-components. The global decorator automatically wraps all components:

1import React from "react";
2import styled from "styled-components";
3
4const StyledButton = styled.button`
5 background-color: #007bff;
6`;
7
8interface ButtonProps {
9 buttonText: string;
10}
11
12export const Button = ({ buttonText }: ButtonProps) => {
13 return <StyledButton>{buttonText}</StyledButton>;
14};

To use Emotion with code components, install the @webflow/emotion-utils package and configure a global decorator to inject styles into the Shadow DOM.

1

Install the Emotion utility library

In your terminal, run the following command to install the Emotion utility package:

$npm i @webflow/emotion-utils

This package requires the following peer dependencies:

$npm i @emotion/cache @emotion/react react react-dom
2

Configure global decorators

Create a global decorators file that exports the Emotion decorator:

globals.ts
1import { emotionShadowDomDecorator } from "@webflow/emotion-utils";
2
3export const decorators = [emotionShadowDomDecorator];

Then reference it in your webflow.json:

webflow.json
1{
2 "library": {
3 "globals": "./src/globals.ts"
4 }
5}
3

Use Emotion in your component

Write your React component using Emotion. The global decorator automatically wraps all components:

1import React from "react";
2import styled from "@emotion/styled";
3
4const StyledButton = styled.button`
5 background-color: #007bff;
6`;
7
8interface ButtonProps {
9 buttonText: string;
10}
11
12export const Button = ({ buttonText }: ButtonProps) => {
13 return <StyledButton>{buttonText}</StyledButton>;
14};

Component libraries

Material UI uses Emotion for styling. Install the @webflow/emotion-utils package and configure a global decorator to inject styles into the Shadow DOM.

1

Install the Emotion utility library

In your terminal, run the following command to install the Emotion utility package:

$npm i @webflow/emotion-utils

This package requires the following peer dependencies:

$npm i @mui/material @emotion/react @emotion/cache
2

Configure global decorators

Create a globals file that exports the Emotion decorator:

globals.ts
1import { emotionShadowDomDecorator } from "@webflow/emotion-utils";
2
3export const decorators = [emotionShadowDomDecorator];

Then reference it in your webflow.json:

webflow.json
1{
2 "library": {
3 "globals": "./src/globals.ts"
4 }
5}
3

Use Material UI in your component

Write your React component using Material UI. The global decorator automatically wraps all components:

1import React from "react";
2import { Button } from "@mui/material";
3
4interface ButtonProps {
5 children: React.ReactNode;
6 variant?: "text" | "outlined" | "contained";
7 color?: "primary" | "secondary" | "error" | "info" | "success" | "warning";
8 onClick?: () => void;
9}
10
11export const CustomButton = ({
12 children,
13 variant = "contained",
14 color = "primary",
15 onClick,
16}: ButtonProps) => {
17 return (
18 <Button variant={variant} color={color} onClick={onClick}>
19 {children}
20 </Button>
21 );
22};

Shadcn/UI is a component library built on Tailwind CSS that provides pre-built, accessible React components. It works with code components but requires path alias configuration. Follow these steps after setting up Tailwind CSS:

1

Configure path aliases

Shadcn/UI uses path aliases that need to be configured in your webpack setup. Add this to your webpack configuration:

webpack.webflow.js
1module.exports = {
2 resolve: {
3 alias: {
4 "@": process.cwd(), // Maps @ to your project root
5 },
6 },
7};
8

For detailed webpack configuration options, see the bundling and import guide.

Preprocessors & post-processing

Configure your project to use Sass with the following steps:

1

Install the Sass loaders

Install the loaders as development dependencies:

$npm install --save-dev sass sass-loader
2

Create a custom webpack configuration

Create a webpack.webflow.js file to customize the webpack configuration to use the Sass loader:

webpack.webflow.js
1module.exports = {
2module: {
3 rules: (currentRules) => {
4 const currentCSSRule = currentRules.find(
5 (rule) =>
6 rule.test instanceof RegExp &&
7 rule.test.test("test.css") &&
8 Array.isArray(rule.use)
9 );
10 return [
11 ...currentRules,
12 {
13 test: /\.scss$/,
14 use: [...currentCSSRule.use, "sass-loader"],
15 },
16 ];
17 },
18 }
19}
3

Update your Webflow configuration

Update your webflow.json file to use the custom webpack configuration:

webflow.json
1{
2 "library": {
3 "name": "React Components Library",
4 "components": ["./src/**/*.webflow.@(js|jsx|mjs|ts|tsx)"],
5 "bundleConfig": "./webpack.webflow.js"
6 }
7 }
8
4

Use Sass in your code component

Import Sass in your code component definition file:

src/components/Button.webflow.tsx
1import '../styles/button.scss';
2
3// Declare the component

Configure your project to use Less with the following steps:

1

Install the Less loaders

Install the loaders as development dependencies:

$npm install --save-dev less less-loader
2

Create a custom webpack configuration

Create a webpack.webflow.js file to customize the webpack configuration to use the Sass loader:

webpack.webflow.js
1// webpack.webflow.js
2module.exports = {
3 module: {
4 rules: (currentRules) => {
5 const currentCSSRule = currentRules.find(
6 (rule) =>
7 rule.test instanceof RegExp &&
8 rule.test.test("test.css") &&
9 Array.isArray(rule.use)
10 );
11
12 return [
13 ...currentRules,
14 {
15 test: /\.less$/i,
16 use: [...currentCSSRule.use, "less-loader"],
17 },
18 ];
19 },
20 },
21 };
22
3

Update your Webflow configuration

Update your webflow.json file to use the custom webpack configuration:

webflow.json
1{
2 "library": {
3 "name": "React Components Library",
4 "components": ["./src/**/*.webflow.@(js|jsx|mjs|ts|tsx)"],
5 "bundleConfig": "./webpack.webflow.js"
6 }
7 }
8
4

Use less in your code component

Import less in your code component definition file:

src/components/Button.webflow.tsx
1import '../styles/button.less';
2
3// Declare the component

Learn about additional configuration options in the bundling and import guide.

Next steps

Troubleshooting

If you’re getting errors when sharing to Webflow, try the following:

  • Ensure you’ve installed the Webflow CLI locally within the project. If you have a global installation, try running the command with npx to ensure the correct version is being used.