diff --git a/client/src/native/keepFieldInView.test.ts b/client/src/native/keepFieldInView.test.ts index 8307c5b57..67f283188 100644 --- a/client/src/native/keepFieldInView.test.ts +++ b/client/src/native/keepFieldInView.test.ts @@ -83,4 +83,39 @@ describe('keepFieldInView', () => { expect(addListener).toHaveBeenCalledWith('keyboardDidShow', expect.any(Function)); expect(addListener).toHaveBeenCalledWith('keyboardWillHide', expect.any(Function)); }); + + it('FE-NATIVE-KBFIELD-005: a field pushed under the open keyboard by a new row above it comes back up, and stops being watched once the keyboard goes', async () => { + // The packing list keeps its entry field open after Enter: each added item lands above it. + Object.defineProperty(window, 'innerHeight', { configurable: true, value: 1000 }); + const handlers: Record void> = {}; + const addListener = vi.fn(async (event: string, listener: (info?: { keyboardHeight: number }) => void) => { + handlers[event] = listener; + return { remove: vi.fn() }; + }); + keepFieldInView({ addListener } as never); + const nextFrame = () => new Promise((resolve) => requestAnimationFrame(resolve)); + + const scroller = pane(); + const list = document.createElement('div'); + scroller.appendChild(list); + const input = field(scroller, 'input', 600, 640); + handlers.keyboardDidShow({ keyboardHeight: KEYBOARD }); + expect(scroller.scrollBy).not.toHaveBeenCalled(); + + // A new row pushes the field to 700..740, under the keyboard's top edge at 700. + input.getBoundingClientRect = () => ({ top: 700, bottom: 740 }) as DOMRect; + list.appendChild(document.createElement('div')); + list.appendChild(document.createElement('div')); + await nextFrame(); + await nextFrame(); + expect(scroller.scrollBy).toHaveBeenCalledTimes(1); + expect(scroller.scrollBy).toHaveBeenCalledWith({ top: 740 - (700 - 24), behavior: 'smooth' }); + + handlers.keyboardWillHide(); + list.appendChild(document.createElement('div')); + await nextFrame(); + await nextFrame(); + expect(scroller.scrollBy).toHaveBeenCalledTimes(1); + expect(scroller.style.paddingBottom).toBe(''); + }); }); diff --git a/client/src/native/keepFieldInView.ts b/client/src/native/keepFieldInView.ts index fe28ecb92..738b87be7 100644 --- a/client/src/native/keepFieldInView.ts +++ b/client/src/native/keepFieldInView.ts @@ -82,8 +82,45 @@ export function releaseRoom(): void { padded = null; } -/** Wires it to the keyboard of the app. */ -export function keepFieldInView(keyboard: KeyboardEvents): void { - void keyboard.addListener('keyboardDidShow', ({ keyboardHeight }) => revealFocusedField(keyboardHeight)); - void keyboard.addListener('keyboardWillHide', () => releaseRoom()); +/** + * While the keyboard is up, a field can still move under it without a new + * keyboard event: a list that keeps its entry field open after Enter grows the + * new row above the field and pushes it down. Any change to the page while the + * keyboard is up asks again, at most once a frame, and a field still in view + * stays where it is. + */ +let watching: { observer: MutationObserver; view: Window; frame: number } | null = null; + +function watchWhileTyping(keyboardHeight: number, doc: Document): void { + stopWatching(); + const view = doc.defaultView; + if (!view || typeof view.MutationObserver !== 'function') return; + const observer = new view.MutationObserver(() => { + if (!watching || watching.frame) return; + watching.frame = view.requestAnimationFrame(() => { + if (watching) watching.frame = 0; + revealFocusedField(keyboardHeight, doc); + }); + }); + watching = { observer, view, frame: 0 }; + observer.observe(doc.body, { childList: true, subtree: true }); +} + +function stopWatching(): void { + if (!watching) return; + watching.observer.disconnect(); + if (watching.frame) watching.view.cancelAnimationFrame(watching.frame); + watching = null; +} + +/** Wires it to the keyboard of the app. */ +export function keepFieldInView(keyboard: KeyboardEvents, doc: Document = document): void { + void keyboard.addListener('keyboardDidShow', ({ keyboardHeight }) => { + revealFocusedField(keyboardHeight, doc); + watchWhileTyping(keyboardHeight, doc); + }); + void keyboard.addListener('keyboardWillHide', () => { + stopWatching(); + releaseRoom(); + }); }