Skip to content

Commit 2a80eb6

Browse files
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]>
1 parent cddefd1 commit 2a80eb6

File tree

8 files changed

+143
-5
lines changed

8 files changed

+143
-5
lines changed

core/src/components/modal/modal.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,16 @@ export class Modal implements ComponentInterface, OverlayInterface {
447447
this.currentBreakpoint = this.initialBreakpoint;
448448

449449
const { inline, delegate } = this.getDelegate(true);
450-
this.usersElement = await attachComponent(delegate, el, this.component, ['ion-page'], this.componentProps, inline);
451450

451+
/**
452+
* Emit ionMount so JS Frameworks have an opportunity
453+
* to add the child component to the DOM. The child
454+
* component will be assigned to this.usersElement below.
455+
*/
452456
this.ionMount.emit();
453457

458+
this.usersElement = await attachComponent(delegate, el, this.component, ['ion-page'], this.componentProps, inline);
459+
454460
/**
455461
* When using the lazy loaded build of Stencil, we need to wait
456462
* for every Stencil component instance to be ready before presenting

core/src/components/popover/popover.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,14 @@ export class Popover implements ComponentInterface, PopoverInterface {
449449
const { el } = this;
450450

451451
const { inline, delegate } = this.getDelegate(true);
452+
453+
/**
454+
* Emit ionMount so JS Frameworks have an opportunity
455+
* to add the child component to the DOM. The child
456+
* component will be assigned to this.usersElement below.
457+
*/
458+
this.ionMount.emit();
459+
452460
this.usersElement = await attachComponent(
453461
delegate,
454462
el,
@@ -463,8 +471,6 @@ export class Popover implements ComponentInterface, PopoverInterface {
463471
}
464472
this.configureDismissInteraction();
465473

466-
this.ionMount.emit();
467-
468474
/**
469475
* When using the lazy loaded build of Stencil, we need to wait
470476
* for every Stencil component instance to be ready before presenting
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<!DOCTYPE html>
2+
<html lang="en" dir="ltr">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Popover - Async</title>
6+
<meta
7+
name="viewport"
8+
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"
9+
/>
10+
<link href="../../../../../css/ionic.bundle.css" rel="stylesheet" />
11+
<link href="../../../../../scripts/testing/styles.css" rel="stylesheet" />
12+
<script src="../../../../../scripts/testing/scripts.js"></script>
13+
<script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script>
14+
<style>
15+
ion-popover {
16+
--width: fit-content;
17+
}
18+
19+
.wrapper {
20+
display: flex;
21+
justify-content: center;
22+
align-items: end;
23+
24+
height: 100%;
25+
}
26+
</style>
27+
</head>
28+
<body>
29+
<ion-content class="ion-padding">
30+
<div class="wrapper">
31+
<button id="button">Open Popover</button>
32+
</div>
33+
34+
<ion-popover trigger="button"></ion-popover>
35+
</ion-content>
36+
37+
<script>
38+
const popover = document.querySelector('ion-popover');
39+
40+
popover.addEventListener('ionMount', () => {
41+
popover.innerHTML = `
42+
<div style="padding: 10px;">
43+
<ion-list>
44+
<ion-item>Item 1</ion-item>
45+
</ion-list>
46+
</div>
47+
`;
48+
});
49+
</script>
50+
</body>
51+
</html>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { expect } from '@playwright/test';
2+
import { configs, test } from '@utils/test/playwright';
3+
4+
/**
5+
* This behavior does not vary across modes/directions
6+
*/
7+
configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, screenshot, config }) => {
8+
test.describe(title('popover: alignment with async component'), async () => {
9+
test('should align popover centered with button when component is added async', async ({ page }) => {
10+
await page.goto('/src/components/popover/test/async', config);
11+
12+
const ionPopoverDidPresent = await page.spyOnEvent('ionPopoverDidPresent');
13+
14+
const button = page.locator('#button');
15+
await button.click();
16+
17+
await ionPopoverDidPresent.next();
18+
19+
await expect(page).toHaveScreenshot(screenshot(`popover-async`));
20+
});
21+
/**
22+
* Framework delegate should fall back to returning the host
23+
* component when no child content is passed otherwise
24+
* the overlay will get stuck when trying to re-present.
25+
*/
26+
test('should open popover even if nothing was passed', async ({ page }) => {
27+
await page.setContent(
28+
`
29+
<ion-popover></ion-popover>
30+
`,
31+
config
32+
);
33+
34+
const popover = page.locator('ion-popover');
35+
const ionPopoverDidPresent = await page.spyOnEvent('ionPopoverDidPresent');
36+
const ionPopoverDidDismiss = await page.spyOnEvent('ionPopoverDidDismiss');
37+
38+
await popover.evaluate((el: HTMLIonPopoverElement) => el.present());
39+
40+
await ionPopoverDidPresent.next();
41+
await expect(popover).toBeVisible();
42+
43+
await popover.evaluate((el: HTMLIonPopoverElement) => el.dismiss());
44+
45+
await ionPopoverDidDismiss.next();
46+
await expect(popover).toBeHidden();
47+
48+
await popover.evaluate((el: HTMLIonPopoverElement) => el.present());
49+
50+
await ionPopoverDidPresent.next();
51+
await expect(popover).toBeVisible();
52+
});
53+
});
54+
});
5.59 KB
Loading
13.3 KB
Loading
5.33 KB
Loading

core/src/utils/framework-delegate.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export const CoreDelegate = () => {
5656
cssClasses: string[] = []
5757
) => {
5858
BaseComponent = parentElement;
59+
let ChildComponent;
5960
/**
6061
* If passing in a component via the `component` props
6162
* we need to append it inside of our overlay component.
@@ -87,6 +88,8 @@ export const CoreDelegate = () => {
8788
*/
8889
BaseComponent.appendChild(el);
8990

91+
ChildComponent = el;
92+
9093
await new Promise((resolve) => componentOnReady(el, resolve));
9194
} else if (
9295
BaseComponent.children.length > 0 &&
@@ -96,7 +99,7 @@ export const CoreDelegate = () => {
9699
* The delegate host wrapper el is only needed for modals and popovers
97100
* because they allow the dev to provide custom content to the overlay.
98101
*/
99-
const root = BaseComponent.children[0] as HTMLElement;
102+
const root = (ChildComponent = BaseComponent.children[0] as HTMLElement);
100103
if (!root.classList.contains('ion-delegate-host')) {
101104
/**
102105
* If the root element is not a delegate host, it means
@@ -111,6 +114,13 @@ export const CoreDelegate = () => {
111114
el.append(...BaseComponent.children);
112115
// Append the new parent element to the original parent element.
113116
BaseComponent.appendChild(el);
117+
118+
/**
119+
* Update the ChildComponent to be the
120+
* newly created div in the event that one
121+
* does not already exist.
122+
*/
123+
ChildComponent = el;
114124
}
115125
}
116126

@@ -130,7 +140,18 @@ export const CoreDelegate = () => {
130140

131141
app.appendChild(BaseComponent);
132142

133-
return BaseComponent;
143+
/**
144+
* We return the child component rather than the overlay
145+
* reference itself since modal and popover will
146+
* use this to wait for any Ionic components in the child view
147+
* to be ready (i.e. componentOnReady) when using the
148+
* lazy loaded component bundle.
149+
*
150+
* However, we fall back to returning BaseComponent
151+
* in the event that a modal or popover is presented
152+
* with no child content.
153+
*/
154+
return ChildComponent ?? BaseComponent;
134155
};
135156

136157
const removeViewFromDom = () => {

0 commit comments

Comments
 (0)