Wraps a standard HTML element so that the standard behavior can then be extended.
ElementBaseSee also basic-autosize-textarea and
basic-current-anchor. The former uses
WrappedStandardElement to wrap a standard and ,
respectively.
The Custom Elements spec does not currently (as of March 2016) allow you to
extend the behavior of a standard HTML element like or .
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
element:
class WrappedA extends WrappedStandardElement.wrap('a') {
customMethod() { ... }
}
document.registerElement('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 aninstanceof the standard class (here, HTMLAnchorElement). Another limitation
is that the resulting will not automatically pick up CSS styles
for standard 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 element will contain inside its
shadow tree an instance of a standard 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 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 writtenClick here. However, the actual element
will be an instance of your custom class, with whatever behavior you've
defined for it.
Wrapped elements should raise the same events as the original standard
elements. E.g., if you wrap an element, the wrapped result will raise
the standard load event as expected.
Some elements, such as , , and cannot be wrapped
and still achieve their standard behavior.
Kind: global class
Extends: ElementBase
* WrappedStandardElement ⇐ ElementBase
* .ariaLabel : string
* .inner : HTMLElement
* .template : string | HTMLTemplateElement
* .wrap(extendsTag)
Kind: instance property of WrappedStandardElement
Kind: instance property of WrappedStandardElement
The default value of this property is a template that includes an instance
the standard element being wrapped, with a element inside that
to pick up the element's light DOM content. For example, if you wrap an element, then the default template will look like:
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".
Kind: instance property of WrappedStandardElement
Note that the resulting class is a subclass of WrappedStandardElement, not
the standard class being wrapped. E.g., if you callWrappedStandardElement.wrap('a'), you will get a class whose shadow tree
will include an anchor element, but the class will not inherit from
HTMLAnchorElement.
Kind: static method of WrappedStandardElement
| Param | Type | Description |
| --- | --- | --- |
| extendsTag | string | the standard HTML element tag to extend |