# All files / button Button.tsx

100% Statements 197/197
37.5% Branches 3/8
100% Functions 1/1
100% Lines 197/197

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
    1981x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    97x
    1x
     "use client";
    import { forwardRef, type ButtonHTMLAttributes } from "react";
    import { useElementInteraction } from "../interaction/useElementInteraction.js";
    import { useHigherContrastChildren } from "../interaction/useHigherContrastChildren.js";
    import { type PropsWithRef } from "../types.js";
    import {
      FloatingActionButton,
      type FloatingActionButtonPosition,
      type FloatingActionButtonProps,
    } from "./FloatingActionButton.js";
    import { button, type ButtonClassNameThemeOptions } from "./buttonStyles.js";

    export interface ButtonProps
      extends ButtonHTMLAttributes<HTMLButtonElement>,
        ButtonClassNameThemeOptions {
      /** @defaultValue `"button"` */
      type?: "button" | "reset" | "submit";

      /**
       * The position within the viewport to display the button as a floating action
       * button.
       */
      floating?: FloatingActionButtonPosition;

      /**
       * Any additional props to provide the to `FAB` container element when the
       * `floating` prop is provided
       */
      floatingProps?: PropsWithRef<FloatingActionButtonProps, HTMLSpanElement>;
    }

    /**
     * **Client Component**
     *
     * @example Simple Example
     * ```tsx
     * import { Button } from "@react-md/core";
     * import type { ReactElement } from "react";
     *
     * function Example(): ReactElement {
     *   return (
     *     <Button
     *       onClick={(event) => {
     *         // do something
     *       }}
     *     >
     *       Content
     *     </Button>
     *   );
     * }
     * ```
     *
     * @example Theme Example
     * ```tsx
     * import { Button } from "@react-md/core";
     * import type { ReactElement } from "react";
     *
     * function Example(): ReactElement {
     *   return (
     *     <Button
     *       theme="primary"
     *       themeType="contained"
     *       onClick={(event) => {
     *         // do something
     *       }}
     *     >
     *       Content
     *     </Button>
     *   );
     * }
     * ```
     *
     * @example Icon Button Example
     * ```tsx
     * import { Button } from "@react-md/core";
     * import { FavoriteSVGIcon } from "@react-md/material-icons";
     * import type { ReactElement } from "react";
     *
     * function Example(): ReactElement {
     *   return (
     *     <Button
     *       theme="secondary"
     *       themeType="outline"
     *       buttonType="icon"
     *       onClick={(event) => {
     *         // do something
     *       }}
     *     >
     *       <FavoriteSVGIcon />
     *     </Button>
     *   );
     * }
     * ```
     *
     * @example Text Button with icons
     * ```tsx
     * import { Button } from "@react-md/core";
     * import { FavoriteSVGIcon } from "@react-md/material-icons";
     * import type { ReactElement } from "react";
     *
     * function Example(): ReactElement {
     *   return (
     *     <Button
     *       onClick={(event) => {
     *         // do something
     *       }}
     *     >
     *       <FavoriteSVGIcon />
     *       Content
     *     </Button>
     *   );
     * }
     * ```
     */
    export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
      function Button(props, ref) {
        const {
          type = "button",
          disabled = false,
          floating = null,
          floatingProps,
          theme = floating ? "secondary" : "clear",
          themeType = floating ? "contained" : "flat",
          iconSize,
          buttonType = floating || iconSize ? "icon" : "text",
          className,
          responsive,
          children: propChildren,
          onBlur,
          onClick,
          onKeyDown,
          onKeyUp,
          onMouseDown,
          onMouseUp,
          onMouseLeave,
          onDragStart,
          onTouchStart,
          onTouchEnd,
          onTouchMove,
          ...remaining
        } = props;
        const isThemeDisabled = theme === "disabled";
        const ariaDisabled = props["aria-disabled"];
        const { pressed, pressedClassName, ripples, handlers } =
          useElementInteraction({
            onBlur,
            onClick,
            onKeyDown,
            onKeyUp,
            onMouseDown,
            onMouseUp,
            onMouseLeave,
            onDragStart,
            onTouchStart,
            onTouchEnd,
            onTouchMove,
            disabled:
              disabled ||
              isThemeDisabled ||
              (ariaDisabled && ariaDisabled !== "false"),
          });

        const children = useHigherContrastChildren(propChildren);

        return (
          <FloatingActionButton position={floating} {...floatingProps}>
            <button
              {...remaining}
              // when the theme is set to `"disabled"`, the event handlers should be
              // removed so that it behaves like a disabled button. you do not want to
              // actually set the `disabled` attribute since it will lose keyboard
              // focus. this is mostly for supporting circular progress bars within
              // buttons
              {...(isThemeDisabled ? {} : handlers)}
              aria-disabled={isThemeDisabled || remaining["aria-disabled"]}
              disabled={disabled}
              ref={ref}
              type={type}
              className={button({
                theme,
                themeType,
                buttonType,
                disabled,
                responsive,
                iconSize,
                pressed,
                pressedClassName,
                className,
              })}
            >
              {children}
              {ripples}
            </button>
          </FloatingActionButton>
        );
      }
    );