Declare a code component
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.
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
(whereComponentName
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.
Styling components
Code Components are rendered in Shadow DOM, encapsulating them from the rest of the page, which impacts several CSS capabilities.
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:
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)
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.
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. →
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
File naming and organization
- Use consistent naming:
ComponentName.webflow.tsx
for all declarations - Keep declarations close: Place
.webflow.tsx
files 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
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:
- 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
- Deploy your library - Share your components with designers and other developers