fix(mobile): pinned field on phones: swipes, focus scroll, viewport pan, iOS lift

Found on a real Android phone (Chrome) and iPhone (Safari) with Send on Enter:

- A swipe that started on the live screen closed the keyboard: the content
  touchstart blurred the focused input before the gesture was known to be a
  tap or a scroll. It now skips the visible #cjkInput
  (_contentTouchStartBlurs()); a real tap still closes it (inert content, a
  link, a long press).
- Focusing the field scrolled the terminal off screen: KeyboardHandler's
  focusin scrollIntoView({ block: 'center' }) centred the fixed field in its
  ancestors. isInputElement() skips #cjkInput like xterm's inputs.
- Android pans the visual viewport to reveal the field (offsetTop 285 in a
  458/744 viewport), leaving only the terminal's last line visible. On phones
  .app now follows visualViewport.offsetTop while the keyboard is up, and the
  fixed bars inside .app are lifted from .app's bottom, so a partial pan does
  not lift them twice. Non-phone branches and resetLayout() clear the shift.
- On iOS the first keyboard open reported innerHeight as the visual height
  (323 vs 640), zeroing the bars' lift; the layout height is now the larger of
  innerHeight and documentElement.clientHeight.

Tests in test/keyboard-handler-pinned-field.test.ts and
test/terminal-touch-tap.test.ts; docs in architecture-invariants and
src/web/CLAUDE.md; changeset sentence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kamil KaliszandClaude Opus 5.5 committed 2026-11-17 13:45:20 +01:00
1 parent 3c5b7e2199
commit e85bb83353
7 files changed
+272 -16

No files matched your search

