Define a code component
A code component definition is a file that tells Webflow how to use your React component on the Webflow canvas. It defines which properties designers can configure and how they’ll appear in the designer.
Every code component definition is a .webflow.tsx file that uses the declareComponent function to define the component.
File structure and naming
Code component definition files follow specific extension and naming patterns:
- File extension:
.webflow.tsxor.webflow.ts - Naming pattern:
ComponentName.webflow.tsx(whereComponentNamematches 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 definition 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 definition file needs to import your React component, Webflow functions, and any styles you want to apply to the component.
Importing styles
To apply global styles or integrate CSS-in-JS libraries, configure a global decorators file with component decorators.
Declare component
The declareComponent function creates a code component definition. It takes two arguments: the React component and a configuration object.
name: The name designers see in the component paneldescription?: Description to provide context for the component’s purpose (optional)group?: Organize components into groups in the component panel (optional)props?: Object defining the props of the component (optional)decorators?: Array of decorators to apply to the component (optional)options?: Object defining the options of the component (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 definition file, and how it appears in Webflow.
React component
Code component
Component in Webflow
This React component expects a buttonText property, and a variant property.
See more examples in the prop types reference. →
Component decorators
Decorators are functions that wrap your React component with providers or other wrapper components. Use them to provide context like themes, internationalization, or feature flags, or to integrate CSS-in-JS libraries. For CSS-in-JS setup, see the frameworks and libraries guide.
Global styles
To apply global styles across all components, import your CSS in a global decorators file and reference it in webflow.json:
Custom decorators
Create a custom decorator when you need to wrap components with additional behavior. A decorator is a higher-order component that takes a component and returns a wrapped version. This example wraps a data-fetching component with an error boundary to handle API failures gracefully:
To apply decorators globally, export a decorators array from your decorators file:
Styling components
Code components render in Shadow DOM, encapsulating them from the rest of the page, which impacts several CSS capabilities.
Options
The options object is used to configure the component for more advanced use cases. Options accepts the following properties:
Tag selectors
Styles targeting a tag selector (for example, h1, p, button) can be automatically provided to the Shadow DOM with the applyTagSelectors option. This is helpful for styling components with CSS selectors.
See more about styling components in the styling documentation. →
Server-side rendering (SSR)
By default, Webflow will load your component on the server. This means that the component will be rendered on the server, but the DOM will be hydrated on the client-side. This is helpful for improving the performance of your component.
You can disable this behavior by setting ssr to false.
Best practices
File naming and organization
- Use consistent naming:
ComponentName.webflow.tsxfor all code component definitions - Keep code component definitions close: Place
.webflow.tsxfiles next to their React components
Component metadata
- 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
Prop definitions
- Provide helpful defaults: Make components work immediately when added
- Use descriptive names: The
nameproperty appears in the designer - Group related props: Consider how props will appear together in the designer
Global styles
Import CSS once in a global decorators file rather than in each component. This keeps your component definitions clean and ensures consistent styling:
Then reference your global decorators file in webflow.json:
Next steps
Now that you understand code component definitions, you can:
- Understand styling - Learn about how to style your components.
- Explore prop types - Learn about all available prop types
- Configure bundling - Set up your build process
- Importing your components - Share your components with designers and other developers