Created
March 27, 2025 15:16
-
-
Save bennobuilder/762e4bf9f44045508ff2e4b6f8dd7e77 to your computer and use it in GitHub Desktop.
use-css-variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use client'; | |
import React, { useId } from 'react'; | |
export function useCssVariable<GValue extends string | number>( | |
elementRef: React.RefObject<HTMLElement | null>, | |
config: TCssVariableConfig<GValue> | (() => TCssVariableConfig<GValue>), | |
deps: React.DependencyList = [] | |
) { | |
const id = useId(); | |
const { variable, variableName, initialValue, unit } = React.useMemo(() => { | |
const resolvedConfig = typeof config === 'function' ? config() : config; | |
const { name, initialValue, unit = '' } = resolvedConfig; | |
const variableName = `--${name}-${id}`; | |
const variable = `var(${variableName}, ${initialValue}${unit})`; | |
return { variable, variableName, initialValue, unit }; | |
}, [config, id, ...deps]); | |
const updateValue = React.useCallback( | |
(value: GValue) => { | |
elementRef.current?.style.setProperty(variableName, `${value}${unit}`); | |
}, | |
[elementRef, variableName, unit] | |
); | |
React.useEffect(() => { | |
updateValue(initialValue); | |
}, [updateValue, initialValue, ...deps]); | |
return [variable, updateValue] as const; | |
} | |
interface TCssVariableConfig<GValue extends string | number> { | |
name: string; | |
initialValue: GValue; | |
unit?: string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My use case
Screen.Recording.2025-03-27.at.16.18.05.mov