Appearance
useCssVar
Manipulate CSS variables
Demo
Usage
tsx
import { useCssVar } from '@reause/core'
import { useRef } from 'react'
const el = useRef<HTMLDivElement>(null)
const [color1, setColor1] = useCssVar('--color', el)
// force a re-render (e.g. with your own state) once `el` is populated
const [key] = useState('--color')
const [colorVal, setColorVal] = useCssVar(key, el)
const [color2, setColor2] = useCssVar('--color', el, { initialValue: '#eee' })
setColor2(null) // removes the --color property from the elementType Declarations
ts
export interface UseCssVarOptions extends ConfigurableWindow {
initialValue?: string;
observe?: boolean;
}
export type UseCssVarElement = HTMLElement | SVGElement | null | undefined
export type UseCssVarReturn = [
value: string | null | undefined,
setValue: Dispatch<SetStateAction<string | null | undefined>>
]
export function useCssVar(prop: string | null | undefined, target?: RefObject<UseCssVarElement | null>, options?: UseCssVarOptions): UseCssVarReturn