# All files / chip Chip.tsx

100% Statements 329/329
100% Branches 19/19
100% Functions 1/1
100% Lines 329/329

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
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    3301x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    39x
    39x
    39x
    39x
    39x
    39x
    41x
    41x
    41x
    40x
    40x
    40x
    40x
    40x
    40x
    40x
    40x
    40x
    40x
    40x
    40x
    40x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    25x
    25x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    21x
    6x
    21x
    15x
    15x
    21x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    41x
    1x
     import { cnb } from "cnbuilder";
    import type {
      ButtonHTMLAttributes,
      CSSProperties,
      HTMLAttributes,
      ReactNode,
    } from "react";
    import { forwardRef } from "react";
    import { useIcon } from "../icon/IconProvider.js";
    import { RippleContainer } from "../interaction/RippleContainer.js";
    import { useElementInteraction } from "../interaction/useElementInteraction.js";
    import { useHigherContrastChildren } from "../interaction/useHigherContrastChildren.js";
    import { useMaxWidthTransition } from "../transition/useMaxWidthTransition.js";
    import { PropsWithRef } from "../types.js";
    import { chip, chipContent } from "./styles.js";

    declare module "react" {
      interface CSSProperties {
        "--rmd-chip-solid-background-color"?: string;
        "--rmd-chip-solid-disabled-background-color"?: string;
        "--rmd-chip-solid-color"?: string;
        "--rmd-chip-theme-background-color"?: string;
        "--rmd-chip-theme-color"?: string;
        "--rmd-chip-outline-background-color"?: string;
        "--rmd-chip-outline-color"?: string;
      }
    }

    /**
     * @remarks \@since 6.0.0 Renamed the `noninteractive` prop to
     * `noninteractable`.
     */
    export interface ChipProps extends ButtonHTMLAttributes<HTMLButtonElement> {
      /**
       * @defaultValue `"solid"`
       */
      theme?: "outline" | "solid";

      /**
       * Set this to `true` if the chip should gain additional box shadow while the
       * user is pressing down on the chip.
       *
       * @defaultValue `false`
       */
      raisable?: boolean;

      /**
       * @defaultValue `false`
       */
      disabled?: boolean;

      /**
       * When this is a boolean, a selected icon will be displayed before the
       * `children` and appear/disappear when `true`/`false`.
       *
       * @see {@link selectedIcon}
       * @see {@link selectedIconAfter}
       * @see {@link selectedThemed}
       */
      selected?: boolean;

      /**
       * Set this to `true` if the chip should change background color while
       * {@link selected} instead of displaying an icon. The default background
       * color is a lighter swatch of the theme primary color.
       *
       * @defaultValue `false`
       */
      selectedThemed?: boolean;

      /**
       * An optional className to provide only while {@link selected} is `true`.
       */
      selectedClassName?: string;

      /**
       * This will be ignored if {@link selected} is `undefined`.
       *
       * @defaultValue `useIcon("selected")`
       */
      selectedIcon?: ReactNode;

      /**
       * Set this to `true` if the {@link selectedIcon} should display as the
       * {@link rightAddon} instead of the {@link leftAddon}.
       *
       * @defaultValue `false`
       * @remarks \@since 6.0.0
       */
      selectedIconAfter?: boolean;

      /**
       * Set this to `true` if the {@link selectedIcon} should not animate and
       * instead apply `display: none`
       *
       * @defaultValue `false`
       */
      disableIconTransition?: boolean;

      /**
       * Set this to `true` to render the chip as a `<span>` instead of a button.
       *
       * @remarks
       * \@since 2.6.0
       * \@since 6.0.0 Renamed from `noninteractable`
       * @defaultValue `false`
       */
      noninteractive?: boolean;

      /**
       * An optional icon, avatar, circular progress, or custom component to render
       * before the `children`. This will remove some of the leading horizontal
       * padding on the chip as well.
       */
      leftAddon?: ReactNode;

      /**
       * An optional icon, avatar, circular progress, or custom component to render
       * after the `children`. This will remove some of the trailing horizontal
       * padding on the chip as well.
       */
      rightAddon?: ReactNode;

      /**
       * Set this to true if the `children` should not be wrapped in a span to apply
       * the {@link chipContent} styles which allow for shrinking text and
       * truncating text with ellipsis.
       *
       * @defaultValue `false`
       */
      disableContentWrap?: boolean;

      /**
       * This will be ignored if {@link disableContentWrap} is `true`.
       *
       * @remarks \@since 6.0.0
       */
      contentProps?: PropsWithRef<HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>;

      /**
       * This will be ignored if {@link disableContentWrap} is `true`.
       */
      contentStyle?: CSSProperties;

      /**
       * This will be ignored if {@link disableContentWrap} is `true`.
       */
      contentClassName?: string;
    }

    /**
     * **Server Component**
     *
     * @example
     * Simple Example
     * ```tsx
     * import { Chip } from "@react-md/core";
     * import type { ReactElement } from "react";
     *
     * function Example(): ReactElement {
     *   return <Chip>Content</Chip>;
     * }
     * ```
     *
     * @example
     * Outlined Example
     * ```tsx
     * import { Chip } from "@react-md/core";
     * import type { ReactElement } from "react";
     *
     * function Example(): ReactElement {
     *   return <Chip theme="outline">Content</Chip>;
     * }
     * ```
     *
     * @example
     * Addons Example
     * ```tsx
     * import { Avatar, Chip } from "@react-md/core";
     * import AddCircleIcon from "@react-md/material-icons/AddCircleIcon";
     * import type { ReactElement } from "react";
     *
     * function Example(): ReactElement {
     *   return (
     *     <Chip
     *       leftAddon={
     *         <Avatar>
     *           <img src="https://i.pravatar.cc/40?img=3" alt="" />
     *         </Avatar>
     *       }
     *       rightAddon={<AddCircleIcon />}
     *     >
     *       Chip
     *     </Chip>
     *   );
     * }
     * ```
     *
     * @remarks \@since 6.0.0 Renamed the `noninteractable` prop to
     * `noninteractive`.
     */
    export const Chip = forwardRef<HTMLButtonElement, ChipProps>(
      function Chip(props, ref) {
        const {
          "aria-pressed": ariaPressed,
          theme = "solid",
          className,
          raisable = false,
          disabled = false,
          selected,
          selectedThemed = false,
          selectedClassName,
          selectedIcon: propSelectedIcon,
          selectedIconAfter = false,
          noninteractive = false,
          disableIconTransition = false,
          children: propChildren,
          onClick,
          onKeyDown,
          onKeyUp,
          onMouseDown,
          onMouseUp,
          onMouseLeave,
          onDragStart,
          onTouchStart,
          onTouchEnd,
          onTouchMove,
          leftAddon: propLeftAddon,
          rightAddon: propRightAddon,
          contentProps,
          contentStyle,
          contentClassName,
          disableContentWrap = false,
          ...remaining
        } = props;

        let buttonProps: ButtonHTMLAttributes<HTMLButtonElement> | undefined;
        if (!noninteractive) {
          buttonProps = {
            "aria-pressed": ariaPressed ?? (!!selected || undefined),
            type: "button",
            disabled,
          };
        }

        let content = propChildren;
        if (!disableContentWrap) {
          content = (
            <span
              style={contentStyle}
              {...contentProps}
              className={cnb(
                chipContent({ className: contentClassName }),
                contentProps?.className
              )}
            >
              {propChildren}
            </span>
          );
        }

        let leftAddon = propLeftAddon;
        let rightAddon = propRightAddon;
        const selectedIconNode = useIcon("selected", propSelectedIcon);
        const isTransitionable =
          !selectedThemed &&
          typeof selected === "boolean" &&
          typeof (selectedIconAfter ? propRightAddon : propLeftAddon) ===
            "undefined";
        const selectedIcon = useMaxWidthTransition({
          element: selectedIconNode,
          transitionIn: !!selected,
          disabled: !isTransitionable,
          disableTransition: disableIconTransition,
        });
        if (isTransitionable) {
          if (selectedIconAfter) {
            rightAddon = selectedIcon;
          } else {
            leftAddon = selectedIcon;
          }
        }

        const { pressed, pressedClassName, rippleContainerProps, handlers } =
          useElementInteraction({
            onClick,
            onKeyDown,
            onKeyUp,
            onMouseDown,
            onMouseUp,
            onMouseLeave,
            onDragStart,
            onTouchStart,
            onTouchEnd,
            onTouchMove,
            disabled: disabled || noninteractive,
          });
        const children = useHigherContrastChildren(content);
        const Component = noninteractive ? "span" : "button";

        return (
          <Component
            {...remaining}
            {...buttonProps}
            {...handlers}
            className={chip({
              className,
              theme,
              pressed: raisable && pressed,
              disabled,
              selected,
              selectedThemed,
              selectedClassName,
              noninteractive,
              pressedClassName,
              leftAddon: !!leftAddon && (selectedIconAfter || selected !== false),
              rightAddon:
                !!rightAddon && (!selectedIconAfter || selected !== false),
            })}
            ref={ref}
          >
            {leftAddon}
            {children}
            {rightAddon}
            {rippleContainerProps && <RippleContainer {...rippleContainerProps} />}
          </Component>
        );
      }
    );