Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions src/app/styles/edgeToEdgeInsets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ describe('android edge-to-edge inset contract', () => {
expect(indexTsx).toContain('installIosPwaViewportHeight();');
expect(iosPwaViewport).toContain("window.matchMedia('(display-mode: standalone)').matches");
expect(iosPwaViewport).toContain('const MIN_KEYBOARD_HEIGHT = 100');
expect(iosPwaViewport).toContain('const keyboardOpen = fullHeight - visibleHeight');
expect(iosPwaViewport).toContain('const height = keyboardOpen ? visibleBottom : fullHeight');
expect(iosPwaViewport).toContain('isEditableFocused()');
expect(iosPwaViewport).toContain('screenHeight - visibleHeight > MIN_KEYBOARD_HEIGHT');
expect(iosPwaViewport).toContain('window.setTimeout(updateHeight, 350)');
expect(iosPwaViewport).not.toContain('fullHeight');
expect(iosPwaViewport).not.toContain('viewportWidth');
});

it('removes the scattered safe-area css consumers', () => {
Expand Down
26 changes: 16 additions & 10 deletions src/app/utils/iosPwaViewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,35 @@ 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;
};

const fullScreenHeight = (): number => {
const { width, height } = window.screen;
return window.matchMedia('(orientation: portrait)').matches
? Math.max(width, height)
: Math.min(width, height);
};

export function installIosPwaViewportHeight(): void {
if (!isStandaloneIosPwa()) return;

let frame = 0;
let settleTimer = 0;
let fullHeight = 0;
let viewportWidth = window.innerWidth;

const updateHeight = () => {
frame = 0;
const viewport = window.visualViewport;
const visibleHeight = viewport?.height ?? window.innerHeight;
const visibleBottom = visibleHeight + (viewport?.offsetTop ?? 0);

if (window.innerWidth !== viewportWidth) {
viewportWidth = window.innerWidth;
fullHeight = visibleBottom;
}

const keyboardOpen = fullHeight - visibleHeight > MIN_KEYBOARD_HEIGHT;
if (!keyboardOpen) fullHeight = Math.max(fullHeight, visibleBottom);
const screenHeight = fullScreenHeight();
const keyboardOpen = isEditableFocused() && screenHeight - visibleHeight > MIN_KEYBOARD_HEIGHT;
const height = keyboardOpen ? visibleBottom : screenHeight;

const height = keyboardOpen ? visibleBottom : fullHeight;
document.documentElement.style.setProperty(IOS_PWA_VIEWPORT_HEIGHT, `${Math.round(height)}px`);
};

Expand Down
Loading