reflectAttribute
Defines a property on element that is backed by an HTML attribute — what a custom element
needs for <my-card heading="…" count="3" selected>, in one line. Reading parses the attribute
by type; writing stores it (a boolean toggles the attribute, null or undefined removes it).
The attribute stays the single source of truth, so setAttribute and the property never disagree.
It defines an own property on the instance, so call it once per element (in a constructor or
connectedCallback). Calling it again for the same property replaces the previous definition.
Available since v3.1.8
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
element | Element | The element that gets the property |
property | string | The property name, e.g. `‘heading’` |
options | ReflectAttributeOptions | Type, attribute name and default — see ReflectAttributeOptions |
Returns
Section titled “Returns”function — A function that removes the property again
Examples
Section titled “Examples”A number property backed by an attribute
Section titled “A number property backed by an attribute”Writing the property sets the attribute, and setAttribute is seen by the property.
A boolean property is the presence of the attribute
Section titled “A boolean property is the presence of the attribute”Like hidden or disabled: true adds the attribute, false removes it.
Related Types
Section titled “Related Types”ReflectAttributeOptions
Section titled “ReflectAttributeOptions”Options for reflectAttribute.
