useCssVar ​
Hook that returns the value of a css variable
utilities
test coverage
Last changed: last month
Installation ​
Library
CLI
Manual
typescript
import { useCssVar } from '@siberiacancode/reactuse';
Usage ​
typescript
const { ref, value, set } = useCssVar('--color', 'red');
// or
const { value, set } = useCssVar(ref, '--color', 'red');
Demo ​
Api ​
Parameters
Name | Type | Default | Note |
---|---|---|---|
key | string | - | The CSS variable key |
initialValue | string | - | The initial value of the CSS variable |
Returns
UseCssVarReturn & { ref: StateRef<Element> }
Parameters
Name | Type | Default | Note |
---|---|---|---|
target | HookTarget | - | The target element |
key | string | - | The CSS variable key |
initialValue | string | - | The initial value of the CSS variable |
Returns
UseCssVarReturn
Type declaration ​
typescript
import type { HookTarget } from '@/utils/helpers';
import type { StateRef } from '../useRefState/useRefState';
export interface UseCssVarReturn {
/** The value of the CSS variable */
value: string;
/** Set the value of the CSS variable */
set: (value: string) => void;
}
export interface UseCssVar {
(key: string, initialValue?: string): UseCssVarReturn & { ref: StateRef<Element> };
(target: HookTarget, key: string, initialValue?: string): UseCssVarReturn;
}
Source ​
Source • DemoContributors ​
D