fn
useCssVar
v0.0.14testeddemoRead and write a CSS custom property on an element as a reactive ref.
Defaults to document.documentElement. Set observe to react to external
changes via a MutationObserver.
Examples
ts
const color = useCssVar('--color', el);
color.value = 'red';ts
const theme = useCssVar('--theme', null, { initialValue: 'light', observe: true });Demo
Loading demo…
Signature
ts
export function useCssVar(
prop: MaybeRefOrGetter<string | null | undefined>,
target?: MaybeComputedElementRef,
options: UseCssVarOptions ={ ... }Parameters
| Parameter | Type | Description |
|---|---|---|
prop | MaybeRefOrGetter<string | null | undefined> | The CSS variable name (e.g. --color) |
target? | MaybeComputedElementRef | Element to read/write the variable on (defaults to documentElement) |
options? | UseCssVarOptions | initialValue, observe, and a configurable window |
Returns
UseCssVarReturnA writable ref; reading returns the current value, writing updates the property