diff --git a/client/src/native/keepFieldInView.test.ts b/client/src/native/keepFieldInView.test.ts index e265ddef5..8307c5b57 100644 --- a/client/src/native/keepFieldInView.test.ts +++ b/client/src/native/keepFieldInView.test.ts @@ -54,6 +54,29 @@ describe('keepFieldInView', () => { expect(scroller.style.paddingBottom).toBe(''); }); + it('FE-NATIVE-KBFIELD-004: a page as tall as the screen gets room at its end and scrolls the field above the keyboard', () => { + // The login on an iPhone: no pane scrolls, and the keyboard plugin holds the body's height. + Object.defineProperty(window, 'innerHeight', { configurable: true, value: 1000 }); + const scrollBy = vi.spyOn(window, 'scrollBy').mockImplementation(() => {}); + const form = document.createElement('form'); + document.body.appendChild(form); + field(form, 'input', 760, 800); + + revealFocusedField(KEYBOARD); + const room = document.body.lastElementChild as HTMLElement; + expect(room.getAttribute('aria-hidden')).toBe('true'); + expect(room.style.height).toBe('300px'); + expect(scrollBy).toHaveBeenCalledWith({ top: 800 - (700 - 24), behavior: 'smooth' }); + + // The next field asks again without stacking a second room. + revealFocusedField(KEYBOARD); + expect(document.querySelectorAll('[aria-hidden="true"]')).toHaveLength(1); + + releaseRoom(); + expect(room.isConnected).toBe(false); + scrollBy.mockRestore(); + }); + it('FE-NATIVE-KBFIELD-002: listens for the keyboard coming up and going', () => { const addListener = vi.fn(async () => ({ remove: vi.fn() })); keepFieldInView({ addListener } as never); diff --git a/client/src/native/keepFieldInView.ts b/client/src/native/keepFieldInView.ts index fcba19067..fe28ecb92 100644 --- a/client/src/native/keepFieldInView.ts +++ b/client/src/native/keepFieldInView.ts @@ -29,6 +29,13 @@ interface KeyboardEvents { /** The pane given room for the keyboard, and what its bottom padding was before. */ let padded: { pane: HTMLElement; before: string } | null = null; +/** + * The room at the end of a page that scrolls in no pane of its own, such as the + * login: as tall as the screen, it has nothing to scroll by, and padding on the + * body does not help while the keyboard plugin holds the body's height. + */ +let pageRoom: HTMLElement | null = null; + function scrollingPane(field: HTMLElement): HTMLElement | null { for (let node = field.parentElement; node; node = node.parentElement) { const overflow = getComputedStyle(node).overflowY; @@ -48,7 +55,14 @@ export function revealFocusedField(keyboardHeight: number, doc: Document = docum const pane = scrollingPane(field); if (!pane) { - field.scrollIntoView({ block: 'center', behavior: 'smooth' }); + if (!pageRoom) { + releaseRoom(); + pageRoom = doc.createElement('div'); + pageRoom.setAttribute('aria-hidden', 'true'); + pageRoom.style.height = `${keyboardHeight}px`; + doc.body.appendChild(pageRoom); + } + view?.scrollBy({ top: box.bottom - (visibleBottom - AIR), behavior: 'smooth' }); return; } if (!padded || padded.pane !== pane) { @@ -61,6 +75,8 @@ export function revealFocusedField(keyboardHeight: number, doc: Document = docum /** Takes the room for the keyboard away again. */ export function releaseRoom(): void { + pageRoom?.remove(); + pageRoom = null; if (!padded) return; padded.pane.style.paddingBottom = padded.before; padded = null;