Components
SSE's page routing model allows us to cleanly separate page-specific code in a very manageable way. But in some cases your code needs to be tied to a "component" rather than a page or path.
These are some examples of "components" that might use code:
A specially configured SwiperJS setup
A custom component you've built such as an accordion
Specialized form validation
A fancy multi-step form
A cool color-picker
Also, any Webflow Component that needs code attached to its functionality
Often, these "components" need to be reused on multiple pages, and your design team might for example duplicate a quiz or a contact form to another page at any time- ideally the dev team would not need to do anything for that component to automatically work on that new page.
Goals
Efficient code execution, only run code when it's needed
Code isolation, e.g. the code & CSS for a multi-step form should be distinct from the rest of your source code
Reusability. Your development work should be easy to repurpose on other projects you build.
Webflow Component support. Take full advantage of the Webflow Team's work on components and leverage it in every way possible to maximize the finished "smart" component.
Create a design paradigm that supports the possibility of multiple "component" instances per page.
Implementation (v2.0+)
New in v2.0: Components are fully implemented with automatic discovery, ComponentBase class, and automatic context detection!
Creating a Component
Components extend ComponentBase for automatic element and context detection:
Using Components in Webflow
To use a component, add the data-component attribute to any element in Webflow:
Select the element in Webflow Designer
Add a custom attribute:
data-component=accordionThe component name must match the name in the
@componentdecorator
Multiple Component Instances
Components automatically support multiple instances on the same page. Each instance gets its own separate class instance:
Both will initialize independently with their own AccordionComponent instance.
Automatic Context Detection
When extending ComponentBase, you automatically get:
this.element
The HTMLElement the component is bound to:
this.context
Component metadata automatically extracted:
Available context properties:
this.context.name- Component name fromdata-componentthis.context.id- Optional ID fromdata-component-idthis.context.dataAttributes- All data-* attributes as key-value pairs
Accessing Page Information from Components
New in v2.0: Components can access the current page via the singleton pattern:
Use the public getPageInfo() accessor (the pageInfo property is protected) whenever a component reads Webflow page context.
Component Discovery and Registration
New in v2.0: Components are automatically discovered using the @component decorator!
Automatic Discovery
Simply decorate your component class and import it:
Register in routes.ts
Import your component files to trigger decorator registration:
That's it! SSE automatically finds all data-component attributes in your HTML and initializes the matching components.
Component Lifecycle
Components follow the same lifecycle as pages:
onPrepare() - Runs synchronously during
<head>loadUse for quick setup that doesn't require DOM manipulation
Context and element are available
onLoad() - Runs asynchronously after DOM is ready
Use for event listeners, DOM queries, async operations
Full DOM access guaranteed
Data Attributes for Configuration
Use data attributes to configure component behavior:
Access in your component:
Best Practices
One component class per file - Keep components organized in
/src/components/Use descriptive names - Component names should be clear:
accordion,multi-step-form,image-galleryScope your queries - Always query within
this.elementto avoid conflicts:Clean up resources - Remove event listeners if component is destroyed
Use data attributes for configuration - Keep components flexible and reusable
Example: Complete Component
Here's a complete example of a tabs component:
Use in Webflow:
Last updated