Skip to content

ui-react: the soft-keyboard hook forces a layout on every keystroke and polls every 250 ms #2590

Open

Description

@schlessera

Context

useSoftKeyboard decides whether the composer is focused with a soft keyboard up, and how many lines the composer draws. Its only consumer hides the shared row once the composer passes three lines with the keyboard up (composer-row.tsx:62-69; D52 §3 R4, docs/decisions/design-kit/sessions-and-drafts.md).

Each read() calls getComputedStyle on the textarea and reads scrollHeight (read, packages/ui-react/src/hooks/use-soft-keyboard.ts:29-40). The hook runs it:

  • on every input event anywhere in the document (:67)
  • every 250 ms while the keyboard is up (POLL_MS, :26-27; the interval at :54)

The input event has just dirtied layout, so reading scrollHeight forces a synchronous style and layout pass on every keystroke. On a phone with a long transcript in the same document, that pass is the expensive part of typing.

The poll exists for a reason (:45-47): an accepted send clears the value and a suggestion fills it, and neither fires an input event. Those value changes matter only because they change the field's height. The kit Composer sizes the field with field-sizing: content and falls back to rows (Composer, packages/ui-kit/src/chrome/Composer.tsx:60-82; the style is at :243). Either way, a value change shows up as a change in the textarea's box, which a ResizeObserver reports without polling and without a forced layout.

Guide performance/performance: "DON'T cause layout thrashing: Avoid interleaving DOM reads (offsetHeight, getBoundingClientRect) and writes (style.height) within the same loop."

Scope

  • Observe textarea[data-composer] with a ResizeObserver while it holds focus. Derive composerLines from the observed content-box block size divided by the line height.
  • Read the line height and vertical padding once per focus, not per update.
  • Keep the visualViewport resize listener and the focusin/focusout handling for open and closed.
  • Remove the 250 ms interval and the document-level input listener.
  • Update the hook's doc comment to say why polling is no longer needed.

Out of scope

  • The R4 threshold, the row's layout, or any change to the shared row.
  • Changing how the kit Composer sizes itself.
  • Detecting the keyboard differently (for example the VirtualKeyboard API). The visual-viewport heuristic and KEYBOARD_MIN_PX stay.

Acceptance criteria

  • A value cleared or filled with no event (the accepted-send and suggestion cases) still updates composerLines while the keyboard is up. Rewrite the existing poll-based case in packages/ui-react/tests/soft-keyboard.test.tsx:79-93 against the observer.
  • Typing does not call getComputedStyle or read scrollHeight/offsetHeight on the composer per input event. A test spies on them, fails on the tree before the change, and passes after.
  • No setInterval remains in the hook, and no input listener remains on document.
  • Real-browser proof: the R4 behaviour (the row hides past three lines with the keyboard up, and returns when the value is cleared by a send) still passes in the existing pointer tests that drive it (working-sessions.pointer.tsx, session-drafts.pointer.tsx). Add a case for the cleared-by-send return if neither covers it.
  • Android support: ResizeObserver is in both Chromium targets and in Firefox for Android, so no fallback is needed. The PR states this. With no field-sizing support, the rows fallback still changes the box and is still observed.

Verification

bun run test packages/ui-react/tests/soft-keyboard.test.tsx
node scripts/visual.mjs --project=rail-coarse --filter=working-sessions
node scripts/visual.mjs --project=rail-coarse --filter=session-drafts
bun run typecheck
bun run lint

Contract impact

  • This touches the integration contract, so it needs the contract label, a CONTRACT: commit prefix, and the doc updated in the same commit.
  • This changes a published package's behaviour, so it needs a changeset.

The hook is internal and its output is unchanged. Patch changeset for @schlessera/brain-ui-react.

Activity

  1. added
    type: fix
    area: ui-react
    priority: p2
    agent-ready
    on Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-readyarea: ui-reactpriority: p2type: fix

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions