Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions core/src/components/menu/menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,17 @@ export class Menu implements ComponentInterface, MenuI {
return false;
}

/**
* A pointer that went down before this animation may already have
* passed canStart. Reset the swipe gesture so that the pointer cannot
* start a swipe while the menu animates, which would call
* beforeAnimation a second time.
*/
if (this.gesture) {
this.gesture.enable(false);
this.gesture.enable(this._isActive() && this.swipeGesture);
}

this.beforeAnimation(shouldOpen, role);

await this.loadAnimation();
Expand Down
41 changes: 41 additions & 0 deletions core/src/components/menu/test/gesture/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8" />
<title>Menu - Gesture</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>
</head>

<body>
<ion-app>
<ion-menu side="start" id="start-menu" menu-id="start-menu" content-id="main">
<ion-header>
<ion-toolbar color="primary">
<ion-title>Menu</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></ion-menu-button>
</ion-buttons>
<ion-title>Menu - Gesture</ion-title>
</ion-toolbar>
</ion-header>
<ion-content class="ion-padding">Content</ion-content>
</div>
</ion-app>
</body>
</html>
101 changes: 101 additions & 0 deletions core/src/components/menu/test/gesture/menu.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import type { Locator } from '@playwright/test';
import { expect } from '@playwright/test';
import type { E2EPage } from '@utils/test/playwright';
import { configs, test } from '@utils/test/playwright';

/**
* Records the time of the last mouse move and of the event that ends
* the menu animation, so the test can check that the drag happened
* while the menu animated. Without that, the test would pass even
* without the fix.
*/
const trackDrag = (menu: Locator, endEvent: 'ionDidOpen' | 'ionDidClose') =>
menu.evaluate((el: HTMLIonMenuElement, endEvent: string) => {
const w = window as any;
w.lastMoveAt = 0;
w.animationEndAt = Infinity;
document.addEventListener('mousemove', () => (w.lastMoveAt = performance.now()));
el.addEventListener(endEvent, () => (w.animationEndAt = performance.now()), { once: true });
}, endEvent);

const draggedDuringAnimation = (page: E2EPage) =>
page.evaluate(() => {
const w = window as any;
return w.lastMoveAt > 0 && w.lastMoveAt < w.animationEndAt;
});

/**
* This behavior does not vary across modes/directions
*/
configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, config }) => {
test.describe(title('menu: gesture'), () => {
test.beforeEach(async ({ page }) => {
// The race needs a running menu animation, which _testing turns off.
await page.goto(`/src/components/menu/test/gesture?ionic:_testing=false`, config);
});

test('should not throw when opened programmatically after a drag has started', async ({ page }) => {
const errors: string[] = [];
page.on('pageerror', (error) => errors.push(error.message));
const ionDidOpen = await page.spyOnEvent('ionDidOpen');
const menu = page.locator('#start-menu');
await trackDrag(menu, 'ionDidOpen');

// The pointer goes down at the menu edge before the menu animates.
await page.mouse.move(10, 300);
await page.mouse.down();

// Resolve as soon as the animation starts, in the same round trip.
await menu.evaluate(
(el: HTMLIonMenuElement) =>
new Promise<void>((resolve) => {
el.addEventListener('ionWillOpen', () => resolve(), { once: true });
el.open();
})
);

// One move passes the gesture threshold while the menu animates.
await page.mouse.move(40, 300);
await page.mouse.up();

await ionDidOpen.next();
expect(await draggedDuringAnimation(page)).toBe(true);
await expect(menu).toHaveClass(/show-menu/);
expect(errors).toEqual([]);
});

test('should not throw when closed programmatically after a drag has started', async ({ page }) => {
const errors: string[] = [];
page.on('pageerror', (error) => errors.push(error.message));
const ionDidOpen = await page.spyOnEvent('ionDidOpen');
const ionDidClose = await page.spyOnEvent('ionDidClose');
const menu = page.locator('#start-menu');

await menu.evaluate((el: HTMLIonMenuElement) => el.open());
await ionDidOpen.next();
await trackDrag(menu, 'ionDidClose');

// The pointer goes down while the menu is open, before it animates.
await page.mouse.move(150, 300);
await page.mouse.down();

// Resolve as soon as the animation starts, in the same round trip.
await menu.evaluate(
(el: HTMLIonMenuElement) =>
new Promise<void>((resolve) => {
el.addEventListener('ionWillClose', () => resolve(), { once: true });
el.close();
})
);

// One move passes the gesture threshold while the menu animates.
await page.mouse.move(120, 300);
await page.mouse.up();

await ionDidClose.next();
expect(await draggedDuringAnimation(page)).toBe(true);
await expect(menu).not.toHaveClass(/show-menu/);
expect(errors).toEqual([]);
});
});
});