diff --git a/src/menu/menu.stories.mdx b/src/menu/menu.stories.mdx index cb6766cf9..4f8bfe195 100644 --- a/src/menu/menu.stories.mdx +++ b/src/menu/menu.stories.mdx @@ -126,6 +126,8 @@ be changed by passing `modal={false}` to `` You may nest the `` component within `` to create submenus. +On smaller viewports where there isn't enough space to render the submenu on the side,it will be rendered under its parent menu item instead. + ({ setAnchorRect: () => undefined, }) +const SubMenuContext = React.createContext<{ isSubMenu: boolean }>({ isSubMenu: false }) + // // Menu // @@ -147,7 +149,7 @@ interface MenuListProps * The dropdown menu itself, containing a list of menu items. */ const MenuList = React.forwardRef(function MenuList( - { exceptionallySetClassName, modal = true, ...props }, + { exceptionallySetClassName, modal = true, flip, ...props }, ref, ) { const { menuStore, getAnchorRect } = React.useContext(MenuContext) @@ -155,6 +157,8 @@ const MenuList = React.forwardRef(function MenuLi throw new Error('MenuList must be wrapped in ') } + const { isSubMenu } = React.useContext(SubMenuContext) + const isOpen = menuStore.useState('open') return isOpen ? ( @@ -168,6 +172,7 @@ const MenuList = React.forwardRef(function MenuLi className={classNames('reactist_menulist', exceptionallySetClassName)} getAnchorRect={getAnchorRect ?? undefined} modal={modal} + flip={flip ?? (isSubMenu ? 'bottom' : undefined)} /> ) : null @@ -324,13 +329,14 @@ const SubMenu = React.forwardRef(function SubMenu( const [button, list] = React.Children.toArray(children) const buttonElement = button as React.ReactElement + const subMenuContextValue = React.useMemo(() => ({ isSubMenu: true }), []) return ( {buttonElement.props.children} - {list} + {list} ) })