zena:web-elements
import {…} from 'zena:web-elements';
Classes
ZenaElement
abstract class ZenaElement
The base class of an element the host defines from this module.
The lifecycle methods have empty defaults; a subclass overrides the
ones it needs. render is called by the host after requestUpdate,
once per microtask however many times it was asked.
var definition: ElementDefinition | null
The definition this instance was made from; set by the host's create.
abstract render(): TemplateResult
What the element shows now.
styles(): CSSResult | null
The element's styles, from css. The host reads them once per
definition, from the instance defineElement constructs, and adopts
one stylesheet into every instance's shadow root. Lit's styles is
a static field; a static cannot be reached from a definition
without a class value, so this is a method, at the same cost.
connected(): void
The element was inserted into a document.
disconnected(): void
The element was removed from its document.
attributeChanged(name: String, value: String | null): void
An observed attribute changed; value is null when it was removed.
updated(first: boolean): void
The host rendered the last result; first on the first render.
requestUpdate(): void
Ask the host to render before the next frame.
dispatchEvent(type: String, bubbles: boolean = true, composed: boolean = true, cancelable: boolean = false): boolean
Dispatch an Event of type from the element, bubbling and
composed by default. The event carries no payload: a listener reads
what it needs off the element, as it does for change or input,
which is also how an element dispatches those standard events. Returns
false when a listener canceled a cancelable event.
CSSResult
final class CSSResult
A stylesheet for an element's styles: the text the host puts in one
constructible stylesheet per definition. Only css results and
numbers interpolate, as in Lit; unsafeCSS admits text the author
vouches for.
PropertyDef
final class PropertyDef
One declared property: its name, its attribute, and its accessors over
boxed values. The accessors are generic over the field's type where
registerProperty makes them, and box so the protocol's typed getter
and setter exports can test the box for the kind.
new()
name: String
attribute: String
The observed attribute, or the empty string for none.
read: (element: ZenaElement) => anyref
write: (element: ZenaElement, value: anyref) => void
var kind: PropertyKind
Decided from the probe instance's value on first demand.
var definition: ElementDefinition | null
The definition that declares the property; set when it is added.
ElementDefinition
final class ElementDefinition
A registered element: what defineElement records and the host reads.
TemplateResult
final class TemplateResult
A template and the values for its holes.
strings is the tagged template's TemplateStringsArray, which the
compiler makes once per call site; that identity is what tells the
host it has seen the template before.
HostValue
final class HostValue
A JavaScript value the module holds without looking inside it, so it
can sit in a template: a DOM node for .node=${…}, a Date for a
child's property. The host hands the value back unchanged.
Event
extension class Event
The event a listener receives: the host's event object, with the capability functions as methods, which are all a module can do with an event unless the host grants more.
An extension class rather than a wrapper, so a listener's event costs no allocation and a method's import is linked only when something calls it: a module whose listeners never read their events imports none of these.
Mixins
property
mixin property<T> on ZenaElement
A property of the element: declared to the host, reflected from its attribute (the lowercased field name), and re-rendering on write.
The #declared initializer runs once per instance; it registers the
field with the definition being probed by defineElement and does
nothing for every later instance. Registering from an initializer is
what field decorators can do today; a per-class registration, run
once at module start, is the form this will take once decorators have
static members.
value: T { get; set; }
9 inherited members
ZenaElement
var definition: ElementDefinition | null
The definition this instance was made from; set by the host's create.
abstract render(): TemplateResult
What the element shows now.
styles(): CSSResult | null
The element's styles, from css. The host reads them once per
definition, from the instance defineElement constructs, and adopts
one stylesheet into every instance's shadow root. Lit's styles is
a static field; a static cannot be reached from a definition
without a class value, so this is a method, at the same cost.
connected(): void
The element was inserted into a document.
disconnected(): void
The element was removed from its document.
attributeChanged(name: String, value: String | null): void
An observed attribute changed; value is null when it was removed.
updated(first: boolean): void
The host rendered the last result; first on the first render.
requestUpdate(): void
Ask the host to render before the next frame.
dispatchEvent(type: String, bubbles: boolean = true, composed: boolean = true, cancelable: boolean = false): boolean
Dispatch an Event of type from the element, bubbling and
composed by default. The event carries no payload: a listener reads
what it needs off the element, as it does for change or input,
which is also how an element dispatches those standard events. Returns
false when a listener canceled a cancelable event.
state
mixin state<T> on ZenaElement
Internal reactive state: re-renders on write, like @property, and is
declared to the host as a property without an attribute, which is
what Lit's @state is.
value: T { get; set; }
9 inherited members
ZenaElement
var definition: ElementDefinition | null
The definition this instance was made from; set by the host's create.
abstract render(): TemplateResult
What the element shows now.
styles(): CSSResult | null
The element's styles, from css. The host reads them once per
definition, from the instance defineElement constructs, and adopts
one stylesheet into every instance's shadow root. Lit's styles is
a static field; a static cannot be reached from a definition
without a class value, so this is a method, at the same cost.
connected(): void
The element was inserted into a document.
disconnected(): void
The element was removed from its document.
attributeChanged(name: String, value: String | null): void
An observed attribute changed; value is null when it was removed.
updated(first: boolean): void
The host rendered the last result; first on the first render.
requestUpdate(): void
Ask the host to render before the next frame.
dispatchEvent(type: String, bubbles: boolean = true, composed: boolean = true, cancelable: boolean = false): boolean
Dispatch an Event of type from the element, bubbling and
composed by default. The event carries no payload: a listener reads
what it needs off the element, as it does for change or input,
which is also how an element dispatches those standard events. Returns
false when a listener canceled a cancelable event.
Enums
PropertyKind
enum PropertyKind
The protocol's property kinds; the values are its value kinds.
Type aliases
CSSValue
type CSSValue = CSSResult | Box<i32> | Box<f64>
TemplateValue
type TemplateValue = String | TemplateResult | Iterable<TemplateValue> | Listener | Box<i32> | Box<f64> | Box<boolean> | HostValue | null
What a hole may hold. A number or boolean arrives boxed: the union
accepts the box, which is what tells the checker to wrap a primitive
interpolation in one. null renders nothing, and an iterable renders
each of its items in turn.
Listener
type Listener = (event: Event) => void
An event listener, for an @event=${…} hole.
Functions
css
function css(strings: TemplateStringsArray, values: FixedArray<CSSValue>): CSSResult
The css tag: a stylesheet, with css results and numbers interpolated.
unsafeCSS
function unsafeCSS(text: String): CSSResult
A stylesheet from text the author vouches for, as Lit's unsafeCSS.
registerProperty
function registerProperty<T>(name: String, get: (element: ZenaElement) => T, set: (element: ZenaElement, value: T) => void, attribute: boolean): i32
Declares a property of the element class being probed: its name, its
accessors over the host, and whether an attribute reflects into it.
Called by @property and @state through the decorator's context.
Returns 0, so it can initialize a field.
defineElement
function defineElement(tag: String, create: () => ZenaElement): ElementDefinition
Registers an element definition. The host defines a custom element
named tag whose instances create makes, with the properties its
@property and @state fields declare.
create is a closure because a class is not a value in Zena today:
there is no constructor reference to pass, so the factory is written
out. The design document lists a constructor reference among the
language changes this library would use.
One instance is constructed here and kept: constructing it is what runs the decorators' registrations, and its values decide each property's kind.
declareElement
function declareElement(create: () => ZenaElement): ElementDefinition
Registers an element definition with no tag name. The host builds the
custom element class from it and leaves naming and registration to the
page (elementDefinitions in @zena-lang/wasm-web-ui/elements),
which is how one module serves pages that disagree about tag names.
definitionFor
function definitionFor(tag: String): ElementDefinition | null
The definition registered under tag, or null.
createElement
function createElement(definition: ElementDefinition): ZenaElement
A new instance of definition, bound to it.
propertyKind
function propertyKind(property: PropertyDef): PropertyKind
A property's kind, decided from its definition's probe instance once.
stylesOf
function stylesOf(definition: ElementDefinition): CSSResult | null
The definition's styles, from its probe instance; null for none.
readProperty
function readProperty(element: ZenaElement, property: PropertyDef): anyref
The boxed value of property on element, as the field's box.
setTextProperty
function setTextProperty(element: ZenaElement, property: PropertyDef, text: String): void
Sets a text property from text; nothing for a property of another kind.
setHostProperty
function setHostProperty(element: ZenaElement, property: PropertyDef, value: externref): void
Sets a host-value property from the host's reference.
setNumberProperty
function setNumberProperty(element: ZenaElement, property: PropertyDef, value: f64): void
Sets a number or boolean property from a number; nothing for another kind.
applyAttribute
function applyAttribute(element: ZenaElement, property: PropertyDef, text: String): void
An observed attribute was set. The attribute's text is converted by the property's kind, as Lit's default converters do: a number is parsed, a boolean attribute is true when present, text is taken as it is. A host-value property takes nothing from an attribute.
removeAttribute
function removeAttribute(element: ZenaElement, property: PropertyDef): void
An observed attribute was removed: a boolean goes false, text goes empty.
propertyForAttribute
function propertyForAttribute(definition: ElementDefinition, name: String): PropertyDef | null
The property whose attribute is name, or null.
webui_element_count
function webui_element_count(): i32
webui_element_at
function webui_element_at(index: i32): ElementDefinition
webui_definition_tag
function webui_definition_tag(definition: ElementDefinition): externref
webui_definition_styles
function webui_definition_styles(definition: ElementDefinition): externref | null
The definition's stylesheet text, or null for none.
webui_definition_property_count
function webui_definition_property_count(definition: ElementDefinition): i32
webui_definition_property
function webui_definition_property(definition: ElementDefinition, index: i32): PropertyDef
webui_definition_create
function webui_definition_create(definition: ElementDefinition): ZenaElement
The constructor: a new instance of the definition.
webui_property_name
function webui_property_name(property: PropertyDef): externref
webui_property_attribute
function webui_property_attribute(property: PropertyDef): externref
webui_property_kind
function webui_property_kind(property: PropertyDef): i32
webui_element_connected
function webui_element_connected(element: ZenaElement): void
webui_element_disconnected
function webui_element_disconnected(element: ZenaElement): void
webui_element_attribute_changed
function webui_element_attribute_changed(element: ZenaElement, property: PropertyDef, value: externref): void
The property's observed attribute was set; value is the attribute's text.
webui_element_attribute_removed
function webui_element_attribute_removed(element: ZenaElement, property: PropertyDef): void
webui_element_get_extern
function webui_element_get_extern(element: ZenaElement, property: PropertyDef): externref | null
webui_element_get_number
function webui_element_get_number(element: ZenaElement, property: PropertyDef): f64
webui_element_set_extern
function webui_element_set_extern(element: ZenaElement, property: PropertyDef, value: externref): void
webui_element_set_number
function webui_element_set_number(element: ZenaElement, property: PropertyDef, value: f64): void
webui_element_render
function webui_element_render(element: ZenaElement): TemplateResult
webui_element_updated
function webui_element_updated(element: ZenaElement, first: i32): void
renderElement
function renderElement(definition: ElementDefinition, attributes: Map<String, String>, tag: String = ''): String
Renders definition as an element: the tag with attributes, a
declarative shadow root with the styles and the template, and the end
tag. The attributes are applied to a fresh instance before it renders,
as the host applies them on the page, so the markup matches what the
page's instance will render from the same attributes. tag names a
definition declared without one, or renames a tagged one, as the page
does when it registers the class itself.
renderResult
function renderResult(result: TemplateResult): String
Renders a template result with hydration markers.
templateDigest
function templateDigest(strings: TemplateStringsArray): String
Lit's digest of a template's strings, which hydration compares against the digest of the template it is given: two 32-bit DJB hashes over each string's UTF-16 code units, alternating per unit, as 8 bytes in base64.
html
function html(strings: TemplateStringsArray, values: FixedArray<TemplateValue>): TemplateResult
An HTML template.
svg
function svg(strings: TemplateStringsArray, values: FixedArray<TemplateValue>): TemplateResult
An SVG template, for use inside an <svg> element.
mathml
function mathml(strings: TemplateStringsArray, values: FixedArray<TemplateValue>): TemplateResult
A MathML template, for use inside a <math> element.
Variables
definitions
let definitions: GrowableArray<ElementDefinition>
Every definition the module registered, in registration order.
nothing
let nothing: TemplateValue
The value that renders nothing.