Loading...
class XToggle extends HTMLElement {
set checked(v) {
this.classList.toggle('checked', v);
}
}class XToggle extends HTMLElement {
#internals = this.attachInternals();
set checked(v) {
if (v) this.#internals.states.add('checked');
else this.#internals.states.delete('checked');
}
}A custom state is owned by the element, so x-toggle:state(checked) matches without exposing a class the page can change.
:--checked selector syntax instead of :state(checked).internals.states.add('checked') after attachInternals().:state(checked), not the old :--checked syntax.aria-* values are still set for assistive tech.Yes. Inside the component styles use :host(:state(checked)); from the page use x-toggle:state(checked).
Any script can add or remove a class. A custom state is set only by the element through ElementInternals, so it works like a built-in pseudo-class.