WrappedStandardElement

Wraps a standard HTML element so that you can create custom element classes that in many ways function as extensions of the standard element.

Type all you want here!
Demo: This Elix AutosizeTextarea element wraps a standard textarea

The above demo of an Elix AutosizeTextarea uses WrappedStandardElement to wrap a standard <textarea>. That gives it a foundation of all the properties, methods, and events of a standard <textarea>, allowing the component to provide additional behavior. In this case, AutosizeTextarea automatically updates the element's height to accommodate its text.

Although the Custom Elements spec defines support for customized built-in elements, Apple has elected not to support them, effectively limiting their use. This means you cannot extend the behavior of a standard HTML element like <a> or <button> and still expect the result to work on all browsers that support Custom Elements.

As a partial workaround, the WrappedStandardElement class can create a class for you that wraps an instance of a standard HTML element. For example, the code below creates a class that will wrap an instance of a standard <a> element:

class WrappedA extends WrappedStandardElement.wrap('a') {
  customMethod() { ... }
}
customElements.define('wrapped-a', WrappedA);

An instance of the resulting class will look to the user like an instance of the standard element class it wraps. The resulting class will not be an instanceof the standard class (here, HTMLAnchorElement). Another limitation is that the resulting <wrapped-a> will not automatically pick up CSS styles for standard <a> elements. However, the resulting class can be extended. E.g., instances of the above class have a customMethod() available to them.

Any properties defined by the original standard element will be exposed on the resulting wrapper class, and calls to get or set those properties will be delegated to the wrapped element instance. Continuing the above example:

let wrapped = document.createElement('wrapped-a');
wrapped.href = 'http://example.com/';
wrapped.textContent = 'Click here';

Here, the created custom <wrapped-a> element will contain inside its shadow tree an instance of a standard <a> element. The call to set the wrapper's href property will ultimately set the href on the inner link. Moreover, the text content of the <wrapped-a> element will appear inside the inner link. The result of all this is that the user will see what looks like a normal link, just as if you had written <a href="http://example.com/">Click here</a>. However, the actual element will be an instance of your custom class, with whatever behavior you've defined for it.

Attributes set on the wrapping element will similarly be delegated to the inner element. For example, the AutosizeTextarea demo above includes a placeholder="Type here" attribute on the wrapping <elix-autosize-textarea> instance. (To see the placeholder, delete all text in the sample textarea.)The class delegates this placeholder attribute to the inner <textarea>. The inner <textarea> then renders the placeholder as a prompt to the user if the text area contains no text.

Wrapped elements should raise the same events as the original standard elements. E.g., if you wrap an <img> element, the wrapped result will raise the standard load event as expected.

Some elements, including <body>, <html>, and <style>, cannot be wrapped and still retain their normal behavior.

API

Ancestry: WrappedStandardElementElementBase → HTMLElement

WrappedStandardElement is extended by AutosizeTextarea and TabButton.

This element uses AttributeMarshallingMixin, ReactiveMixin, RenderUpdatesMixin, and ShadowTemplateMixin.

$ property

The collection of references to the elements with IDs 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.$.foo to obtain the corresponding button in the component instance's shadow tree.

Such references simplify a component's access to its own elements. In exchange, this mixin trades off a one-time cost of querying all elements in the shadow tree instead of paying an ongoing cost to query for an element each time the component wants to inspect or manipulate it.

These shadow element references are established the first time you read the $ property. They are * not updated if you subsequently modify the shadow tree yourself (to replace one item with another, to add new items with id attributes, etc.).

Type: object

Defined by ShadowTemplateMixin inherited from ElementBase

defaultState property

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

Type: object

Defined by ReactiveMixin inherited from ElementBase

inner property

Returns a reference to the inner standard HTML element.

Type: HTMLElement

render() method

Render the component to the DOM.

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

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

Defined by ReactiveMixin inherited from ElementBase

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.

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

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

Defined by ReactiveMixin inherited from ElementBase

shouldComponentUpdate(nextState) method

Return true if the component should update.

The default implementation does a shallow check of property values like React's PureComponent. This seems adequate for most web components. You can override this to always return true (like React's base Component class), or to perform more specific, deeper checks for changes in state.

Returns: boolean - true if the component should update (rerender)

Parameters:
  • nextState: objectthe proposed new state for the element

Defined by ReactiveMixin inherited from ElementBase

state property

The component's current state. The returned state object is immutable. To update it, invoke setState.

Type: object

Defined by ReactiveMixin inherited from ElementBase

template property

The template copied into the shadow tree of new instances of this element.

The default value of this property is a template that includes an instance the standard element being wrapped, with a <slot> element inside that to pick up the element's light DOM content. For example, if you wrap an <a> element, then the default template will look like:

<template>
  <style>
  :host {
    display: inline-block;
  }
  </style>
  <a id="inner">
    <slot></slot>
  </a>
</template>

The display styling applied to the host will be block for elements that are block elements by default, and inline-block (not inline) for other elements.

If you'd like the template to include other elements, then override this property and return a template of your own. The template should include an instance of the standard HTML element you are wrapping, and the ID of that element should be "inner".

Type: string, HTMLTemplateElement

updates property

The attributes and properies that should be applied to the component on render. By default, this is an empty plain object. Your mixin or component can extend this to identify the properties to set on the host element or elements in the shadow subtree.

Type: object

Defined by RenderUpdatesMixin inherited from ElementBase

wrap(extendsTag) static method

Creates a class that wraps a standard HTML element.

Note that the resulting class is a subclass of WrappedStandardElement, not the standard class being wrapped. E.g., if you call WrappedStandardElement.wrap('a'), you will get a class whose shadow tree will include an anchor element, but the class will not inherit from HTMLAnchorElement.

Parameters:
  • extendsTag: stringthe standard HTML element tag to extend

© 2016-2018 Component Kitchen Inc. and contributors to the Elix project. ELIX and the Elix project logo are registered or unregistered trademarks of Component Kitchen Inc. in the United States and/or other countries. Other trademarks are the property of their respective owners. Any use of such trademarks without written permission sought and obtained from Component Kitchen is prohibited.

The Elix project is administered, as a courtesy, by Component Kitchen for the benefit of the project’s contributors and the developer community as a whole.