From 16cf280bc2036db62cb70937044ccb43b0e66ae3 Mon Sep 17 00:00:00 2001 From: Ultranmus Date: Thu, 3 Dec 2026 20:00:50 +0530 Subject: [PATCH] fix(popover): dismiss inline popover when its parent is removed --- core/src/components/popover/popover.tsx | 50 +++++++++++++++++++ .../popover/test/inline/popover.e2e.ts | 38 ++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/core/src/components/popover/popover.tsx b/core/src/components/popover/popover.tsx index 70195e38c88..5adcb756d2f 100644 --- a/core/src/components/popover/popover.tsx +++ b/core/src/components/popover/popover.tsx @@ -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 = {}; @@ -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(); } } @@ -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(); @@ -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]) { @@ -590,6 +601,8 @@ export class Popover implements ComponentInterface, PopoverInterface { focusFirstDescendant(el); } + this.initParentRemovalObserver(); + unlock(); } @@ -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 ( + 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'); + } + }); + + 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 @@ -664,6 +712,8 @@ export class Popover implements ComponentInterface, PopoverInterface { ); if (shouldDismiss) { + this.cleanupParentRemovalObserver(); + if (destroyKeyboardInteraction) { destroyKeyboardInteraction(); this.destroyKeyboardInteraction = undefined; diff --git a/core/src/components/popover/test/inline/popover.e2e.ts b/core/src/components/popover/test/inline/popover.e2e.ts index b7f1ea77ba2..25326c7cdc5 100644 --- a/core/src/components/popover/test/inline/popover.e2e.ts +++ b/core/src/components/popover/test/inline/popover.e2e.ts @@ -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( + ` + +
+ + + Popover content + +
+
+ `, + 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/); + }); + }); }); const testPopover = async (page: E2EPage, buttonID: string) => {