# All files SegmentedButton.tsx

0% Statements 0/181
0% Branches 0/1
0% Functions 0/1
0% Lines 0/181

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

     "use client";
    import type { ButtonHTMLAttributes, 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 { segmentedButton } from "./segmentedButtonStyles.js";

    /**
     * @remarks \@since 6.0.0
     */
    export interface SegmentedButtonProps
      extends ButtonHTMLAttributes<HTMLButtonElement> {
      /**
       * Set this to `true` to apply selected styles and an optional
       * {@link selectedIcon}
       *
       * @defaultValue `false`
       */
      selected?: boolean;

      /** @defaultValue `useIcon("selected")` */
      selectedIcon?: ReactNode;

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

      /**
       * Set this to `true` to not render the {@link selectedIcon} when
       * {@link selected} is `true`.
       *
       * @defaultValue `false`
       */
      disableSelectedIcon?: boolean;

      /**
       * Set this to `true` to disable the {@link selectedIcon} enter/exit
       * transition and instead just use `display: none`.
       *
       * @defaultValue `false`
       */
      disableSelectedTransition?: boolean;

      /**
       * An optional addon to render before the {@link children} and after the
       * {@link selectedIcon}. This is only useful when rendering text children so
       * it can appear above the interaction states.
       */
      leftAddon?: ReactNode;

      /**
       * An optional addon to render after the {@link children}. This is only useful
       * when rendering text children so it can appear above the interaction states.
       */
      rightAddon?: ReactNode;
    }

    /**
     * **Client Component**
     *
     * @example
     * Simple Example
     * ```tsx
     * import {
     *   SegmentedButton,
     *   SegmentedButtonContainer,
     * } from "@react-md/core";
     * import type { ReactElement } from "react";
     * import { useState } from "react";
     *
     * function Example(): ReactElement {
     *   const [value, setValue] = useState("a");
     *   return (
     *     <SegmentedButtonContainer>
     *       <SegmentedButton
     *         onClick={() => setValue("a")}
     *         selected={value === "a"}
     *       >
     *         First
     *       </SegmentedButton>
     *       <SegmentedButton
     *         onClick={() => setValue("b")}
     *         selected={value === "b"}
     *       >
     *         Second
     *       </SegmentedButton>
     *       <SegmentedButton
     *         onClick={() => setValue("c")}
     *         selected={value === "c"}
     *         disableSelectedIcon
     *       >
     *         Third
     *       </SegmentedButton>
     *     </SegmentedButtonContainer>
     *   );
     * }
     * ```
     *
     * @remarks \@since 6.0.0
     */
    export const SegmentedButton = forwardRef<
      HTMLButtonElement,
      SegmentedButtonProps
    >(function SegmentedButton(props, ref) {
      const {
        className,
        type = "button",
        leftAddon,
        rightAddon,
        children: propChildren,
        selected,
        selectedIcon: propSelectedIcon,
        selectedClassName,
        disableSelectedIcon,
        disableSelectedTransition,
        onBlur,
        onClick,
        onKeyDown,
        onKeyUp,
        onMouseDown,
        onMouseUp,
        onMouseLeave,
        onDragStart,
        onTouchStart,
        onTouchEnd,
        onTouchMove,
        disabled,
        ...remaining
      } = props;

      const children = useHigherContrastChildren(propChildren);
      const selectedIconNode = useIcon("selected", propSelectedIcon);
      const selectedIcon = useMaxWidthTransition({
        element: selectedIconNode,
        transitionIn: !!selected,
        disabled: disableSelectedIcon,
        disableTransition: disableSelectedTransition,
      });
      const { pressedClassName, handlers, rippleContainerProps } =
        useElementInteraction({
          onBlur,
          onClick,
          onKeyDown,
          onKeyUp,
          onMouseDown,
          onMouseUp,
          onMouseLeave,
          onDragStart,
          onTouchStart,
          onTouchEnd,
          onTouchMove,
          disabled,
        });

      return (
        <button
          {...remaining}
          {...handlers}
          aria-pressed={selected}
          ref={ref}
          type={type}
          disabled={disabled}
          className={segmentedButton({
            className,
            selected,
            selectedClassName,
            pressedClassName,
          })}
        >
          {!disableSelectedIcon && selectedIcon}
          {leftAddon}
          {children}
          {rightAddon}
          {rippleContainerProps && <RippleContainer {...rippleContainerProps} />}
        </button>
      );
    });