I'm using Ionic 7 and React 18. I would like to design an Ionic hamburger menu for my application, taht I would like to reside in the upper right of my toolbar. I have designed this Menu.tsx component
const Menu: React.FC = () =>
{
const onClickHandler = (): void =>
{
menuController.close();
};
return (
<React.Fragment>
<IonMenu
side="start"
menuId="first"
content-id="menuContent"
swipe-gesture={true}
disabled={false}
maxEdgeStart={100}
hidden={false}
type="overlay">
<IonHeader translucent>
<IonToolbar color="primary">
<IonTitle>
<IonIcon slot="start" icon={menuOutline}></IonIcon>
</IonTitle>
<IonButtons slot="start">
<IonMenuButton
autoHide={true}
onClick={() => onClickHandler()}
></IonMenuButton>
</IonButtons>
</IonToolbar>
</IonHeader>
<IonContent>
<IonList>
<IonItem>Menu Item</IonItem>
<IonItem>Menu Item</IonItem>
<IonItem>Menu Item</IonItem>
<IonItem>Menu Item</IonItem>
<IonItem>Menu Item</IonItem>
</IonList>
</IonContent>
</IonMenu>
</React.Fragment>
);
};
export default Menu;
and then I have put it on my App.tsx page in the IonHeader bar
<IonHeader>
<IonToolbar>
<IonButtons slot='start'>
<IonButton>
<IonIcon icon={logoReact} />
</IonButton>
</IonButtons>
<IonTitle>My site</IonTitle>
<IonButtons slot='end'>
<IonButton>
<IonIcon icon={personCircle} />
</IonButton>
</IonButtons>
<Menu/>
</IonToolbar>
</IonHeader>
However, this does not display, per my demo -- https://stackblitz.com/edit/an5yjh-vhnvgg?file=src%2Fcomponents%2FMenu.tsx,src%2FApp.tsx . What else do I need to do to get my hamburger menu to appear?
You were close, but some adjustments required to make it work:
<Menu/>
element as a direct child of the <IonApp>
element.<IonContent>
inside the App.tsx the same id in the menu element. so, menuContent
This will enable dragging behaviour to open the drawerBonus: I also added the menu button in the toolbar, so users can open the menu with that button as well. Also make the IonPage element as root, so your layout works!
Menu.tsx:
...
<IonMenu
side="start" // this puts the toolbar on the left side for ltr locales
menuId="first"
content-id="menuContent" // you should also use the same id in App.
swipe-gesture={true}
disabled={false}
maxEdgeStart={100}
hidden={false}
type="overlay">
...
App.tsx
...
export default function App() {
return (
<IonApp>
<Menu />
<IonPage>
<IonHeader>
<IonToolbar>
<IonButtons slot="start">
<IonMenuButton/>
</IonButtons>
<IonTitle>My site</IonTitle>
<IonButtons slot="end">
<IonButton>person</IonButton>
</IonButtons>
</IonToolbar>
</IonHeader>
<IonContent className="ion-padding" id="menuContent">
<p>Hi there</p>
</IonContent>
</IonPage>
</IonApp>
);
}