Skip to content

fix(scroll lock): keep Safari from panning the locked page for the on-screen keyboard - #727

Merged
blvdmitry merged 4 commits into
mainfrom
claude/happy-lovelace-517cdv
Nov 22, 2026
Merged

blvdmitry merged 4 commits into
mainfrom
claude/happy-lovelace-517cdv

Conversation

@blvdmitry

Copy link
Copy Markdown
Contributor

Summary

Replaces #726, which followed the pan with the modal position. That commit is reverted on this branch, so the diff against main is only the scroll lock change below.

Since #723, edge modals end above the on-screen keyboard. That layout is correct as long as the page stays where it is. But iOS Safari pans the whole page to reveal a focused field, even while the page scroll is locked with overflow. The pan moves fixed content up with the page, so a modal ended above the keyboard by the keyboard height with its top off screen.

A pan is the page scrolling behind an open overlay, so the fix is in the scroll lock. While the page scroll is locked on iOS (preventKeyboardPan in packages/utilities/src/scroll/keyboardPan.ts, called from lockSafariScroll):

  • Focus without a pan: on mousedown (capture) over a field that opens the keyboard, the lock focuses the field itself with preventScroll. For the synchronous focus call, the field is moved 2000px above the screen and hidden, then restored before anything is painted. Safari resolves the pan from the field's geometry at focus time, and preventScroll alone doesn't stop it for fields inside transformed containers, which every modal is.
  • Caret: Safari leaves the caret where the field last had it when the field is focused ahead of its own tap handling, so the caret is put at the press with caretPositionFromPoint.
  • Reveal: focusing without scrolling also skips scrolling the field into view. After the keyboard opens (visual viewport resize), the focused field, or the caret of an editable area, is scrolled into the visible part of its closest scrollable container. The page isn't moved.
  • Backstop: if the page is panned anyway while a keyboard field is focused, it is scrolled back to the position it was locked at. This covers focus that doesn't come from a press on the field, such as the keyboard's previous/next arrows, labels and programmatic focus. Skipped while the page is pinch-zoomed.

Left alone: a field that is already focused (the native tap moves its caret), buttons, links and selects inside an editable area, contenteditable="false" islands, and inputs that don't open a keyboard (checkbox, readonly, disabled and so on).

How it compares to other libraries

  • Base UI (Drawer.VirtualKeyboardProvider): the focus trick is the same as its focusKeyboardInputWithoutPageScroll: off-screen transform plus preventScroll for the synchronous focus call. Its source notes that "iOS Safari can still scroll the page for transformed sheets even with preventScroll". It also puts a panned page back. It triggers on touchend and re-dispatches the click; this PR triggers on mousedown and doesn't prevent anything.
  • React Aria (usePreventScroll): older versions, like the copy bundled in Vaul, used the same off-screen transform on touchend and focus, and scrolled the window back. The current version dropped the transform. It patches HTMLElement.prototype.focus to always pass preventScroll, re-focuses the incoming field from the blur of the outgoing one, cancels touchmove outside scrollable areas, and injects overscroll-behavior: contain. This PR does none of those four.

Related Issue

Follow-up to #723: the Untap page modal ended far above the keyboard on iOS.

Screenshots / Recordings

Not tested on an iOS device yet. All of this only runs when isIOS() is true.

Notes for Reviewers

  • New keyboardPan.test.ts (8 tests) covers: the focus call seeing the overridden geometry and preventScroll, inline styles restored, caret placement, editable areas and the controls inside them, fields left to the browser, the page put back (and not while zoomed or without a focused field), the locked scroll position kept, the reveal after the keyboard opens, and release.
  • lockSafari.test.ts checks the lock installs and removes the handling.
  • Known gap: a field focused by something other than a press on it isn't focused through the override, so the page can pan briefly before the backstop puts it back.
  • Dragging the page with a finger while the keyboard is up isn't cancelled, as React Aria does with touchmove. The backstop puts the page back instead.
  • Because the lock focuses the field in the capture phase, app mousedown handlers that check whether a field is focused will see it focused already.

Generated by Claude Code

… the keyboard

iOS pans the visual viewport down the layout to reveal a focused field once the on-screen keyboard
opens, which moves fixed modals up with the page. Edge modals ended above the keyboard by the
panned distance with their top outside of the screen. The keyboard store now tracks the pan,
useVirtualKeyboard returns it as offset, and Modal moves it from its bottom inset to its top one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKGZMi1nf8CnLci2kLKjSy
…-screen keyboard

iOS pans the whole page to reveal a focused field even while the page scroll is locked with
overflow, which moves the fixed content rendered above it, like modals, outside of the screen.
While the page is locked on iOS, pressed fields are focused without letting Safari pan for them,
the focused field is revealed inside its own scrollable container, and a page panned anyway is
put back. Replaces following the pan with the modal position.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKGZMi1nf8CnLci2kLKjSy
@github-actions

Copy link
Copy Markdown

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
Library / JS 54.73 KB (+1.26% 🔺) 1.1 s (+1.26% 🔺) 0 ms (+100% 🔺) 1.1 s
Library / CSS 24.88 KB (0%) 498 ms (0%) 0 ms (+100% 🔺) 498 ms
Theming / JS 7.8 KB (0%) 156 ms (0%) 106 ms (+554.62% 🔺) 262 ms
Theming with a default theme definition / JS 8.68 KB (0%) 174 ms (0%) 24 ms (+8.7% 🔺) 197 ms

@blvdmitry
blvdmitry merged commit 8b8ae9a into main Nov 22, 2026
11 checks passed
@blvdmitry
blvdmitry deleted the claude/happy-lovelace-517cdv branch November 22, 2026 11:32
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.

2 participants