+40 -11
View File
@@ -435,21 +435,44 @@ const KeyboardHandler = {
// BEHIND the keyboard with its own search box out of sight.
const caseSheet = document.querySelector('.mobile-case-picker.active .mobile-case-picker-sheet');
// A focused field that starts behind the keyboard (the pinned CJK / Send on
// Enter field) makes Android pan the visual viewport down to reveal it (measured
// on Chrome: offsetTop 285px with a 458px visual viewport in a 744px layout one).
// The app is sized to the visual viewport but laid out at the top of the layout
// viewport, so that pan left only the terminal's last line on screen. On phones,
// follow the pan while the keyboard is up, whatever has focus: without a pan
// (offsetTop 0, the usual case) nothing moves, and a focus change that keeps the
// pan cannot drop the shift. Every other branch clears it.
// ⚠️ The transform makes .app the box its position:fixed children (header,
// toolbar, accessory bar, the field) are placed against, so their keyboard lift
// below is measured from .app's bottom, not the layout viewport's, or they would
// be lifted twice on a partial pan. The case sheet lives outside .app.
const appEl = document.querySelector('.app');
const pan = isSmallMedium ? Math.round(window.visualViewport.offsetTop || 0) : 0;
if (appEl) appEl.style.transform = pan > 0 ? `translateY(${pan}px)` : '';
// The layout viewport's height, which position:fixed bottoms are measured from.
// ⚠️ Not window.innerHeight alone: on the first keyboard open after focusing the
// pinned field, iOS Safari reported innerHeight as the shrunk visual height (323)
// while the layout viewport stayed 640 (documentElement.clientHeight), so the lift
// came out 0 and the bars stayed behind the keyboard. Earlier a delayed
// scrollIntoView re-ran this pass once innerHeight had caught up; without it the
// stale value stuck. The larger of the two cannot be stale in that direction.
const layoutHeight = Math.max(window.innerHeight, document.documentElement?.clientHeight || 0);
const visualBottom = window.visualViewport.offsetTop + window.visualViewport.height;
const keyboardOffset = Math.max(0, layoutHeight - visualBottom);
const insideLift = pan > 0 && appEl ? Math.max(0, pan + appEl.offsetHeight - visualBottom) : keyboardOffset;
if (isSmallMedium) {
// Phones/small tablets: toolbar and accessory bar are position:fixed
// via CSS. Use translateY to lift them above the keyboard.
const toolbar = document.querySelector('.toolbar');
const main = document.querySelector('.main');
const layoutHeight = window.innerHeight;
const visualBottom = window.visualViewport.offsetTop + window.visualViewport.height;
const keyboardOffset = Math.max(0, layoutHeight - visualBottom);
if (toolbar) {
toolbar.style.transform = keyboardOffset > 0 ? `translateY(${-keyboardOffset}px)` : '';
toolbar.style.transform = insideLift > 0 ? `translateY(${-insideLift}px)` : '';
}
if (accessoryBar) {
accessoryBar.style.transform = keyboardOffset > 0 ? `translateY(${-keyboardOffset}px)` : '';
accessoryBar.style.transform = insideLift > 0 ? `translateY(${-insideLift}px)` : '';
}
if (caseSheet) {
caseSheet.style.transform = keyboardOffset > 0 ? `translateY(${-keyboardOffset}px)` : '';
@@ -476,11 +499,8 @@ const KeyboardHandler = {
// CJK textarea positioning (always position:fixed on touch devices).
if (cjkInput?.classList.contains('cjk-input-visible') && keyboardHeight > 0) {
if (isSmallMedium) {
// Phones: use translateY like toolbar/accessory bar.
const layoutHeight = window.innerHeight;
const visualBottom = window.visualViewport.offsetTop + window.visualViewport.height;
const keyboardOffset = Math.max(0, layoutHeight - visualBottom);
cjkInput.style.transform = keyboardOffset > 0 ? `translateY(${-keyboardOffset}px)` : '';
// Phones: use translateY like toolbar/accessory bar (it sits inside .app too).
cjkInput.style.transform = insideLift > 0 ? `translateY(${-insideLift}px)` : '';
cjkInput.style.bottom = '';
} else {
// iPad: direct bottom = keyboard + accessory bar height.
@@ -503,7 +523,11 @@ const KeyboardHandler = {
// keyboard was still up must still have its inline offset cleared, or the
// next open slides in already displaced.
const caseSheet = document.querySelector('.mobile-case-picker-sheet');
const appEl = document.querySelector('.app');
if (appEl) {
appEl.style.transform = '';
}
if (toolbar) {
toolbar.style.transform = '';
}
@@ -733,6 +757,11 @@ const KeyboardHandler = {
if (el.closest('.xterm') || el.closest('.terminal-container')) {
return false;
}
// The pinned CJK / Send on Enter field is position:fixed and lifted above the
// keyboard by updateLayoutForKeyboard(). Scrolling it into view centred it in its
// scrollable ancestors instead, which pushed the terminal off screen whenever the
// field took focus.
if (el.id === 'cjkInput') return false;
const tagName = el.tagName?.toLowerCase();
// Exclude type=range, type=checkbox, type=radio (don't trigger keyboard)
+17 -1
View File
@@ -1378,7 +1378,7 @@ Object.assign(CodemanApp.prototype, {
// selectSession() ends with scrollToLastNonEmptyLine(), that made
// the keyboard unreachable after every tab switch.
ev.preventDefault();
this._blurMobileTerminalInput();
if (this._contentTouchStartBlurs()) this._blurMobileTerminalInput();
}
// Hold still and this press becomes a text selection. Cancelled by any
// travel past the shared tap slop below, so a scroll can never become one.
@@ -5701,6 +5701,19 @@ Object.assign(CodemanApp.prototype, {
return 'content';
},
/**
* Whether a touchstart on transcript content should blur the focused terminal input.
* It does for xterm's helper textarea: that blur, with the preventDefault beside it,
* keeps the compatibility click from opening the keyboard. It must NOT for the pinned
* CJK field: at touchstart the gesture may still be a scroll, and that field owns the
* keyboard while it is visible, so blurring it closed the keyboard on every swipe that
* started on the live screen. A real tap on inert transcript still closes the keyboard
* at touchend (the startedWithTerminalFocus branch), so a tap keeps its meaning.
*/
_contentTouchStartBlurs() {
return document.activeElement?.id !== 'cjkInput';
},
_blurMobileTerminalInput() {
const active = document.activeElement;
if (
@@ -5899,6 +5912,9 @@ Object.assign(CodemanApp.prototype, {
) {
// No focus change: a 'content' tap was already blurred by touchstart, and
// popping the keyboard behind a tab that is about to take over is noise.
// The pinned CJK field is the exception touchstart leaves focused (a swipe must
// not close its keyboard), so a tap that opens a link closes it here instead.
if (!this._contentTouchStartBlurs()) this._blurMobileTerminalInput();
return 'link';
}