# All files / src/icon styles.ts

100% Statements 167/167
100% Branches 9/9
100% Functions 2/2
100% Lines 167/167

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
    1681x
    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
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    13x
    13x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    956x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    508x
    508x
    508x
    508x
    508x
    508x
    508x
    508x
    508x
    508x
     import { cnb } from "cnbuilder";
    import { cssUtils, type TextColor, type ThemeColor } from "../cssUtils.js";
    import { bem } from "../utils/bem.js";
    import type { MaterialIconFamily, MaterialSymbolFamily } from "./material.js";

    declare module "react" {
      interface CSSProperties {
        "--rmd-icon-color"?: string;
        "--rmd-icon-size"?: string | number;
        "--rmd-icon-spacing"?: string | number;
        "--rmd-icon-rotate-from"?: string | number;
        "--rmd-icon-rotate-to"?: string | number;
      }
    }

    const styles = bem("rmd-icon");
    const rotatorStyles = bem("rmd-icon-rotator");

    /** @remarks \@since 6.0.0 */
    export interface SVGIconClassNameOptions {
      className?: string;

      /**
       * An optional theme color to apply to the icon. When this is `undefined`, the
       * default icon color will be used instead.
       *
       * - `primary -> $primary-color`
       * - `secondary -> $secondary-color`
       * - `warning -> $warning-color`
       * - `success -> $success-color`
       * - `error -> $error-color`
       * - `text-primary -> $text-primary-color`
       * - `text-secondary -> $text-primary-color`
       * - `text-hint -> $text-hint-color`
       * - `text-disabled -> $text-disabled-color`
       */
      theme?: ThemeColor | TextColor;

      /**
       * Boolean if the font icon should use the dense spec.
       *
       * @defaultValue `false`
       */
      dense?: boolean;

      /**
       * Set this to `true` if the icon should display inline with other text (like
       * a paragraph) by applying `vertical-align: bottom`.
       *
       * @example
       * ```tsx
       * <Typography>
       *   <InfoIcon inline className="rmd-icon--before" />
       *   Some additional information around xyz.
       * </Typography>
       * ```
       *
       * @defaultValue `false`
       */
      inline?: boolean;
    }

    /** @remarks \@since 6.0.0 */
    export interface FontIconClassNameOptions extends SVGIconClassNameOptions {
      /**
       * The font icon class name to use.
       *
       * @defaultValue `"material-icons"`
       */
      iconClassName?: string;
    }

    /** @remarks \@since 6.0.0 */
    export interface MaterialIconClassNameOptions extends SVGIconClassNameOptions {
      family: MaterialIconFamily;
    }

    /** @remarks \@since 6.0.0 */
    export interface MaterialSymbolClassNameOptions
      extends SVGIconClassNameOptions {
      family: MaterialSymbolFamily;
    }

    /** @remarks \@since 6.0.0 */
    export type IconClassNameOptions =
      | ({ type: "font" } & FontIconClassNameOptions)
      | ({ type: "svg" } & SVGIconClassNameOptions)
      | ({ type: "material" } & MaterialIconClassNameOptions)
      | ({ type: "symbol" } & MaterialSymbolClassNameOptions);

    /**
     *
     * @remarks \@since 6.0.0
     */
    export function icon(options: IconClassNameOptions): string {
      const {
        className,
        type,
        theme,
        family = "",
        dense = false,
        inline = false,
        iconClassName,
      } = options as FontIconClassNameOptions &
        SVGIconClassNameOptions & {
          type: "font" | "svg" | "symbol" | "material";
          family?: MaterialIconFamily;
        };

      const isFont = type === "font";
      const isSvg = type === "svg";
      const isSymbol = type === "symbol";
      const isMaterial = type === "material";

      return cnb(
        styles({
          svg: isSvg,
          font: isFont || isMaterial,
          symbol: isSymbol,
          vab: inline,
          dense,
        }),
        isSymbol && `material-symbols-${family}`,
        isMaterial &&
          `material-icons${
            family === "filled" ? "" : `-${family === "rounded" ? "round" : family}`
          }`,
        cssUtils({
          textColor: theme,
        }),
        iconClassName,
        className
      );
    }

    /** @remarks \@since 6.0.0 */
    export interface IconRotatorClassNameOptions {
      className?: string;

      /**
       * Boolean if the icon is currently rotated.
       */
      rotated: boolean;

      /**
       * Boolean if changing the {@link rotated} state should no longer transition.
       *
       * @defaultValue `false`
       */
      disableTransition?: boolean;
    }

    /**
     *
     * @remarks \@since 6.0.0
     */
    export function iconRotator(options: IconRotatorClassNameOptions): string {
      const { className, rotated, disableTransition = false } = options;

      return cnb(
        rotatorStyles({
          animate: !disableTransition,
          rotated,
        }),
        className
      );
    }