pre-installations and preparations
This commit is contained in:
1 parent
3e5a0329cb
commit
201d621ac3
6977 files changed
+742826
No files matched your search
+3
@@ -0,0 +1,3 @@
|
||||
export { useMenuItem } from './useMenuItem';
|
||||
export * from './useMenuItem.types';
|
||||
export * from './useMenuItemContextStabilizer';
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
'use client';
|
||||
|
||||
export { useMenuItem } from './useMenuItem';
|
||||
export * from './useMenuItem.types';
|
||||
export * from './useMenuItemContextStabilizer';
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"sideEffects": false,
|
||||
"module": "./index.js",
|
||||
"main": "../node/useMenuItem/index.js",
|
||||
"types": "./index.d.ts"
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
import type { UseMenuItemParameters, UseMenuItemReturnValue } from './useMenuItem.types';
|
||||
/**
|
||||
*
|
||||
* 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 declare function useMenuItem(params: UseMenuItemParameters): UseMenuItemReturnValue;
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
'use client';
|
||||
|
||||
import _extends from "@babel/runtime/helpers/esm/extends";
|
||||
import * as React from 'react';
|
||||
import { unstable_useId as useId, unstable_useForkRef as useForkRef } from '@mui/utils';
|
||||
import { useButton } from '../useButton';
|
||||
import { useListItem } from '../useList';
|
||||
import { DropdownActionTypes } from '../useDropdown';
|
||||
import { DropdownContext } from '../useDropdown/DropdownContext';
|
||||
import { combineHooksSlotProps } from '../utils/combineHooksSlotProps';
|
||||
import { useCompoundItem } from '../useCompound';
|
||||
import { extractEventHandlers } from '../utils/extractEventHandlers';
|
||||
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) {
|
||||
var _React$useContext;
|
||||
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 != null ? id : '',
|
||||
label,
|
||||
ref: itemRef
|
||||
}), [disabled, id, label]);
|
||||
const {
|
||||
dispatch
|
||||
} = (_React$useContext = React.useContext(DropdownContext)) != null ? _React$useContext : FALLBACK_MENU_CONTEXT;
|
||||
const {
|
||||
getRootProps: getListRootProps,
|
||||
highlighted
|
||||
} = useListItem({
|
||||
item: id,
|
||||
handlePointerOverEvents: !disableFocusOnHover
|
||||
});
|
||||
const {
|
||||
index,
|
||||
totalItemCount
|
||||
} = useCompoundItem(id != null ? 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 => {
|
||||
var _otherHandlers$onClic;
|
||||
(_otherHandlers$onClic = otherHandlers.onClick) == null || _otherHandlers$onClic.call(otherHandlers, event);
|
||||
if (event.defaultMuiPrevented) {
|
||||
return;
|
||||
}
|
||||
dispatch({
|
||||
type: DropdownActionTypes.close,
|
||||
event
|
||||
});
|
||||
};
|
||||
const getOwnHandlers = (otherHandlers = {}) => _extends({}, otherHandlers, {
|
||||
onClick: createHandleClick(otherHandlers)
|
||||
});
|
||||
function getRootProps(externalProps = {}) {
|
||||
const externalEventHandlers = extractEventHandlers(externalProps);
|
||||
const getCombinedRootProps = combineHooksSlotProps(getOwnHandlers, combineHooksSlotProps(getButtonProps, getListRootProps));
|
||||
return _extends({}, 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
|
||||
};
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
/// <reference types="react" />
|
||||
import { UseButtonRootSlotProps } from '../useButton';
|
||||
import { MuiCancellableEventHandler } from '../utils/MuiCancellableEvent';
|
||||
interface UseMenuItemRootSlotOwnProps {
|
||||
id: string | undefined;
|
||||
role: 'menuitem';
|
||||
ref: React.RefCallback<Element> | null;
|
||||
}
|
||||
export interface MenuItemMetadata {
|
||||
id: string;
|
||||
disabled: boolean;
|
||||
label?: string;
|
||||
ref: React.RefObject<HTMLElement>;
|
||||
}
|
||||
export type UseMenuItemRootSlotProps<ExternalProps = {}> = ExternalProps & UseMenuItemRootSlotOwnProps & UseButtonRootSlotProps<ExternalProps> & {
|
||||
onClick: MuiCancellableEventHandler<React.MouseEvent>;
|
||||
};
|
||||
export interface UseMenuItemParameters {
|
||||
disabled?: boolean;
|
||||
id?: string;
|
||||
label?: string;
|
||||
onClick?: React.MouseEventHandler<any>;
|
||||
rootRef: React.Ref<Element>;
|
||||
/**
|
||||
* If `true`, the menu item won't receive focus when the mouse moves over it.
|
||||
*
|
||||
* @default false
|
||||
*/
|
||||
disableFocusOnHover?: boolean;
|
||||
}
|
||||
export interface UseMenuItemReturnValue {
|
||||
/**
|
||||
* Resolver for the root slot's props.
|
||||
* @param externalProps event handlers for the root slot
|
||||
* @returns props that should be spread on the root slot
|
||||
*/
|
||||
getRootProps: <ExternalProps extends Record<string, unknown> = {}>(externalProps?: ExternalProps) => UseMenuItemRootSlotProps<ExternalProps>;
|
||||
/**
|
||||
* If `true`, the component is disabled.
|
||||
*/
|
||||
disabled: boolean;
|
||||
/**
|
||||
* If `true`, the component is being focused using keyboard.
|
||||
*/
|
||||
focusVisible: boolean;
|
||||
/**
|
||||
* If `true`, the component is being highlighted.
|
||||
*/
|
||||
highlighted: boolean;
|
||||
/**
|
||||
* 0-based index of the item in the menu.
|
||||
*/
|
||||
index: number;
|
||||
/**
|
||||
* The ref to the component's root DOM element.
|
||||
*/
|
||||
rootRef: React.RefCallback<Element> | null;
|
||||
/**
|
||||
* Total number of items in the menu.
|
||||
*/
|
||||
totalItemCount: number;
|
||||
}
|
||||
export {};
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {};
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Stabilizes the ListContext value for the MenuItem component, so it doesn't change when sibling items update.
|
||||
*
|
||||
* @param id The id of the MenuItem. If undefined, it will be generated with useId.
|
||||
* @returns The stable ListContext value and the id of the MenuItem.
|
||||
*
|
||||
* Demos:
|
||||
*
|
||||
* - [Menu](https://mui.com/base-ui/react-menu/#hooks)
|
||||
*
|
||||
* API:
|
||||
*
|
||||
* - [useMenuItemContextStabilizer API](https://mui.com/base-ui/react-menu/hooks-api/#use-menu-item-context-stabilizer)
|
||||
*/
|
||||
export declare function useMenuItemContextStabilizer(id: string | undefined): {
|
||||
contextValue: {
|
||||
dispatch: (action: import("../useList").ListAction<string>) => void;
|
||||
getItemState: (itemValue: string) => {
|
||||
highlighted: boolean;
|
||||
selected: boolean;
|
||||
focusable: boolean;
|
||||
};
|
||||
};
|
||||
id: string | undefined;
|
||||
};
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
'use client';
|
||||
|
||||
import * as React from 'react';
|
||||
import { unstable_useId as useId } from '@mui/utils';
|
||||
import { ListContext } from '../useList';
|
||||
|
||||
/**
|
||||
* Stabilizes the ListContext value for the MenuItem component, so it doesn't change when sibling items update.
|
||||
*
|
||||
* @param id The id of the MenuItem. If undefined, it will be generated with useId.
|
||||
* @returns The stable ListContext value and the id of the MenuItem.
|
||||
*
|
||||
* Demos:
|
||||
*
|
||||
* - [Menu](https://mui.com/base-ui/react-menu/#hooks)
|
||||
*
|
||||
* API:
|
||||
*
|
||||
* - [useMenuItemContextStabilizer API](https://mui.com/base-ui/react-menu/hooks-api/#use-menu-item-context-stabilizer)
|
||||
*/
|
||||
export function useMenuItemContextStabilizer(id) {
|
||||
const listContext = React.useContext(ListContext);
|
||||
if (!listContext) {
|
||||
throw new Error('MenuItem: ListContext was not found.');
|
||||
}
|
||||
const itemId = useId(id);
|
||||
const {
|
||||
getItemState,
|
||||
dispatch
|
||||
} = listContext;
|
||||
let itemState;
|
||||
if (itemId != null) {
|
||||
itemState = getItemState(itemId);
|
||||
} else {
|
||||
itemState = {
|
||||
focusable: true,
|
||||
highlighted: false,
|
||||
selected: false
|
||||
};
|
||||
}
|
||||
const {
|
||||
highlighted,
|
||||
selected,
|
||||
focusable
|
||||
} = itemState;
|
||||
|
||||
// The local version of getItemState can be only called with the current Option's value.
|
||||
// It doesn't make much sense to render an Option depending on other Options' state anyway.
|
||||
const localGetItemState = React.useCallback(itemValue => {
|
||||
if (itemValue !== itemId) {
|
||||
throw new Error(['Base UI MenuItem: Tried to access the state of another MenuItem.', `itemValue: ${itemValue} | id: ${itemId}`, 'This is unsupported when the MenuItem uses the MenuItemContextStabilizer as a performance optimization.'].join('/n'));
|
||||
}
|
||||
return {
|
||||
highlighted,
|
||||
selected,
|
||||
focusable
|
||||
};
|
||||
}, [highlighted, selected, focusable, itemId]);
|
||||
|
||||
// Create a local (per MenuItem) instance of the ListContext that changes only when
|
||||
// the getItemState's return value changes.
|
||||
// This makes MenuItems re-render only when their state actually change, not when any MenuItem's state changes.
|
||||
const localContextValue = React.useMemo(() => ({
|
||||
dispatch,
|
||||
getItemState: localGetItemState
|
||||
}), [dispatch, localGetItemState]);
|
||||
return {
|
||||
contextValue: localContextValue,
|
||||
id: itemId
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user