# All files / src/theme useCSSVariables.ts

96.64% Statements 173/179
85.71% Branches 18/21
100% Functions 1/1
96.64% Lines 173/179

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    1801x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    60x
    60x
    60x
    60x
    44x
    2x
    2x
    42x
    42x
    42x
    42x
    42x
    41x
    41x
    1x
    1x
    1x
    1x

    42x
    44x

    42x
    42x
    42x
    45x
    45x
    45x
    45x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    45x
    45x
    45x
    42x
    42x
    45x
    45x
    42x
    60x
    60x
    60x
    44x
    42x
    42x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    60x
    60x
     "use client";
    import type { RefObject } from "react";
    import { useEffect, useMemo } from "react";
    import type { CSSVariableName, DefinedCSSVariableName } from "./types.js";

    /**
     * @remarks \@since 6.0.0
     */
    export interface CSSVariable<
      Name extends CSSVariableName = DefinedCSSVariableName,
    > {
      name: Name;
      value: string | number;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export type CSSVariablesProperties<
      Name extends CSSVariableName = DefinedCSSVariableName,
    > = {
      [key in Name]?: string | number;
    };

    /**
     * @remarks \@since 6.0.0
     */
    export type ReadonlyCSSVariableList<
      Name extends CSSVariableName = DefinedCSSVariableName,
    > = readonly Readonly<CSSVariable<Name>>[];

    /**
     * @example
     * Applying Variables the root html element
     * ```ts
     * import {
     *   contrastColor,
     *   pinkAccent200,
     *   purple500,
     *   useCSSVariables,
     *  } from "@react-md/core";
     * import { useMemo } from "react";
     *
     * function Example(): null {
     *   // 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;
     * }
     * ```
     *
     * @remarks \@since 6.0.0
     */
    export function useCSSVariables<Name extends CSSVariableName>(
      variables: ReadonlyCSSVariableList<Name>,
      rootNode?: RefObject<HTMLElement> | HTMLElement
    ): void;
    /**
     * @example
     * Applying variables through inline styles
     * ```tsx
     * import {
     *   contrastColor,
     *   ReadonlyCSSVariableList,
     *   pinkAccent200,
     *   purple500,
     *   useCSSVariables,
     *  } from "@react-md/core";
     * import { useMemo } from "react";
     * import type { ReactElement, ReactNode } from "react";
     *
     * function Example({ children }: { children: ReactNode }): ReactElement {
     *   const customVariables = useMemo<ReadonlyCSSVariableList>(() => {
     *     return [
     *       {
     *         name: "--rmd-primary-color",
     *         value: purple500,
     *       },
     *       {
     *         name: "--rmd-on-primary-color",
     *         value: contrastColor(purple500),
     *       },
     *     ];
     *   }, []);
     *   const style = useCSSVariables(customVariables, true);
     *
     *   return <div style={style}>{children}</div>;
     * }
     * ```
     *
     * @remarks \@since 6.0.0
     */
    export function useCSSVariables<Name extends CSSVariableName>(
      variables: ReadonlyCSSVariableList<Name>,
      inlineStyle: true
    ): CSSVariablesProperties<Name>;
    /**
     * @remarks \@since 6.0.0
     */
    export function useCSSVariables<Name extends CSSVariableName>(
      variables: ReadonlyCSSVariableList<Name>,
      rootNodeOrInlineStyle?: RefObject<HTMLElement> | HTMLElement | boolean
    ): CSSVariablesProperties<Name> | void {
      useEffect(() => {
        if (rootNodeOrInlineStyle === true || !variables.length) {
          return;
        }

        // use an iife so that hoisting doesn't cause a possible "null" issue for
        // the root
        const root = (() => {
          if (!rootNodeOrInlineStyle) {
            return document.documentElement;
          }

          if ("current" in rootNodeOrInlineStyle) {
            return rootNodeOrInlineStyle.current;
          }

          return rootNodeOrInlineStyle;
        })();

        if (!root) {
          return;
        }

        // const root = document.documentElement;
        variables.forEach(({ name, value }) => {
          if (
            process.env.NODE_ENV !== "production" &&
            root.style.getPropertyValue(name)
          ) {
            const currentValue = root.style.getPropertyValue(name);
            const overwritten =
              currentValue !== value
                ? ` and will be overwritten to "${value}"`
                : "";
            // eslint-disable-next-line no-console
            console.warn(
              `The "${name}" css variable has already been set to "${currentValue}" ` +
                `on the root element${overwritten}. There might be conflicting overrides.`
            );
          }

          root.style.setProperty(name, `${value}`);
        });
        return () => {
          variables.forEach(({ name }) => {
            root.style.removeProperty(name);
          });
        };
      }, [variables, rootNodeOrInlineStyle]);

      return useMemo(() => {
        if (rootNodeOrInlineStyle !== true) {
          return;
        }

        return variables.reduce<CSSVariablesProperties<Name>>(
          (style, { name, value }) => {
            style[name] = value;
            return style;
          },
          {}
        );
      }, [rootNodeOrInlineStyle, variables]);
    }