mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 18:34:09 +00:00
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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user