Repository navigation
[lexical-list][lexical-playground] Bug Fix: keep the focus on the editor when a press lands in a check list item's label - #9088
Conversation
…g a check list item when space is typed in its label ## Description Putting the caret in the text of a check list item and pressing space toggles the checkbox in Firefox instead of typing a space. Chrome and Safari type it. The item carries role="checkbox" and tabIndex="-1" so the plugin can move focus onto it, and getActiveCheckListItem reads that focus as "the checkbox is what the keyboard is operating". Firefox also focuses the <li> on its own when a click lands inside it, because the caret's nearest focusable ancestor is that <li>. The DOM selection is the same collapsed caret inside the item's text in every case, including the two flows that are supposed to toggle, so only the origin of the focus separates them. The plugin now marks the item when it focuses one on purpose, in handleClick and in the two arrow handlers, and clears the mark on the capture-phase pointer press that already runs on the root. getActiveCheckListItem requires that mark, so a focus Firefox moved by itself no longer counts. The item is not reachable by Tab, so programmatic focus and the browser's own click behavior are the only two ways it gets focus. Closes meta#4680 ## Test plan ### Before Three e2e tests under "Checklist space key" in List.spec.mjs. On Firefox the first one fails and the two that cover toggling pass: Error: expect(locator).toHaveAttribute(expected) failed Locator: locator('li[role="checkbox"]').first() Expected: "false" Received: "true" 1 failed [firefox] List.spec.mjs:173:3 > Checklist space key > typing a space in the label leaves the checkbox alone 2 passed (20.0s) ### After firefox 3 passed (6.7s) chromium 3 passed (2.7s) webkit 3 passed (3.5s) vitest --project unit packages/lexical-list: 127 tests passing. tsc, prettier and eslint clean. The whole List.spec.mjs on Firefox leaves one failure, "Should outdent if indented when the backspace key is pressed only at the front", which fails the same way with checkList.ts stashed.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
etrepum
left a comment
There was a problem hiding this comment.
I'm not sure this is solving the right problem, what's happening here is that a click anywhere in the list is setting the document.activeElement to the li tag which is not the expected result unless you click on the checkbox.
…label A check list item carries tabIndex="-1" so the plugin can focus it for the checkbox role, and that also makes it the nearest focusable ancestor of the caret. Measured on a press in the item's text, every browser focuses the <li>: Chrome and Safari hand the focus back to the root before mouseup, Firefox leaves it there. The key handlers read a focused item as "the checkbox is what the keyboard is operating", which is why Space toggled the item in Firefox instead of typing a space. Correct the focus rather than the space handler. A press that does not land on the check mark returns the focus to the root, one task later so the press can finish placing the caret first, and getActiveCheckListItem goes back to what it was. The new test covers clicking the label of an item that already holds the focus, right after its checkbox was clicked. That press moves no focus at all, so Firefox kept toggling there too.
|
You're right. I went and looked at what the focus does on a press in the item's text, and it lands on the Chrome and Safari give the focus back to the root before mouseup, Firefox leaves it on the The new commit corrects the focus instead. A press that does not land on the check mark returns the focus to the root, one task later so the browser can finish placing the caret first. It also caught a case my first attempt missed. Clicking the label of an item that already holds the focus, right after clicking its checkbox, fires no focus event at all, so Firefox kept toggling there. There is a test for it now. |
…ntry, and drop the two timers ## Description The commit before this one returns the focus to the root when a press lands in a check list item's content rather than on its check mark, which is the right shape: the browsers that get this right are correcting the focus, not the space handler. Two things are left over. Text entry is not covered. Chrome and Safari also hand focus back to the editing host when text is entered, even when the item was focused first, so after clicking a check mark and typing, the checkbox stops being what the keyboard operates. Firefox keeps the item focused, so Space toggles the item instead of typing a space -- meta#4680 again, one step further in. A `beforeinput` listener in the capture group does the same correction. A toggling Space never reaches it: the command handler calls `preventDefault()` on keydown, so all three ways an item comes to hold focus -- a press on its check mark, arrow navigation between items, and the left arrow at the start of a label -- still leave it focused and still toggle. Both of the plugin's `setTimeout(..., 0)` calls also go. The focus restore used one so the press could finish placing the caret first; the capture-phase handler now records what the press landed on and the restore runs when the gesture ends, on `pointerup` or `click`, whichever comes first, which is inside the gesture rather than a task behind it. That decoupling is why the press is recorded as `'checkMark' | 'content' | 'none'`: with the restore no longer attached to the press itself, `'none'` keeps a click with no press behind it -- one a screen reader synthesizes on the focused item -- from moving the focus. `handleArrowUpOrDown` used the other timer to focus the item after the reconciler had moved the selection, which is what `editor.update`'s `onUpdate` means; the element is resolved from the node key there, so a reconciliation that replaced it is followed to the current one. Closes meta#4680 ## Test plan Six e2e tests under "Checklist space key" in List.spec.mjs, run against the built playground on port 4000. Two are added here: the left arrow onto an item, and text entry handing focus back. ### Before npx playwright test --project=firefox List.spec.mjs -g "Checklist space key" 1) Checklist space key › typing in the label hands focus back to the editor Expected: true Received: false (the item still holds focus after typing) 1 failed 5 passed (12.9s) ### After firefox 41 passed (1.0m) chromium 41 passed (45.1s) That is the whole of List.spec.mjs, which also covers the two "Checklist focus option" tests for `disableTakeFocusOnClick` and the existing keyboard navigation test. The focus assertion on the first test in the group drops its `expect.poll`, which the timer needed; the restore is synchronous now. vitest --project unit packages/lexical-list: 127 tests passing. tsc, prettier and eslint clean. WebKit was not exercised: its Linux dependencies are missing in this environment.
…cused only while the keyboard is operating its checkbox ## Description The commits before this one return the focus to the editing host when a press lands in a check list item's content, and when text is entered while the item holds the focus. Two cases are still left holding it on the item while the keyboard and caret are operating the text, so Space keeps toggling the checkbox instead of typing a space in Firefox (meta#4680). A caret-moving key. Putting the caret at the start of a label and pressing the left arrow moves the focus onto the checkbox, which is deliberate, but pressing the right arrow from there moves the caret back into the text and the checkbox stays focused; the same goes for any arrow that moves the caret while an item is focused. A `keydown` listener hands the focus back for every key the checkbox does not itself use: Space toggles it, the vertical arrows move to the sibling item, Escape hands the focus back on its own, and a modifier pressed alone operates nothing. A toggling Space never reaches the listener either way, since the command handler calls `preventDefault()` on keydown, so the three ways an item comes to hold focus -- a press on its check mark, arrow navigation between items, and the left arrow at the start of a label -- still leave it focused and still toggle. The press itself. The restore runs when the gesture ends, so the item holds the focus from mousedown until the button comes back up. It now also runs from `focusin`, as soon as the browser moves the focus, which is Firefox only: Chrome and Safari correct themselves as part of placing the caret, and stepping in before they do puts the caret at the start of the item instead, measured on a press at three offsets in both engines. A press on an item that already holds the focus moves no focus at all and so fires no `focusin`, which is what the release is still there for. Closes meta#4680 ## Test plan Three e2e tests are added under "Checklist space key" in List.spec.mjs, one per case above plus the left arrow onto an item. ### Before The two arrow tests fail on Firefox and pass on Chromium: npx playwright test --project=$B List.spec.mjs -g "arrow" firefox 2 failed, 2 passed (12.0s) chromium 4 passed (6.4s) Expected: true Received: false (the item still holds the focus) The press test fails against the gesture-end restore alone: 1 failed ### After Whole file, both engines, measured on the same change rebased onto the previous head of this branch: firefox 44 passed (1.1m) chromium 44 passed (46.8s) vitest --project unit packages/lexical-list: 127 tests passing. tsc, prettier and eslint clean. WebKit was not exercised: its Linux dependencies are missing in this environment. The run against this exact base is in flight; this note will be wrong only if it disagrees.
…a check list item it put the focus on
## Description
The commit before this one hands the focus back to the editing host for every
key a focused checkbox does not itself use, so that moving the caret through a
label stops leaving the checkbox as what the keyboard operates. The left arrow
should not have been among them: it is the one key that means either thing.
Pressing it at the start of a label is what moves the focus onto the checkbox,
and pressing it again is what leaves the item, and `KEY_ARROW_LEFT_COMMAND`
tells those apart by asking whether the item already holds the focus. Handing
the focus back before it runs makes every left arrow look like the first kind,
so it took the focus back onto the checkbox and called `preventDefault()`, and
the caret could not get out of the item at all. That reads as the caret being
stuck on the checkbox, in every browser, since the handler is the same in all
of them.
The left arrow is left to its own handler, which now hands the focus back
itself on the second press, where it used to only let the caret go.
## Test plan
### Before
npx playwright test --project=$B List.spec.mjs -g "left arrow leaves"
1) the left arrow leaves the item when the checkbox already holds focus
Expected: true
Received: false (the checkbox still holds the focus)
firefox 1 failed
chromium 1 failed
### After
firefox 45 passed (1.1m)
chromium 45 passed (47.2s)
That is the whole of List.spec.mjs. vitest --project unit
packages/lexical-list: 129 tests passing. tsc, prettier and eslint clean.
WebKit was not exercised: its Linux dependencies are missing in this
environment.
## Description Sync with main (24 commits, through 1723cd1). Conflict resolutions and semantic reconciliation with the checklist/semantic-nesting work: - ListItemNode.insertAfter split: keep `$copyListForSplit` (carries the semantic nesting mark) and adopt main's meta#7032 numbering fix on top of it (`setStart($getNewListStart(...))` for numbered lists). - ListItemNode.remove / mergeLists: main added a same-listType guard before merging surrounding sublists (meta#9050). `$collapseWrapperPair` now implements that policy for both call sites: differing-type boundary lists leave the two wrapper items as separate siblings (matching main's new tests) instead of collapsing them into one multi-list item. - ListImportExtension: adopt main's `listNode.createListItemNode()` subclass hook in `$normalizeListChildren`, threading it through `$normalizeSemanticChildren` as the wrapper-item factory; keep the shared `$isDomChecklist` helper over main's local copy. - checkList focus lifecycle (meta#9088/meta#9102): adopt main's press-tracking, returnFocusToRoot, CHECKBOX_KEYS keydown, and onUpdate-based arrow focus (replacing setTimeout) — reconciled with the native checkbox input mode: returnFocusToRoot resolves the focused input to its row via getActiveCheckListItem; the capture keydown defers ArrowRight on the input to KEY_ARROW_RIGHT_COMMAND (which needs the input still focused to fix the possibly-stale caret first); ArrowLeft on a focused native input stays a no-op (Right returns to the text, Escape exits) while the legacy li-focus mode keeps main's exit-to-root behavior; handleArrowUpOrDown keeps the wrapper-aware traversal, $selectCheckRowStart and plain-row focus-mode exit inside main's onUpdate structure. - $insertList: keep the three-argument $isSelectingEmptyListItem; adopt main's `$newListFrom` (copy-based retype) with `$copySemanticNestingMark` preserved at both call sites. - $removeList: keep the nested-list relocation and adopt main's format/indent/direction/style copy onto the replacement paragraph. - LexicalUtils: union — main's `removeEmptyDOMAttribute` plus the readonly `getCachedClassNameArray` docs. ## Test plan ### After ``` pnpm run test-unit — all packages pass pnpm vitest --project browser run packages/lexical-list Tests 25 passed (25) (4 consecutive runs) pnpm run ci-check — passes ``` Co-Authored-By: Claude Fable 5 <[email protected]>
Description
In Firefox, putting the caret in the text of a check list item and pressing space toggles the checkbox instead of typing a space. Chrome and Safari type the space.
The item carries
role="checkbox"andtabIndex="-1"so the plugin can move focus onto it, andgetActiveCheckListItemreads a focused item as "the checkbox is what the keyboard is operating". ThattabIndexalso makes the<li>the nearest focusable ancestor of the caret, so pressing the mouse in the item's text focuses the item. Every browser does that. What differs is what happens next, measured in the playground on a press in the wordTask:focusout DIV,focusin LILI[checkbox]focusout DIV,focusin LI,focusout LI,focusin DIVDIV[textbox]DIV[textbox]Chrome and Safari hand the focus back to the root before mouseup. Firefox leaves it on the item, and that is the focus the space handler was reading.
So the plugin hands it back itself now. A pointer press that does not land on the check mark returns the focus to the root, one task later so the press can finish placing the caret first, and only if a check list item is still what holds it.
getActiveCheckListItemis unchanged frommain.Closes #4680
Test plan
Four e2e tests in
List.spec.mjsunderChecklist space key. Two are the defect: typing a space in the label, and clicking the label of an item that already holds the focus, which is the press that moves no focus at all. The other two cover the flows that have to keep toggling, which had no coverage before.Before
With
packages/lexical-list/src/checkList.tsreverted tomain:The two toggling tests pass without the change, so the failures are the defect and nothing else.
After
Also run:
vitest --project unit packages/lexical-list: 11 files, 127 tests passingtsc --noEmit,prettier --list-differentandeslinton the two changed files: cleanList.spec.mjsin the three browsers: 39 passing and one failure,Nested List > Should outdent if indented when the backspace key is pressed only at the front. It fails the same way withcheckList.tsreverted tomain, which I confirmed in Firefox and Chromium, so it is not from this change. I did not check that one againstmainin WebKit.I did not exercise the mobile touch path or a collab editor, and the new tests skip collab the way the neighbouring checklist tests do.