pre-installations and preparations

This commit is contained in:
MuhammadKhalilzadeh committed 2024-05-05 11:21:00 +03:30
1 parent 3e5a0329cb
commit 201d621ac3
6977 files changed
+742826

No files matched your search

+15
View File
@@ -0,0 +1,15 @@
import * as React from 'react';
import { ListContextValue } from '../useList/ListContext';
import { MenuItemMetadata } from '../useMenuItem';
import { CompoundComponentContextValue } from '../useCompound';
export type MenuProviderValue = CompoundComponentContextValue<string, MenuItemMetadata> & ListContextValue<string>;
export interface MenuProviderProps {
value: MenuProviderValue;
children: React.ReactNode;
}
/**
* Sets up the contexts for the underlying MenuItem components.
*
* @ignore - do not document.
*/
export declare function MenuProvider(props: MenuProviderProps): React.JSX.Element;
+41
View File
@@ -0,0 +1,41 @@
'use client';
import * as React from 'react';
import { ListContext } from '../useList/ListContext';
import { CompoundComponentContext } from '../useCompound';
import { jsx as _jsx } from "react/jsx-runtime";
/**
* Sets up the contexts for the underlying MenuItem components.
*
* @ignore - do not document.
*/
export function MenuProvider(props) {
const {
value,
children
} = props;
const {
dispatch,
getItemIndex,
getItemState,
registerItem,
totalSubitemCount
} = value;
const listContextValue = React.useMemo(() => ({
dispatch,
getItemState,
getItemIndex
}), [dispatch, getItemIndex, getItemState]);
const compoundComponentContextValue = React.useMemo(() => ({
getItemIndex,
registerItem,
totalSubitemCount
}), [registerItem, getItemIndex, totalSubitemCount]);
return /*#__PURE__*/_jsx(CompoundComponentContext.Provider, {
value: compoundComponentContextValue,
children: /*#__PURE__*/_jsx(ListContext.Provider, {
value: listContextValue,
children: children
})
});
}
+3
View File
@@ -0,0 +1,3 @@
export { useMenu } from './useMenu';
export * from './useMenu.types';
export * from './MenuProvider';
+5
View File
@@ -0,0 +1,5 @@
'use client';
export { useMenu } from './useMenu';
export * from './useMenu.types';
export * from './MenuProvider';
+12
View File
@@ -0,0 +1,12 @@
/// <reference types="react" />
import { ListAction, ListActionContext } from '../useList';
import { ActionWithContext } from '../utils/useControllableReducer.types';
import { MenuInternalState } from './useMenu.types';
export type MenuActionContext = ListActionContext<string> & {
listboxRef: React.RefObject<HTMLElement>;
};
export declare function menuReducer(state: MenuInternalState, action: ActionWithContext<ListAction<string>, MenuActionContext>): MenuInternalState | {
open: boolean;
highlightedValue: string | null;
selectedValues: string[];
};
+42
View File
@@ -0,0 +1,42 @@
import _extends from "@babel/runtime/helpers/esm/extends";
import { ListActionTypes, listReducer } from '../useList';
export function menuReducer(state, action) {
if (action.type === ListActionTypes.itemHover) {
return _extends({}, state, {
highlightedValue: action.item
});
}
const newState = listReducer(state, action);
// make sure an item is always highlighted
if (newState.highlightedValue === null && action.context.items.length > 0) {
return _extends({}, newState, {
highlightedValue: action.context.items[0]
});
}
if (action.type === ListActionTypes.keyDown) {
if (action.event.key === 'Escape') {
return _extends({}, newState, {
open: false
});
}
}
if (action.type === ListActionTypes.blur) {
var _action$context$listb;
if (!((_action$context$listb = action.context.listboxRef.current) != null && _action$context$listb.contains(action.event.relatedTarget))) {
var _action$context$listb2, _action$event$related;
// To prevent the menu from closing when the focus leaves the menu to the button.
// For more details, see https://github.com/mui/material-ui/pull/36917#issuecomment-1566992698
const listboxId = (_action$context$listb2 = action.context.listboxRef.current) == null ? void 0 : _action$context$listb2.getAttribute('id');
const controlledBy = (_action$event$related = action.event.relatedTarget) == null ? void 0 : _action$event$related.getAttribute('aria-controls');
if (listboxId && controlledBy && listboxId === controlledBy) {
return newState;
}
return _extends({}, newState, {
open: false,
highlightedValue: action.context.items[0]
});
}
}
return newState;
}
+6
View File
@@ -0,0 +1,6 @@
{
"sideEffects": false,
"module": "./index.js",
"main": "../node/useMenu/index.js",
"types": "./index.d.ts"
}
+12
View File
@@ -0,0 +1,12 @@
import { UseMenuParameters, UseMenuReturnValue } from './useMenu.types';
/**
*
* Demos:
*
* - [Menu](https://mui.com/base-ui/react-menu/#hooks)
*
* API:
*
* - [useMenu API](https://mui.com/base-ui/react-menu/hooks-api/#use-menu)
*/
export declare function useMenu(parameters?: UseMenuParameters): UseMenuReturnValue;
+190
View File
@@ -0,0 +1,190 @@
'use client';
import _extends from "@babel/runtime/helpers/esm/extends";
import * as React from 'react';
import { unstable_useForkRef as useForkRef, unstable_useId as useId, unstable_useEnhancedEffect as useEnhancedEffect } from '@mui/utils';
import { menuReducer } from './menuReducer';
import { DropdownContext } from '../useDropdown/DropdownContext';
import { ListActionTypes, useList } from '../useList';
import { DropdownActionTypes } from '../useDropdown';
import { useCompoundParent } from '../useCompound';
import { combineHooksSlotProps } from '../utils/combineHooksSlotProps';
import { extractEventHandlers } from '../utils/extractEventHandlers';
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:
*
* - [useMenu API](https://mui.com/base-ui/react-menu/hooks-api/#use-menu)
*/
export function useMenu(parameters = {}) {
var _useId, _React$useContext;
const {
listboxRef: listboxRefProp,
onItemsChange,
id: idParam,
disabledItemsFocusable = true,
disableListWrap = false,
autoFocus = true,
componentName = 'useMenu'
} = parameters;
const rootRef = React.useRef(null);
const handleRef = useForkRef(rootRef, listboxRefProp);
const listboxId = (_useId = useId(idParam)) != null ? _useId : '';
const {
state: {
open,
changeReason
},
dispatch: menuDispatch,
triggerElement,
registerPopup
} = (_React$useContext = React.useContext(DropdownContext)) != null ? _React$useContext : FALLBACK_MENU_CONTEXT;
// store the initial open state to prevent focus stealing
// (the first menu items gets focued only when the menu is opened by the user)
const isInitiallyOpen = React.useRef(open);
const {
subitems,
contextValue: compoundComponentContextValue
} = useCompoundParent();
const subitemKeys = React.useMemo(() => Array.from(subitems.keys()), [subitems]);
const getItemDomElement = React.useCallback(itemId => {
var _subitems$get$ref$cur, _subitems$get;
if (itemId == null) {
return null;
}
return (_subitems$get$ref$cur = (_subitems$get = subitems.get(itemId)) == null ? void 0 : _subitems$get.ref.current) != null ? _subitems$get$ref$cur : null;
}, [subitems]);
const isItemDisabled = React.useCallback(id => {
var _subitems$get2;
return (subitems == null || (_subitems$get2 = subitems.get(id)) == null ? void 0 : _subitems$get2.disabled) || false;
}, [subitems]);
const getItemAsString = React.useCallback(id => {
var _subitems$get3, _subitems$get4;
return ((_subitems$get3 = subitems.get(id)) == null ? void 0 : _subitems$get3.label) || ((_subitems$get4 = subitems.get(id)) == null || (_subitems$get4 = _subitems$get4.ref.current) == null ? void 0 : _subitems$get4.innerText);
}, [subitems]);
const reducerActionContext = React.useMemo(() => ({
listboxRef: rootRef
}), [rootRef]);
const {
dispatch: listDispatch,
getRootProps: getListRootProps,
contextValue: listContextValue,
state: {
highlightedValue
},
rootRef: mergedListRef
} = useList({
disabledItemsFocusable,
disableListWrap,
focusManagement: 'DOM',
getItemDomElement,
getInitialState: () => ({
selectedValues: [],
highlightedValue: null
}),
isItemDisabled,
items: subitemKeys,
getItemAsString,
rootRef: handleRef,
onItemsChange,
reducerActionContext,
selectionMode: 'none',
stateReducer: menuReducer,
componentName
});
useEnhancedEffect(() => {
registerPopup(listboxId);
}, [listboxId, registerPopup]);
useEnhancedEffect(() => {
if (open && (changeReason == null ? void 0 : changeReason.type) === 'keydown' && changeReason.key === 'ArrowUp') {
listDispatch({
type: ListActionTypes.highlightLast,
event: changeReason
});
}
}, [open, changeReason, listDispatch]);
React.useEffect(() => {
if (open && autoFocus && highlightedValue && !isInitiallyOpen.current) {
var _subitems$get5;
(_subitems$get5 = subitems.get(highlightedValue)) == null || (_subitems$get5 = _subitems$get5.ref) == null || (_subitems$get5 = _subitems$get5.current) == null || _subitems$get5.focus();
}
}, [open, autoFocus, highlightedValue, subitems, subitemKeys]);
React.useEffect(() => {
var _rootRef$current;
// set focus to the highlighted item (but prevent stealing focus from other elements on the page)
if ((_rootRef$current = rootRef.current) != null && _rootRef$current.contains(document.activeElement) && highlightedValue !== null) {
var _subitems$get6;
subitems == null || (_subitems$get6 = subitems.get(highlightedValue)) == null || (_subitems$get6 = _subitems$get6.ref.current) == null || _subitems$get6.focus();
}
}, [highlightedValue, subitems]);
const createHandleBlur = otherHandlers => event => {
var _otherHandlers$onBlur, _rootRef$current2;
(_otherHandlers$onBlur = otherHandlers.onBlur) == null || _otherHandlers$onBlur.call(otherHandlers, event);
if (event.defaultMuiPrevented) {
return;
}
if ((_rootRef$current2 = rootRef.current) != null && _rootRef$current2.contains(event.relatedTarget) || event.relatedTarget === triggerElement) {
return;
}
menuDispatch({
type: DropdownActionTypes.blur,
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 === 'Escape') {
menuDispatch({
type: DropdownActionTypes.escapeKeyDown,
event
});
}
};
const getOwnListboxHandlers = (otherHandlers = {}) => ({
onBlur: createHandleBlur(otherHandlers),
onKeyDown: createHandleKeyDown(otherHandlers)
});
const getListboxProps = (externalProps = {}) => {
const getCombinedRootProps = combineHooksSlotProps(getOwnListboxHandlers, getListRootProps);
const externalEventHandlers = extractEventHandlers(externalProps);
return _extends({}, externalProps, externalEventHandlers, getCombinedRootProps(externalEventHandlers), {
id: listboxId,
role: 'menu'
});
};
React.useDebugValue({
subitems,
highlightedValue
});
return {
contextValue: _extends({}, compoundComponentContextValue, listContextValue),
dispatch: listDispatch,
getListboxProps,
highlightedValue,
listboxRef: mergedListRef,
menuItems: subitems,
open,
triggerElement
};
}
+89
View File
@@ -0,0 +1,89 @@
import * as React from 'react';
import { ListAction, ListState, UseListRootSlotProps } from '../useList';
import { MenuItemMetadata } from '../useMenuItem';
import { MenuProviderValue } from './MenuProvider';
export interface UseMenuParameters {
/**
* If `true` (Default) will focus the highligted item. If you set this prop to `false`
* the focus will not be moved inside the Menu component. This has severe accessibility implications
* and should only be considered if you manage focus otherwise.
* @default true
*/
autoFocus?: boolean;
/**
* The id of the menu. If not provided, it will be generated.
*/
id?: string;
/**
* If `true`, it will be possible to highlight disabled items.
* @default true
*/
disabledItemsFocusable?: boolean;
/**
* If `true`, the highlight will not wrap around the list if arrow keys are used.
* @default false
*/
disableListWrap?: boolean;
/**
* Callback fired when the menu items change.
*/
onItemsChange?: (items: string[]) => void;
/**
* The ref to the menu's listbox node.
*/
listboxRef?: React.Ref<Element>;
/**
* The name of the component using useMenu.
* For debugging purposes.
* @default 'useMenu'
*/
componentName?: string;
}
export interface UseMenuReturnValue {
/**
* The value to be passed into the MenuProvider.
*/
contextValue: MenuProviderValue;
/**
* Action dispatcher for the menu component.
* Allows to programmatically control the menu.
*/
dispatch: (action: ListAction<string>) => void;
/**
* Resolver for the listbox slot's props.
* @param externalProps additional props for the listbox component
* @returns props that should be spread on the listbox component
*/
getListboxProps: <ExternalProps extends Record<string, unknown> = {}>(externalProps?: ExternalProps) => UseMenuListboxSlotProps;
/**
* The highlighted option in the menu listbox.
*/
highlightedValue: string | null;
/**
* The ref to the menu's listbox node.
*/
listboxRef: React.RefCallback<Element> | null;
/**
* Items in the menu listbox.
*/
menuItems: Map<string, MenuItemMetadata>;
/**
* If `true`, the menu is open.
*/
open: boolean;
/**
* An element that triggers the visibility of the menu.
*/
triggerElement: HTMLElement | null;
}
interface UseMenuListboxSlotEventHandlers {
onBlur: React.FocusEventHandler;
onKeyDown: React.KeyboardEventHandler;
}
export type UseMenuListboxSlotProps<ExternalProps = {}> = UseListRootSlotProps<Omit<ExternalProps, keyof UseMenuListboxSlotEventHandlers> & UseMenuListboxSlotEventHandlers> & {
ref: React.RefCallback<Element> | null;
role: React.AriaRole;
};
export interface MenuInternalState extends ListState<string> {
}
export {};
+1
View File
@@ -0,0 +1 @@
export {};