Motivation
The platform composes traits:
popoverhiddentitlelanglistloadingAuthors get:
<new-element>data-*, no lifecycleMost reusable behaviour is something an element has, not something it is.
Motivation
<table>
<tr>
<td-value value="0.5">
<td>Half</td>
</td-value>
</tr>
</table>
Parser moves it out of the table.
Wrapped
<dt-format type="relative"> <time datetime="2025-11-15"></time> </dt-format>
Attribute
<time datetime="2025-11-15"
dt-format="relative"></time>
Motivation
| Vue | v-focus |
|---|---|
| Angular | appHighlight |
| Svelte | {@attach tooltip} |
| Solid | use:clickOutside |
| Alpine.js | x-uppercase |
| htmx | hx-post |
Same idea, none portable.
Motivation
data-required-change |
Like required, until edited |
|---|---|
data-disable-with |
Disable submit while sending |
data-warn-unsaved-changes |
Prompt before leaving a dirty form |
.js-session-resumable |
Restore fields after navigation |
data-hotkey |
Keyboard shortcut for any element |
data-show-dialog-id |
Open a dialog by id |
Firefox bug 1422522: one 136-selector
querySelectorAll, on every DOM change.
Motivation
data-* namesCustom elements had these pieces too.
Replacing is=
customElements.define(
"save-button", SaveButton,
{ extends: "button" },
);
<button is="save-button">
is per elementReplacing is=
"...list this as 'things to remove'." Westbrook Johnson, interop#1342
Named alternatives:
Removal needs a replacement.
Replacing is=
<button is="save-button"><button save-action> <input type="password" pwd-toggle> <input persist-value required-trimmed> <a href="/" my-tooltip="Home">
is= |
Attributes | |
|---|---|---|
| Per element | one | many |
| Add later | no | yes |
| Hosts | one tag | any HTML element |
| Methods on | element |
Attr
|
Replacing is=
class PwdToggle extends Attr {
connectedCallback() {
if (this.ownerElement?.type !== "password") {
return console.warn("not a password");
}
/* ... */
}
}
Semantics
class PersistValue extends Attr {
#owner = null;
connectedCallback() {
this.#owner = this.ownerElement;
if (!this.#owner) return;
const saved = localStorage.getItem(this.value);
if (saved !== null) this.#owner.value = saved;
this.#owner.addEventListener("input", this);
}
disconnectedCallback() {
this.#owner?.removeEventListener("input", this);
this.#owner = null;
}
handleEvent() {
localStorage.setItem(this.value, this.#owner.value);
}
}
customAttributes.define("persist-value", PersistValue);
<input persist-value="draft">
el.attributes["persist-value"] instanceof PersistValue; // true
Semantics
Platform
HTMLElement.prototype.showPopover = ...; el.showPopover();
Userland
// my-popover.js
export class MyPopover extends Attr { show() { /* ... */ } }
export function showPopover(el) {
el.attributes["my-popover"]?.show?.();
}
// app.js
import { showPopover } from "./my-popover.js";
showPopover(el);
Semantics
attributeChangedCallback |
added, changed, removed; once on upgrade |
connectedCallback |
attached and connected |
disconnectedCallback |
detached or disconnected |
connectedMoveCallback |
moveBefore() |
adoptedCallback |
new document |
Upgrade: changed, then connected.
Own changes only. No observedAttributes.
Semantics
Attrattached, or defined
then
createAttribute() is synchronousSemantics
const registry = new CustomAttributeRegistry();
registry.define("my-tooltip", Tooltip);
host.attachShadow({
mode: "open",
customAttributeRegistry: registry,
});
Rejected: per element class
Semantics
| persist-value | valid |
| tooltip | no hyphen |
| my-Tooltip | uppercase |
| data-*, aria-* | reserved |
| xml*, xlink* | reserved |
New platform attributes avoid hyphens.
Semantics
observedAttributesStatus
import "custom-attributes-polyfill";pre-1.0
MutationObserver gaps:
1.0 when the spec merges.
Status
CustomAttributeRegistryAttr constructor