# All files / src/menu MenuItemButton.tsx

95.15% Statements 157/165
88% Branches 22/25
100% Functions 7/7
95.15% Lines 157/165

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
    1661x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    42x
    42x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    104x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    8x
    8x
    8x
    2x
    2x
    6x
    6x
    6x
    6x

    8x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    132x
    7x
    7x
    7x
    7x
    7x
    132x
    16x
    16x
    16x
    16x

    16x
    2x
    1x
    1x
    1x
    1x
    2x
    16x
    16x
    132x
    11x
    11x
    2x
    2x
    9x
    9x
    9x
    9x
    132x
    8x
    8x
    1x
    1x
    7x
    7x
    7x
    132x
    132x
    132x
    132x
    132x
    1x
     "use client";
    import { forwardRef, useEffect } from "react";
    import { useHoverMode } from "../hoverMode/useHoverMode.js";
    import { IconRotator } from "../icon/IconRotator.js";
    import { getIcon } from "../icon/iconConfig.js";
    import { useUserInteractionMode } from "../interaction/UserInteractionModeProvider.js";
    import { useAppSize } from "../media-queries/AppSizeProvider.js";
    import { useEnsuredId } from "../useEnsuredId.js";
    import { type BaseMenuButtonProps } from "./MenuButton.js";
    import { useMenuConfiguration } from "./MenuConfigurationProvider.js";
    import { MenuItem, type MenuItemProps } from "./MenuItem.js";
    import { useMenuVisibility } from "./MenuVisibilityProvider.js";
    import { useMenuBarContext } from "./useMenuBarProvider.js";

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

    /**
     * @internal
     * @remarks \@since 5.0.0
     */
    export interface MenuItemButtonProps
      extends BaseMenuButtonProps,
        MenuItemProps {}

    /**
     * **Client Component**
     *
     * This is just an internal component that handles rendering a submenu as a
     * menuitem for a `DropdownMenu` with a conditional dropdown icon.
     *
     * @internal
     * @remarks \@since 5.0.0
     */
    export const MenuItemButton = forwardRef<HTMLLIElement, MenuItemButtonProps>(
      function MenuItemButton(props, ref) {
        const {
          id: propId,
          children,
          onClick = noop,
          onKeyDown = noop,
          onMouseEnter = noop,
          onMouseLeave = noop,
          rightAddon: propRightAddon,
          iconRotatorProps,
          disableDropdownIcon = typeof propRightAddon !== "undefined",
          ...remaining
        } = props;
        const { disabled } = props;

        const id = useEnsuredId(propId, "menuitem");
        const mode = useUserInteractionMode();
        const { renderAsSheet } = useMenuConfiguration();
        const { isPhone } = useAppSize();
        const isSheet =
          renderAsSheet === true || (renderAsSheet === "phone" && isPhone);
        const {
          root,
          menubar,
          activeId,
          enableHoverMode,
          disableHoverMode,
          startDisableTimer,
          clearDisableTimer,
          leaveTimeoutRef,
          hoverTimeoutRef,
        } = useMenuBarContext();
        const { visible, setVisible, defaultFocusIndex } = useMenuVisibility();
        const { startShowFlow, clearVisibilityTimeout } = useHoverMode({
          setVisible,
          enableHoverMode,
          disableHoverMode,
          startDisableTimer,
          leaveTimeoutRef,
          hoverTimeoutRef,
          clearDisableTimer,
        });
        const { horizontal } = useMenuConfiguration();

        useEffect(() => {
          setVisible(id === activeId);
        }, [activeId, defaultFocusIndex, id, menubar, setVisible]);

        const dropdownIcon = getIcon(root ? "dropdown" : "forward");
        let rightAddon = propRightAddon;
        if (!disableDropdownIcon) {
          rightAddon = (
            <IconRotator {...iconRotatorProps} rotated={visible}>
              {dropdownIcon}
            </IconRotator>
          );
        }

        const updateVisibility = (nextVisible: boolean, focusIndex = 0): void => {
          defaultFocusIndex.current = focusIndex;
          setVisible(nextVisible);
          if (!menubar) {
            return;
          }

          if (nextVisible) {
            enableHoverMode(id);
          } else {
            disableHoverMode();
          }
        };

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

              event.stopPropagation();
              updateVisibility(!visible);
            }}
            onKeyDown={(event) => {
              onKeyDown(event);

              switch (event.key) {
                case "ArrowDown":
                  if (horizontal || root) {
                    event.preventDefault();
                    event.stopPropagation();
                    updateVisibility(true);
                  }
                  break;
                case "ArrowRight":
                  if (!horizontal && !root) {
                    event.preventDefault();
                    event.stopPropagation();
                    updateVisibility(true);
                  }
                  break;
              }
            }}
            onMouseEnter={(event) => {
              onMouseEnter(event);
              if (mode === "touch" || disabled || !menubar) {
                return;
              }

              defaultFocusIndex.current = 0;
              startShowFlow(id);
            }}
            onMouseLeave={(event) => {
              onMouseLeave(event);
              if (mode === "touch" || disabled || !menubar) {
                return;
              }

              clearVisibilityTimeout();
            }}
          >
            {children}
          </MenuItem>
        );
      }
    );