mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-11-09 08:09:32 +08:00
fix(menu) menus on the same side are not automatically disabled (#28269)
Issue number: resolves #18974 --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> When multiple menus on the same `side` are registered, all but the most recent menu are disabled. For example, if a user starts on PageA with a `start` menu and then navigates to PageB which also has a `start` menu, then the menu on PageA will be disabled. The problem is that if users navigates back to PageA they will be unable to open the menu on that view because it is still disabled. This behavior impacts any Ionic developer trying to open a menu whether by calling the `open` method on the menu itself or on the `menuController`. After discussing with the team, we believe the original intent of this behavior was to prevent users from accidentally opening the wrong menu when calling `menuController.open('start')`. This API allows developers to reference a menu by side, and since it's possible to have multiple menus on the same side it's also possible to open the wrong menu when referencing by side only. However, this API starts to break down pretty quickly in a navigation scenario. Sample Repo: https://github.com/liamdebeasi/multiple-menu-bug-repro ## Scenario 1: Referencing Menu by Side 1. On the "home" route click "Open 'start' menu". Observe that the home page menu opens. 2. Close the menu and click "Go to Page Two". 3. On the "page-two" route click "Open 'start' menu". Observe that the page two menu opens. 4. Go back to "home". 5. Click "Open 'start' menu". Observe that nothing happens. 6. Click "Enable and Open 'start'" Menu". Observe that the home menu opens. ## Scenario 2: Referencing Menu by ID 1. On the "home" route click "Open '#menu1' menu". Observe that the home page menu opens. 2. Close the menu and click "Go to Page Two". 3. On the "page-two" route click "Open '#menu2' menu". Observe that the page two menu opens. 4. Go back to "home". 5. Click "Open '#menu1' menu". Observe that nothing happens. 6. Click "Enable and Open '#menu1'" Menu". Observe that the home menu opens. ## Scenario 3: Using 3 or more menus even when enabling menus 1. On the "home" route click "Open 'start' menu". Observe that the home page menu opens. 2. Close the menu and click "Go to Page Two". 3. On the "page-two" route click "Open 'start' menu". Observe that the page two menu opens. 4. Close the menu and click "Go to Page Three" 5. On the "page-three" route click "Open 'start' menu". Observe that the page three menu opens. 6. Go back to "page-two". 8. Click "Open 'start' menu". Observe that nothing happens. 9. Click "Enable and Open 'start' Menu". Observe that nothing happens. The menu controller attempts to find an enabled menu on the specified side:a04a11be35/core/src/utils/menu-controller/index.ts (L79C12-L79C12)Step 6 is where this breaks down. In this scenario, the menus on "home" and "page-two" are disabled. This leads menu controller to use its fallback which tries to get the first menu registered on the specified side:a04a11be35/core/src/utils/menu-controller/index.ts (L86)This means that the menu controller would attempt to open the "home" menu even though the user is on "page-two" (because the start menu on "home" was the first to be registered). ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - Menus are no longer automatically disabled when a new menu on the same side is registered - Referencing menus by side when multiple menus with that side exist in the DOM will cause a warning to be logged This change has a couple implications: 1. Developers no longer need to manually enable a menu as noted in https://ionicframework.com/docs/api/menu#multiple-menus. Note that continuing to manually enable the menus will not cause any adverse side effects and will effectively be a no-op. 2. Developers using the menuController to open a menu based on "side" may end up having the wrong menu get opened. Example before to this change: 1. Start on PageA with a `start` menu. Calling `menuController.open('start')` opens the menu on PageA. 2. Go to PageB with a `start` menu. Calling `menuController.open('start')` opens the menu on PageB because the menu on PageA is disabled. Example after to this change: 1. Start on PageA with a `start` menu. Calling `menuController.open('start')` opens the menu on PageA. 2. Go to PageB with a `start` menu. Calling `menuController.open('start')` attempts to opens the menu on PageA because both menus are enabled. However, since PageA is hidden nothing will appear to happen. ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change, please describe the impact and migration path for existing applications below. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> I manually verified that removing the Angular Universal code does not regress the behavior fixed in https://github.com/ionic-team/ionic-framework/pull/27814. The menu is never automatically disabled, so the bug does not happen. This is a partial fix for https://github.com/ionic-team/ionic-framework/issues/18683. Properly fixing this requires another change which is out of scope for this work.
This commit is contained in:
@ -30,7 +30,6 @@ export interface MenuControllerI {
|
|||||||
_setOpen(menu: MenuI, shouldOpen: boolean, animated: boolean): Promise<boolean>;
|
_setOpen(menu: MenuI, shouldOpen: boolean, animated: boolean): Promise<boolean>;
|
||||||
_register(menu: MenuI): void;
|
_register(menu: MenuI): void;
|
||||||
_unregister(menu: MenuI): void;
|
_unregister(menu: MenuI): void;
|
||||||
_setActiveMenu(menu: MenuI): void;
|
|
||||||
|
|
||||||
getMenus(): Promise<HTMLIonMenuElement[]>;
|
getMenus(): Promise<HTMLIonMenuElement[]>;
|
||||||
getOpenSync(): HTMLIonMenuElement | undefined;
|
getOpenSync(): HTMLIonMenuElement | undefined;
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
import type { ComponentInterface, EventEmitter } from '@stencil/core';
|
import type { ComponentInterface, EventEmitter } from '@stencil/core';
|
||||||
import { Build, Component, Element, Event, Host, Listen, Method, Prop, State, Watch, h } from '@stencil/core';
|
import { Build, Component, Element, Event, Host, Listen, Method, Prop, State, Watch, h } from '@stencil/core';
|
||||||
import { getTimeGivenProgression } from '@utils/animation/cubic-bezier';
|
import { getTimeGivenProgression } from '@utils/animation/cubic-bezier';
|
||||||
import { doc } from '@utils/browser';
|
|
||||||
import { GESTURE_CONTROLLER } from '@utils/gesture';
|
import { GESTURE_CONTROLLER } from '@utils/gesture';
|
||||||
import type { Attributes } from '@utils/helpers';
|
import type { Attributes } from '@utils/helpers';
|
||||||
import { inheritAriaAttributes, assert, clamp, isEndSide as isEnd } from '@utils/helpers';
|
import { inheritAriaAttributes, assert, clamp, isEndSide as isEnd } from '@utils/helpers';
|
||||||
@ -762,18 +761,6 @@ export class Menu implements ComponentInterface, MenuI {
|
|||||||
*/
|
*/
|
||||||
this.afterAnimation(false);
|
this.afterAnimation(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (doc?.contains(this.el)) {
|
|
||||||
/**
|
|
||||||
* Only set the active menu if the menu element is
|
|
||||||
* present in the DOM. Otherwise if it was destructively
|
|
||||||
* re-hydrated (through Angular Universal), then ignore
|
|
||||||
* setting the removed node as the active menu.
|
|
||||||
*/
|
|
||||||
if (!this.disabled) {
|
|
||||||
menuController._setActiveMenu(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|||||||
74
core/src/components/menu/test/multiple/index.html
Normal file
74
core/src/components/menu/test/multiple/index.html
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en" dir="ltr">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<title>Menu - Multiple</title>
|
||||||
|
<meta
|
||||||
|
name="viewport"
|
||||||
|
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"
|
||||||
|
/>
|
||||||
|
<link href="../../../../../css/ionic.bundle.css" rel="stylesheet" />
|
||||||
|
<link href="../../../../../scripts/testing/styles.css" rel="stylesheet" />
|
||||||
|
<script src="../../../../../scripts/testing/scripts.js"></script>
|
||||||
|
<script nomodule src="../../../../../dist/ionic/ionic.js"></script>
|
||||||
|
<script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script>
|
||||||
|
<script type="module">
|
||||||
|
import { menuController } from '../../../../dist/ionic/index.esm.js';
|
||||||
|
window.menuController = menuController;
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<ion-app>
|
||||||
|
<ion-menu side="start" menu-id="primary-menu" id="primary-menu" content-id="main">
|
||||||
|
<ion-header>
|
||||||
|
<ion-toolbar color="primary">
|
||||||
|
<ion-title>Primary</ion-title>
|
||||||
|
</ion-toolbar>
|
||||||
|
</ion-header>
|
||||||
|
<ion-content class="ion-padding"> Menu Content </ion-content>
|
||||||
|
</ion-menu>
|
||||||
|
|
||||||
|
<ion-menu side="start" menu-id="secondary-menu" id="secondary-menu" content-id="main">
|
||||||
|
<ion-header>
|
||||||
|
<ion-toolbar color="secondary">
|
||||||
|
<ion-title>Secondary</ion-title>
|
||||||
|
</ion-toolbar>
|
||||||
|
</ion-header>
|
||||||
|
<ion-content class="ion-padding"> Menu Content </ion-content>
|
||||||
|
</ion-menu>
|
||||||
|
|
||||||
|
<ion-menu disabled="true" side="end" menu-id="success-menu" id="success-menu" content-id="main">
|
||||||
|
<ion-header>
|
||||||
|
<ion-toolbar color="success">
|
||||||
|
<ion-title>Success</ion-title>
|
||||||
|
</ion-toolbar>
|
||||||
|
</ion-header>
|
||||||
|
<ion-content class="ion-padding"> Menu Content </ion-content>
|
||||||
|
</ion-menu>
|
||||||
|
|
||||||
|
<ion-menu disabled="true" side="end" menu-id="danger-menu" id="danger-menu" content-id="main">
|
||||||
|
<ion-header>
|
||||||
|
<ion-toolbar color="danger">
|
||||||
|
<ion-title>Danger</ion-title>
|
||||||
|
</ion-toolbar>
|
||||||
|
</ion-header>
|
||||||
|
<ion-content class="ion-padding"> Menu Content </ion-content>
|
||||||
|
</ion-menu>
|
||||||
|
|
||||||
|
<div class="ion-page" id="main">
|
||||||
|
<ion-header>
|
||||||
|
<ion-toolbar>
|
||||||
|
<ion-buttons slot="start">
|
||||||
|
<ion-menu-button menu="primary-menu" color="primary"></ion-menu-button>
|
||||||
|
<ion-menu-button menu="secondary-menu" color="secondary"></ion-menu-button>
|
||||||
|
</ion-buttons>
|
||||||
|
<ion-title>Menu - Multiple</ion-title>
|
||||||
|
</ion-toolbar>
|
||||||
|
</ion-header>
|
||||||
|
<ion-content class="ion-padding">Content</ion-content>
|
||||||
|
</div>
|
||||||
|
</ion-app>
|
||||||
|
</body>
|
||||||
|
<scrip
|
||||||
|
</html>
|
||||||
79
core/src/components/menu/test/multiple/menu.e2e.ts
Normal file
79
core/src/components/menu/test/multiple/menu.e2e.ts
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
import { expect } from '@playwright/test';
|
||||||
|
import { configs, test } from '@utils/test/playwright';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This behavior does not vary across modes/directions
|
||||||
|
*/
|
||||||
|
configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => {
|
||||||
|
test.describe(title('menu: multiple'), () => {
|
||||||
|
test.beforeEach(async ({ page }, testInfo) => {
|
||||||
|
testInfo.annotations.push({
|
||||||
|
type: 'issue',
|
||||||
|
description: 'https://github.com/ionic-team/ionic-framework/issues/18974',
|
||||||
|
});
|
||||||
|
|
||||||
|
await page.goto(`/src/components/menu/test/multiple`, config);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should present each menu on the same side individually', async ({ page }) => {
|
||||||
|
const primaryMenu = page.locator('ion-menu#primary-menu');
|
||||||
|
const secondaryMenu = page.locator('ion-menu#secondary-menu');
|
||||||
|
|
||||||
|
await primaryMenu.evaluate((el: HTMLIonMenuElement) => el.open());
|
||||||
|
await expect(primaryMenu).toBeVisible();
|
||||||
|
|
||||||
|
await primaryMenu.evaluate((el: HTMLIonMenuElement) => el.close());
|
||||||
|
await expect(primaryMenu).toBeHidden();
|
||||||
|
|
||||||
|
await secondaryMenu.evaluate((el: HTMLIonMenuElement) => el.open());
|
||||||
|
await expect(secondaryMenu).toBeVisible();
|
||||||
|
|
||||||
|
await secondaryMenu.evaluate((el: HTMLIonMenuElement) => el.close());
|
||||||
|
await expect(secondaryMenu).toBeHidden();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should close first menu when showing another menu on same side', async ({ page }) => {
|
||||||
|
const primaryMenu = page.locator('ion-menu#primary-menu');
|
||||||
|
const secondaryMenu = page.locator('ion-menu#secondary-menu');
|
||||||
|
|
||||||
|
await primaryMenu.evaluate((el: HTMLIonMenuElement) => el.open());
|
||||||
|
await expect(primaryMenu).toBeVisible();
|
||||||
|
|
||||||
|
await secondaryMenu.evaluate((el: HTMLIonMenuElement) => el.open());
|
||||||
|
await expect(primaryMenu).toBeHidden();
|
||||||
|
await expect(secondaryMenu).toBeVisible();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('passing side to the menuController when multiple menus have that side should result in a warning', async ({
|
||||||
|
page,
|
||||||
|
}) => {
|
||||||
|
const logs: string[] = [];
|
||||||
|
|
||||||
|
page.on('console', (msg) => {
|
||||||
|
if (msg.type() === 'warning') {
|
||||||
|
logs.push(msg.text());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await page.evaluate(() => (window as any).menuController.open('start'));
|
||||||
|
|
||||||
|
expect(logs.length).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('passing side to the menuController when multiple disabled menus have that side should result in a warning', async ({
|
||||||
|
page,
|
||||||
|
}) => {
|
||||||
|
const logs: string[] = [];
|
||||||
|
|
||||||
|
page.on('console', (msg) => {
|
||||||
|
if (msg.type() === 'warning') {
|
||||||
|
logs.push(msg.text());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await page.evaluate(() => (window as any).menuController.open('end'));
|
||||||
|
|
||||||
|
expect(logs.length).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -1,3 +1,5 @@
|
|||||||
|
import { printIonWarning } from '@utils/logging';
|
||||||
|
|
||||||
import type { MenuI } from '../../components/menu/menu-interface';
|
import type { MenuI } from '../../components/menu/menu-interface';
|
||||||
import type { AnimationBuilder, BackButtonEvent } from '../../interface';
|
import type { AnimationBuilder, BackButtonEvent } from '../../interface';
|
||||||
import { MENU_BACK_BUTTON_PRIORITY } from '../hardware-back-button';
|
import { MENU_BACK_BUTTON_PRIORITY } from '../hardware-back-button';
|
||||||
@ -12,7 +14,7 @@ const createMenuController = () => {
|
|||||||
const menus: MenuI[] = [];
|
const menus: MenuI[] = [];
|
||||||
|
|
||||||
const open = async (menu?: string | null): Promise<boolean> => {
|
const open = async (menu?: string | null): Promise<boolean> => {
|
||||||
const menuEl = await get(menu);
|
const menuEl = await get(menu, true);
|
||||||
if (menuEl) {
|
if (menuEl) {
|
||||||
return menuEl.open();
|
return menuEl.open();
|
||||||
}
|
}
|
||||||
@ -20,7 +22,7 @@ const createMenuController = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const close = async (menu?: string | null): Promise<boolean> => {
|
const close = async (menu?: string | null): Promise<boolean> => {
|
||||||
const menuEl = await (menu !== undefined ? get(menu) : getOpen());
|
const menuEl = await (menu !== undefined ? get(menu, true) : getOpen());
|
||||||
if (menuEl !== undefined) {
|
if (menuEl !== undefined) {
|
||||||
return menuEl.close();
|
return menuEl.close();
|
||||||
}
|
}
|
||||||
@ -28,7 +30,7 @@ const createMenuController = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const toggle = async (menu?: string | null): Promise<boolean> => {
|
const toggle = async (menu?: string | null): Promise<boolean> => {
|
||||||
const menuEl = await get(menu);
|
const menuEl = await get(menu, true);
|
||||||
if (menuEl) {
|
if (menuEl) {
|
||||||
return menuEl.toggle();
|
return menuEl.toggle();
|
||||||
}
|
}
|
||||||
@ -70,20 +72,48 @@ const createMenuController = () => {
|
|||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
const get = async (menu?: string | null): Promise<HTMLIonMenuElement | undefined> => {
|
/**
|
||||||
|
* Finds and returns the menu specified by "menu" if registered.
|
||||||
|
* @param menu - The side or ID of the desired menu
|
||||||
|
* @param logOnMultipleSideMenus - If true, this function will log a warning
|
||||||
|
* if "menu" is a side but multiple menus on the same side were found. Since this function
|
||||||
|
* is used in multiple places, we default this log to false so that the calling
|
||||||
|
* functions can choose whether or not it is appropriate to log this warning.
|
||||||
|
*/
|
||||||
|
const get = async (
|
||||||
|
menu?: string | null,
|
||||||
|
logOnMultipleSideMenus: boolean = false
|
||||||
|
): Promise<HTMLIonMenuElement | undefined> => {
|
||||||
await waitUntilReady();
|
await waitUntilReady();
|
||||||
|
|
||||||
if (menu === 'start' || menu === 'end') {
|
if (menu === 'start' || menu === 'end') {
|
||||||
// there could be more than one menu on the same side
|
// there could be more than one menu on the same side
|
||||||
// so first try to get the enabled one
|
// so first try to get the enabled one
|
||||||
const menuRef = find((m) => m.side === menu && !m.disabled);
|
const menuRefs = menus.filter((m) => m.side === menu && !m.disabled);
|
||||||
if (menuRef) {
|
if (menuRefs.length >= 1) {
|
||||||
return menuRef;
|
if (menuRefs.length > 1 && logOnMultipleSideMenus) {
|
||||||
|
printIonWarning(
|
||||||
|
`menuController queried for a menu on the "${menu}" side, but ${menuRefs.length} menus were found. The first menu reference will be used. If this is not the behavior you want then pass the ID of the menu instead of its side.`,
|
||||||
|
menuRefs.map((m) => m.el)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return menuRefs[0].el;
|
||||||
}
|
}
|
||||||
|
|
||||||
// didn't find a menu side that is enabled
|
// didn't find a menu side that is enabled
|
||||||
// so try to get the first menu side found
|
// so try to get the first menu side found
|
||||||
return find((m) => m.side === menu);
|
const sideMenuRefs = menus.filter((m) => m.side === menu);
|
||||||
|
if (sideMenuRefs.length >= 1) {
|
||||||
|
if (sideMenuRefs.length > 1 && logOnMultipleSideMenus) {
|
||||||
|
printIonWarning(
|
||||||
|
`menuController queried for a menu on the "${menu}" side, but ${sideMenuRefs.length} menus were found. The first menu reference will be used. If this is not the behavior you want then pass the ID of the menu instead of its side.`,
|
||||||
|
sideMenuRefs.map((m) => m.el)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sideMenuRefs[0].el;
|
||||||
|
}
|
||||||
} else if (menu != null) {
|
} else if (menu != null) {
|
||||||
// the menuId was not left or right
|
// the menuId was not left or right
|
||||||
// so try to get the menu by its "id"
|
// so try to get the menu by its "id"
|
||||||
@ -131,9 +161,6 @@ const createMenuController = () => {
|
|||||||
|
|
||||||
const _register = (menu: MenuI) => {
|
const _register = (menu: MenuI) => {
|
||||||
if (menus.indexOf(menu) < 0) {
|
if (menus.indexOf(menu) < 0) {
|
||||||
if (!menu.disabled) {
|
|
||||||
_setActiveMenu(menu);
|
|
||||||
}
|
|
||||||
menus.push(menu);
|
menus.push(menu);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -145,14 +172,6 @@ const createMenuController = () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const _setActiveMenu = (menu: MenuI) => {
|
|
||||||
// if this menu should be enabled
|
|
||||||
// then find all the other menus on this same side
|
|
||||||
// and automatically disable other same side menus
|
|
||||||
const side = menu.side;
|
|
||||||
menus.filter((m) => m.side === side && m !== menu).forEach((m) => (m.disabled = true));
|
|
||||||
};
|
|
||||||
|
|
||||||
const _setOpen = async (menu: MenuI, shouldOpen: boolean, animated: boolean): Promise<boolean> => {
|
const _setOpen = async (menu: MenuI, shouldOpen: boolean, animated: boolean): Promise<boolean> => {
|
||||||
if (isAnimatingSync()) {
|
if (isAnimatingSync()) {
|
||||||
return false;
|
return false;
|
||||||
@ -238,7 +257,6 @@ const createMenuController = () => {
|
|||||||
_register,
|
_register,
|
||||||
_unregister,
|
_unregister,
|
||||||
_setOpen,
|
_setOpen,
|
||||||
_setActiveMenu,
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user