fix(native): keep the keyboard open for controls that hold the focus in their field

A tap away blurred the field on touchend, before the sign toggle or the note format bar could cancel their mousedown, so the keypad closed mid entry. The field now lets go only once the tap's pointerdown and mousedown pass uncancelled.
This commit is contained in:
Maurice
2026-11-23 15:03:05 +01:00
parent dec3058777
commit 3c00544e93
2 changed files with 92 additions and 6 deletions
+50 -3
View File
@@ -152,18 +152,30 @@ describe('closeOnTapAway', () => {
if (type === 'touchend') Object.defineProperty(e, 'touches', { value: [] });
target.dispatchEvent(e);
}
function tap(target: Element, moveBy = 0) {
/** A finger's tap as a web view sends it: the pointer, the touch, then the mouse it makes of it. */
function tap(target: Element, moveBy = 0, mouse = true) {
const pointer = new Event('pointerdown', { bubbles: true, cancelable: true });
if (target.dispatchEvent(pointer) === false) mouse = false;
touch(target, 'touchstart', 100, 100);
touch(target, 'touchend', 100, 100 + moveBy);
if (mouse && !moveBy) target.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, cancelable: true }));
}
function sheet(): { notes: HTMLTextAreaElement; title: HTMLInputElement; label: HTMLElement; stop: () => void } {
function sheet(): {
notes: HTMLTextAreaElement;
title: HTMLInputElement;
label: HTMLElement;
sign: HTMLButtonElement;
stop: () => void;
} {
document.body.innerHTML =
'<div><p id="label">Notes for this day</p><textarea id="notes"></textarea><input id="title"></div>';
'<div><p id="label">Notes for this day</p><textarea id="notes"></textarea><input id="title">' +
'<button id="sign" type="button">±</button></div>';
const stop = prepareKeyboardFields(document);
return {
notes: document.getElementById('notes') as HTMLTextAreaElement,
title: document.getElementById('title') as HTMLInputElement,
label: document.getElementById('label') as HTMLElement,
sign: document.getElementById('sign') as HTMLButtonElement,
stop,
};
}
@@ -188,6 +200,41 @@ describe('closeOnTapAway', () => {
stop();
});
it('keeps it for a button that cancels its mousedown or pointerdown to keep the field', () => {
vi.useFakeTimers();
const { notes, sign, label, stop } = sheet();
notes.focus();
const keep = (e: Event) => e.preventDefault();
sign.addEventListener('mousedown', keep);
tap(sign);
vi.runAllTimers();
expect(document.activeElement).toBe(notes);
sign.removeEventListener('mousedown', keep);
sign.addEventListener('pointerdown', keep);
tap(sign);
vi.runAllTimers();
expect(document.activeElement).toBe(notes);
tap(label);
expect(document.activeElement).not.toBe(notes);
stop();
vi.useRealTimers();
});
it('closes it on a plain button, and also when no mousedown follows the tap', () => {
vi.useFakeTimers();
const { notes, sign, label, stop } = sheet();
notes.focus();
tap(sign);
expect(document.activeElement).not.toBe(notes);
notes.focus();
tap(label, 0, false);
expect(document.activeElement).toBe(notes);
vi.runAllTimers();
expect(document.activeElement).not.toBe(notes);
stop();
vi.useRealTimers();
});
it('stops listening when told to', () => {
const { notes, label, stop } = sheet();
stop();
+42 -3
View File
@@ -140,29 +140,62 @@ function typedInto(el: Element | null): el is HTMLElement {
const EDITABLE = 'input, textarea, select, [contenteditable]:not([contenteditable="false"])';
/** How far a finger may travel and still count as a tap rather than a scroll, in CSS pixels. */
const TAP_SLOP = 10;
/** How long after the tap the mousedown a browser makes of it may come, in milliseconds. */
const MOUSE_AFTER_TAP = 350;
/**
* A tap away from the field closes the keyboard. A textarea keeps its return key
* for new lines, and the app hides the keyboard's bar with its "done" button, so
* otherwise nothing closes it and it covers the rest of the sheet. A tap on another
* field moves the focus there instead, and a scroll leaves the keyboard open.
*
* A control built to keep the focus in its field (a sign toggle, a format bar,
* a send button) says so by cancelling its pointerdown or mousedown. The mousedown
* comes after the touch ends, so the field lets go only once it has passed
* uncancelled, or when none comes at all.
*/
function closeOnTapAway(doc: Document): { start: (e: TouchEvent) => void; end: (e: TouchEvent) => void } {
function closeOnTapAway(doc: Document): {
start: (e: TouchEvent) => void;
end: (e: TouchEvent) => void;
pointer: (e: PointerEvent) => void;
mouse: (e: MouseEvent) => void;
stop: () => void;
} {
let from: { x: number; y: number } | null = null;
let pointerKept = false;
let pending: { field: HTMLElement; timer: ReturnType<typeof setTimeout> } | null = null;
const settle = (blur: boolean) => {
if (!pending) return;
clearTimeout(pending.timer);
if (blur && doc.activeElement === pending.field) pending.field.blur();
pending = null;
};
return {
start(e) {
const touch = e.touches[0];
from = e.touches.length === 1 && touch ? { x: touch.clientX, y: touch.clientY } : null;
},
pointer(e) {
pointerKept = e.defaultPrevented;
},
end(e) {
const touch = e.changedTouches[0];
const tapped = from && touch && Math.hypot(touch.clientX - from.x, touch.clientY - from.y) <= TAP_SLOP;
from = null;
const kept = pointerKept;
pointerKept = false;
settle(false);
const field = doc.activeElement;
if (!tapped || !typedInto(field)) return;
if (!tapped || kept || !typedInto(field)) return;
const target = e.target instanceof Element ? e.target : null;
if (target && (field.contains(target) || target.closest(EDITABLE))) return;
field.blur();
pending = { field, timer: setTimeout(() => settle(true), MOUSE_AFTER_TAP) };
},
mouse(e) {
settle(!e.defaultPrevented);
},
stop() {
settle(false);
},
};
}
@@ -178,9 +211,15 @@ export function prepareKeyboardFields(doc: Document = document): () => void {
const tapAway = closeOnTapAway(doc);
doc.addEventListener('touchstart', tapAway.start, { capture: true, passive: true });
doc.addEventListener('touchend', tapAway.end, { capture: true, passive: true });
// Bubbling, so they see whether a handler on the way cancelled them.
doc.addEventListener('pointerdown', tapAway.pointer);
doc.addEventListener('mousedown', tapAway.mouse);
return () => {
tapAway.stop();
doc.removeEventListener('touchstart', tapAway.start, { capture: true });
doc.removeEventListener('touchend', tapAway.end, { capture: true });
doc.removeEventListener('pointerdown', tapAway.pointer);
doc.removeEventListener('mousedown', tapAway.mouse);
doc.removeEventListener('touchstart', onTouch, { capture: true });
doc.removeEventListener('focusin', onFocus, true);
doc.removeEventListener('keydown', closeOnDone, true);