import * as React from 'react'; import Menu from '@mui/joy/Menu'; import MenuItem, { menuItemClasses } from '@mui/joy/MenuItem'; import List from '@mui/joy/List'; import ListItem from '@mui/joy/ListItem'; import ListItemButton from '@mui/joy/ListItemButton'; import ListDivider from '@mui/joy/ListDivider'; import Typography, { typographyClasses } from '@mui/joy/Typography'; import Dropdown from '@mui/joy/Dropdown'; import MenuButton from '@mui/joy/MenuButton'; const MenuBarButton = React.forwardRef( ({ children, menu, open, onOpen, onKeyDown, ...props }, ref) => { return ( {children} {React.cloneElement(menu, { slotProps: { listbox: { id: `toolbar-example-menu-${children}`, 'aria-label': children, }, }, placement: 'bottom-start', disablePortal: false, variant: 'soft', sx: (theme) => ({ width: 288, boxShadow: '0 2px 8px 0px rgba(0 0 0 / 0.38)', '--List-padding': 'var(--ListDivider-gap)', '--ListItem-minHeight': '32px', [`&& .${menuItemClasses.root}`]: { transition: 'none', '&:hover': { ...theme.variants.solid.primary, [`& .${typographyClasses.root}`]: { color: 'inherit', }, }, }, }), })} ); }, ); export default function MenuToolbarExample() { const menus = React.useRef([]); const [menuIndex, setMenuIndex] = React.useState(null); const renderShortcut = (text) => ( {text} ); const openNextMenu = () => { if (typeof menuIndex === 'number') { if (menuIndex === menus.current.length - 1) { setMenuIndex(0); } else { setMenuIndex(menuIndex + 1); } } }; const openPreviousMenu = () => { if (typeof menuIndex === 'number') { if (menuIndex === 0) { setMenuIndex(menus.current.length - 1); } else { setMenuIndex(menuIndex - 1); } } }; const handleKeyDown = (event) => { if (event.key === 'ArrowRight') { openNextMenu(); } if (event.key === 'ArrowLeft') { openPreviousMenu(); } }; const createHandleButtonKeyDown = (index) => (event) => { if (event.key === 'ArrowRight') { if (index === menus.current.length - 1) { menus.current[0]?.focus(); } else { menus.current[index + 1]?.focus(); } } if (event.key === 'ArrowLeft') { if (index === 0) { menus.current[menus.current.length]?.focus(); } else { menus.current[index - 1]?.focus(); } } }; const itemProps = { onClick: () => setMenuIndex(null), onKeyDown: handleKeyDown, }; return ( { setMenuIndex((prevMenuIndex) => (prevMenuIndex === null ? 0 : null)); }} onKeyDown={createHandleButtonKeyDown(0)} onMouseEnter={() => { if (typeof menuIndex === 'number') { setMenuIndex(0); } }} ref={(instance) => { menus.current[0] = instance; }} menu={ { menus.current[0]?.focus(); }} > New File New Text File... {renderShortcut('⌥ ⌘ N')} New Window {renderShortcut('⇧ ⌘ N')} Open {renderShortcut('⌘ O')} Open Folder } > File { setMenuIndex((prevMenuIndex) => (prevMenuIndex === null ? 1 : null)); }} onKeyDown={createHandleButtonKeyDown(1)} onMouseEnter={() => { if (typeof menuIndex === 'number') { setMenuIndex(1); } }} ref={(instance) => { menus.current[1] = instance; }} menu={ { menus.current[1]?.focus(); }} > Undo {renderShortcut('⌘ Z')} Redo {renderShortcut('⇧ ⌘ Z')} Cut {renderShortcut('⌘ X')} Copy {renderShortcut('⌘ Z')} Paste {renderShortcut('⌘ V')} } > Edit { setMenuIndex((prevMenuIndex) => (prevMenuIndex === null ? 2 : null)); }} onKeyDown={createHandleButtonKeyDown(2)} onMouseEnter={() => { if (typeof menuIndex === 'number') { setMenuIndex(2); } }} ref={(instance) => { menus.current[2] = instance; }} menu={ { menus.current[2]?.focus(); }} > Select All {renderShortcut('⌘ A')} Expand Selection {renderShortcut('⌃ ⇧ ⌘ →')} Shrink Selection {renderShortcut('⌃ ⇧ ⌘ ←')} } > Selection ); }