# All files / src/button TooltippedButton.tsx

100% Statements 120/120
100% Branches 3/3
100% Functions 2/2
100% Lines 120/120

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
    1211x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    4x
    4x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
     "use client";
    import { forwardRef, type ReactNode } from "react";
    import { Tooltip, type TooltipProps } from "../tooltip/Tooltip.js";
    import {
      useTooltip,
      type ProvidedTooltipProps,
      type TooltipOptions,
      type TooltippedElementEventHandlers,
    } from "../tooltip/useTooltip.js";
    import { Button, type ButtonProps } from "./Button.js";
    import { type ButtonType } from "./buttonStyles.js";

    const noop = (): void => {
      // do nothing
    };

    /**
     * @remarks \@since 6.0.0
     */
    export interface TooltippedButtonProps extends ButtonProps {
      /** @defaultValue `"icon"` */
      buttonType?: ButtonType;

      /**
       * The tooltip children to render. When this is falsey, the tooltip event
       * listeners will not be enabled and the tooltip will never display.
       */
      tooltip?: ReactNode;

      /**
       * Any additional props to pass to the `Tooltip` component (normally styling
       * props).
       */
      tooltipProps?: Omit<TooltipProps, keyof ProvidedTooltipProps>;

      /**
       * Any additional tooltip options to pass to {@link useTooltip}. The most
       * common options would be:
       *
       * ```ts
       * tooltipOptions={{
       *   overflowOnly: true,
       *
       *   // whatever values you want for these
       *   hoverTimeout: 0,
       *   leaveTimeout: 150,
       *   defaultPosition: "left",
       * }}
       * ```
       */
      tooltipOptions?: Omit<TooltipOptions, keyof TooltippedElementEventHandlers>;
    }

    /**
     * **Client Component**
     *
     * A simple wrapper around the `Button` and `Tooltip` components to dynamically
     * add tooltips to buttons. The `buttonType` will default to `icon` instead of
     * `text`.
     *
     * @example
     * ```tsx
     * import { TooltippedButton } from "@react-md/core";
     * import FavoriteIcon from "@react-md/material-icons/FavoriteIcon";
     *
     * export default function Example(): ReactElement {
     *   return (
     *     <TooltippedButton tooltip="I am a tooltip!" aria-label="Favorite">
     *       <FavoriteIcon />
     *     </TooltippedButton>
     *   );
     * }
     * ```
     *
     * @remarks \@since 6.0.0
     */
    export const TooltippedButton = forwardRef<
      HTMLButtonElement,
      TooltippedButtonProps
    >(function TooltippedButton(props, ref) {
      const {
        tooltip,
        tooltipProps,
        tooltipOptions,
        buttonType = "icon",
        onBlur = noop,
        onFocus = noop,
        onMouseEnter = noop,
        onMouseLeave = noop,
        onTouchStart = noop,
        onTouchEnd = noop,
        onContextMenu = noop,
        ...remaining
      } = props;
      const { tooltipProps: providedTooltipProps, elementProps } = useTooltip({
        ...tooltipOptions,
        disabled: !tooltip || tooltipOptions?.disabled,
        onBlur,
        onFocus,
        onMouseEnter,
        onMouseLeave,
        onTouchEnd,
        onTouchStart,
        onContextMenu,
      });

      return (
        <>
          <Button
            {...elementProps}
            {...remaining}
            ref={ref}
            buttonType={buttonType}
          />
          <Tooltip {...providedTooltipProps} {...tooltipProps}>
            {tooltip}
          </Tooltip>
        </>
      );
    });