Skip to content

fix: keep focus in the standalone Editor after the first keystroke - #416

Open
tunglambk wants to merge 1 commit into
FormidableLabs:masterfrom
tunglambk:fix/standalone-editor-focus
Open

tunglambk wants to merge 1 commit into
FormidableLabs:masterfrom
tunglambk:fix/standalone-editor-focus

Conversation

@tunglambk

Copy link
Copy Markdown

Description

Fixes #415.

The standalone Editor lost DOM focus after the first keystroke, so only one character could be typed until the user clicked back in. use-editable keys its editing-setup layout effect on the element ref, which is still null during the first render. LiveProvider re-renders when its initial transpile resolves, so that effect settles before the user types; the standalone Editor does not re-render until the first edit, so the first edit is what makes the effect re-run. The effect's cleanup resets contentEditable (which blurs the element in Chrome) and its setup then calls focus() while the element is still non-editable, so focus is not restored.

Editor now re-renders once after mount, so use-editable's setup settles before the editor can be edited.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Added packages/react-live/src/components/Editor/index.test.js. It mounts the standalone Editor, drives an edit through use-editable the same way the browser does (DOM mutation plus keydown/keyup), and asserts the editing surface is not torn down and rebuilt.

Without the fix it fails: contentEditable is written twice (undefined, then "plaintext-only"), which is the teardown/rebuild that resets the surface and drops focus. With the fix no writes occur and the edit is applied. Running the same scenario against <LiveProvider> + <LiveEditor> never writes contentEditable, matching the report that the provider path was unaffected.

jsdom has no contentEditable editing or focus model, so the browser-level focus loss itself cannot be reproduced in this suite; the regression test asserts the cause instead. I did not run a real-browser test.

Validation, all run locally on the branch:

  • pnpm test - 6 suites, 20 tests passed.
  • pnpm lint - passed.
  • pnpm run -C packages/react-live typecheck - tsc --noEmit passed.
  • pnpm build - passed.

Checklist:

  • I have included a changeset if this change will require a version change to one of the packages.
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have run all builds, tests, and linting and all checks pass
  • I have added tests that prove my fix is effective or that my feature works
  • My changes generate no new warnings

use-editable keys its editing setup on the element ref, which is null on the
first render. LiveProvider re-renders when its initial transpile resolves, so
the setup settles before the user can type. Standalone Editor does not
re-render until the first edit, so that edit tears the contenteditable surface
down and rebuilds it: `contentEditable` is reset, which blurs the element in
Chrome, and the setup's focus() runs while the element is still non-editable.

Re-render once after mount so the setup settles before the first edit.

Fixes FormidableLabs#415
@vercel

vercel Bot commented Sep 15, 2026

Copy link
Copy Markdown

Someone is attempting to deploy a commit to the Nearform Team on Vercel.

A member of the Team first needs to authorize it.

@changeset-bot

changeset-bot Bot commented Sep 15, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3b8cc3e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
react-live Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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.

Standalone Editor loses focus after the first keystroke

1 participant