Skip to content

fix(terminal): focus xterm on first click and on attach#2132

Merged
jonathanlab merged 2 commits into
mainfrom
posthog-code/fix-terminal-click-focus
May 14, 2026
Merged

fix(terminal): focus xterm on first click and on attach#2132
jonathanlab merged 2 commits into
mainfrom
posthog-code/fix-terminal-click-focus

Conversation

@posthog
Copy link
Copy Markdown
Contributor

@posthog posthog Bot commented May 13, 2026

Summary

James reported that clicking into the terminal didn't put the cursor in the right place to start typing — he had to click a second time before keystrokes registered.

The root cause was in Terminal.tsx:

  • The wrapper <Box> used onClick to call terminalManager.focus(). React's synthetic click fires after the browser's native focus handling on mousedown/mouseup, so when the click landed on the padding (or anywhere outside the xterm canvas), focus had already shifted off the xterm helper textarea by the time we tried to put it back.
  • Switching tabs also remounts the Terminal, but attach() never called term.focus(), so the terminal panel never auto-focused on appear.

Changes

  • apps/code/src/renderer/features/terminal/components/Terminal.tsx
    • Swap onClickonMouseDown so focus is set before the browser's native focus shift, not after.
    • Call terminalManager.focus(sessionId) right after attach() so opening/switching to a terminal tab focuses the xterm immediately.
  • apps/code/src/renderer/features/sessions/components/SessionView.tsx

Test plan

  • Open a task with a terminal panel — typing works without any click.
  • Switch away from and back to the terminal tab — typing works without any click.
  • Click on the padding around the xterm — first keystroke goes into the terminal.
  • Click directly on the xterm canvas — still focuses (mousedown-driven, idempotent).
  • Drag-select text in the terminal — selection still works (we don't preventDefault).

Created with PostHog Code

Clicking into the terminal required a second click before typing worked.
onClick fires after the browser's native focus handling, so the xterm
helper textarea didn't end up focused. Switch to onMouseDown so focus
is set before the focus shift, and focus on attach so opening/switching
to a terminal tab puts the cursor in the right place immediately.

Also remove a duplicate `isOnline` declaration in SessionView that was
blocking typecheck.

Generated-By: PostHog Code
Task-Id: d2835cfc-8408-4791-b028-49313a248a7c
@jonathanlab jonathanlab marked this pull request as ready for review May 14, 2026 00:37
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 14, 2026

Reviews (1): Last reviewed commit: "Merge branch 'main' into posthog-code/fi..." | Re-trigger Greptile

@jonathanlab jonathanlab enabled auto-merge (squash) May 14, 2026 00:40
@jonathanlab jonathanlab merged commit b0974ef into main May 14, 2026
15 checks passed
@jonathanlab jonathanlab deleted the posthog-code/fix-terminal-click-focus branch May 14, 2026 00:44
MattBro pushed a commit that referenced this pull request May 14, 2026
Co-authored-by: PostHog Code <code@posthog.com>
Co-authored-by: Jonathan Mieloo <32547391+jonathanlab@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant