useCssVar (React)
Set, get and remove a
CSS custom property
from the comfort of a React component. This could result in slightly more
performant code when you're setting the style
property directly since React
will re-run your component fewer times.
import { useMemo, useEffect } from 'react'type UseCssVarProps = { name: string root?: HTMLElement}type CssVarControls = { set: (value: string) => void get: () => string remove: () => void}export const useCssVar = ({ name, root = document.body,}: UseCssVarProps): CssVarControls => { const controls: CssVarControls = useMemo( () => ({ set: value => root.style.setProperty(name, value), get: () => root.style.getPropertyValue(name), remove: () => root.style.removeProperty(name), }), [name, root] ) useEffect(() => { return () => controls.remove() }, [controls]) return controls}
Info
-
We use
useMemo
, so that if you follow hook rules and use the helpers in a function inside auseEffect
,useCallback
, etc, you won't have code (re-)running unnecessarily. -
If you don't want the variable to be removed when the hook is no longer used, you can either remove the
useEffect
, or add an extra prop and conditionally return a cleanup function in theuseEffect
. -
This hook doesn't use
useState
since it would nullify the performance aspect, and I didn't feel the need to be notified when the variable changed on the root. You could useMutationObserver
if you wanted to know if the variable was updated on theroot
.
Demo
If you open up your browser's devtools, find the <main>
element and look at
its style
attribute, you'll see the --example-css-var
CSS
variable change as you change the input's value or press the button.
The image's rotation is handled by styling it with transform:
rotate(calc(var(--example-css-var, 45) * 1deg))
.
License & Attribution
Attribution
You should place the following attribution (and probably the license below it) above your copy of the code, but 🤷♂️.
This snippet "useCssVar (React)" from "https://soorria.com/snippets/use-css-var-react"
was created by Soorria Saruva (https://soorria.com) and is covered under the
MIT license found here: https://github.com/soorria/soorria.com/blob/master/LICENSE.
License
If you want, you can (and probably should) include the entire license below. I'm not a lawyer, though so idrk.
MIT License Copyright (c) 2022 Soorria Saruva Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.