Skip to content
Merged
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
57 changes: 57 additions & 0 deletions core/src/components/popover/popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export class Popover implements ComponentInterface, PopoverInterface {
private destroyKeyboardInteraction?: () => void;
private destroyDismissInteraction?: () => void;
private headerResizeObserver?: ResizeObserver;
private parentRemovalObserver?: MutationObserver;
private cachedOriginalParent?: HTMLElement;

private inline = false;
private inheritedAttributes: Attributes = {};
Expand Down Expand Up @@ -369,6 +371,7 @@ export class Popover implements ComponentInterface, PopoverInterface {
// The disconnect tore down the header observer, and `present()` is its
// only other caller, so a move mid-layout would lose the recalculation.
this.recalculateContentOnHeaderReady();
this.initParentRemovalObserver();
}
}

Expand All @@ -384,6 +387,8 @@ export class Popover implements ComponentInterface, PopoverInterface {
this.headerResizeObserver = undefined;
}

this.cleanupParentRemovalObserver();

// Clean up aria-hidden if removed without dismiss() being called
if (this.presented) {
cleanupRootFocusTrapAccessibility();
Expand All @@ -395,6 +400,12 @@ export class Popover implements ComponentInterface, PopoverInterface {

const attributesToInherit = ['aria-label', 'role'];
this.inheritedAttributes = inheritAttributes(el, attributesToInherit);

// Cache original parent before popover gets moved to the app root during presentation
if (el.parentNode) {
this.cachedOriginalParent = el.parentNode as HTMLElement;
}

if (htmlAttributes !== undefined) {
attributesToInherit.forEach((attribute) => {
if (htmlAttributes[attribute]) {
Expand Down Expand Up @@ -590,6 +601,8 @@ export class Popover implements ComponentInterface, PopoverInterface {
focusFirstDescendant(el);
}

this.initParentRemovalObserver();

unlock();
}

Expand Down Expand Up @@ -633,6 +646,48 @@ export class Popover implements ComponentInterface, PopoverInterface {
this.headerResizeObserver.observe(header);
}

/**
* Inline popovers are moved to the app root on present, so removing the
* element they were declared in does not disconnect them. The observer
* watches the body, since an observer on the parent never fires when the
* parent itself is removed.
*/
private initParentRemovalObserver() {
const { cachedOriginalParent } = this;

// Controller popovers and app-root parents are never removed, and a
// subtree observer on the body slows Angular change detection.
if (
typeof MutationObserver === 'undefined' ||
!cachedOriginalParent ||
this.hasController ||
cachedOriginalParent.nodeType !== Node.ELEMENT_NODE ||
cachedOriginalParent === document.body ||
cachedOriginalParent.tagName === 'ION-APP'
) {
return;
}

this.cleanupParentRemovalObserver();

this.parentRemovalObserver = new MutationObserver(() => {
if (!cachedOriginalParent.isConnected) {
this.cleanupParentRemovalObserver();
this.dismiss(undefined, 'parent-removed', false);
}
});

this.parentRemovalObserver.observe(document.body, {
childList: true,
subtree: true,
});
}

private cleanupParentRemovalObserver() {
this.parentRemovalObserver?.disconnect();
this.parentRemovalObserver = undefined;
}

/**
* Dismiss the popover overlay after it has been presented.
* This is a no-op if the overlay has not been presented yet. If you want
Expand Down Expand Up @@ -664,6 +719,8 @@ export class Popover implements ComponentInterface, PopoverInterface {
);

if (shouldDismiss) {
this.cleanupParentRemovalObserver();

if (destroyKeyboardInteraction) {
destroyKeyboardInteraction();
this.destroyKeyboardInteraction = undefined;
Expand Down
108 changes: 107 additions & 1 deletion core/src/components/popover/test/inline/popover.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from '@playwright/test';
import type { E2EPage } from '@utils/test/playwright';
import { configs, test } from '@utils/test/playwright';
import { configs, detachAndReattach, test } from '@utils/test/playwright';

import { closePopover, openPopover } from '../test.utils';

Expand All @@ -20,6 +20,112 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) =>
await testPopover(page, 'method');
});
});

test.describe(title('popover: inline parent removal'), async () => {
test('should dismiss when the element it was declared in is removed', async ({ page }, testInfo) => {
testInfo.annotations.push({
type: 'issue',
description: 'https://github.com/ionic-team/ionic-framework/issues/31858',
});

await page.setContent(
`
<ion-app>
<div id="container">
<button id="trigger">Open</button>
<ion-popover trigger="trigger">
<ion-content>Popover content</ion-content>
</ion-popover>
</div>
</ion-app>
`,
config
);

const ionPopoverDidPresent = await page.spyOnEvent('ionPopoverDidPresent');
const ionPopoverDidDismiss = await page.spyOnEvent('ionPopoverDidDismiss');
const popover = page.locator('ion-popover');

await page.click('#trigger');
await ionPopoverDidPresent.next();
await expect(popover).toBeVisible();

await page.locator('#container').evaluate((el: HTMLElement) => el.remove());

const dismissEvent = await ionPopoverDidDismiss.next();
expect(dismissEvent.detail.role).toBe('parent-removed');
await expect(popover).toBeHidden();
await expect(page.locator('body')).not.toHaveClass(/backdrop-no-scroll/);
});
Comment thread
thetaPC marked this conversation as resolved.
Resolved

test('should still dismiss on parent removal after the popover was moved', async ({ page }) => {
await page.setContent(
`
<ion-app>
<div id="container">
<button id="trigger">Open</button>
<ion-popover trigger="trigger">
<ion-content>Popover content</ion-content>
</ion-popover>
</div>
</ion-app>
`,
config
);

const ionPopoverDidPresent = await page.spyOnEvent('ionPopoverDidPresent');
const ionPopoverDidDismiss = await page.spyOnEvent('ionPopoverDidDismiss');
const popover = page.locator('ion-popover');

await page.click('#trigger');
await ionPopoverDidPresent.next();

await detachAndReattach(popover);
await page.locator('#container').evaluate((el: HTMLElement) => el.remove());

const dismissEvent = await ionPopoverDidDismiss.next();
expect(dismissEvent.detail.role).toBe('parent-removed');
await expect(popover).toBeHidden();
});

test('should not dismiss the parent popover when a nested popover is removed', async ({ page }) => {
await page.setContent(
`
<ion-app>
<button id="parent-trigger">Open</button>
<ion-popover trigger="parent-trigger" class="parent-popover">
<ion-content>
<div id="submenu">
<ion-item button id="child-trigger">Open submenu</ion-item>
<ion-popover trigger="child-trigger" class="child-popover">
<ion-content>Submenu content</ion-content>
</ion-popover>
</div>
</ion-content>
</ion-popover>
</ion-app>
`,
config
);

const ionPopoverDidPresent = await page.spyOnEvent('ionPopoverDidPresent');
const ionPopoverDidDismiss = await page.spyOnEvent('ionPopoverDidDismiss');
const parentPopover = page.locator('.parent-popover');
const childPopover = page.locator('.child-popover');

await page.click('#parent-trigger');
await ionPopoverDidPresent.next();
await page.click('#child-trigger');
await ionPopoverDidPresent.next();

await page.locator('#submenu').evaluate((el: HTMLElement) => el.remove());

const dismissEvent = await ionPopoverDidDismiss.next();
expect(dismissEvent.detail.role).toBe('parent-removed');
await expect(childPopover).toBeHidden();
await expect(parentPopover).toBeVisible();
});
});
});

const testPopover = async (page: E2EPage, buttonID: string) => {
Expand Down
Loading