Skip to content

Commit

Permalink
Use the useDropdown hook
Browse files Browse the repository at this point in the history
  • Loading branch information
mnajdova committed Oct 10, 2023
1 parent 8edc2a3 commit 1d6e378
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
3 changes: 1 addition & 2 deletions docs/pages/base-ui/api/use-menu.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"name": "(items: string[]) => void",
"description": "(items: string[]) => void"
}
},
"open": { "type": { "name": "boolean", "description": "boolean" } }
}
},
"returnValue": {
"contextValue": {
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/experiments/material-next/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function LegacyUsage(props: MenuProps) {
);
}

function StableComponentUsage(props: MenuProps) {
function StableComponentUsage(props: { MenuListProps?: { disabledItemsFocusable?: boolean } }) {
const [anchorEl, setAnchorEl] = React.useState<Element | null>(null);
const open = Boolean(anchorEl);
const handleClick = (event: React.MouseEvent) => {
Expand Down
5 changes: 1 addition & 4 deletions packages/mui-base/src/useMenu/useMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export function useMenu(parameters: UseMenuParameters = {}): UseMenuReturnValue
onItemsChange,
id: idParam,
disabledItemsFocusable = true,
open: openParameter,
} = parameters;

const rootRef = React.useRef<HTMLElement>(null);
Expand All @@ -51,14 +50,12 @@ export function useMenu(parameters: UseMenuParameters = {}): UseMenuReturnValue
const listboxId = useId(idParam) ?? '';

const {
state: { open: contextOpen },
state: { open },
dispatch: menuDispatch,
triggerElement,
registerPopup,
} = React.useContext(DropdownContext) ?? FALLBACK_MENU_CONTEXT;

const open = openParameter !== undefined ? openParameter : contextOpen;

// 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);
Expand Down
1 change: 0 additions & 1 deletion packages/mui-base/src/useMenu/useMenu.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export interface UseMenuParameters {
* The ref to the menu's listbox node.
*/
listboxRef?: React.Ref<Element>;
open?: boolean;
}

export interface UseMenuReturnValue {
Expand Down
24 changes: 21 additions & 3 deletions packages/mui-material-next/src/Menu/Menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import PropTypes from 'prop-types';
import clsx from 'clsx';
import { unstable_composeClasses as composeClasses } from '@mui/base/composeClasses';
import { useMenu, MenuProvider } from '@mui/base/useMenu';
import { useDropdown, DropdownContext } from '@mui/base/useDropdown';
import { useSlotProps } from '@mui/base/utils';
import { ListActionTypes } from '@mui/base/useList';
import { HTMLElementType } from '@mui/utils';
Expand Down Expand Up @@ -66,7 +67,7 @@ const MenuListbox = styled('ul', {
outline: 0,
});

const Menu = React.forwardRef(function Menu(inProps, ref) {
const MenuInner = React.forwardRef(function Menu(inProps, ref) {
const props = useThemeProps({ props: inProps, name: 'MuiMenu' });

const {
Expand All @@ -77,7 +78,6 @@ const Menu = React.forwardRef(function Menu(inProps, ref) {
disableAutoFocusItem = false,
MenuListProps = {},
onClose,
open: openProp,
PaperProps = {},
PopoverClasses,
transitionDuration = 'auto',
Expand Down Expand Up @@ -107,7 +107,6 @@ const Menu = React.forwardRef(function Menu(inProps, ref) {
const { contextValue, getListboxProps, dispatch, open, triggerElement } = useMenu({
// onItemsChange,
disabledItemsFocusable: Boolean(MenuListProps.disabledItemsFocusable),
open: openProp,
});

React.useImperativeHandle(
Expand Down Expand Up @@ -256,6 +255,25 @@ const Menu = React.forwardRef(function Menu(inProps, ref) {
);
});

const Menu = React.forwardRef(function Menu(inProps, ref) {
const { open, anchorEl, ...other } = inProps;
const upperDropdownContext = React.useContext(DropdownContext);

const { contextValue: dropdownContextValue } = useDropdown({
open,
anchorEl,
});

const Wrapper = !upperDropdownContext ? DropdownContext.Provider : React.Fragment;
const wrapperProps = !upperDropdownContext ? { value: dropdownContextValue } : {};

return (
<Wrapper {...wrapperProps}>
<MenuInner ref={ref} {...inProps} />
</Wrapper>
);
});

Menu.propTypes /* remove-proptypes */ = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
Expand Down

0 comments on commit 1d6e378

Please sign in to comment.