Declare a code component

Understand how declaration files bridge React components and the Webflow designer.
Private Beta

A code component declaration is a file that tells Webflow how to use your React component in the visual designer. It’s the bridge between your React code and Webflow’s interface, defining which properties designers can configure and how they’ll appear in the designer.

Every code component declaration is a .webflow.tsx file that uses the declareComponent function to define the component.

1import { declareComponent } from '@webflow/react';
2import { props } from '@webflow/data-types';
3import { Button } from './Button';
4import '../styles/global.css'; // Import global styles
5
6// Declare the component
7export default declareComponent(Button, {
8
9 // Component metadata
10 name: "Button",
11 description: "A button component with a text and a style variant",
12 group: "Interactive",
13
14 // Prop definitions
15 props: {
16 text: props.Text({
17 name: "Button Text",
18 defaultValue: "Click me"
19 }),
20 variant: props.Variant({
21 name: "Style",
22 options: ["primary", "secondary"],
23 defaultValue: "primary"
24 }),
25 },
26 // Optional configuration
27 options: {
28 applyTagSelectors: true,
29 },
30});

File structure and naming

Code component declaration files follow specific extension and naming patterns:

  • File extension: .webflow.tsx or .webflow.ts
  • Naming pattern: ComponentName.webflow.tsx (where ComponentName matches your React component)
  • Location: Typically alongside your React component file

If you have specific naming needs, you can configure this pattern in webflow.json. It’s recommended to create your code component file alongside your React component, adding .webflow to the name. For example, Button.webflow.tsx for Button.tsx.

File names are the unique identifier of your code component

Renaming a declaration file creates a new component and removes the old one from your library. If designers are already using the old component in their projects, those instances will break and need to be manually replaced.

Imports

Every code component declaration file needs to import your React component, Webflow functions, and any styles you want to apply to the component.

Button.webflow.tsx
1import { declareComponent } from '@webflow/react';
2import { props } from '@webflow/data-types';
3import { Button } from './Button';
4import '../styles/global.css'; // Import global styles
Styling components

Code Components are rendered in Shadow DOM, encapsulating them from the rest of the page, which impacts several CSS capabilities.

Learn more about styling components →.

Declare component

The declareComponent function is used to create a code component declaration. It takes two arguments:

  • The React component
  • An object with: Component metadata, prop definitions, and optional configuration

Component metadata

The metadata properties define how your component appears in the Webflow designer:

Button.webflow.tsx
1import { declareComponent } from '@webflow/react';
2import { Button } from './Button';
3import '../styles/global.css'; // Import global styles
4
5// Declare the component
6export default declareComponent(Button, {
7
8 // Component metadata
9 name: "Button",
10 description: "A button component with a text and a style variant",
11 group: "Interactive",
12});
  • name: The name designers see in the component panel
  • description?: Description to provide context for the component’s purpose (optional)
  • group?: Organize components into groups in the component panel (optional)

Prop definitions

The props object defines which properties of your React component a designer can edit in Webflow. Declare a prop for each editable property in your React component and provide metadata that will appear in the designer. To see a list of all available prop types and their configuration options, see the prop types reference. →

The below examples show a React component, its corresponding code component declaration file, and how it appears in Webflow.

This React component expects a buttonText property, and a variant property.

Button.tsx
1import React from 'react';
2import styles from './Button.module.css';
3
4interface ButtonProps {
5 text: string;
6 variant: 'primary' | 'secondary';
7}
8
9export const Button: React.FC<ButtonProps> = ({ text, variant }) => {
10 return (
11 <button
12 className={`${styles.button} ${styles[variant]}`}
13 type="button"
14 >
15 {text}
16 </button>
17 );
18};

See more examples in the prop types reference. →

Options

The options object is used to configure the component for more advanced use cases. Options accepts the following properties:

  • applyTagSelectors: Whether to apply tag selectors to the component.

Tag selectors

Styles targeting a tag selector (for example, h1, p, button) can be automatically provided to the Shadow DOM with the applyTagSelectors option in the component definition. This is helpful for styling components with CSS selectors.

See more about styling components in the styling documentation. →

Best practices

  • Use consistent naming: ComponentName.webflow.tsx for all declarations
  • Keep declarations close: Place .webflow.tsx files next to their React components
  • Use clear names: Make it obvious what the component does
  • Add descriptions: Help designers understand the component’s purpose
  • Group logically: Use groups to organize components in the panel
  • Provide helpful defaults: Make components work immediately when added
  • Use descriptive names: The name property appears in the designer
  • Group related props: Consider how props will appear together in the designer

Next steps

Now that you understand code component declarations, you can: