diff --git a/host/web/navigation/routes.js b/host/web/navigation/routes.js index 3772efe6f..62a2ade97 100644 --- a/host/web/navigation/routes.js +++ b/host/web/navigation/routes.js @@ -131,6 +131,9 @@ const WIDE_WIDTH = 900, WIDE = `(min-width: ${WIDE_WIDTH}px)`, wide = () => { let splitLists = new WeakMap(), rowOf = new WeakMap(), actionRows = new WeakMap(), visits = new Map(); const activationRequired = new WeakSet(), activated = new WeakSet(); const sheet = (route) => ["modal", "fullscreen"].includes(route?.getAttribute("navigationPresentation")); +// The fields whose focus raises a narrow sheet on a touch screen, and where it does (neutral.css). +const FIELD = 'textarea, input:not([type="checkbox"], [type="radio"], [type="range"], [type="button"], [type="submit"], [type="reset"], [type="color"], [type="file"], [type="image"]), [contenteditable]:not([contenteditable="false"])'; +const RAISES = "(width < 900px) and (min-height: 480px) and (any-pointer: coarse)"; // A sheet's frame (`sheetFrame`, below), which the projection writes once a host hands it over: the wasm host's glue // for every plan, the JS target's compiler only for a plan that can present a sheet (LLP 1047.002 D1). A plan that // cannot has no route in a sheet, so there is nothing to write. @@ -832,6 +835,16 @@ export const navigation = { hostGo = goTo; routeMatches = matchesRoute; canonicalOf = canonical; + // On a touch screen a field with the focus raises its narrow sheet to the tallest (neutral.css, as the keyboard does + // on iOS). A press on a control in that sheet, by a finger or by a mouse beside it, leaves the focus in the field, as + // a UIKit control leaves the first responder: a mousedown's focus would blur the field and drop the sheet to its + // detent before the click, which would then land beside the control. + addEventListener("mousedown", (e) => { + const field = document.activeElement, raised = field?.closest?.("[data-exact-detent]"); + if (raised?.contains(e.target) && field.matches(FIELD) && !e.target.closest?.(`${FIELD}, select, label`) && globalThis.matchMedia?.(RAISES).matches) { + e.preventDefault(); + } + }, true); for (const type of [ "wheel", "touchmove", diff --git a/host/web/tests/sheet-detent.test.mjs b/host/web/tests/sheet-detent.test.mjs index f748e16dd..c93e1afa6 100644 --- a/host/web/tests/sheet-detent.test.mjs +++ b/host/web/tests/sheet-detent.test.mjs @@ -26,7 +26,7 @@ const abs = 'position:absolute;inset:0;display:flex;flex-direction:column'; const page = '' + style + `

Home

Sheet

-

Pushed

+

Pushed

Over

Full

Top

@@ -43,6 +43,8 @@ const page = ' inChrome(async (evaluate, size, call) => { + // The case this guards: a route pushed inside a sheet whose field has the focus (an autofocused one). + expect(await open(evaluate, 'medium large')).toEqual([428, 416]); + await call('Emulation.setTouchEmulationEnabled', { enabled: true, maxTouchPoints: 1 }); + await size(390, 844); + expect(await evaluate(`select('3'); document.getElementById('pushedField').focus(); box('pushed')`)).toEqual([12, 832]); + const [x, y] = await evaluate(`(b => [b.x + b.width / 2, b.y + b.height / 2])(document.getElementById('pushedGo').getBoundingClientRect())`); + const settled = `new Promise(r => setTimeout(r, 300)).then(() => [clicks.splice(0), document.activeElement.id, box('pushed')])`; + await call('Input.dispatchTouchEvent', { type: 'touchStart', touchPoints: [{ x, y }] }); + await call('Input.dispatchTouchEvent', { type: 'touchEnd', touchPoints: [] }); + expect(await evaluate(settled)).toEqual([['pushedGo'], 'pushedField', [12, 832]]); + // A mouse on the same touch screen: the sheet is raised all the same. + for (const type of ['mousePressed', 'mouseReleased']) await call('Input.dispatchMouseEvent', { type, x, y, button: 'left', buttons: type === 'mousePressed' ? 1 : 0, clickCount: 1 }); + expect(await evaluate(settled)).toEqual([['pushedGo'], 'pushedField', [12, 832]]); +}), 60000); + check('a bound value keeps the detents UIKit keeps: a word it cannot read or one out of order is dropped', () => inChrome(async (evaluate) => { expect(await open(evaluate, '0% large')).toEqual([12, 832]); expect(await open(evaluate, 'tall 300 large')).toEqual([544, 300]);