Skip to content

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

NameTypeDefaultNote
keystring-The CSS variable key
initialValuestring-The initial value of the CSS variable

Returns

UseCssVarReturn & { ref: StateRef<Element> }

Parameters

NameTypeDefaultNote
targetHookTarget-The target element
keystring-The CSS variable key
initialValuestring-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 • Demo

Contributors ​

D
debabin
debabin

Released under the MIT License.