UNPKG

@mui/base

Version:

MUI Base is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.

131 lines (128 loc) 3.39 kB
'use client'; import * as React from 'react'; import { unstable_useId as useId, unstable_useForkRef as useForkRef } from '@mui/utils'; import { useButton } from "../useButton/index.js"; import { useListItem } from "../useList/index.js"; import { DropdownActionTypes } from "../useDropdown/index.js"; import { DropdownContext } from "../useDropdown/DropdownContext.js"; import { combineHooksSlotProps } from "../utils/combineHooksSlotProps.js"; import { useCompoundItem } from "../useCompound/index.js"; import { extractEventHandlers } from "../utils/extractEventHandlers.js"; function idGenerator(existingKeys) { return `menu-item-${existingKeys.size}`; } const FALLBACK_MENU_CONTEXT = { dispatch: () => {}, popupId: '', registerPopup: () => {}, registerTrigger: () => {}, state: { open: true, changeReason: null }, triggerElement: null }; /** * * Demos: * * - [Menu](https://mui.com/base-ui/react-menu/#hooks) * * API: * * - [useMenuItem API](https://mui.com/base-ui/react-menu/hooks-api/#use-menu-item) */ export function useMenuItem(params) { const { disabled = false, id: idParam, rootRef: externalRef, label, disableFocusOnHover = false } = params; const id = useId(idParam); const itemRef = React.useRef(null); const itemMetadata = React.useMemo(() => ({ disabled, id: id ?? '', label, ref: itemRef }), [disabled, id, label]); const { dispatch } = React.useContext(DropdownContext) ?? FALLBACK_MENU_CONTEXT; const { getRootProps: getListRootProps, highlighted } = useListItem({ item: id, handlePointerOverEvents: !disableFocusOnHover }); const { index, totalItemCount } = useCompoundItem(id ?? idGenerator, itemMetadata); const { getRootProps: getButtonProps, focusVisible, rootRef: buttonRefHandler } = useButton({ disabled, focusableWhenDisabled: true }); const handleRef = useForkRef(buttonRefHandler, externalRef, itemRef); React.useDebugValue({ id, highlighted, disabled, label }); const createHandleClick = otherHandlers => event => { otherHandlers.onClick?.(event); if (event.defaultMuiPrevented) { return; } dispatch({ type: DropdownActionTypes.close, event }); }; const getOwnHandlers = (otherHandlers = {}) => ({ ...otherHandlers, onClick: createHandleClick(otherHandlers) }); function getRootProps(externalProps = {}) { const externalEventHandlers = extractEventHandlers(externalProps); const getCombinedRootProps = combineHooksSlotProps(getOwnHandlers, combineHooksSlotProps(getButtonProps, getListRootProps)); return { ...externalProps, ...externalEventHandlers, ...getCombinedRootProps(externalEventHandlers), id, ref: handleRef, role: 'menuitem' }; } // If `id` is undefined (during SSR in React < 18), we fall back to rendering a simplified menu item // which does not have access to infortmation about its position or highlighted state. if (id === undefined) { return { getRootProps, disabled: false, focusVisible, highlighted: false, index: -1, totalItemCount: 0, rootRef: handleRef }; } return { getRootProps, disabled, focusVisible, highlighted, index, totalItemCount, rootRef: handleRef }; }