MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useMenu

Function useMenu

packages/react-aria/src/menu/useMenu.ts:82–123  ·  view source on GitHub ↗
(
  props: AriaMenuOptions<T>,
  state: TreeState<T>,
  ref: RefObject<HTMLElement | null>
)

Source from the content-addressed store, hash-verified

80 * @param state - State for the menu, as returned by `useListState`.
81 */
82export function useMenu<T>(
83 props: AriaMenuOptions<T>,
84 state: TreeState<T>,
85 ref: RefObject<HTMLElement | null>
86): MenuAria {
87 let {shouldFocusWrap = true, onKeyDown, onKeyUp, ...otherProps} = props;
88 let {keyboardProps} = useKeyboard({onKeyDown, onKeyUp});
89
90 if (!props['aria-label'] && !props['aria-labelledby'] && process.env.NODE_ENV !== 'production') {
91 console.warn('An aria-label or aria-labelledby prop is required for accessibility.');
92 }
93
94 let domProps = filterDOMProps(props, {labelable: true});
95 let {listProps} = useSelectableList({
96 ...otherProps,
97 ref,
98 selectionManager: state.selectionManager,
99 collection: state.collection,
100 disabledKeys: state.disabledKeys,
101 shouldFocusWrap,
102 linkBehavior: 'override'
103 });
104
105 menuData.set(state, {
106 onClose: props.onClose,
107 onAction: props.onAction,
108 shouldUseVirtualFocus: props.shouldUseVirtualFocus
109 });
110
111 return {
112 menuProps: mergeProps(domProps, keyboardProps, {
113 role: 'menu',
114 ...listProps,
115 onKeyDown: e => {
116 // don't clear the menu selected keys if the user is presses escape since escape closes the menu
117 if (e.key !== 'Escape' || props.shouldUseVirtualFocus) {
118 listProps.onKeyDown?.(e);
119 }
120 }
121 })
122 };
123}

Callers 7

MenuFunction · 0.90
VirtualizedMenuFunction · 0.90
MenuPopupFunction · 0.90
MenuInnerFunction · 0.90
Menu.tsxFile · 0.90
MenuInnerFunction · 0.90

Calls 6

useKeyboardFunction · 0.90
filterDOMPropsFunction · 0.90
useSelectableListFunction · 0.90
mergePropsFunction · 0.90
onKeyDownMethod · 0.80
setMethod · 0.45

Tested by 3

MenuFunction · 0.72
VirtualizedMenuFunction · 0.72