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
50 changes: 50 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,41 @@ export class Popover implements ComponentInterface, PopoverInterface {
this.headerResizeObserver.observe(header);
}

// Inline popovers are moved to the app root on present, so watch the original parent instead.
private initParentRemovalObserver() {
const { cachedOriginalParent } = this;

if (
Comment on lines +649 to +653

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • The comment says the method watches the original parent, but it observes document.body. An observer on the parent never fires when the parent itself is removed, so someone trusting the comment could switch to observe(cachedOriginalParent) and break the dismiss.
  • The guard skips controller popovers and body or ion-app parents for performance, not validity: a subtree observer on body hung Angular apps when this ran for controller modals. Without the reason, a later cleanup could drop those clauses.
Suggested change
// Inline popovers are moved to the app root on present, so watch the original parent instead.
private initParentRemovalObserver() {
const { cachedOriginalParent } = this;
if (
/**
* 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');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Leaving dismissParentPopover at its default makes removing a submenu's container also close the parent popover, with role parent-removed, even though the parent's own container is still there. That's a regression for nested menus.
  • Every other automatic dismiss of a single nested popover passes false, such as the parent's ionPopoverWillDismiss listener and the hover and ArrowLeft handlers in utils.ts.
  • Could you add a nested case to the test too: open a submenu, remove the element it's declared in, and check that the parent stays open?
Suggested change
this.dismiss(undefined, 'parent-removed');
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 +712,8 @@ export class Popover implements ComponentInterface, PopoverInterface {
);

if (shouldDismiss) {
this.cleanupParentRemovalObserver();

if (destroyKeyboardInteraction) {
destroyKeyboardInteraction();
this.destroyKeyboardInteraction = undefined;
Expand Down
38 changes: 38 additions & 0 deletions core/src/components/popover/test/inline/popover.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,44 @@ 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/);
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Moving an open popover runs disconnectedCallback, which tears down the observer, so only the new initParentRemovalObserver() call in connectedCallback brings it back. The test above never moves the popover, so it still passes if that call is removed.
  • Modal covers this case with "it should still dismiss on parent removal after the modal was moved" in modal/test/inline/modal.e2e.ts.
Suggested change
});
});
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();
});

The import at the top of the file also needs detachAndReattach:

import { configs, detachAndReattach, test } from '@utils/test/playwright';

});
});

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