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
30 changes: 30 additions & 0 deletions core/src/components/item-sliding/item-sliding.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ComponentInterface, EventEmitter } from '@stencil/core';
import { Component, Element, Event, Host, Method, Prop, State, Watch, h } from '@stencil/core';
import { doc } from '@utils/browser';
import { findClosestIonContent, disableContentScrollY, resetContentScrollY } from '@utils/content';
import { componentOnReady, isEndSide } from '@utils/helpers';
import { printIonWarning } from '@utils/logging';
Expand Down Expand Up @@ -117,6 +118,8 @@ export class ItemSliding implements ComponentInterface {
openSlidingItem = undefined;
}

this.stopWatchingForOutsidePress();

if (this.mutationObserver) {
this.mutationObserver.disconnect();
this.mutationObserver = undefined;
Expand Down Expand Up @@ -202,6 +205,7 @@ export class ItemSliding implements ComponentInterface {

const width = side === 'end' ? this.optsWidthRightSide : -this.optsWidthLeftSide;
openSlidingItem = this.el;
this.watchForOutsidePress();

this.setOpenAmount(width, false);
this.state = side === 'end' ? SlidingState.End : SlidingState.Start;
Expand Down Expand Up @@ -309,6 +313,7 @@ export class ItemSliding implements ComponentInterface {
}

openSlidingItem = this.el;
this.watchForOutsidePress();

if (this.tmr !== undefined) {
clearTimeout(this.tmr);
Expand Down Expand Up @@ -387,6 +392,30 @@ export class ItemSliding implements ComponentInterface {
}
}

/**
* Native lists close an open item when the user presses anywhere else.
* Capture so the item still closes if an application's own pointerdown
* handler stops the event before it reaches the document.
*/
private watchForOutsidePress() {
doc?.addEventListener('pointerdown', this.onPointerDownOutside, { capture: true });
}

private stopWatchingForOutsidePress() {
doc?.removeEventListener('pointerdown', this.onPointerDownOutside, { capture: true });
}

/**
* The item can live in another component's Shadow DOM, where a document
* listener sees the target retargeted to the outer host, so the composed
* path is used to tell a press on the item apart.
*/
private onPointerDownOutside = (ev: Event) => {
if (!ev.composedPath().includes(this.el)) {
this.close();
}
};

private calculateOptsWidth() {
this.optsWidthRightSide = 0;
if (this.rightOptions) {
Expand Down Expand Up @@ -460,6 +489,7 @@ export class ItemSliding implements ComponentInterface {
}, 600);

openSlidingItem = undefined;
this.stopWatchingForOutsidePress();
style.transform = '';
return;
}
Expand Down
62 changes: 62 additions & 0 deletions core/src/components/item-sliding/test/basic/item-sliding.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,68 @@ configs({ modes: ['md'], directions: ['ltr'] }).forEach(({ title, screenshot, co

expect(await scrollEl.evaluate((el: HTMLElement) => el.scrollTop)).toEqual(0);
});

test.describe('pressing outside', () => {
test.beforeEach(async ({ page }, testInfo) => {
testInfo.annotations.push({
type: 'issue',
description: 'https://github.com/ionic-team/ionic-framework/issues/29504',
});

await page.setContent(
`
<ion-content>
<button id="outside">Outside</button>
<ion-list>
<ion-item-sliding>
<ion-item>
<ion-label>Sliding Item</ion-label>
</ion-item>
<ion-item-options side="end">
<ion-item-option>Archive</ion-item-option>
</ion-item-options>
</ion-item-sliding>
</ion-list>
</ion-content>
`,
config
);
});

const getOpenAmount = (page: E2EPage) =>
page.locator('ion-item-sliding').evaluate((el: HTMLIonItemSlidingElement) => el.getOpenAmount());

const openItem = async (page: E2EPage) => {
await page.locator('ion-item-sliding').evaluate((el: HTMLIonItemSlidingElement) => el.open('end'));
await expect.poll(() => getOpenAmount(page)).not.toBe(0);
};

test('should close an item opened with open() when pressing outside of it', async ({ page }) => {
await openItem(page);

await page.locator('#outside').click();

await expect.poll(() => getOpenAmount(page)).toBe(0);
});

test('should close a swiped open item when pressing outside of it', async ({ page }) => {
await dragElementBy(page.locator('ion-item-sliding'), page, -150);
await expect.poll(() => getOpenAmount(page)).not.toBe(0);

await page.locator('#outside').click();

await expect.poll(() => getOpenAmount(page)).toBe(0);
});

test('should not close the open item when pressing one of its options', async ({ page }) => {
await openItem(page);

await page.locator('ion-item-option').click();
await page.waitForChanges();

expect(await getOpenAmount(page)).not.toBe(0);
});
});
});
});

Expand Down