Reactive CSS: state, events, and DOM bindings right inside stylesheets ⚡️
← Go back

Reactive CSS: state, events, and DOM bindings right inside stylesheets ⚡️

An overview of Reactive CSS, an experimental zero-build runtime that brings local state, event handling, and DOM attribute bindings directly into plain CSS files.

Reactive CSS: state, events, and DOM bindings right inside stylesheets ⚡️

I came across an intriguing experimental library called Reactive CSS (currently at 0.10.0). It is a zero-build JavaScript runtime that lets you describe UI interactivity directly inside plain .css files.

While a tool like prop-for-that works in one direction—merely piping browser metrics into CSS custom properties—Reactive CSS goes further and turns a stylesheet into a full-fledged state machine for UI components.

How it looks in practice

For example, an accessible dropdown menu with aria-expanded can be written entirely in styles:

/* 1. Local state on the container */
.menu-wrapper {
  --open: state(false);
}

/* 2. Sync state with an HTML attribute */
.menu-wrapper button {
  --attr-aria-expanded: var(--open);
}

/* 3. Event handler via the :on() pseudo-class */
.menu-wrapper button:on(click) {
  --open: toggle();
}

/* 4. Styling via the conditional :when() pseudo-class */
.menu-wrapper:when(--open) .dropdown {
  display: block;
}

How it works under the hood

The browser’s native CSS parser simply ignores unfamiliar constructs like :on(click) or state(false). All the heavy lifting is done by the included runtime <script>:

  • Parsing and dependency graph: on page load, the script scans stylesheets, extracts the custom syntax, and wires triggers to variables.
  • Per-DOM-node isolation: state lives on individual elements just like standard custom properties. If a page has ten of these menus, each one keeps its own independent state.
  • Delegating to native CSS: the runtime attaches real JS event listeners, updates inline custom properties and attributes (--attr-*) on click, and hands control back to the browser—letting :when() selectors, transition, and @keyframes do the rest.

What else the library can do

  • Persistence: state("light", persist("theme")) saves values to localStorage and restores them before first paint.
  • Sensors and gestures: drag tracking (:on(drag(move))), cursor coordinates (mouse()), element dimensions (element() via ResizeObserver), viewport visibility (visible()), and timers (after(), every()).
  • History and forms: previous() and direction() functions, the :changed() pseudo-class, two-way input binding with bind(value), plus debounce() and throttle().
  • MCP for AI tools: ships with an official @reactivecss/mcp server so AI coding agents know the current syntax instead of hallucinating outdated rules.

The authors position it as glue for small-scale UI logic where native :has(), :checked, or popover fall short, yet pulling in a full JS framework feels like overkill. A few caveats: the project is still experimental (syntax changes between minor releases), and the full bundle weighs 60.8 KB gzipped (though a built-in Bundle Builder lets you compile a lighter build with only the modules you need).

👉 Explore the demos and docs: reactivecss.com (opens in new tab)

What do you think of this approach? A neat experiment in pushing CSS boundaries, or is putting localStorage, timers, and conditionals into .css going too far? 👇

Copied