Repository navigation
Commit b8bf7a0
authored
fix: keep focus in the standalone Editor after the first keystroke (#416)
### 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.
The branch is rebased on master, so the test uses the vitest setup from
#418: `packages/react-live/src/components/Editor/index.test.jsx`.
#### Type of Change
- [x] Bug fix (non-breaking change which fixes an issue)
### How Has This Been Tested?
Added a regression test in
`packages/react-live/src/components/Editor/index.test.jsx`. 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.
The test takes `act` from `@testing-library/react` rather than `react`,
because `React.act` only exists from 18.3.0 and the `React 18.0.0` job
fails with `act is not a function` without it. The library re-exports
`act` for whichever React it is testing against, and the other tests in
this package already import it from there.
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 rebased branch against the lockfile
React 19.3.0 and against React 18.0.0 (pinned exactly the way the
`react-compat` job does):
- `npm run check` under React 19.3.0 - lint, format, typecheck, and
tests passed (8 files, 58 tests).
- `npm run check` under React 18.0.0 - same result, and the test fails
with `act is not a function` before the import change.
- `npm run build` - library and docs site build passed under both.
### Checklist:
- [x] I have included a changeset if this change will require a version
change to one of the packages.
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have run all builds, tests, and linting and all checks pass
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] My changes generate no new warnings1 parent 876a2a9 commit b8bf7a0
3 files changed
Lines changed: 122 additions & 2 deletions
File tree
- .changeset
- packages/react-live/src/components/Editor
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
2 | 4 | | |
3 | 5 | | |
4 | 6 | | |
| |||
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
13 | 84 | | |
14 | 85 | | |
15 | 86 | | |
| |||
102 | 173 | | |
103 | 174 | | |
104 | 175 | | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
105 | 208 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
31 | 43 | | |
32 | 44 | | |
33 | 45 | | |
| |||
0 commit comments