# All files / src/menu MenuButton.tsx

100% Statements 135/135
100% Branches 16/16
100% Functions 4/4
100% Lines 135/135

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
    1361x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    18x
    18x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    55x
    55x
    55x
    55x
    55x
    55x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    15x
    15x
    15x
    15x
    15x
    58x
    3x
    3x
    3x
    3x
    1x
    1x
    1x
    1x
    1x
    3x
    1x
    1x
    1x
    1x
    1x
    3x
    3x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    1x
     "use client";
    import { forwardRef } from "react";
    import { Button, type ButtonProps } from "../button/Button.js";
    import { getIcon } from "../icon/iconConfig.js";
    import { IconRotator, type IconRotatorProps } from "../icon/IconRotator.js";
    import { type TextIconSpacingProps } from "../icon/TextIconSpacing.js";
    import { useAppSize } from "../media-queries/AppSizeProvider.js";
    import { useEnsuredId } from "../useEnsuredId.js";
    import { useMenuConfiguration } from "./MenuConfigurationProvider.js";
    import { useMenuVisibility } from "./MenuVisibilityProvider.js";

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

    /** @remarks \@since 5.0.0 */
    export type MenuButtonTextIconSpacingProps = Pick<
      TextIconSpacingProps,
      "icon" | "iconAfter"
    >;

    /** @remarks \@since 5.0.0 */
    export type MenuButtonIconRotatorProps = Omit<
      IconRotatorProps,
      "children" | "rotated"
    >;

    /**
     * @remarks \@since 5.0.0
     * @remarks \@since 6.0.0 No longer extends the {@link ButtonProps}, no longer
     * requires an `id`, and no longer supports `textIconSpacingProps`.
     */
    export interface BaseMenuButtonProps extends MenuButtonTextIconSpacingProps {
      /**
       * Any additional props to pass to the {@link IconRotator} component that
       * surrounds the {@link buttonChildren}
       */
      iconRotatorProps?: Readonly<MenuButtonIconRotatorProps>;

      /**
       * Boolean if the dropdown icon should be included with the button children.
       *
       * @defaultValue `buttonType === "icon"`
       */
      disableDropdownIcon?: boolean;
    }

    /**
     * @remarks \@since 5.0.0
     * @remarks \@since 6.0.0 See {@link BaseMenuButtonProps} for breaking changes.
     */
    export interface MenuButtonProps extends ButtonProps, BaseMenuButtonProps {}

    /**
     * **Client Component**
     *
     * An internal component that handles rendering a button for the `DropdownMenu`
     * while implementing the correct accessibility and keyboard movement.
     *
     * @remarks \@since 5.0.0
     * @remarks \@since 6.0.0 See {@link BaseMenuButtonProps} for breaking changes.
     */
    export const MenuButton = forwardRef<HTMLButtonElement, MenuButtonProps>(
      function MenuButton(props, ref) {
        const {
          id: propId,
          onClick = noop,
          onKeyDown = noop,
          icon: propIcon,
          iconAfter = true,
          iconRotatorProps,
          floating,
          buttonType = floating ? "icon" : "text",
          disableDropdownIcon = buttonType === "icon",
          children,
          ...remaining
        } = props;
        const id = useEnsuredId(propId, "menubutton");
        const { visible, setVisible, defaultFocusIndex } = useMenuVisibility();
        const { renderAsSheet } = useMenuConfiguration();
        const { isPhone } = useAppSize();
        const isSheet =
          renderAsSheet === true || (renderAsSheet === "phone" && isPhone);

        const dropdownIcon = getIcon("dropdown", propIcon);
        let icon = propIcon;
        if (!disableDropdownIcon) {
          icon = (
            <IconRotator {...iconRotatorProps} rotated={visible}>
              {dropdownIcon}
            </IconRotator>
          );
        }

        return (
          <Button
            {...remaining}
            aria-haspopup={isSheet ? "dialog" : "menu"}
            aria-expanded={visible || undefined}
            id={id}
            ref={ref}
            floating={floating}
            buttonType={buttonType}
            onClick={(event) => {
              onClick(event);

              defaultFocusIndex.current = 0;
              setVisible((prevVisible) => !prevVisible);
            }}
            onKeyDown={(event) => {
              onKeyDown(event);

              switch (event.key) {
                case "ArrowDown":
                  event.preventDefault();
                  event.stopPropagation();
                  defaultFocusIndex.current = 0;
                  setVisible(true);
                  break;
                case "ArrowUp":
                  event.preventDefault();
                  event.stopPropagation();
                  defaultFocusIndex.current = -1;
                  setVisible(true);
                  break;
              }
            }}
          >
            {!iconAfter && icon}
            {children}
            {iconAfter && icon}
          </Button>
        );
      }
    );