From 6f568043f69e8f5bcbb4b9e14025a756f1f410fa Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Sun, 16 Aug 2026 19:41:16 +0200 Subject: [PATCH] fix(ios): anchor the pwa viewport and banner to visualViewport.offsetTop --- .../NotificationBanner.tsx | 13 ++++++------ src/app/styles/edgeToEdgeInsets.test.ts | 8 +++---- src/app/utils/iosPwaViewport.ts | 21 +++++++------------ 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/app/components/notification-banner/NotificationBanner.tsx b/src/app/components/notification-banner/NotificationBanner.tsx index 25468254e..cf4cc0633 100644 --- a/src/app/components/notification-banner/NotificationBanner.tsx +++ b/src/app/components/notification-banner/NotificationBanner.tsx @@ -201,14 +201,13 @@ export function NotificationBanner() { if (!container) return; const visualViewport = window.visualViewport!; - // Calculate how much of the screen is covered by the keyboard - // When keyboard opens, visualViewport.height shrinks - const keyboardHeight = window.innerHeight - visualViewport.height; + // The banner is fixed to the layout viewport, so iOS scrolls it off the top when the + // keyboard pushes the visual viewport down. offsetTop is that shift. + const shift = Math.round(visualViewport.offsetTop); - // Position the banner down by the keyboard height so it appears at the top of the visible area - // This puts it "halfway down the page" when keyboard covers half the screen - if (keyboardHeight > 0) { - container.style.top = `${keyboardHeight}px`; + if (shift > 0) { + // Keep the safe-area inset, otherwise the banner slides under the status bar. + container.style.top = `calc(var(--safe-area-inset-top, env(safe-area-inset-top, 0px)) + ${shift}px)`; } else { // Reset to CSS default (env(safe-area-inset-top)) container.style.top = ''; diff --git a/src/app/styles/edgeToEdgeInsets.test.ts b/src/app/styles/edgeToEdgeInsets.test.ts index 4f1125cdd..2c6905c7b 100644 --- a/src/app/styles/edgeToEdgeInsets.test.ts +++ b/src/app/styles/edgeToEdgeInsets.test.ts @@ -89,14 +89,14 @@ describe('android edge-to-edge inset contract', () => { expect(indexCss).toContain('var(--sable-ios-pwa-viewport-height, 100dvh)'); expect(indexTsx).toContain('installIosPwaViewportHeight();'); expect(iosPwaViewport).toContain("window.matchMedia('(display-mode: standalone)').matches"); - expect(iosPwaViewport).toContain('const MIN_KEYBOARD_HEIGHT = 100'); - expect(iosPwaViewport).toContain('isEditableFocused()'); - expect(iosPwaViewport).toContain('const layoutHeight = window.innerHeight'); - expect(iosPwaViewport).toContain('layoutHeight - visibleHeight > MIN_KEYBOARD_HEIGHT'); + expect(iosPwaViewport).toContain('viewport.height + viewport.offsetTop'); expect(iosPwaViewport).toContain('window.setTimeout(updateHeight, 350)'); expect(iosPwaViewport).not.toContain('fullHeight'); expect(iosPwaViewport).not.toContain('viewportWidth'); expect(iosPwaViewport).not.toContain('window.screen'); + // The height must not depend on keyboard detection or on a stale window.innerHeight. + expect(iosPwaViewport).not.toContain('MIN_KEYBOARD_HEIGHT'); + expect(iosPwaViewport).not.toContain('isEditableFocused'); }); it('removes the scattered safe-area css consumers', () => { diff --git a/src/app/utils/iosPwaViewport.ts b/src/app/utils/iosPwaViewport.ts index e83e26b81..f445c3d41 100644 --- a/src/app/utils/iosPwaViewport.ts +++ b/src/app/utils/iosPwaViewport.ts @@ -1,16 +1,9 @@ const IOS_PWA_VIEWPORT_HEIGHT = '--sable-ios-pwa-viewport-height'; -const MIN_KEYBOARD_HEIGHT = 100; const isStandaloneIosPwa = (): boolean => window.matchMedia('(display-mode: standalone)').matches && CSS.supports('-webkit-touch-callout: none'); -const isEditableFocused = (): boolean => { - const el = document.activeElement as HTMLElement | null; - if (!el) return false; - return el.tagName === 'INPUT' || el.tagName === 'TEXTAREA' || el.isContentEditable; -}; - export function installIosPwaViewportHeight(): void { if (!isStandaloneIosPwa()) return; @@ -20,14 +13,14 @@ export function installIosPwaViewportHeight(): void { const updateHeight = () => { frame = 0; const viewport = window.visualViewport; - const visibleHeight = viewport?.height ?? window.innerHeight; - const visibleBottom = visibleHeight + (viewport?.offsetTop ?? 0); + // Reach the bottom of the visible area. window.innerHeight is unusable here: iOS shrinks it + // on the first keyboard open and never restores it until the app is force-quit. + const visibleBottom = viewport ? viewport.height + viewport.offsetTop : window.innerHeight; - const layoutHeight = window.innerHeight; - const keyboardOpen = isEditableFocused() && layoutHeight - visibleHeight > MIN_KEYBOARD_HEIGHT; - const height = keyboardOpen ? visibleBottom : layoutHeight; - - document.documentElement.style.setProperty(IOS_PWA_VIEWPORT_HEIGHT, `${Math.round(height)}px`); + document.documentElement.style.setProperty( + IOS_PWA_VIEWPORT_HEIGHT, + `${Math.round(visibleBottom)}px` + ); }; const scheduleUpdate = () => {