1.6 · Selectors & structure
States for your own elements: :state()
Custom elements expose states that CSS selects like built-in :checked.
Built-in elements have states like :checked and
:open. A custom element can now have its own:
this.attachInternals().states.add('on') in JavaScript,
toggle-switch:state(on) in CSS.
The state lives inside the element. It doesn’t leak into attributes or classes that page code could change, and it reads like a real pseudo-class.
// Reflect state as an attribute or class
this.toggleAttribute('data-on', on)
toggle-switch[data-on] { … }
// In the element
this.#internals.states.add('on')
/* In CSS */
toggle-switch:state(on) { … }