You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.6 KiB
JavaScript
94 lines
2.6 KiB
JavaScript
'use client';
|
|
|
|
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
import * as React from 'react';
|
|
import { unstable_useForkRef as useForkRef } from '@mui/utils';
|
|
import { DropdownContext } from '../useDropdown/DropdownContext';
|
|
import { DropdownActionTypes } from '../useDropdown/useDropdown.types';
|
|
import { useButton } from '../useButton/useButton';
|
|
import { combineHooksSlotProps } from '../utils/combineHooksSlotProps';
|
|
import { extractEventHandlers } from '../utils';
|
|
|
|
/**
|
|
*
|
|
* Demos:
|
|
*
|
|
* - [Menu](https://mui.com/base-ui/react-menu/#hooks)
|
|
*
|
|
* API:
|
|
*
|
|
* - [useMenuButton API](https://mui.com/base-ui/react-menu/hooks-api/#use-menu-button)
|
|
*/
|
|
export function useMenuButton(parameters = {}) {
|
|
const {
|
|
disabled = false,
|
|
focusableWhenDisabled,
|
|
rootRef: externalRef
|
|
} = parameters;
|
|
const menuContext = React.useContext(DropdownContext);
|
|
if (menuContext === null) {
|
|
throw new Error('useMenuButton: no menu context available.');
|
|
}
|
|
const {
|
|
state,
|
|
dispatch,
|
|
registerTrigger,
|
|
popupId
|
|
} = menuContext;
|
|
const {
|
|
getRootProps: getButtonRootProps,
|
|
rootRef: buttonRootRef,
|
|
active
|
|
} = useButton({
|
|
disabled,
|
|
focusableWhenDisabled,
|
|
rootRef: externalRef
|
|
});
|
|
const handleRef = useForkRef(buttonRootRef, registerTrigger);
|
|
const createHandleClick = otherHandlers => event => {
|
|
otherHandlers.onClick?.(event);
|
|
if (event.defaultMuiPrevented) {
|
|
return;
|
|
}
|
|
dispatch({
|
|
type: DropdownActionTypes.toggle,
|
|
event
|
|
});
|
|
};
|
|
const createHandleKeyDown = otherHandlers => event => {
|
|
otherHandlers.onKeyDown?.(event);
|
|
if (event.defaultMuiPrevented) {
|
|
return;
|
|
}
|
|
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
event.preventDefault();
|
|
dispatch({
|
|
type: DropdownActionTypes.open,
|
|
event
|
|
});
|
|
}
|
|
};
|
|
const getOwnRootProps = (otherHandlers = {}) => ({
|
|
onClick: createHandleClick(otherHandlers),
|
|
onKeyDown: createHandleKeyDown(otherHandlers)
|
|
});
|
|
const getRootProps = (externalProps = {}) => {
|
|
const externalEventHandlers = extractEventHandlers(externalProps);
|
|
const getCombinedProps = combineHooksSlotProps(getOwnRootProps, getButtonRootProps);
|
|
return _extends({
|
|
'aria-haspopup': 'menu',
|
|
'aria-expanded': state.open,
|
|
'aria-controls': popupId
|
|
}, externalProps, externalEventHandlers, getCombinedProps(externalEventHandlers), {
|
|
tabIndex: 0,
|
|
// this is needed to make the button focused after click in Safari
|
|
ref: handleRef
|
|
});
|
|
};
|
|
return {
|
|
active,
|
|
getRootProps,
|
|
open: state.open,
|
|
rootRef: handleRef
|
|
};
|
|
} |