Skip to main content

DeclarativeShadowElement

Base class that handles declarative shadow DOM hydration for components mounted after initial page render.

Class Definition

component.js

Usage

Declarative shadow DOM is automatically initialized on page load. For dynamically added components:

When to Use

Use DeclarativeShadowElement when you need:
  • Encapsulated styles that don’t leak to the page
  • Shadow DOM for component isolation
  • Support for declarative shadow DOM templates
For most Horizon components, use the Component class instead, which extends DeclarativeShadowElement.

Component

Main base class for Horizon web components with automatic refs management, declarative event handling, and lifecycle hooks.

Class Signature

Properties

RefsType<T>
Object containing references to child elements with ref attributes. Automatically populated and kept in sync with DOM changes.
string[]
Array of ref names that must be present. Throws MissingRefError if any are missing.
(ShadowRoot | Component)[]
Read-only property returning the root nodes of the component. If the component has a shadow root, returns both the component and its shadow root. Otherwise, returns just the component.

Lifecycle Methods

connectedCallback()

Called when the component is inserted into the DOM.
component.js
Usage:

updatedCallback()

Called when the component is re-rendered by the Section Rendering API.
component.js
Usage:

disconnectedCallback()

Called when the component is removed from the DOM.
component.js
Usage:

Refs System

Basic Refs

Elements with ref attributes are automatically tracked:

Array Refs

Use ref="name[]" syntax to collect multiple elements:

Required Refs

Validate that critical refs exist:

TypeScript Refs

Type-safe refs with TypeScript:

Declarative Event Handling

The Component class automatically sets up event listeners using the on: attribute syntax.

Basic Events

Supported Events

component.js

Target Selector

Specify which component should handle the event:

Passing Data

Pass data to event handlers:

Mutation Observer

The Component class automatically observes DOM changes to keep refs in sync:
component.js
This means refs are automatically updated when:
  • Elements are added or removed
  • ref attributes change
  • Child components are mounted/unmounted

Complete Example

Helper Functions

Internal helper functions used by the Component class:

getAncestor(node)

Finds the ancestor of a node, traversing shadow DOM boundaries:
component.js

getClosestComponent(node)

Recursively finds the closest ancestor Component instance:
component.js

Error Classes

MissingRefError

Thrown when a required ref is not found:
component.js
Example: