Skip to main content
react-md

useCSSVariables

function useCSSVariables<Name extends CSSVariableName>(
  variables: ReadonlyCSSVariableList<Name>,
  rootNode?: RefObject<HTMLElement> | HTMLElement
): CSSVariablesProperties<Name> | undefined;

The useCSSVariables hook can be used to dynamically set CSS Variables (custom properties) on the root <html> or other elements.

Example Usage

import { purple500 } from "@react-md/core/theme/colors";
import { useCSSVariables } from "@react-md/core/theme/useCSSVariables";
import { contrastColor } from "@react-md/core/theme/utils";
import { useMemo } from "react";

function Example() {
  // Note: You should use `useMemo` so that the custom variables are not
  // added and removed during each render.
  useCSSVariables(
    useMemo(() => {
      return [
        {
          name: "--rmd-primary-color",
          value: purple500,
        },
        {
          name: "--rmd-on-primary-color",
          value: contrastColor(purple500),
        },
      ];
    }, [])
  );

  return null;
}

Parameters

Returns

Nothing.