Repository navigation
Description
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
inputevent 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 aResizeObserverwhile it holds focus. DerivecomposerLinesfrom 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
visualViewportresizelistener and thefocusin/focusouthandling for open and closed. - Remove the 250 ms interval and the document-level
inputlistener. - 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
Composersizes itself. - Detecting the keyboard differently (for example the VirtualKeyboard API). The visual-viewport heuristic and
KEYBOARD_MIN_PXstay.
Acceptance criteria
- A value cleared or filled with no event (the accepted-send and suggestion cases) still updates
composerLineswhile the keyboard is up. Rewrite the existing poll-based case inpackages/ui-react/tests/soft-keyboard.test.tsx:79-93against the observer. - Typing does not call
getComputedStyleor readscrollHeight/offsetHeighton the composer perinputevent. A test spies on them, fails on the tree before the change, and passes after. - No
setIntervalremains in the hook, and noinputlistener remains ondocument. - 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:
ResizeObserveris in both Chromium targets and in Firefox for Android, so no fallback is needed. The PR states this. With nofield-sizingsupport, therowsfallback 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 lintContract impact
- This touches the integration contract, so it needs the
contractlabel, aCONTRACT: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