NOSCRIPT

Page 02: The handler

Retires classList.toggle() and the click handler wrapped around it

Most interface code was never logic. It was a click handler whose whole job was to put a class on some other element.

Open the menu: toggle a class. Mark the row as selected: toggle a class. Show the error: add a class, and remember to take it off again. The state lived in JavaScript variables and was copied into the document by hand, which is how the two came to disagree.

The document has state of its own. A checkbox is checked or it is not. A details element is open or closed, a field is valid or invalid, the URL has a fragment or it does not. What was missing was a way for one element's state to style a different element somewhere else on the page. :has() is that way, and it has been in every engine since Firefox 121, at the end of 2023.

Wasstate kept in two places
input.addEventListener("change", () => {
  card.classList.toggle(
    "is-selected", input.checked);
  updateCount();
});
Nowstate kept in one
.card:has(input:checked) {
  border-color: var(--red);
}

A lock with a thousand states

Three dials, ten digits each. They are radio buttons, so you can click the digit above or below the one that is showing, or Tab to a dial and use the arrow keys. Each drum turns because its own checked radio tells it to. The readout is assembled by CSS counters from whichever three radios are checked, and the stylesheet knows a dozen HTTP status codes by name. One of them opens the door. It is the one every request hopes for.

In a browser with :has(), these thirty radio buttons are the faces of three drums that turn as you choose, with a readout and a door that opens on the right code. Without it you get the form underneath, which still works as a form, and the note that was behind the door.

First digit
Second digit
Third digit

Dials in the right place:

200 OK. You have just driven a machine with a thousand states. No handler ran, because there is none to run. The catch is on the cover, under “What this cannot do”: leave this page and the dials forget.

How it works. Ten rules of the form .drum:has(> :nth-child(5) > :checked) set how far a drum has turned. The door opens on .lock:has(#a2:checked):has(#b0:checked):has(#c0:checked). The red digits are counter(a) counter(b) counter(c), where each checked radio adds its own value to its dial's counter.

The accordion

The accordion was the first widget many of us ever downloaded. It needed JavaScript twice: once so that opening a panel closed the others, and once to animate a height from zero to “whatever the content needs”, a value CSS refused to interpolate.

The first job now belongs to HTML. Give several details elements the same name and the browser keeps one open at a time, in every engine since Firefox 130. The second job is interpolate-size: allow-keywords, which lets a transition end at auto. Your browser has it, so the panels below slide. Your browser does not have it yet (only Chrome does, since 129), so the panels below snap. They still open, still close each other and still work from the keyboard.

The panels are about the other kinds of state a document keeps for you.

Open is state details[open]

Details elements, dialogs and popovers all expose whether they are open. This panel is turning its own plus sign into a cross with that selector, and its neighbours closed when you opened it because they share a name attribute.

The URL is state :target

A link to #something sets it and the back button unsets it. It is the only CSS state that survives a reload and can be sent to someone else. Lightboxes and tab sets were built on it long before :has() existed.

Validity is state :user-invalid

A field knows whether its value passes its own constraints. :user-invalid waits until the reader has finished with the field before it says so, which used to take a blur handler and a “touched” flag for every input.

Focus is state :focus-within

This was the first selector that let a parent react to a child, years before :has(). A menu that stays open while the keyboard is inside it needs nothing else. The dials above borrow the idea: the radio that has focus is invisible, so its dial draws the focus ring for it.

Quantity is state :has(> :nth-child(5))

That selector matches a list with five or more items. A layout can now change with the amount of content it was given, which used to mean counting children in a script and writing the answer into a class name.