Custom attributes

class extends Attr

k9l.io/tpac-ca

Keith Cirkel, Lea Verou - TPAC 2026

Motivation

Platform behaviour lives in attributes; authors only get elements

The platform composes traits:

  • popover
  • hidden
  • title
  • lang
  • list
  • loading

Authors get:

  • <new-element>
  • data-*, no lifecycle

Most reusable behaviour is something an element has, not something it is.

Motivation

Wrappers break markup and add noise

<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

Every framework reinvented attribute behaviours

Vue v-focus
Angular appHighlight
Svelte {@attach tooltip}
Solid use:clickOutside
Alpine.js x-uppercase
htmx hx-post

Same idea, none portable.

Motivation

GitHub was built on attribute behaviours

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

MutationObserver can't stand in for a primitive

Custom elements had these pieces too.

Replacing is=

Customised built-ins never reached every engine

customElements.define(
  "save-button", SaveButton,
  { extends: "button" },
);
<button is="save-button">
  • Not in WebKit
  • One is per element
  • Fixed at creation
  • State of HTML 2025: a top pain point

Replacing is=

Interop 2027 proposal: deprecate is=

"...list this as 'things to remove'." Westbrook Johnson, interop#1342
  • Specified for a decade
  • Two engines, gaps in one
  • WebKit opposed

Named alternatives:

  • Platform-provided behaviours
  • Custom attributes (WebKit positive!)
  • Customisable form controls

Removal needs a replacement.

Replacing is=

Custom attributes cover is=, and compose

<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=

An attribute checks its own host

class PwdToggle extends Attr {
  connectedCallback() {
    if (this.ownerElement?.type !== "password") {
      return console.warn("not a password");
    }
    /* ... */
  }
}
  • Host filters could come later

Semantics

Subclass Attr, define a hyphenated name

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

Userland APIs can be modules, not prototype methods

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

An attribute is connected when its element is

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

Attributes upgrade in place, like elements

Parser creates a plain Attr

attached, or defined

Prototype swapped, constructor runs

then

Changed, then connected callbacks
  • Upgrades whenever a registry resolves
  • On insertion, host element upgrades first
  • createAttribute() is synchronous

Semantics

Registries scope to trees, like custom elements

const registry = new CustomAttributeRegistry();
registry.define("my-tooltip", Tooltip);

host.attachShadow({
  mode: "open",
  customAttributeRegistry: registry,
});

Rejected: per element class

  • Prototype-chain lookups
  • Realm-bound constructors
  • A second scoping model

Semantics

Names need a hyphen; platform prefixes stay reserved

persist-value valid
tooltip no hyphen
my-Tooltip uppercase
data-*, aria-* reserved
xml*, xlink* reserved

New platform attributes avoid hyphens.

Semantics

Deferred from v1, not closed off

Status

We have a polyfill available

import "custom-attributes-polyfill";
pre-1.0

MutationObserver gaps:

  • Reactions a microtask late
  • Quick re-insert unseen
  • Only observed trees

1.0 when the spec merges.

Status

HTML and DOM text is drafted

HTML
  • CustomAttributeRegistry
  • Attr constructor
  • Upgrades, reactions
DOM
  • Attr custom state
  • Registries on nodes
  • Upgrade on insert, set

Next

Review the spec and test PRs, then engine positions