Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions core/src/components/content/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
/**
Expand Down Expand Up @@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a 100ms or so debounce to this? I'm worried about performance issues from this being called a ton during resizing

});
}

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) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just use HTMLElement for this? Shouldn't need to use any, preferably

if (node.tagName === 'ION-HEADER' || node.tagName === 'ION-FOOTER') {
shouldUpdate = true;
}
});

mutation.removedNodes.forEach((node: any) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just use HTMLElement for this? Shouldn't need to use any, preferably

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

headers.forEach((header) => this.resizeObserver!.observe(header));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check this.resizeObserver and return out if it's not set, rather than using a non-null assertion in these two loops

footers.forEach((footer) => this.resizeObserver!.observe(footer));
}

private onScrollStart() {
this.isScrolling = true;
this.ionScrollStart.emit({
Expand Down
48 changes: 48 additions & 0 deletions core/src/components/content/test/auto-offset/content.e2e.ts
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 }) => {
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`));
});
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions core/src/components/content/test/auto-offset/index.html
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>
Loading