diff --git a/core/src/components/modal/modal.tsx b/core/src/components/modal/modal.tsx index 49e6731023c..71ac099966d 100644 --- a/core/src/components/modal/modal.tsx +++ b/core/src/components/modal/modal.tsx @@ -55,6 +55,7 @@ import { getRootSafeAreaTop, onRootSafeAreaTopChange, getModalCoveredAxes, + measureCoveredAxes, type ModalSafeAreaContext, } from './safe-area-utils'; import { onModalHeightChange, setCardStatusBarDark, setCardStatusBarDefault } from './utils'; @@ -320,8 +321,14 @@ export class Modal implements ComponentInterface, OverlayInterface { // Sheet modals: re-compute the internal offset property since safe-area // values may change on device rotation (e.g., portrait notch vs landscape). + // The horizontal insets are re-evaluated too, since the viewport may have + // crossed the breakpoint where the sheet spans the full width. if (context.isSheetModal) { this.updateSheetOffsetTop(); + applySafeAreaOverrides( + this.el, + getInitialSafeAreaConfig({ ...context, coveredAxes: measureCoveredAxes(this.el) }) + ); } // Regular (non-sheet, non-card) modals: update safe-area overrides @@ -1489,8 +1496,8 @@ export class Modal implements ComponentInterface, OverlayInterface { /** * Creates the context object for safe-area utilities. * - * `coveredAxes` is only set by `setInitialSafeAreaOverrides()` - * because it is only read by `getInitialSafeAreaConfig()`. Other callers + * `coveredAxes` is only set where `getInitialSafeAreaConfig()` reads it + * (`setInitialSafeAreaOverrides()` and the sheet resize path). Other callers * (resize handler, post-animation update, fullscreen-padding apply) would * pay a `getComputedStyle()` cost for a value they never consult. */ @@ -1539,7 +1546,9 @@ export class Modal implements ComponentInterface, OverlayInterface { private setInitialSafeAreaOverrides(): void { const context: ModalSafeAreaContext = { ...this.getSafeAreaContext(), - coveredAxes: getModalCoveredAxes(this.el), + // A sheet's `--width` and `--height` don't tell whether `--max-width` + // keeps it clear of the edges, so its rendered size is measured instead. + coveredAxes: this.isSheetModal ? measureCoveredAxes(this.el) : getModalCoveredAxes(this.el), }; const safeAreaConfig = getInitialSafeAreaConfig(context); applySafeAreaOverrides(this.el, safeAreaConfig); @@ -1577,8 +1586,8 @@ export class Modal implements ComponentInterface, OverlayInterface { const { wrapperEl, el } = this; const context = this.getSafeAreaContext(); - // Sheet modals: safe-area is fully determined at presentation time - // (top is always 0px, height is frozen). Nothing to update. + // Sheet modals: safe-area is determined at presentation time and on + // resize (top is always 0px, height is frozen). Nothing to update. if (context.isSheetModal) return; // Card modals have fixed safe-area requirements set by initial prediction. diff --git a/core/src/components/modal/safe-area-utils.ts b/core/src/components/modal/safe-area-utils.ts index 5d0f7d9150d..827d0def5d7 100644 --- a/core/src/components/modal/safe-area-utils.ts +++ b/core/src/components/modal/safe-area-utils.ts @@ -135,7 +135,7 @@ export const getModalCoveredAxes = (hostEl: HTMLElement): ModalCoveredAxes => { * enter animation, which initially translates it by its own height, while * the translation does not affect its measured size. */ -const measureCoveredAxes = (hostEl: HTMLElement): ModalCoveredAxes => { +export const measureCoveredAxes = (hostEl: HTMLElement): ModalCoveredAxes => { const wrapperEl = hostEl.shadowRoot?.querySelector('.modal-wrapper'); if (wrapperEl == null || win === undefined) { return { vertical: false, horizontal: false }; @@ -172,13 +172,15 @@ export const getInitialSafeAreaConfig = (context: ModalSafeAreaContext): SafeAre // top edge is handled by --ion-modal-offset-top (set in modal.tsx) using // the resolved root value, so --ion-safe-area-top is never needed for // height calculation. Keeping it at 0px prevents header content from - // getting double-offset padding. + // getting double-offset padding. The horizontal insets are kept when the + // sheet spans the viewport width (e.g. a phone in landscape). if (isSheetModal) { + const horizontal = context.coveredAxes?.horizontal ?? true; return { top: '0px', bottom: 'inherit', - left: '0px', - right: '0px', + left: horizontal ? 'inherit' : '0px', + right: horizontal ? 'inherit' : '0px', }; } diff --git a/core/src/components/modal/test/safe-area/modal.e2e.ts b/core/src/components/modal/test/safe-area/modal.e2e.ts index 740eb83c298..54896ef5708 100644 --- a/core/src/components/modal/test/safe-area/modal.e2e.ts +++ b/core/src/components/modal/test/safe-area/modal.e2e.ts @@ -136,30 +136,104 @@ configs({ modes: ['ios', 'md'], directions: ['ltr'] }).forEach(({ title, config expect(safeAreaBottom).toBe('0px'); }); - test('sheet modal should only use bottom safe-area', async ({ page }, testInfo) => { + test('sheet modal should only zero the top safe-area when it spans the viewport width', async ({ + page, + }, testInfo) => { testInfo.annotations.push({ type: 'issue', description: 'https://github.com/ionic-team/ionic-framework/issues/30900', }); + testInfo.annotations.push({ + type: 'issue', + description: 'https://github.com/ionic-team/ionic-framework/issues/31818', + }); - const ionModalDidPresent = await page.spyOnEvent('ionModalDidPresent'); + // Simulate a landscape phone with a notch on the left and right + await page.setViewportSize({ width: 844, height: 390 }); + await page.evaluate(() => { + document.documentElement.style.setProperty('--ion-safe-area-left', '47px'); + document.documentElement.style.setProperty('--ion-safe-area-right', '47px'); + }); + const ionModalDidPresent = await page.spyOnEvent('ionModalDidPresent'); await page.click('#sheet-modal'); await ionModalDidPresent.next(); - const modal = page.locator('ion-modal'); + // The sheet doesn't touch the top edge, but it touches the bottom, + // left and right edges + expect(await getSettledSafeArea(page)).toEqual({ + top: '0px', + bottom: 'inherit', + left: 'inherit', + right: 'inherit', + }); - // Sheet modals should have top safe-area zeroed (doesn't touch top edge) - // but bottom safe-area inherited (touches bottom edge) - const safeAreaTop = await modal.evaluate((el: HTMLIonModalElement) => { - return el.style.getPropertyValue('--ion-safe-area-top'); + // The sheet's toolbar pads itself with the root insets + const toolbarSafeArea = await page.locator('ion-modal ion-header ion-toolbar').evaluate((el: HTMLElement) => { + const styles = getComputedStyle(el); + return [ + styles.getPropertyValue('--ion-safe-area-left').trim(), + styles.getPropertyValue('--ion-safe-area-right').trim(), + ]; }); - const safeAreaBottom = await modal.evaluate((el: HTMLIonModalElement) => { - return el.style.getPropertyValue('--ion-safe-area-bottom'); + expect(toolbarSafeArea).toEqual(['47px', '47px']); + }); + + test('sheet modal should zero horizontal safe-area when max-width keeps it clear of the edges', async ({ + page, + }, testInfo) => { + testInfo.annotations.push({ + type: 'issue', + description: 'https://github.com/ionic-team/ionic-framework/issues/31818', }); - expect(safeAreaTop).toBe('0px'); - expect(safeAreaBottom).toBe('inherit'); + await page.setViewportSize({ width: 844, height: 390 }); + await page.addStyleTag({ + content: 'ion-modal.modal-sheet { --width: 100%; --height: 100%; --max-width: 480px; }', + }); + + const ionModalDidPresent = await page.spyOnEvent('ionModalDidPresent'); + await page.click('#sheet-modal'); + await ionModalDidPresent.next(); + + const wrapperWidth = await page + .locator('ion-modal') + .evaluate( + (el: HTMLIonModalElement) => el.shadowRoot!.querySelector('.modal-wrapper')!.getBoundingClientRect().width + ); + expect(wrapperWidth).toBe(480); + + const { left, right } = await getSettledSafeArea(page); + expect([left, right]).toEqual(['0px', '0px']); + }); + + test('sheet modal should update horizontal safe-area on resize', async ({ page }, testInfo) => { + testInfo.annotations.push({ + type: 'issue', + description: 'https://github.com/ionic-team/ionic-framework/issues/31818', + }); + + // The sheet is centered with a fixed width on a tablet viewport, so it + // stays clear of the left and right edges. + await page.setViewportSize(Viewports.tablet.portrait); + + const ionModalDidPresent = await page.spyOnEvent('ionModalDidPresent'); + await page.click('#sheet-modal'); + await ionModalDidPresent.next(); + + const modal = page.locator('ion-modal'); + const getHorizontalSafeArea = () => + modal.evaluate((el: HTMLIonModalElement) => [ + el.style.getPropertyValue('--ion-safe-area-left'), + el.style.getPropertyValue('--ion-safe-area-right'), + ]); + + expect(await getHorizontalSafeArea()).toEqual(['0px', '0px']); + + // Resize to a landscape phone viewport, where the sheet spans the full width + await page.setViewportSize({ width: 844, height: 390 }); + + await expect.poll(getHorizontalSafeArea).toEqual(['inherit', 'inherit']); }); test('fullscreen modal without footer should set safe-area scroll padding on ion-content', async ({