v0.20.230 - 7.5 / 5.8

Custom Element

Includes information about Custom Elements on the context of the renderer.

# Attributes vs Properties

Please refer to the page Attributes and Properties

# customElement (factory)

customElement(name, constructor, options?) registers a custom element with customElements.define, but only if the tag name has not been defined yet. This makes it safe to call from modules that may be imported more than once (hot reload, multiple entry points) without throwing a NotSupportedError.

nametypedescription
namestringtag name — must contain a hyphen
constructorCustomElementConstructorclass extending HTMLElement (or CustomElement from pota/components)
options?ElementDefinitionOptionsforwarded to customElements.define (for example { extends: "button" })

# CustomElement Class

The CustomElement Class provides an API for common needed things on Custom Elements. It was developed to be used as a base class for a pota components library.

On construct it will attach a shadowRoot in mode open. If the constructor has a static array field named styleSheets, then, the shadowRoot will adopt the stylesheets. Example:

You may click re-run to see it in action
namekindargumentdescription
addStyleSheetsMethod(CSSStyleSheet|urls)[] Given an array containing CSSStyleSheet or urls, it will append the stylesheets to document.adoptedStyleSheets. When it's an URL it will fetch, create a CSSStyleSheet and cache it for the given URL. So different Custom Elements all share the same CSSStyleSheet.
addStyleSheetExternalMethodurlHelper for addStyleSheets. See above
queryMethodselectorAlias for element.querySelector
htmlSetterComponenthtml is a setter that can be used with a string or any Component. The result will replace childrens on the shadowRoot.
hiddenSetterboolean | signalhidden is a setter that will check the value passed for truthiness and add or remove an attribute named hidden on the Element.
emitMethodeventName, [data]Method to dispatch an event from a custom element.
hasSlotMethodslotNameReturns a boolean indicating if a slot is present. TODO: This should return a signal instead.