Skip to content

A finger's tap on a control in a sheet a field's focus raised reaches the control - #12

Merged
janicduplessis merged 2 commits into
mainfrom
@janic/sheet-push-autofocus-tap
Nov 15, 2026
Merged

janicduplessis merged 2 commits into
mainfrom
@janic/sheet-push-autofocus-tap

Conversation

@janicduplessis

@janicduplessis janicduplessis commented Nov 15, 2026 •

Copy link
Copy Markdown

Description

On a touch screen, the first tap on a button in a narrow sheet whose field has the focus does nothing. A mouse click on a touch-screen device does the same. The second tap works. An app hits this with a sheet that pushes a page with an autofocused field: a menu sheet pushing "Open a case", say. The first tap on Back, Close or the submit button is lost.

A focused field raises the sheet to the tallest height, through neutral.css's @media (any-pointer: coarse) rule ([data-exact-detent]:has(… :focus)), the way the keyboard does on iOS. A tap (through its compatibility mousedown) or a mouse press sends a mousedown, and Chrome moves the focus to the button there. The field blurs, the :has(:focus) rule stops matching, and the sheet drops back to its detent before the click. The control is no longer under the finger, so the click goes to an ancestor.

Solution

navigation.connect adds a capture mousedown listener. When the focused field is in a route with a detent, the target is in that same route, and the window is where the raise applies, it calls preventDefault(). The field keeps the focus, the sheet stays raised, and the click reaches the control. UIKit behaves the same way: tapping a control doesn't resign the first responder, so the keyboard stays up.

It only applies while the raise can apply: a narrow window on a device with a coarse pointer (any-pointer: coarse), so a mouse on a touch screen is covered too. A press on another field, a select or a label, a sheet without a detent, and a window outside the raise's media query all behave as before.

Test plan

  • host/web/tests/sheet-detent.test.mjs: a new case focuses a field in a route pushed inside a medium large sheet, under Chrome's touch emulation at 390×844, then taps a button below it with Input.dispatchTouchEvent and clicks it with Input.dispatchMouseEvent. With the fix the button gets each click, the field keeps the focus, and the sheet stays at the tallest. Without it, the click lands on the stack's nav and the sheet drops to 428 / 416. All 10 cases pass.
  • bun scripts/caps.mjs passes.
  • An app whose menu sheet pushes a page with an autofocused field, built against this branch: its web contract tests went from 62 passed, 2 failed (both a first tap on that page lost under prefer hover none) to 64 passed, 0 failed.
  • focus, js-autofocus, prevent-focus, route-focus, route-pages, sheet-ids, tree-routes and navigation-diagnostics have the same 63 passes and the same 10 failures with and without this change. The failures are Firefox and WebKit cases plus wasm/js cases that need builds I don't have here.

@janicduplessis
janicduplessis merged commit 7282921 into main Nov 15, 2026
2 checks passed
@janicduplessis
janicduplessis deleted the @janic/sheet-push-autofocus-tap branch November 15, 2026 06:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant