Repository navigation
[lexical-list] Bug Fix: Toggle checklist items on mobile tap - #8390
Conversation
The CheckList plugin's touchstart listener calls event.preventDefault() to keep the caret away from the marker. On iOS Safari and Android Chrome that suppression also cancels the synthesized click, so handleClick never runs and the checkbox cannot be toggled by tap. Reproducible in the official playground. Add a pointerup listener that runs the same toggle logic when event.pointerType === 'touch'. Deduplicate against any click that does fire (browsers where preventDefault didn't suppress it) via a 500ms window so we never double-toggle. Adds packages/lexical-list/src/__tests__/unit/checkList.test.tsx covering: touch pointerup over the marker toggles, mouse pointerup is ignored (click stays the desktop path), and pointerup followed by a synthesized click does not double-toggle.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
etrepum
left a comment
There was a problem hiding this comment.
This is directionally good, I think the tests could be cleaned up and maybe the tests should explicitly test clicking on multiple checkboxes in a short amount of time because I think this global state would make that not work very well.
| // tap. We additionally listen for pointerup with pointerType === 'touch' | ||
| // and run the same toggle logic, deduplicating against any click that | ||
| // does fire on browsers where preventDefault doesn't suppress it. | ||
| let lastHandledTimeStamp = 0; |
There was a problem hiding this comment.
Would it make more sense to store this on the element itself so you're not prevented from tapping or clicking on several checkboxes really fast?
There was a problem hiding this comment.
Done in 13e7f91 : switched to WeakMap<EventTarget, number> keyed by event.target. Added a regression test that taps two <li> 100ms apart and asserts both toggle (would fail under the previous global timestamp).
There was a problem hiding this comment.
I would just write it directly to the element. You can use a symbol if you want to hide it away but lexical already sets several properties on HTMLElements it manages
There was a problem hiding this comment.
Done in e3ad7fb : switched to __lexicalCheckListLastHandled set directly on the event target (with // @ts-ignore internal field ).
| // | ||
| // Dedup state is per-target: a global window would block tapping a second | ||
| // checkbox within 500ms of toggling the first. | ||
| const lastHandledByTarget = new WeakMap<EventTarget, number>(); |
There was a problem hiding this comment.
I think this would be a bit cleaner and maybe slightly more debuggable if the property was stored directly on the HTMLElement, either with a symbol or a prefixed property. Plenty of other examples of doing this to elements that the editor manages in the codebase, __lexicalEditor on root elements, __lexicalListType on all checklists, __lexicalTableSelection on selected tables, etc.
…t, and swipes from it scroll ## Description On a phone, a check list's checkbox was hard to tap and impossible to scroll from: - A tap toggles on the touch `pointerup`. A finger that moves a few pixels during the tap starts a scroll, and the pointer then ends in `pointercancel`, so the tap does nothing and the page shifts. Chrome scrolls once the finger passes about 8 CSS px; Firefox sends `pointercancel` for any movement at all. A checkbox often took two or three taps. - The `touchstart` on the check mark is canceled (meta#8105) to keep the caret and the on-screen keyboard away, which also keeps the touch from scrolling: a swipe that starts on a checkbox leaves the page where it is, and its `pointerup` then toggles the item (meta#8390). - The 32px touch padding of the hit area was only ever tested on the `<li>` itself, so a touch just left of the checkbox, which lands on the list or whatever lies beside it, toggled nothing. A tap is now taken from touch events, which end in `touchend` either way. The finger may travel 16px; whatever the drift scrolled is put back and held there while a fling it started runs out, and the `touchend` is canceled, so the browser neither places the caret nor sends a click. Moving further is a swipe, which scrolls and toggles nothing. The `touchstart` is no longer canceled. Canceling the touchmoves of a tap instead would also keep it from scrolling in Chrome, but Firefox then holds the whole touch, swipes included. A touch that lands beside an item is matched to the item within the padding to either side, and counts only on the marker's side, so a touch on the item's text still places the caret. The touch listeners are on the document, since such a touch can land outside the root element. The touch `pointerup` toggle goes, and with it the window that kept the click following it from toggling the item again: the canceled `touchend` suppresses that click. ## Test plan ### Before Chromium and Firefox on Android 15 (emulator, Gboard), a 30-item check list: - A tap that drifts 11 CSS px (`input swipe`, 120ms) ends in `pointercancel`, toggles nothing, and scrolls the page; at 9px it toggles about half the time. - A vertical swipe starting on a checkbox does not scroll (scrollTop 15 → 15) and checks the item. - A tap 11px left of the checkbox toggles nothing. ### After Same devices: - Taps drifting up to 15px toggle the item once, and the page stays where it was (scrollTop change 0, from 2–14px in Chromium without the hold). - Swipes of 60–700px starting on a checkbox scroll and toggle nothing. - A tap up to 30px left of the checkbox toggles it; a tap on the item's text places the caret and opens the keyboard. - With the keyboard open, taps toggle the item and the keyboard stays open. `pnpm vitest --project unit --no-watch packages/lexical-list`: ```text Test Files 16 passed (16) Tests 152 passed (152) ``` `tsc`, `tsc-test`, ESLint, and Prettier pass. iOS Safari was not exercised. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t, and swipes from it scroll ## Description On a phone, a check list's checkbox was hard to tap and impossible to scroll from: - A tap toggles on the touch `pointerup`. A finger that moves a few pixels during the tap starts a scroll, and the pointer then ends in `pointercancel`, so the tap does nothing and the page shifts. Chrome scrolls once the finger passes about 8 CSS px; Firefox sends `pointercancel` for any movement at all. A checkbox often took two or three taps. - The `touchstart` on the check mark is canceled (meta#8105) to keep the caret and the on-screen keyboard away, which also keeps the touch from scrolling: a swipe that starts on a checkbox leaves the page where it is, and its `pointerup` then toggles the item (meta#8390). A tap is now taken from touch events, which end in `touchend` either way. The finger may travel 16px; whatever the drift scrolled is put back and held there while a fling it started runs out, and the `touchend` is canceled, so the browser neither places the caret nor sends a click. Moving further is a swipe, which scrolls and toggles nothing. The `touchstart` is no longer canceled. Canceling the touchmoves of a tap instead would also keep it from scrolling in Chrome, but Firefox then holds the whole touch, swipes included. The touch `pointerup` toggle goes, and with it the per-item dedup window that kept the click following it from toggling the item again: the canceled `touchend` suppresses that click. Mouse clicks are unchanged, and tests that desktop clicks toggle every time and that taps on two items toggle both are kept. ## Test plan ### Before `pnpm exec vitest run --project unit packages/lexical-list/src/__tests__/unit/checkList.test.tsx`: ```text × a tap on the marker toggles the item once, canceling the pointerdown and touchend but not the touchstart × a tap whose finger drifts within the slop still toggles × taps on two different items toggle both AssertionError: expected true to be false // Object.is equality AssertionError: expected false to be true // Object.is equality Tests 3 failed | 2 passed (5) ``` Chromium and Firefox on Android 15 (emulator, Gboard), a 30-item check list: - A tap that drifts 11 CSS px (`input swipe`, 120ms) ends in `pointercancel`, toggles nothing, and scrolls the page; at 9px it toggles about half the time. - A vertical swipe starting on a checkbox does not scroll (scrollTop 15 → 15) and checks the item. ### After ```text Tests 5 passed (5) ``` `pnpm exec vitest run --project unit packages/lexical-list`: ```text Test Files 16 passed (16) Tests 152 passed (152) ``` Same devices: - Taps drifting up to 15px toggle the item once, and the page stays where it was (scrollTop change 0, from 2–14px in Chromium without the hold). - Swipes of 60–700px starting on a checkbox scroll and toggle nothing. - With the keyboard open, taps toggle the item and the keyboard stays open. `tsc`, ESLint and Prettier pass. iOS Safari was not exercised. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t, and swipes from it scroll ## Description On a phone, a check list's checkbox was hard to tap and impossible to scroll from: - A tap toggles on the touch `pointerup`. A finger that moves a few pixels during the tap starts a scroll, and the pointer then ends in `pointercancel`, so the tap does nothing and the page shifts. Chrome scrolls once the finger passes about 8 CSS px; Firefox sends `pointercancel` for any movement at all. A checkbox often took two or three taps. - The `touchstart` on the check mark is canceled (meta#8105) to keep the caret and the on-screen keyboard away, which also keeps the touch from scrolling: a swipe that starts on a checkbox leaves the page where it is, and its `pointerup` then toggles the item (meta#8390). A tap is now taken from touch events, which end in `touchend` either way. The finger may travel 16px; whatever the drift scrolled is put back and held there while a fling it started runs out, and the `touchend` is canceled, so the browser neither places the caret nor sends a click. Moving further is a swipe, which scrolls and toggles nothing. The `touchstart` is no longer canceled. Canceling the touchmoves of a tap instead would also keep it from scrolling in Chrome, but Firefox then holds the whole touch, swipes included. The touch `pointerup` toggle goes, and with it the per-item dedup window that kept the click following it from toggling the item again: the canceled `touchend` suppresses that click. Mouse clicks are unchanged, and tests that desktop clicks toggle every time and that taps on two items toggle both are kept. ## Test plan ### Before `pnpm exec vitest run --project unit packages/lexical-list/src/__tests__/unit/checkList.test.tsx`: ```text × a tap on the marker toggles the item once, canceling the pointerdown and touchend but not the touchstart × a tap whose finger drifts within the slop still toggles × taps on two different items toggle both AssertionError: expected true to be false // Object.is equality AssertionError: expected false to be true // Object.is equality Tests 3 failed | 2 passed (5) ``` Chromium and Firefox on Android 15 (emulator, Gboard), a 30-item check list: - A tap that drifts 11 CSS px (`input swipe`, 120ms) ends in `pointercancel`, toggles nothing, and scrolls the page; at 9px it toggles about half the time. - A vertical swipe starting on a checkbox does not scroll (scrollTop 15 → 15) and checks the item. ### After ```text Tests 5 passed (5) ``` `pnpm exec vitest run --project unit packages/lexical-list`: ```text Test Files 16 passed (16) Tests 152 passed (152) ``` Same devices: - Taps drifting up to 15px toggle the item once, and the page stays where it was (scrollTop change 0, from 2–14px in Chromium without the hold). - Swipes of 60–700px starting on a checkbox scroll and toggle nothing. - With the keyboard open, taps toggle the item and the keyboard stays open. `tsc`, ESLint and Prettier pass. iOS Safari was not exercised. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Description
You can't tap to toggle a checklist item on iOS Safari or Android Chrome. The bug is reproducible in the Lexical playground and in Chrome DevTools mobile emulation.
The cause: the
touchstartlistener registered by the CheckList plugin callsevent.preventDefault()to keep the caret away from the marker, which on mobile also cancels the synthesizedclick, and the click is whathandleClickis wired to. So the toggle path is never reached. PR #7655 widened the touch hit-test, but the click that the hit-test was meant to handle was already gone by then, so taps still did nothing.The fix is to also listen for
pointerupand run the toggle from there whenevent.pointerType === 'touch'. To stay safe on browsers where the click does eventually fire, a 500 ms dedup window swallows the second event so we never toggle twice. Desktop click is left alone.While I was in there, I widened the handler signatures from
MouseEvent | TouchEventtoPointerEvent | MouseEvent | TouchEvent(they were already accepting pointer events at runtime, this just makes it explicit) and replaced the(event as PointerEvent).pointerTypecast insidehandleCheckItemEventwith a'pointerType' in eventguard.Closes #7651
Test plan
Before
Tap a checklist marker in the playground on iOS Safari / Android Chrome / Chrome DevTools mobile emulation : nothing happens.
The new
packages/lexical-list/src/__tests__/unit/checkList.test.tsxfile fails onmain:🔴 touch pointerup over the marker area toggles the item
🔴 : pointerup followed by a synthesized click does not double-toggle
🟢 mouse pointerup is ignored (click stays the desktop path)
After
Same tap toggles the checkbox.
The three new tests pass, and the rest of the
lexical-listsuite still does (86/86).pnpm tscandpnpm lintare clean.