# All files / menu MenuWidget.tsx

98.9% Statements 181/183
72.72% Branches 16/22
100% Functions 6/6
98.9% Lines 181/183

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
    1841x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    118x
    118x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    180x
    180x
    180x
    180x
    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
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    65x
    65x
    65x
    58x
    58x
    7x
    7x
    7x
    7x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    53x
    53x
    47x
    47x
    6x
    6x
    1x
    1x
    6x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    5x
    5x
    5x
    5x
    5x

    5x
    5x
    5x
    5x
    5x
    5x
    5x
    5x
    5x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    180x
    1x
     "use client";
    import { cnb } from "cnbuilder";
    import type { HTMLAttributes } from "react";
    import { forwardRef, useRef, useState } from "react";
    import { List } from "../list/List.js";
    import type { GetDefaultFocusedIndex } from "../movement/types.js";
    import { useKeyboardMovementProvider } from "../movement/useKeyboardMovementProvider.js";
    import { NonNullMutableRef } from "../types.js";
    import { bem } from "../utils/bem.js";
    import type { MenuListConvenienceProps } from "./Menu.js";
    import { MenuWidgetKeyboardProvider } from "./MenuWidgetKeyboardProvider.js";
    import {
      MenuBarProvider,
      useMenuBarContext,
      useMenuBarProvider,
    } from "./useMenuBarProvider.js";

    const styles = bem("rmd-menu");
    const noop = (): void => {
      // do nothing
    };

    /**
     * @remarks \@since 6.0.0
     */
    export interface MenuClassNameOptions {
      className?: string;
      horizontal?: boolean;
      elevated?: boolean;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export function menu(options: MenuClassNameOptions = {}): string {
      const { className, horizontal, elevated } = options;

      return cnb(styles({ horizontal, elevated }), className);
    }

    /**
     * @internal
     */
    export interface MenuWidgetProps
      extends HTMLAttributes<HTMLDivElement>,
        MenuListConvenienceProps {
      isSheet: boolean;
      horizontal: boolean;
      disableElevation?: boolean;
      cancelUnmountFocus: NonNullMutableRef<boolean>;
      getDefaultFocusedIndex?: GetDefaultFocusedIndex;
    }

    /**
     * **Client Component**
     *
     * This component was added to support the listbox role and the `useId()` hook.
     * If the `temporary` prop is set, the `MenuItem`'s ids will not be the same the
     * next time the menu opens, so the aria-activedescendant will point to a
     * non-existing id
     *
     * @internal
     */
    export const MenuWidget = forwardRef<HTMLDivElement, MenuWidgetProps>(
      function MenuWidget(props, ref) {
        const {
          id,
          role = "menu",
          className,
          listStyle,
          listClassName,
          listProps,
          children,
          onClick,
          onBlur = noop,
          onFocus = noop,
          onKeyDown = noop,
          tabIndex = -1,
          isSheet,
          horizontal,
          disableElevation,
          cancelUnmountFocus,
          getDefaultFocusedIndex,
          ...remaining
        } = props;
        const isListbox = role === "listbox";
        const { menubar } = useMenuBarContext();

        // Since there is the possibility of other tab focusable elements within the
        // sheet and the menu items are programmatically focused, the menu's
        // tabIndex needs to be set to `-1` while one of the child menu items are
        // focused. This allows Shift+Tab correctly focuses the previous focusable
        // element within the sheet. Since `onFocus` and `onBlur` will be bubbled up
        // to the menu widget each time a new MenuItem is focused, only disable the
        // focused state if the blur event is fired without another focus event
        // within an animation frame.
        const [sheetMenuFocused, setSheetMenuFocused] = useState(false);
        const sheetBlurredFame = useRef(0);
        const menuBarContext = useMenuBarProvider({
          root: false,
          menubar,
          hoverTimeout: menubar ? 0 : undefined,
          defaultActiveId: id,
        });
        const { movementProps, movementContext } = useKeyboardMovementProvider({
          onClick,
          onFocus(event) {
            onFocus(event);

            if (!isSheet) {
              return;
            }

            window.cancelAnimationFrame(sheetBlurredFame.current);
            setSheetMenuFocused(true);
          },
          onKeyDown,
          horizontal,
          loopable: true,
          searchable: true,
          programmatic: true,
          includeDisabled: true,
          getDefaultFocusedIndex,
        });

        return (
          <MenuWidgetKeyboardProvider disabled={isListbox} value={movementContext}>
            <MenuBarProvider value={menuBarContext}>
              <div
                aria-orientation={horizontal ? "horizontal" : undefined}
                {...remaining}
                {...(isListbox ? { onClick, onFocus, onKeyDown } : movementProps)}
                id={id}
                ref={ref}
                role={role}
                className={menu({
                  className,
                  elevated: !disableElevation && !isSheet,
                  horizontal,
                })}
                tabIndex={isSheet && !sheetMenuFocused ? 0 : tabIndex}
                onBlur={(event) => {
                  onBlur(event);
                  if (!isSheet) {
                    return;
                  }

                  sheetBlurredFame.current = window.requestAnimationFrame(() => {
                    setSheetMenuFocused(false);
                  });
                }}
              >
                <List
                  {...listProps}
                  style={listStyle ?? listProps?.style}
                  className={listClassName || listProps?.className}
                  horizontal={horizontal}
                  onClick={(event) => {
                    listProps?.onClick?.(event);

                    // this makes it so you can click on the menu/list without
                    // closing the menu
                    if (event.target === event.currentTarget) {
                      event.stopPropagation();
                    }

                    // This might be a test only workaround since clicking links move focus
                    // somewhere else
                    if (event.target instanceof HTMLElement) {
                      cancelUnmountFocus.current = event.currentTarget.contains(
                        event.target.closest("a")
                      );
                    }
                  }}
                >
                  {children}
                </List>
              </div>
            </MenuBarProvider>
          </MenuWidgetKeyboardProvider>
        );
      }
    );