# All files / src/form Label.tsx

100% Statements 118/118
100% Branches 13/13
100% Functions 2/2
100% Lines 118/118

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
    1191x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    1x
    454x
    9x
    453x
    2x
    2x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    454x
    1x
     import { cnb } from "cnbuilder";
    import { forwardRef } from "react";
    import { type TextColor, type ThemeColor, cssUtils } from "../cssUtils.js";
    import { bem } from "../utils/bem.js";
    import { type LabelClassNameOptions, type LabelProps } from "./types.js";

    declare module "react" {
      interface CSSProperties {
        "--rmd-label-floating-top"?: string | number;
        "--rmd-label-left-offset"?: string | number;
        "--rmd-label-top-offset"?: string | number;
        "--rmd-label-active-padding"?: string | number;
        "--rmd-label-active-background-color"?: string;
      }
    }

    const labelStyles = bem("rmd-label");

    /**
     * @remarks \@since 6.0.0
     */
    export function label(options: LabelClassNameOptions): string {
      const {
        className,
        gap = false,
        error = false,
        dense = false,
        active = false,
        stacked = false,
        reversed = false,
        disabled = false,
        floating = false,
        inactive = false,
        floatingActive = active,
      } = options;

      let textColor: TextColor | ThemeColor | undefined;
      if (disabled) {
        textColor = "text-disabled";
      } else if (error) {
        textColor = "error";
      } else if (floating && inactive) {
        textColor = "text-secondary";
      }

      return cnb(
        labelStyles({
          gap,
          error,
          dense,
          active,
          disabled,
          floating,
          stacked: stacked && !reversed,
          reversed: !stacked && reversed,
          "stacked-reversed": stacked && reversed,
          "floating-dense": floating && dense,
          "floating-active": floating && floatingActive,
        }),
        cssUtils({
          textColor,
        }),
        className
      );
    }

    /**
     * **Server Component**
     *
     * Most of the form components already use this `Label` internally when a
     * `label` prop has been provided. You should generally use this component if
     * you need to separate the label from an existing form component or you need to
     * create a custom implementation of a form component.
     *
     * @remarks \@since 6.0.0 Updated to be usable externally and combines the
     * floating label styles instead of having separate components.
     */
    export const Label = forwardRef<HTMLLabelElement, LabelProps>(
      function Label(props, ref) {
        const {
          gap = false,
          error = false,
          dense = false,
          active = false,
          stacked = false,
          reversed = false,
          disabled = false,
          floating = false,
          inactive = false,
          floatingActive = active,
          className,
          children,
          ...remaining
        } = props;

        return (
          <label
            ref={ref}
            {...remaining}
            className={label({
              gap,
              error,
              dense,
              active,
              stacked,
              reversed,
              disabled,
              floating,
              floatingActive,
              inactive,
              className,
            })}
          >
            {children}
          </label>
        );
      }
    );