iOS: the keyboard stays open — the viewport guard no longer blurs the field you are typing in (#242)

Tapping the name or invite-code field on the login sheet threw the keyboard straight back
out, about four times in five, and the same happened in every other sheet on iPad and iPhone.

`realign()` corrects a page iOS left displaced after the keyboard closed, and it used to let
go of a text field that still held focus, because WebKit does not blur an input when a button
is tapped and iOS only puts the viewports back once focus has left. The guard around it is
`keyboardOpen()`, which compares `innerHeight` with the visual viewport — and both of those
move while the keyboard animates in, because the browser chrome collapses at the same time.
Measured on iOS 26.6: `innerHeight` fell 684 → 374 while the visual viewport sat at 158, so
the difference dipped back under the threshold and the keyboard read as closed roughly 100 ms
after every tap. The guard then blurred the field the person had just tapped.

The detector cannot be made reliable, so nothing acts on it while somebody is typing: with a
text field focused, `realign()` now does nothing at all — no blur, no scroll, and no unpin of
a body that a sheet has pinned. The correction still runs from `focusout`, which is when iOS
actually needs it, and the two cases the blur was written for already let go of the field
themselves: Workout.jsx blurs when a set is ticked, Modals.jsx when a sheet opens.

Reported by FabianReitz with a screen recording, and diagnosed by hectorroaz, who instrumented
a build on a real device and posted the timings that show the mid-animation misfire.
This commit is contained in:
Duarte Santos committed 2026-09-20 19:56:42 +02:00
1 parent c04fd512d4
commit a7d710902c
2 files changed
+30 -11

No files matched your search

+11 -5
View File
@@ -37,11 +37,17 @@ export function keyboardOpen(win = window) {
export function realign(win = window) {
if (keyboardOpen(win)) return false
if (viewportDisplacement(win) <= 1) return false
// The keyboard is gone but a text field may still hold focus: on WebKit tapping a <button>
// (the set's tick, the RIR cell) does not blur the input, and iOS only puts the viewports
// back once focus has left. Nothing is being typed with the keyboard down, so let it go.
const active = win.document.activeElement
if (isText(active)) active.blur?.()
// Never touch a page while a text field has focus — no blur, no scroll, no unpin (issue #242).
// `keyboardOpen` cannot be trusted mid-animation: both sides of its subtraction move, because
// the browser chrome collapses while the keyboard comes up. Measured on iOS 26.6 with the
// keyboard animating in, `innerHeight` fell 684 → 374 while the visual viewport sat at 158,
// so the difference dropped back under the threshold and the keyboard read as closed about
// 100 ms after every tap. This used to blur the field there, which is why the login sheet's
// name and invite-code fields threw the keyboard straight back out.
// Nothing is lost by standing down: the correction still runs on `focusout`, which is when
// iOS actually needs it, and the two cases the blur was written for let go of the field
// themselves — Workout.jsx blurs when a set is ticked, Modals.jsx when a sheet opens.
if (isText(win.document.activeElement)) return false
if (bodyPinned(win)) return realignPinned(win)
win.scrollTo(win.scrollX || 0, win.scrollY || 0)
return true
+19 -6
View File
@@ -43,20 +43,33 @@ describe('viewport guard', () => {
expect(realign(fakeWindow({ offsetTop: 190, vvHeight: 480 }))).toBe(false) // keyboard still up
})
it('lets go of a text field that kept focus after the keyboard closed, then realigns', () => {
// WebKit: tapping the set's tick does not blur the weight field. With the keyboard down
// and the page displaced, that focus is what keeps iOS from putting the viewports back.
it('stands down while a text field has focus, however displaced the page looks (issue #242)', () => {
// `keyboardOpen` is unreliable while the keyboard animates in — the browser chrome collapses
// at the same time, so `innerHeight` falls with the visual viewport and the difference dips
// back under the threshold. A displaced page plus a focused field is therefore "somebody is
// typing", not "the keyboard has gone": it used to blur the field and throw the keyboard out.
const active = { tagName: 'INPUT', blur: vi.fn() }
const w = fakeWindow({ offsetTop: 190, active })
expect(realign(w)).toBe(true)
expect(active.blur).toHaveBeenCalledTimes(1)
expect(w.scrollTo).toHaveBeenCalledWith(0, 0)
expect(realign(w)).toBe(false)
expect(active.blur).not.toHaveBeenCalled()
expect(w.scrollTo).not.toHaveBeenCalled()
// the same while a sheet has the body pinned: no unpin/repin under the keyboard either
const pinned = fakeWindow({ offsetTop: 190, active: { tagName: 'TEXTAREA', blur: vi.fn() }, bodyStyle: { position: 'fixed', top: '-120px' } })
expect(realign(pinned)).toBe(false)
expect(pinned.scrollTo).not.toHaveBeenCalled()
// an aligned page is left alone, focus included (a desktop browser mid-typing)
const typing = { tagName: 'INPUT', blur: vi.fn() }
expect(realign(fakeWindow({ active: typing }))).toBe(false)
expect(typing.blur).not.toHaveBeenCalled()
})
it('still realigns a displaced page once focus has left the field', () => {
// The correction that used to ride on the blur now happens on `focusout`, when iOS needs it.
const w = fakeWindow({ offsetTop: 190, active: { tagName: 'BUTTON' } })
expect(realign(w)).toBe(true)
expect(w.scrollTo).toHaveBeenCalledWith(0, 0)
})
it('unpins the body for one scroll when a sheet has it fixed, and pins it back where it was', () => {
const bodyStyle = { position: 'fixed', top: '-240px' }
const w = fakeWindow({ offsetTop: 190, scrollY: 0, bodyStyle })