@ Proposal & Explainer
An early proposal for creating declarative state controls in CSS, and styling elements based on the current state.
Declarative controls for presentational state
Bookmark from Tab Atkins
There are many use-cases where an interaction (keyboard, mouse, or gesture) on one element toggles a ‘state’ that can be shared with other elements. This can range from switching light/dark mode, to activating slide-in navigation, opening and closing menus, or interacting with sectioned content such as tabs, accordions, or carousels.
We propose generalizing the ability to declaratively toggle and share presentational state, so that it can be applied to any element using a declarative syntax in CSS, with built-in accessibility and performance. For example:
/* establish a color-mode toggle on the html element */
/* with light, dark, and (default) auto states */
toggle-root: color-mode [auto light dark] at auto;
/* on activation, increment color-mode to next state */
Both the syntax and functionality are still likely to change, as we work out the details. There are a number of major issues to resolve still, especially when it comes to accessibility.
The OddBird team has built a rough JS polyfill of the proposed syntax, to help understand those remaining issues. Hopefully these are all solvable problems, and we can move this work into the official CSSWG standards track before long.