fix(native): an entry field pushed under the open keyboard by a new row comes back above it

The packing list keeps its entry field open after Enter, and each added item
lands above it. With the keyboard already up no keyboard event fires, so the
field slid under the keyboard after the first entry. While the keyboard is up,
a change to the page now checks the focused field again, once a frame at most.
This commit is contained in:
Maurice
2026-11-14 23:29:33 +01:00
parent c72c8dcaf7
commit 87bcb5a519
2 changed files with 76 additions and 4 deletions
+41 -4
View File
@@ -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();
});
}