Hook that reads/writes #CSS variables. Turns a CSS variable into a useState() hook for your web components and lets you change them at run-time.
✓ Published0🌍 Public
Aabonello
Last edited Jul 8, 2021
Created on Dec 30, 2021
This example demonstrates a React hook that bridges CSS variables and component state, allowing developers to read and update a CSS custom property at run-time. It shows how a single value can be synchronized between a React component’s state and the document’s stylesheet, enabling dynamic theming or style changes without a separate state management system. The `useCSSVariable` hook initializes its state by reading the variable via `window.getComputedStyle` and `document.documentElement`, then updates both the React state and the CSS property using `setProperty`. It relies on React’s `useState` and `useCallback` APIs, and returns a tuple similar to `useState` for easy integration.
AI-generated description