Skip to content

fix(popover): dismiss inline popover when its parent is removed - #31868

Open
Ultranmus wants to merge 1 commit into
ionic-team:mainfrom
Ultranmus:fix/popover-parent-removed
Open

Ultranmus wants to merge 1 commit into
ionic-team:mainfrom
Ultranmus:fix/popover-parent-removed

Conversation

@Ultranmus

Copy link
Copy Markdown

Issue number: resolves #31858


What is the current behavior?

An inline ion-popover is moved to the app root while it's presented. If the element it was declared in gets removed (an @if turning false, for example), the popover stays open over the page, with its backdrop and backdrop-no-scroll on body, until the user taps the backdrop.

What is the new behavior?

  • The popover caches its original parent and, while presented, dismisses itself with the parent-removed role when that parent is removed. This is the same thing ion-modal does since fix(modal): dismiss modal when parent element is removed from DOM #30544.
  • The observer is set up at the end of present() and again if the presented popover is moved, and is cleaned up on dismiss and disconnect.
  • Controller popovers and popovers whose parent is body or ion-app are skipped, as in modal.

Does this introduce a breaking change?

  • Yes
  • No

Other information

Added an e2e test in popover/test/inline that opens a popover, removes its container and checks it's dismissed with the right role and that body no longer has backdrop-no-scroll. It fails without the change. The popover, modal, select and datetime-button e2e suites pass locally on Mobile Chrome, along with the popover spec tests and lint.

@Ultranmus
Ultranmus requested a review from a team as a code owner December 3, 2026 14:31
@Ultranmus
Ultranmus requested a review from ShaneK December 3, 2026 14:31
@vercel

vercel Bot commented Dec 3, 2026

Copy link
Copy Markdown

@Ultranmus is attempting to deploy a commit to the Ionic Team on Vercel.

A member of the Team first needs to authorize it.

@thetaPC thetaPC left a comment

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.

Thanks for the fix!

The approach matches what ion-modal does, and checking isConnected alone is an improvement: unlike the modal, the popover stays open when its parent is only moved. A couple of changes below, the first one for nested popovers.

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);

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';

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

if (

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 (

@vercel

vercel Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
ionic-framework Ready Ready Preview Dec 3, 2026 11:11pm UTC

Request Review

This branch was successfully deployed

1 active deployment
Preview — 16cf280b Deployed Dec 3, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: inline popover stays presented after the component that holds it is removed

2 participants