v 0.2.0

Radiant Element


The RadiantElement class serves as a base for creating custom web components with reactive properties, event handling, and template rendering capabilities. It extends the native HTMLElement and implements the IRadiantElement interface, providing a robust framework for building interactive and dynamic elements.

The suggested way to create a custom element is to extend the RadiantElement class and use the provided decorators and methods to define reactive properties, event listeners, and template rendering logic. This approach simplifies the process of creating and managing custom elements, making it easier to build complex and interactive components.

It is still possible to use the RadiantElement class without decorators by manually calling the corresponding methods. However, using decorators is the recommended way to take advantage of the built-in features and simplify the code.

Please have a look to the Decorator Section to see the available decorators.

Key Features

Example

Following is an example of a custom element created using the RadiantElement class with decorators:

@customElement('my-custom-element')
class MyCustomElement extends RadiantElement {
  @reactiveProp({ type: String, defaultValue: 'Foo' }) foo: string;
  @query({ ref: 'paragraph' }) paragraph!: HTMLParagraphElement;

  @onUpdated('foo')
  updateParagraph() {
    this.paragraph.textContent = `Hello ${this.foo}`;
  }

  @onEvent({ selector: 'button', type: 'click' })
  handleClick() {
    this.foo = 'World';
  }
}

The above example demonstrates how to create a custom element using the RadiantElement class with decorators. The element has a reactive property foo, a reference to a paragraph element, and an event listener for a button click. When the button is clicked, the foo property is updated, triggering the updateParagraph method to update the paragraph text.

Example without Decorators

class MyCustomElement extends RadiantElement {
  declare foo: string;
  paragraph = this.getRef<HTMLParagraphElement>('paragraph');

  constructor() {
    super();
    this.createReactiveProp('foo', { type: String, defaultValue: 'Foo' });
    this.registerUpdateCallback('foo', () => {
      this.paragraph.textContent = `Hello ${this.foo}`;
    });
    this.subscribeEvent({
      selector: 'button',
      type: 'click',
      listener: () => {
        this.foo = 'World';
      },
    });
  }
}
customElements.define('my-custom-element-plain', MyCustomElementPlain);

The above example demonstrates how to create a custom element using the RadiantElement class without decorators. The element has a reactive property foo, a reference to a paragraph element, and an event listener for a button click. When the button is clicked, the foo property is updated, triggering the update callback to update the paragraph text.

Usage

<my-custom-element>
  <p data-ref="paragraph"></p>
  <button>Click me</button>
</my-custom-element>

Methods

createReactiveProp

Creates a reactive property for the element.

This method defines a reactive property on the element, allowing it to automatically update the UI when the property changes.


createReactiveField

Creates a reactive field for the element.

This method defines a reactive field on the element, allowing it to automatically update the UI when the field changes.


registerUpdateCallback

Registers a callback to be executed when a property is updated.

This method allows you to define custom logic that runs when a specific property is updated.


notifyUpdate

Called when a property of the element is updated.

This method triggers any registered update callbacks for the specified property.


subscribeEvent

Subscribes to a specific event on the Radiant element.

This method allows you to listen for events and execute a callback when the event occurs.


subscribeEvents

Subscribes to multiple events at once.

This method is useful for batch subscribing to multiple events for the element.


removeAllSubscribedEvents

Removes all event listeners that have been subscribed to the Radiant element.

This method is important for cleaning up event listeners to avoid memory leaks.


registerCleanupCallback

Registers a callback to be executed when the element is disconnected from the DOM.

This method allows you to define cleanup logic that runs when the element is removed from the DOM.


renderTemplate

Renders a specified template into a target element.

This method allows you to dynamically insert HTML content into the element.


connectedContextCallback

Called when the Radiant element is connected to a specific context.

This method can be overridden to perform actions when the element is connected to a context.


getRef

Retrieves a child element by its data-ref attribute.

This method simplifies accessing child elements based on their data-ref attributes.