Skip to content

fix(PromptInput): place caret at end on initial focus - #237

Open
astandrik wants to merge 3 commits into
mainfrom
astandrik/fix-152-prompt-input-caret
Open

fix(PromptInput): place caret at end on initial focus#237
astandrik wants to merge 3 commits into
mainfrom
astandrik/fix-152-prompt-input-caret

Conversation

@astandrik

@astandrik astandrik commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

What changed

  • Move the caret to the end of a non-empty value on the first focus after mount.
  • Defer pointer-originated initial focus handling until pointerup, after the browser has completed a click or drag selection.
  • Preserve native caret placement on subsequent focus and click events.
  • Preserve explicit text selections created during the first focus.
  • Cover pointer focus, slow pointer selection, refocus, autofocus, and explicit selection with regression tests based on the existing Playground story.

Verification

  • npm run playwright:docker -- --grep "@PromptInput" — 42 passed
  • npm run playwright:docker — 420 passed
  • npm run playwright:docker -- --grep "caret|autofocus|selection" --repeat-each=20 — 100 passed
  • npm test — 281 client and 19 server tests passed
  • npm run lint
  • npm run build
  • Chromium, Firefox, and WebKit Storybook interaction matrix — 48/48 scenarios passed
  • git diff --check
  • git merge-tree --write-tree origin/main HEAD

Closes #152

@gravity-ui

gravity-ui Bot commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

🚀 Prerelease version published!

Install this PR version:

npm i --save-dev @gravity-ui/aikit@2.20.2-beta.14c80e411a3ea24056ed623e72808a95f1c2349d.0

@gravity-ui-bot

Copy link
Copy Markdown

Preview is ready.

@astandrik
astandrik marked this pull request as ready for review August 22, 2026 09:51
Copilot AI lite review requested due to automatic review settings August 22, 2026 09:51
@gravity-ui

gravity-ui Bot commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

🎭 Component Tests Report is ready.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes PromptInput’s initial caret placement so that when the textarea mounts with a non-empty value, the first focus moves the caret to the end (including pointer focus and autofocus), while subsequent focuses/clicks keep the browser’s native caret behavior. This aligns PromptInput UX with typical “resume typing at end” expectations and addresses #152.

Changes:

  • Add a one-time onFocus handler in PromptInputBody that moves the caret to the end on the first focus after mount (using requestAnimationFrame to avoid fighting pointer-based placement timing).
  • Add Playwright component regression tests covering first-focus behavior, refocus behavior, and autofocus behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/components/organisms/PromptInput/tests/PromptInput.visual.spec.tsx Adds regression interaction tests for caret placement on first focus, refocus, and autofocus.
src/components/molecules/PromptInputBody/PromptInputBody.tsx Implements one-time initial-focus caret-to-end behavior via an onFocus handler.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we defer pointer-originated initial focus handling until pointerup? During a slow drag, the first requestAnimationFrame can run while the selection is still collapsed and move the caret to the end before the drag creates a range, so an explicit selection may be lost. I suggest preserving non-collapsed drag selections and moving only a collapsed click to the end, while keeping the keyboard/programmatic autofocus path.
Suggestion: please add a slow mouse down/drag/up regression test; the current selectText() test establishes the range before the RAF runs.

@astandrik astandrik Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9083eda.

On the first pointer focus, we now wait for pointerup before changing the caret. A regular click still puts a collapsed caret at the end. If the user drags to select text, we leave the selection created by the browser intact.

I added a regression test that holds the mouse button down through two animation frames, then drags across some and releases it. On the previous commit, the selection collapsed at the end (4..4). With this fix, the whole value stays selected (0..4).

Verification passed: 42 @PromptInput tests, 420 component tests, and all CI checks on 9083eda. I also checked the refreshed preview: a regular click and autofocus end at 4..4, while an empty value stays at 0..0.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recorded the same first-focus drag in Chromium, Firefox and WebKit (Playwright 1.56.1, Linux Docker). The old version loses the selection in Chromium and Firefox; WebKit already preserves it. With 9083eda, all three preserve the selection.

These are the bundled browser versions shown in the recording. Manual checks in Chrome 152 on macOS did not reproduce the loss.

pr237-browser-matrix

@astandrik
astandrik requested review from ananas7 and a balanced review from Copilot September 1, 2026 14:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@astandrik
astandrik force-pushed the astandrik/fix-152-prompt-input-caret branch from 9083eda to 14c80e4 Compare September 5, 2026 07:11
@astandrik
astandrik requested review from ananas7 and removed request for ananas7 September 5, 2026 07:13
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.

PromptInput: сursor is not placed at the end when input has initialValue

4 participants