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.

Component is a significant term in Webflow, but SSE "components" aren't strictly tied to Webflow Components. We'll be discussing both here- to distinguish...

  • "Components" (capitalized) will refer to Webflow Components.

  • "components" (lowercase) will refer to SSE components.

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+)

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:

  1. Select the element in Webflow Designer

  2. Add a custom attribute: data-component = accordion

  3. The component name must match the name in the @component decorator

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 from data-component

  • this.context.id - Optional ID from data-component-id

  • this.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:

  1. onPrepare() - Runs synchronously during <head> load

    • Use for quick setup that doesn't require DOM manipulation

    • Context and element are available

  2. 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

  1. One component class per file - Keep components organized in /src/components/

  2. Use descriptive names - Component names should be clear: accordion, multi-step-form, image-gallery

  3. Scope your queries - Always query within this.element to avoid conflicts:

  4. Clean up resources - Remove event listeners if component is destroyed

  5. 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