-
Notifications
You must be signed in to change notification settings - Fork 13.4k
fix(content): automatically recalculate offset when header/footer changes #30862
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
ada35da
3d62d93
c080622
95ddd56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,8 @@ import type { ScrollBaseDetail, ScrollDetail } from './content-interface'; | |
| }) | ||
| export class Content implements ComponentInterface { | ||
| private watchDog: ReturnType<typeof setInterval> | null = null; | ||
| private mutationObserver: MutationObserver | null = null; | ||
| private resizeObserver: ResizeObserver | null = null; | ||
| private isScrolling = false; | ||
| private lastScroll = 0; | ||
| private queued = false; | ||
|
|
@@ -168,10 +170,12 @@ export class Content implements ComponentInterface { | |
| closestTabs.addEventListener('ionTabBarLoaded', this.tabsLoadCallback); | ||
| } | ||
| } | ||
| this.connectObservers(); | ||
| } | ||
|
|
||
| disconnectedCallback() { | ||
| this.onScrollEnd(); | ||
| this.disconnectObservers(); | ||
|
|
||
| if (hasLazyBuild(this.el)) { | ||
| /** | ||
|
|
@@ -420,6 +424,85 @@ export class Content implements ComponentInterface { | |
| return promise; | ||
| } | ||
|
|
||
| /** | ||
| * We need to observe the parent element to detect when | ||
| * <ion-header> or <ion-footer> elements are added/removed | ||
| * or resized. This ensures the content offset is recalculated | ||
| * dynamically. | ||
| */ | ||
| private connectObservers() { | ||
| if (!Build.isBrowser) { | ||
| return; | ||
| } | ||
|
|
||
| const parent = this.el.parentElement; | ||
| if (!parent) { | ||
| return; | ||
| } | ||
|
|
||
| if ('ResizeObserver' in window) { | ||
| this.resizeObserver = new ResizeObserver(() => { | ||
| this.resize(); | ||
| }); | ||
|
Comment on lines
+444
to
+453
Member
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. The Could you hoist this to a class field (something like
Author
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. I adjusted it in the latest commit.
Member
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. Awesome! Yeah, a rAF might be a better solution there, probably worth trying out. It should be a pretty quick test to see if it works better, basically the same structure, you'd even want to cache it and clear it out the same. |
||
| } | ||
|
|
||
| if ('MutationObserver' in window) { | ||
| this.mutationObserver = new MutationObserver((mutations) => { | ||
| let shouldUpdate = false; | ||
|
|
||
| for (const mutation of mutations) { | ||
| if (mutation.type === 'childList') { | ||
| mutation.addedNodes.forEach((node: any) => { | ||
|
HappyKnuckles marked this conversation as resolved.
Outdated
|
||
| if (node.tagName === 'ION-HEADER' || node.tagName === 'ION-FOOTER') { | ||
| shouldUpdate = true; | ||
| } | ||
| }); | ||
|
|
||
| mutation.removedNodes.forEach((node: any) => { | ||
|
HappyKnuckles marked this conversation as resolved.
Outdated
|
||
| if (node.tagName === 'ION-HEADER' || node.tagName === 'ION-FOOTER') { | ||
| shouldUpdate = true; | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| if (shouldUpdate) { | ||
| this.refreshResizeObserver(); | ||
| this.resize(); | ||
| } | ||
| }); | ||
|
|
||
| this.mutationObserver.observe(parent, { childList: true }); | ||
| } | ||
|
|
||
| this.refreshResizeObserver(); | ||
| } | ||
|
|
||
| private disconnectObservers() { | ||
| if (this.mutationObserver) { | ||
| this.mutationObserver.disconnect(); | ||
| this.mutationObserver = null; | ||
| } | ||
| if (this.resizeObserver) { | ||
| this.resizeObserver.disconnect(); | ||
| this.resizeObserver = null; | ||
| } | ||
| } | ||
|
|
||
| private refreshResizeObserver() { | ||
| if (!this.resizeObserver || !this.el.parentElement) { | ||
| return; | ||
| } | ||
|
|
||
| this.resizeObserver.disconnect(); | ||
|
|
||
| const headers = this.el.parentElement.querySelectorAll('ion-header'); | ||
| const footers = this.el.parentElement.querySelectorAll('ion-footer'); | ||
|
ShaneK marked this conversation as resolved.
Outdated
|
||
|
|
||
| headers.forEach((header) => this.resizeObserver!.observe(header)); | ||
|
HappyKnuckles marked this conversation as resolved.
Outdated
|
||
| footers.forEach((footer) => this.resizeObserver!.observe(footer)); | ||
| } | ||
|
|
||
| private onScrollStart() { | ||
| this.isScrolling = true; | ||
| this.ionScrollStart.emit({ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { expect } from '@playwright/test'; | ||
| import { test, configs } from '@utils/test/playwright'; | ||
|
|
||
| configs({ modes: ['ios'] }).forEach(({ title, screenshot, config }) => { | ||
|
ShaneK marked this conversation as resolved.
Outdated
|
||
| test.describe(title('content: auto offset'), () => { | ||
| test('should not have visual regressions', async ({ page }) => { | ||
| await page.goto(`/src/components/content/test/auto-offset`, config); | ||
| await page.setIonViewport(); | ||
| await expect(page).toHaveScreenshot(screenshot(`content-auto-offset-initial`)); | ||
| }); | ||
|
|
||
| test('should update offsets when header height changes', async ({ page }) => { | ||
| await page.goto(`/src/components/content/test/auto-offset`, config); | ||
| await page.setIonViewport(); | ||
|
|
||
| const content = page.locator('ion-content'); | ||
| const before = await content.evaluate((el: HTMLElement) => getComputedStyle(el).getPropertyValue('--offset-top')); | ||
|
|
||
| await page.click('#expand-header-btn'); | ||
|
|
||
| await expect(content).not.toHaveCSS('--offset-top', before); | ||
|
|
||
| await expect(page).toHaveScreenshot(screenshot(`content-auto-offset-header-updated`)); | ||
| }); | ||
|
|
||
| test('should update offsets when footer height changes', async ({ page }) => { | ||
| await page.goto(`/src/components/content/test/auto-offset`, config); | ||
| await page.setIonViewport(); | ||
|
|
||
| const content = page.locator('ion-content'); | ||
| const before = await content.evaluate((el: HTMLElement) => | ||
| getComputedStyle(el).getPropertyValue('--offset-bottom') | ||
| ); | ||
|
|
||
| await page.click('#expand-footer-btn'); | ||
|
|
||
| await expect(content).not.toHaveCSS('--offset-bottom', before); | ||
|
|
||
| const after = await content.evaluate((el: HTMLElement) => | ||
| getComputedStyle(el).getPropertyValue('--offset-bottom') | ||
| ); | ||
|
|
||
| expect(after).not.toBe(before); | ||
|
|
||
| await expect(page).toHaveScreenshot(screenshot(`content-auto-offset-footer-updated`)); | ||
|
ShaneK marked this conversation as resolved.
Outdated
|
||
| }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en" dir="ltr"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <title>Content - Auto Offset</title> | ||
| <meta | ||
| name="viewport" | ||
| content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" | ||
| /> | ||
| <link href="../../../../../css/ionic.bundle.css" rel="stylesheet" /> | ||
| <link href="../../../../../scripts/testing/styles.css" rel="stylesheet" /> | ||
| <script src="../../../../../scripts/testing/scripts.js"></script> | ||
| <script nomodule src="../../../../../dist/ionic/ionic.js"></script> | ||
| <script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script> | ||
| </head> | ||
|
|
||
| <body> | ||
| <ion-app mode="ios"> | ||
| <ion-header translucent> | ||
| <ion-toolbar> | ||
| <ion-title>Auto Offset Test</ion-title> | ||
| </ion-toolbar> | ||
| <ion-toolbar id="extra-header-content" style="display: none"> | ||
| <ion-searchbar label="dynamic"></ion-searchbar> | ||
| </ion-toolbar> | ||
| </ion-header> | ||
|
|
||
| <ion-content fullscreen style="--background: blue"> | ||
| <button id="expand-header-btn">Expand Header</button> | ||
| <br /><br /> | ||
| <button id="expand-footer-btn">Expand Footer</button> | ||
| </ion-content> | ||
|
|
||
| <ion-footer translucent> | ||
| <ion-toolbar id="extra-footer-content" style="display: none"> | ||
| <div class="ion-padding">Expanded Footer Area</div> | ||
| </ion-toolbar> | ||
| <ion-toolbar> | ||
| <ion-title>Footer</ion-title> | ||
| </ion-toolbar> | ||
| </ion-footer> | ||
| </ion-app> | ||
| <script> | ||
| const headerBtn = document.getElementById('expand-header-btn'); | ||
| const footerBtn = document.getElementById('expand-footer-btn'); | ||
| const extraHeaderToolBar = document.getElementById('extra-header-content'); | ||
| const extraFooterToolbar = document.getElementById('extra-footer-content'); | ||
|
|
||
| headerBtn.addEventListener('click', () => { | ||
| extraHeaderToolBar.style.display = 'flex'; | ||
| }); | ||
|
|
||
| footerBtn.addEventListener('click', () => { | ||
| extraFooterToolbar.style.display = 'flex'; | ||
| }); | ||
| </script> | ||
| </body> | ||
| </html> |
Uh oh!
There was an error while loading. Please reload this page.