# All files / src cssUtils.ts

100% Statements 234/234
91.42% Branches 32/35
100% Functions 1/1
100% Lines 234/234

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
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    2351x
    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
    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
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5284x
    5272x
    5271x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    166x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
    5285x
     import { cnb } from "cnbuilder";

    /**
     * - `primary -> $primary-color`
     * - `secondary -> $secondary-color`
     * - `warning -> $warning-color`
     * - `success -> $success-color`
     * - `error -> $error-color`
     *
     * @remarks \@since 6.0.0
     */
    export type ThemeColor =
      | "primary"
      | "secondary"
      | "warning"
      | "success"
      | "error";

    /**
     * - `text-primary -> $text-primary-color`
     * - `text-secondary -> $text-secondary-color`
     * - `text-hint -> $text-hint-color`
     * - `text-disabled -> $text-disabled-color`
     *
     * @remarks \@since 6.0.0
     */
    export type TextColor =
      | "text-primary"
      | "text-secondary"
      | "text-hint"
      | "text-disabled"
      | `on-${ThemeColor}`;

    /**
     * @remarks \@since 6.0.0
     */
    export type OutlineColor = ThemeColor | "greyscale" | "current-color";

    /**
     * @remarks \@since 6.0.0
     */
    export type BackgroundColor = ThemeColor | "surface" | "current-color";

    export type TextAlign = "left" | "center" | "right";
    export type TextDecoration = "underline" | "overline" | "line-through" | "none";
    export type TextTransform = "capitalize" | "uppercase" | "lowercase";
    export type FontStyle = "italic" | "oblique" | "normal";

    /**
     * The supported css `font-weight` values.
     *
     * Note: You will need to ensure that you are using a web safe font for all the
     * font weights to work, use variable fonts, or load all font weights for your
     * custom font.
     *
     * @see {@link https://www.google.com/search?q=web+safe+fonts | Web Safe Fonts}
     * @see {@link https://www.google.com/search?q=variable+fonts | Variable FOnts}
     * @remarks \@since 6.0.0 This was `TextWeight` beforehand.
     */
    export type FontWeight =
      | "thin"
      | "light"
      | "regular"
      | "medium"
      | "bold"
      | "semi-bold"
      | "black";

    /**
     * Since the typography within react-md tries to not modify base elements, the
     * default margin applied to heading tags (h1-h6) and paragraph (p) might have
     * large margin that you don't want applied when using this component. You can
     * disable:
     *
     * - only the top margin by setting this prop to `"bottom"`
     * - only the bottom margin by setting this prop to `"top"`
     * - top and bottom margin by setting this prop to `"none"`
     * - or keep the initial behavior: `"initial"`
     *
     * @remarks \@since 6.0.0
     */
    export type Margin = "none" | "top" | "bottom" | "centered";

    /** @remarks \@since 6.0.0 */
    export type TextOverflow = "allow" | "nowrap" | "ellipsis";

    /** @remarks \@since 6.0.0 */
    export interface TextCssUtilsOptions {
      textColor?: ThemeColor | TextColor;

      textAlign?: TextAlign;

      textDecoration?: TextDecoration;

      textTransform?: TextTransform;

      fontWeight?: FontWeight;

      fontStyle?: FontStyle;

      /**
       * This can be used to remove margin from an element (usually typography
       * elements since the browser applies default margin for those elements).
       *
       * @see {@link Margin}
       */
      margin?: Margin;

      /**
       * Set this to `"nowrap"` to only prevent line wrap behavior or `"ellipsis"`
       * to also hide additional text with ellipsis.
       *
       * @defaultValue `"allow"`
       */
      textOverflow?: TextOverflow;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface CssUtilsOptions extends TextCssUtilsOptions {
      className?: string;

      /**
       * Set this to `true` to allow the content to only be visible for screen
       * readers. Set this to `"focusable"` to allow the content to be visible to
       * screen readers and once focused. Set this to `"phone"` to only render the
       * the content as screen reader only text on phones.
       *
       * @defaultValue `false`
       */
      srOnly?: boolean | "focusable" | "phone";

      /**
       * Set this to `"current-color"` to inherit the current text color or a
       * specific theme color as the outline `box-shadow`.
       */
      outlineColor?: OutlineColor;

      /**
       * When this is defined, a specific theme background-color will be applied
       * along with updating the text color be either `#000` or `#fff` depending on
       * which has the highest contrast ratio.
       */
      backgroundColor?: BackgroundColor;

      /**
       * Set this to `true` if the element should act as an interaction surface
       * which will:
       * - display a pointer while hovered
       * - increase the z-index of children to enable higher contrast when
       *   `core.$interaction-disable-higher-contrast` is not `true`
       * - add a `::before` pseudo element that will:
       *   - gain the focus shadow while `:focus`-ed
       *   - add a background-color while `:hover`-ed
       *   - show no hover/focus styles while `:disabled` or `[aria-disabled="true"]`
       *
       * @defaultValue `false`
       */
      surface?: boolean;

      /**
       * Set this to `"light"` or `"dark"` to update the hover, focus, press,
       * selected, and ripple colors for the surface type. `"light"` surfaces will
       * use `#000` while `"dark"` will use `#fff`.
       */
      surfaceColor?: "light" | "dark";
    }

    /**
     * This is used to apply css utility functions to any element within your
     * application such as:
     * - specific theme colors as background, text, or outline color
     * - removing margin
     * - screen reader visible only text
     * - text-align, text-decoration, text-transform, font-style, font-weight
     * - disable line wrap
     *
     * @remarks \@since 6.0.0
     */
    export function cssUtils(options: CssUtilsOptions): string {
      const {
        className,
        srOnly,
        textColor,
        outlineColor,
        backgroundColor,
        surface,
        surfaceColor,
        margin,
        fontStyle,
        fontWeight,
        textAlign,
        textDecoration,
        textTransform,
        textOverflow,
      } = options;

      const isMarginTop = margin === "top";
      const isTextColor =
        textColor === "text-primary" ||
        textColor === "text-secondary" ||
        textColor === "text-hint" ||
        textColor === "text-disabled";

      return cnb(
        !backgroundColor && !isTextColor && textColor && `rmd-${textColor}-color`,
        !backgroundColor && isTextColor && `rmd-${textColor}-color`,
        backgroundColor && "rmd-background-container",
        backgroundColor &&
          backgroundColor !== "current-color" &&
          `rmd-${backgroundColor}-container`,
        outlineColor && "rmd-outline",
        outlineColor && `rmd-${outlineColor}-outline`,
        (isMarginTop || margin === "bottom") &&
          `rmd-no-margin-${isMarginTop ? "bottom" : "top"}`,
        margin === "none" && "rmd-no-margin",
        margin === "centered" && "rmd-centered",
        textAlign && `rmd-align-${textAlign}`,
        textDecoration && `rmd-${textDecoration}`,
        textTransform && `rmd-${textTransform}`,
        fontStyle && `rmd-${fontStyle}`,
        fontWeight && `rmd-${fontWeight}`,
        srOnly && srOnly !== "phone" && "rmd-sr-only",
        srOnly === "phone" && "rmd-phone-sr-only",
        srOnly === "focusable" && "rmd-sr-only--focusable",
        surfaceColor === "light" && "rmd-light-surface",
        surfaceColor === "dark" && "rmd-dark-surface",
        textOverflow && textOverflow !== "allow" && "rmd-nowrap",
        textOverflow === "ellipsis" && "rmd-ellipsis",
        surface && "rmd-interaction-surface",
        className
      );
    }