oficina / docs Public

web: the bar follows the visual viewport only while the keyboard is up

Review 5 on #181. The bar was moved down by visualViewport.offsetTop
whenever that was above zero, so a pinch-zoom carried it down the page
over what the person was looking at. It now moves only while the
keyboard is up and the visual viewport is not zoomed (scale within
0.01 of 1).

TestAPinchZoomLeavesTheBarAlone zooms the scripted visual viewport 2x
and 300px down, with and without the caret: the bar stays at the top
of the page. It failed before this (the bar at 300).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev e2d688267fdb6990a810adae71a3936fcaf57a1c parent 69db3c1
3 files changed, +30 −2
  • internal/docs/brand/phone.js +5 −2
  • internal/docs/phone_format_browser_test.go +11 −0
  • internal/docs/phone_sheet_browser_test.go +14 −0
diff --git a/internal/docs/brand/phone.js b/internal/docs/brand/phone.js
index 6250df1..95ebb75 100644
--- a/internal/docs/brand/phone.js
+++ b/internal/docs/brand/phone.js
@@ -291,9 +291,12 @@
var focused = !!editor && editor.contains(document.activeElement);
var up = phone && focused && !!vv && vv.height < window.innerHeight - 150;
if (up !== keyboardUp) { keyboardUp = up; fold(); }
- // The bar stays at the top of what can be seen.
+ // The bar stays at the top of what can be seen while the keyboard
+ // is up. Pinch-zoomed (scale not 1), the page is being looked at,
+ // not written in, and the bar stays where the page has it.
if (bar) {
- var off = phone && vv ? Math.max(0, vv.offsetTop) : 0;
+ var zoomed = !!vv && Math.abs((vv.scale || 1) - 1) > 0.01;
+ var off = up && !zoomed ? Math.max(0, vv.offsetTop) : 0;
var t = off > 0.5 ? "translateY(" + off + "px)" : "";
if (bar.style.transform !== t) bar.style.transform = t;
}
diff --git a/internal/docs/phone_format_browser_test.go b/internal/docs/phone_format_browser_test.go
index a021482..17cf1d5 100644
--- a/internal/docs/phone_format_browser_test.go
+++ b/internal/docs/phone_format_browser_test.go
@@ -50,6 +50,17 @@ const fakeViewport = `(() => {
vv.dispatchEvent(new Event('scroll'))
return true
}
+ // A pinch-zoom: the visual viewport scale times smaller, top pixels
+ // down the layout one, and no keyboard.
+ window.__zoom = (scale, top) => {
+ Object.defineProperty(window, 'innerHeight', { configurable: true, get: () => real() })
+ vv.scale = scale
+ vv.height = real() / scale
+ vv.offsetTop = top || 0
+ vv.dispatchEvent(new Event('resize'))
+ vv.dispatchEvent(new Event('scroll'))
+ return true
+ }
})()`
type formatProbe struct {
diff --git a/internal/docs/phone_sheet_browser_test.go b/internal/docs/phone_sheet_browser_test.go
index c1076f3..67a8fed 100644
--- a/internal/docs/phone_sheet_browser_test.go
+++ b/internal/docs/phone_sheet_browser_test.go
@@ -273,3 +273,17 @@ func TestPickersInTheSheet(t *testing.T) {
}
}
}
+
+// Review 5: the bar follows the visual viewport only while the keyboard
+// is up. Pinch-zoomed, with or without the caret, it stays on the page.
+func TestAPinchZoomLeavesTheBarAlone(t *testing.T) {
+ s := newSheetRig(t, "simple.bin", "")
+ s.run("zoomed, no caret", do(`__zoom(2, 300)`), settle)
+ if p := s.probe("zoomed"); p.BarTop != 0 {
+ t.Errorf("pinch-zoomed with no keyboard: the bar's top at %.1f, want 0 (not moved)", p.BarTop)
+ }
+ s.run("zoomed, the caret", chromedp.Click(chromedp.CSS(`[docs-text] p:first-child`)), do(`__zoom(2, 300)`), settle)
+ if p := s.probe("zoomed with the caret"); p.BarTop != 0 {
+ t.Errorf("pinch-zoomed with the caret: the bar's top at %.1f, want 0", p.BarTop)
+ }
+}