Repository navigation
fix(popover): dismiss inline popover when its parent is removed #31868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ultranmus
wants to merge
1
commit into
ionic-team:main
Choose a base branch
from
Ultranmus:fix/popover-parent-removed
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+88
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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'); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
| }); | ||||||
|
|
||||||
| 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; | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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/); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The import at the top of the file also needs import { configs, detachAndReattach, test } from '@utils/test/playwright'; |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const testPopover = async (page: E2EPage, buttonID: string) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
document.body. An observer on the parent never fires when the parent itself is removed, so someone trusting the comment could switch toobserve(cachedOriginalParent)and break the dismiss.bodyorion-appparents for performance, not validity: a subtree observer onbodyhung Angular apps when this ran for controller modals. Without the reason, a later cleanup could drop those clauses.