Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

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

Duarte Santosa7d710902cc0
diff --git a/frontend/src/lib/viewport-guard.js b/frontend/src/lib/viewport-guard.js
index 2ba61023c..390f3b0c4 100644
--- a/frontend/src/lib/viewport-guard.js
+++ b/frontend/src/lib/viewport-guard.js
@@ -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
diff --git a/frontend/src/lib/viewport-guard.test.js b/frontend/src/lib/viewport-guard.test.js
index b2a709b39..dffc46735 100644
--- a/frontend/src/lib/viewport-guard.test.js
+++ b/frontend/src/lib/viewport-guard.test.js
@@ -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 })