UNPKG

2.87 kBJavaScriptView Raw
1'use client';
2
3import _extends from "@babel/runtime/helpers/esm/extends";
4import * as React from 'react';
5import { unstable_useForkRef as useForkRef } from '@mui/utils';
6import { DropdownContext } from '../useDropdown/DropdownContext';
7import { DropdownActionTypes } from '../useDropdown/useDropdown.types';
8import { useButton } from '../useButton/useButton';
9import { combineHooksSlotProps } from '../utils/combineHooksSlotProps';
10import { extractEventHandlers } from '../utils';
11
12/**
13 *
14 * Demos:
15 *
16 * - [Menu](https://mui.com/base-ui/react-menu/#hooks)
17 *
18 * API:
19 *
20 * - [useMenuButton API](https://mui.com/base-ui/react-menu/hooks-api/#use-menu-button)
21 */
22export function useMenuButton(parameters = {}) {
23 const {
24 disabled = false,
25 focusableWhenDisabled,
26 rootRef: externalRef
27 } = parameters;
28 const menuContext = React.useContext(DropdownContext);
29 if (menuContext === null) {
30 throw new Error('useMenuButton: no menu context available.');
31 }
32 const {
33 state,
34 dispatch,
35 registerTrigger,
36 popupId
37 } = menuContext;
38 const {
39 getRootProps: getButtonRootProps,
40 rootRef: buttonRootRef,
41 active
42 } = useButton({
43 disabled,
44 focusableWhenDisabled,
45 rootRef: externalRef
46 });
47 const handleRef = useForkRef(buttonRootRef, registerTrigger);
48 const createHandleClick = otherHandlers => event => {
49 var _otherHandlers$onClic;
50 (_otherHandlers$onClic = otherHandlers.onClick) == null || _otherHandlers$onClic.call(otherHandlers, event);
51 if (event.defaultMuiPrevented) {
52 return;
53 }
54 dispatch({
55 type: DropdownActionTypes.toggle,
56 event
57 });
58 };
59 const createHandleKeyDown = otherHandlers => event => {
60 var _otherHandlers$onKeyD;
61 (_otherHandlers$onKeyD = otherHandlers.onKeyDown) == null || _otherHandlers$onKeyD.call(otherHandlers, event);
62 if (event.defaultMuiPrevented) {
63 return;
64 }
65 if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
66 event.preventDefault();
67 dispatch({
68 type: DropdownActionTypes.open,
69 event
70 });
71 }
72 };
73 const getOwnRootProps = (otherHandlers = {}) => ({
74 onClick: createHandleClick(otherHandlers),
75 onKeyDown: createHandleKeyDown(otherHandlers)
76 });
77 const getRootProps = (externalProps = {}) => {
78 const externalEventHandlers = extractEventHandlers(externalProps);
79 const getCombinedProps = combineHooksSlotProps(getOwnRootProps, getButtonRootProps);
80 return _extends({
81 'aria-haspopup': 'menu',
82 'aria-expanded': state.open,
83 'aria-controls': popupId
84 }, externalProps, externalEventHandlers, getCombinedProps(externalEventHandlers), {
85 tabIndex: 0,
86 // this is needed to make the button focused after click in Safari
87 ref: handleRef
88 });
89 };
90 return {
91 active,
92 getRootProps,
93 open: state.open,
94 rootRef: handleRef
95 };
96}
\No newline at end of file