-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(popover): dynamic width popover is positioned correctly (#28072)
Issue number: resolves #27190, resolves #24780 --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> Popovers with dynamic widths were not being positioned correctly relative to the trigger element. This was happening because the child component always had dimensions of 0 x 0. Ionic has logic built-in to wait for the child components to be rendered, but this was not working as intended for two reasons: 1. `this.usersElement` was referencing the popover element itself not the user’s component. When calling `deepReady` on https://github.com/ionic-team/ionic-framework/blob/01fc9b45116f7ad6ddc56c7fb1535dec798c2b3a/core/src/components/popover/popover.tsx#L477 we are waiting for the popover to be hydrated, not the child content. The popover was already hydrated on page load, so this resolves immediately. However, the child content that was just added to the DOM has not yet been hydrated, so we aren’t waiting long enough. This is happening because we return `BaseComponent `from `attachComponent` which is a reference to the overlay: https://github.com/ionic-team/ionic-framework/blob/01fc9b45116f7ad6ddc56c7fb1535dec798c2b3a/core/src/utils/framework-delegate.ts#L133 Other framework delegates return the actual child content: - Core delegate with controller: https://github.com/ionic-team/ionic-framework/blob/01fc9b45116f7ad6ddc56c7fb1535dec798c2b3a/core/src/utils/framework-delegate.ts#L35 (this is part of why the controller popover works but the inline popover does not) - React delegate: https://github.com/ionic-team/ionic-framework/blob/01fc9b45116f7ad6ddc56c7fb1535dec798c2b3a/packages/react/src/framework-delegate.tsx#L31 - Vue delegate: https://github.com/ionic-team/ionic-framework/blob/01fc9b45116f7ad6ddc56c7fb1535dec798c2b3a/packages/vue/src/framework-delegate.ts#L45 2. `attachComponent` is unable to return the correct element currently because the child content has not been mounted yet in this scenario. `ionMount` is emitted after `attachComponent` resolves: https://github.com/ionic-team/ionic-framework/blob/01fc9b45116f7ad6ddc56c7fb1535dec798c2b3a/core/src/components/popover/popover.tsx#L466 ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - `ionMount` is emitted before `attachComponent` runs - `attachComponent` now consistently returns the child view if present in the DOM - Added a test ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change, please describe the impact and migration path for existing applications below. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> Dev build: `7.3.2-dev.11693321763.15a54694` --------- Co-authored-by: ionitron <[email protected]>
- Loading branch information
1 parent
cddefd1
commit 2a80eb6
Showing
8 changed files
with
143 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<!DOCTYPE html> | ||
<html lang="en" dir="ltr"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<title>Popover - Async</title> | ||
<meta | ||
name="viewport" | ||
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover" | ||
/> | ||
<link href="../../../../../css/ionic.bundle.css" rel="stylesheet" /> | ||
<link href="../../../../../scripts/testing/styles.css" rel="stylesheet" /> | ||
<script src="../../../../../scripts/testing/scripts.js"></script> | ||
<script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script> | ||
<style> | ||
ion-popover { | ||
--width: fit-content; | ||
} | ||
|
||
.wrapper { | ||
display: flex; | ||
justify-content: center; | ||
align-items: end; | ||
|
||
height: 100%; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<ion-content class="ion-padding"> | ||
<div class="wrapper"> | ||
<button id="button">Open Popover</button> | ||
</div> | ||
|
||
<ion-popover trigger="button"></ion-popover> | ||
</ion-content> | ||
|
||
<script> | ||
const popover = document.querySelector('ion-popover'); | ||
|
||
popover.addEventListener('ionMount', () => { | ||
popover.innerHTML = ` | ||
<div style="padding: 10px;"> | ||
<ion-list> | ||
<ion-item>Item 1</ion-item> | ||
</ion-list> | ||
</div> | ||
`; | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { expect } from '@playwright/test'; | ||
import { configs, test } from '@utils/test/playwright'; | ||
|
||
/** | ||
* This behavior does not vary across modes/directions | ||
*/ | ||
configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, screenshot, config }) => { | ||
test.describe(title('popover: alignment with async component'), async () => { | ||
test('should align popover centered with button when component is added async', async ({ page }) => { | ||
await page.goto('/src/components/popover/test/async', config); | ||
|
||
const ionPopoverDidPresent = await page.spyOnEvent('ionPopoverDidPresent'); | ||
|
||
const button = page.locator('#button'); | ||
await button.click(); | ||
|
||
await ionPopoverDidPresent.next(); | ||
|
||
await expect(page).toHaveScreenshot(screenshot(`popover-async`)); | ||
}); | ||
/** | ||
* Framework delegate should fall back to returning the host | ||
* component when no child content is passed otherwise | ||
* the overlay will get stuck when trying to re-present. | ||
*/ | ||
test('should open popover even if nothing was passed', async ({ page }) => { | ||
await page.setContent( | ||
` | ||
<ion-popover></ion-popover> | ||
`, | ||
config | ||
); | ||
|
||
const popover = page.locator('ion-popover'); | ||
const ionPopoverDidPresent = await page.spyOnEvent('ionPopoverDidPresent'); | ||
const ionPopoverDidDismiss = await page.spyOnEvent('ionPopoverDidDismiss'); | ||
|
||
await popover.evaluate((el: HTMLIonPopoverElement) => el.present()); | ||
|
||
await ionPopoverDidPresent.next(); | ||
await expect(popover).toBeVisible(); | ||
|
||
await popover.evaluate((el: HTMLIonPopoverElement) => el.dismiss()); | ||
|
||
await ionPopoverDidDismiss.next(); | ||
await expect(popover).toBeHidden(); | ||
|
||
await popover.evaluate((el: HTMLIonPopoverElement) => el.present()); | ||
|
||
await ionPopoverDidPresent.next(); | ||
await expect(popover).toBeVisible(); | ||
}); | ||
}); | ||
}); |
Binary file added
BIN
+5.59 KB
...st/async/popover.e2e.ts-snapshots/popover-async-ios-ltr-Mobile-Chrome-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+13.3 KB
...t/async/popover.e2e.ts-snapshots/popover-async-ios-ltr-Mobile-Firefox-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+5.33 KB
...st/async/popover.e2e.ts-snapshots/popover-async-ios-ltr-Mobile-Safari-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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