pre-installations and preparations
This commit is contained in:
1 parent
3e5a0329cb
commit
201d621ac3
6977 files changed
+742826
No files matched your search
+2
@@ -0,0 +1,2 @@
|
||||
export { useMenuButton } from './useMenuButton';
|
||||
export * from './useMenuButton.types';
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
'use client';
|
||||
|
||||
export { useMenuButton } from './useMenuButton';
|
||||
export * from './useMenuButton.types';
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"sideEffects": false,
|
||||
"module": "./index.js",
|
||||
"main": "../node/useMenuButton/index.js",
|
||||
"types": "./index.d.ts"
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
import { UseMenuButtonParameters, UseMenuButtonReturnValue } from './useMenuButton.types';
|
||||
/**
|
||||
*
|
||||
* 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 declare function useMenuButton(parameters?: UseMenuButtonParameters): UseMenuButtonReturnValue;
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
'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 => {
|
||||
var _otherHandlers$onClic;
|
||||
(_otherHandlers$onClic = otherHandlers.onClick) == null || _otherHandlers$onClic.call(otherHandlers, event);
|
||||
if (event.defaultMuiPrevented) {
|
||||
return;
|
||||
}
|
||||
dispatch({
|
||||
type: DropdownActionTypes.toggle,
|
||||
event
|
||||
});
|
||||
};
|
||||
const createHandleKeyDown = otherHandlers => event => {
|
||||
var _otherHandlers$onKeyD;
|
||||
(_otherHandlers$onKeyD = otherHandlers.onKeyDown) == null || _otherHandlers$onKeyD.call(otherHandlers, 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
|
||||
};
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
/// <reference types="react" />
|
||||
export interface UseMenuButtonParameters {
|
||||
/**
|
||||
* If `true`, the component is disabled.
|
||||
* @default false
|
||||
*/
|
||||
disabled?: boolean;
|
||||
/**
|
||||
* If `true`, allows a disabled button to receive focus.
|
||||
* @default false
|
||||
*/
|
||||
focusableWhenDisabled?: boolean;
|
||||
/**
|
||||
* The ref to the root element.
|
||||
*/
|
||||
rootRef?: React.Ref<HTMLElement>;
|
||||
}
|
||||
type UseMenuButtonRootSlotProps<ExternalProps = {}> = ExternalProps & UseMenuButtonRootSlotOwnProps;
|
||||
interface UseMenuButtonRootSlotOwnProps {
|
||||
'aria-haspopup': 'menu';
|
||||
'aria-expanded': boolean;
|
||||
'aria-controls': string;
|
||||
/**
|
||||
* Callback fired when the button is clicked.
|
||||
*/
|
||||
onClick: React.MouseEventHandler;
|
||||
/**
|
||||
* The ref to the button element.
|
||||
*/
|
||||
ref: React.RefCallback<Element> | null;
|
||||
}
|
||||
export interface UseMenuButtonReturnValue {
|
||||
/**
|
||||
* If `true`, the component is active (pressed).
|
||||
*/
|
||||
active: boolean;
|
||||
/**
|
||||
* Resolver for the root slot's props.
|
||||
* @param externalProps props for the root slot
|
||||
* @returns props that should be spread on the root slot
|
||||
*/
|
||||
getRootProps: <ExternalProps extends Record<string, unknown> = {}>(externalProps?: ExternalProps) => UseMenuButtonRootSlotProps<ExternalProps>;
|
||||
open: boolean;
|
||||
/**
|
||||
* The ref to the root element.
|
||||
*/
|
||||
rootRef: React.RefCallback<Element> | null;
|
||||
}
|
||||
export {};
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {};
|
||||
Reference in new issue
Block a user