ReactiveElement

General-purpose base for writing components in functional-reactive style

Overview

ReactiveElement is a general-purpose base for writing components in functional-reactive style. It simply bundles a small set of mixins:

  1. AttributeMarshallingMixin, which converts changes to element attributes into changes in element properties.
  2. ReactiveMixin, which gives a component class a functional-reactive programming (FRP) architecture that can track internal state and render that state to the DOM.
  3. ShadowTemplateMixin, which handles the task of populating the component's shadow root when it is first connected to the document.

These mixins are used together so often that, for convenience, they are used to create an Elix component base class called ReactiveElement. There is nothing special about the ReactiveElement base class, and it can easily be recreated by applying the above mixins to HTMLElement:

const ReactiveElement =
  AttributeMarshallingMixin(
  ReactiveMixin(
  ShadowTemplateMixin(
    HTMLElement
  )));

Example

A simple increment/decrement component using ReactiveElement might look like this:

import { ReactiveElement, internal, template } from "elix";

class IncrementDecrement extends ReactiveElement {

  [internal.componentDidMount]() {
    if (super.componentDidMount) { super[internal.componentDidMount](); }
    // Wire up event handlers once the shadow tree has been populated.
    this[internal.ids].decrement.addEventListener('click', () => {
      this.value--;
    });
    this[internal.ids].increment.addEventListener('click', () => {
      this.value++;
    });
  }

  // This property becomes the initial value of this[internal.state] at constructor time.
  get [internal.defaultState]() {
    return { value: 0 };
  }

  // Provide a public property that gets/sets state.
  get value() {
    return this[internal.state].value;
  }
  set value(value) {
    this[internal.setState]({ value });
  }

  // Indicate what should update when state changes.
  [internal.render](changed) {
    super[internal.render](changed);
    if (changed.value) {
      // When the value changes, show the value as the span's text.
      this[internal.ids].value.textContent = this[internal.state].value;
    }
  }

  // Define an initial component template.
  get [internal.template]() {
    return template.html`
      <button id="decrement">-</button>
      <span id="value"></span>
      <button id="increment">+</button>
    `;
  }

}
Demo: A simple increment/decrement component defined with ReactiveElement

The internal.template property defines the template that ShadowTemplateMixin will use to populate the component's shadow root. The internal.render method then updates the DOM when state changes. In this case, when the value state member changes, the component will update the textContent of the span.

If you don't want to use an HTML template to define your component's initial DOM structure, or want to render state changes a different way, you might consider using the more fundamental ReactiveMixin directly.

API

Built with mixins AttributeMarshallingMixin, ReactiveMixin, and ShadowTemplateMixin.

Extended by classes AlertDialog, ArrowDirectionButton, AutoCompleteComboBox, AutoCompleteInput, AutoSizeTextarea, Backdrop, Button, CalendarDay, CalendarDayNamesHeader, CalendarDays, CalendarMonth, CalendarMonthNavigator, CalendarMonthYearHeader, Carousel, CarouselSlideshow, CarouselWithThumbnails, CenteredStrip, CenteredStripHighlight, CenteredStripOpacity, ComboBox, CrossfadeStage, DateComboBox, DateInput, Dialog, Drawer, DrawerWithGrip, DropdownList, ExpandablePanel, ExpandableSection, Explorer, FilterComboBox, FilterListBox, HamburgerMenuButton, Hidden, Input, ListBox, ListComboBox, ListExplorer, ListWithSearch, Menu, MenuButton, MenuItem, MenuSeparator, ModalBackdrop, Modes, Overlay, OverlayFrame, PageDot, Popup, PopupButton, PopupSource, ProgressSpinner, PullToRefresh, SeamlessButton, Slideshow, SlideshowWithPlayControls, SlidingPages, SlidingStage, TabButton, Tabs, TabStrip, Thumbnail, Toast, and WrappedStandardElement.

[internal.defaultState] property

The default state for the component. This can be extended by mixins and classes to provide additional default state.

Type: State

Defined by ReactiveMixin

[internal.ids] property

A convenient shortcut for looking up an element by ID in the component's Shadow DOM subtree.

Example: if component's template contains a shadow element <button id="foo">, you can use the reference this[internal.ids].foo to obtain the corresponding button in the component instance's shadow tree. The ids property is simply a shorthand for getElementById, so this[internal.ids].foo is the same as this.shadowRoot.getElementById('foo').

Type: object

Defined by ShadowTemplateMixin

[internal.render](changed) method

Render the indicated changes in state to the DOM.

The default implementation of this method does nothing. Override this method in your component to update your component's host element and any shadow elements to reflect the component's new state. See the rendering example.

Be sure to call super in your method implementation so that your component's base classes and mixins have a chance to perform their own render work.

Parameters:

  • changed: objectdictionary of flags indicating which state members have changed since the last render

Defined by ReactiveMixin

[internal.renderChanges]() method

Render any pending component changes to the DOM.

This method does nothing if the state has not changed since the last render call.

ReactiveMixin will invoke this method following a setState call; you should not need to invoke this method yourself.

This method invokes all internal render methods, then invokes componentDidMount (for first render) or componentDidUpdate (for subsequent renders).

Defined by ReactiveMixin

[internal.setState](changes) method

Update the component's state by merging the specified changes on top of the existing state. If the component is connected to the document, and the new state has changed, this returns a promise to asynchronously render the component. Otherwise, this returns a resolved promise.

Parameters:

  • changes: objectthe changes to apply to the element's state

Returns: Promise - resolves when the new state has been rendered

Defined by ReactiveMixin

[internal.state] property

The component's current state.

The returned state object is immutable. To update it, invoke internal.setState.

It's extremely useful to be able to inspect component state while debugging. If you append ?elixdebug=true to a page's URL, then ReactiveMixin will conditionally expose a public state property that returns the component's state. You can then access the state in your browser's debug console.

Type: State

Defined by ReactiveMixin