mikecd
November 6, 2023, 1:37pm
1
I have an ion-menu that, when a button is clicked, successfully routes to a different component/page. Is there a way to programmatically close the menu either before or after the route is pushed? As it is working right now, the menu stays up until the user clicks something on the page/component that we pushed to.
Code showing the ion-menu:
<ion-menu side="start" content-id="main-content" id="mainslideoutmenu">
<ion-header>
<ion-toolbar>
<ion-title>Menu</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-list>
<ion-item button @click="routeToSettings">Settings</ion-item>
</ion-list>
</ion-content>
</ion-menu>
Code showing the functions:
function routeToSettings() {
closeMenu(); //doesn't work either placing before or after the push
router.push('/settings');
}
async function closeMenu(this: any) {
console.log("here");
await menuController.close('mainslideoutmenu');
}
I’m sure I’m not doing something right in the closeMenu function. Anyone have any ideas on what may be going wrong?
HAve you tried something like that
onClick={async () => await menuController.toggle()}
Or maybe to toggle
opened 12:44PM - 29 Dec 21 UTC
closed 04:17PM - 24 Mar 22 UTC
package: react
type: bug
### Prerequisites
- [X] I have read the [Contributing Guidelines](https://githu… b.com/ionic-team/ionic-framework/blob/main/.github/CONTRIBUTING.md#creating-an-issue).
- [X] I agree to follow the [Code of Conduct](https://ionicframework.com/code-of-conduct).
- [X] I have searched for [existing issues](https://github.com/ionic-team/ionic-framework/issues) that already report this problem, without success.
### Ionic Framework Version
- [ ] v4.x
- [ ] v5.x
- [X] v6.x
### Current Behavior
When I try to programmatically toggle ion-menu using `await menuController.toggle(menuId)`, menu does not open.
### Expected Behavior
In previous versions of Ionic (5.9.3) calling toggle on menuController opened the menu correctly - see my other branch https://github.com/nika1001/ionic6-menu-controller-issue/tree/ionic5-working-menu
I would like to have the same behavior also in Ionic6.
### Steps to Reproduce
1. Start an application provided in the github: https://github.com/nika1001/ionic6-menu-controller-issue
2. Pressing buttons under "Toggle menu by MenuController" header does nothing
### Code Reproduction URL
https://github.com/nika1001/ionic6-menu-controller-issue
### Ionic Info
Ionic:
Ionic CLI : 6.18.1 (C:\Tools\nvm\v16.10.0\node_modules\@ionic\cli)
Ionic Framework : @ionic/react 6.0.1
Capacitor:
Capacitor CLI : 3.3.3
@capacitor/android : not installed
@capacitor/core : 3.3.3
@capacitor/ios : not installed
Utility:
cordova-res : not installed globally
native-run : 1.5.0
System:
NodeJS : v16.10.0 (C:\Program Files\nodejs\node.exe)
npm : 8.0.0
OS : Windows 10
### Additional Information
Reason for using menuController is that I need multiple menus on the same side and in order to do that I had to first enable a menu to be toggled:
```
const toggleMenu = async (menuId: string) => {
await menuController.enable(true, menuId)
await menuController.toggle(menuId)
}
```
Inspired by: https://forum.ionicframework.com/t/allow-multiple-menus/189934/4
If I don't enable the menu, I can toggle only 1 of the menus assigned to "start" side. This is demonstrated by buttons under "Toggle menu directly" header in my demo application: https://github.com/nika1001/ionic6-menu-controller-issue
mikecd
November 8, 2023, 12:34pm
3
Got it to work by wrapping the ion-item in an ion-menu-toggle
<ion-menu-toggle>
<ion-item button @click="routeToSettings">Settings</ion-item>
</ion-menu-toggle>
Thank you for your help. It definitely got me pointed in the right direction.
1 Like