React Hook Lets UI State Drive CSS Custom Properties

In an earlier post titled “Custom Properties as State,” the author of the source article mentioned that libraries such as React and Vue could, in theory, automatically map the state they manage into CSS Custom Properties, allowing that state to be used directly in CSS. That post also included a call for someone to build a useStateWithCustomProperties hook to do exactly that, tagged #freeidea.

Andrew Bloyce picked up the suggestion and published the hook as the npm package use-state-in-custom-properties. According to the source, it works just as the author had hoped: the hook returns a component that acts as the Custom Property “boundary,” and any state passed to it is mapped to those custom properties. A basic demo accompanies the package.

The source calls the hook clever and already useful, but adds that it will be extremely useful if the concept of higher level custom properties lands. In that scenario, flipping a single custom property would let a whole block of styling change at once, similar to what media queries already make possible.

CSS-Tricks star logo