diff --git a/packages/lexical-list/src/__tests__/unit/checkList.test.tsx b/packages/lexical-list/src/__tests__/unit/checkList.test.tsx index d8791591bc4d..a5ddef703696 100644 --- a/packages/lexical-list/src/__tests__/unit/checkList.test.tsx +++ b/packages/lexical-list/src/__tests__/unit/checkList.test.tsx @@ -245,4 +245,111 @@ describe('CheckListExtension mobile tap toggle', () => { expect(readChecked(editor, 0)).toBe(true); }); + + // https://github.com/facebook/lexical/issues/4861 + describe('a press on the content released over the check mark', () => { + const CHECK_MARK_X = 10; + const CONTENT_X = 200; + + /** A mouse gesture: press, release, and the click that follows. */ + function mouseGesture( + li: HTMLElement, + pressX: number, + releaseX: number, + button = 0, + ) { + const init = {bubbles: true, button, cancelable: true, clientY: 5}; + li.dispatchEvent( + new PointerEvent('pointerdown', { + ...init, + clientX: pressX, + pointerType: 'mouse', + }), + ); + li.dispatchEvent(new MouseEvent('mousedown', {...init, clientX: pressX})); + li.dispatchEvent( + new PointerEvent('pointerup', { + ...init, + clientX: releaseX, + pointerType: 'mouse', + }), + ); + li.dispatchEvent(new MouseEvent('mouseup', {...init, clientX: releaseX})); + if (button === 0) { + li.dispatchEvent(new MouseEvent('click', {...init, clientX: releaseX})); + } + } + + it('does not toggle the item (a text selection dragged onto it)', () => { + using editor = buildEditor(); + mouseGesture(getCheckListItem(0), CONTENT_X, CHECK_MARK_X); + expect(readChecked(editor, 0)).toBe(false); + }); + + it('does not toggle the item on touch either', () => { + using editor = buildEditor(); + const li = getCheckListItem(0); + const init = {bubbles: true, cancelable: true, clientY: 5}; + li.dispatchEvent( + new PointerEvent('pointerdown', { + ...init, + clientX: CONTENT_X, + pointerType: 'touch', + }), + ); + li.dispatchEvent( + new PointerEvent('pointerup', { + ...init, + clientX: CHECK_MARK_X, + pointerType: 'touch', + }), + ); + li.dispatchEvent( + new MouseEvent('click', {...init, clientX: CHECK_MARK_X}), + ); + expect(readChecked(editor, 0)).toBe(false); + }); + + it('still toggles for a press and release on the check mark (control)', () => { + using editor = buildEditor(); + mouseGesture(getCheckListItem(0), CHECK_MARK_X, CHECK_MARK_X); + expect(readChecked(editor, 0)).toBe(true); + }); + + it('still toggles for a click with no press, eg. from a screen reader (control)', () => { + using editor = buildEditor(); + getCheckListItem(0).dispatchEvent( + new MouseEvent('click', { + bubbles: true, + cancelable: true, + clientX: CHECK_MARK_X, + clientY: 5, + }), + ); + expect(readChecked(editor, 0)).toBe(true); + }); + + it('still toggles on the next click after a right click on the content', () => { + using editor = buildEditor(); + const li = getCheckListItem(0); + mouseGesture(li, CONTENT_X, CONTENT_X, 2); + li.dispatchEvent( + new MouseEvent('click', { + bubbles: true, + cancelable: true, + clientX: CHECK_MARK_X, + clientY: 5, + }), + ); + expect(readChecked(editor, 0)).toBe(true); + }); + + it('does not carry over to a later check mark press', () => { + using editor = buildEditor(); + const li = getCheckListItem(0); + mouseGesture(li, CONTENT_X, CHECK_MARK_X); + mouseGesture(li, CHECK_MARK_X, CHECK_MARK_X); + expect(readChecked(editor, 0)).toBe(true); + }); + }); }); diff --git a/packages/lexical-list/src/checkList.ts b/packages/lexical-list/src/checkList.ts index 31a4d34f4197..102fe2e2c524 100644 --- a/packages/lexical-list/src/checkList.ts +++ b/packages/lexical-list/src/checkList.ts @@ -140,18 +140,26 @@ export function registerCheckList( // screen reader synthesizes on the focused item and which must not move // focus at all. let lastPress: 'checkMark' | 'content' | 'none' = 'none'; + // The press of the gesture that the last primary pointerup ended, kept for + // the click that follows it: the pointerup consumes lastPress first. A + // press on the item's content that is released over the check mark (eg. a + // text selection dragged onto it) must not toggle the item (#4861). + let releasedPress: 'checkMark' | 'content' | 'none' = 'none'; const configHandleSelectDefaults = ( event: PointerEvent | MouseEvent | TouchEvent, ) => { + releasedPress = 'none'; lastPress = handleSelectDefaults(event, peekDisableTakeFocusOnClick()) ? 'checkMark' : 'content'; }; const returnFocusAfterPress = () => { + const press = lastPress === 'none' ? releasedPress : lastPress; if (lastPress === 'content') { returnFocusToRoot(editor); } lastPress = 'none'; + return press; }; return mergeRegister( editor.registerCommand( @@ -282,12 +290,19 @@ export function registerCheckList( // pointerup also covers a gesture that never produces a click, // such as a right click opening the context menu. click: event => { - returnFocusAfterPress(); - configHandleClick(event); + const press = returnFocusAfterPress(); + releasedPress = 'none'; + if (press !== 'content') { + configHandleClick(event); + } }, pointerup: event => { - returnFocusAfterPress(); - configHandlePointerUp(event); + const press = returnFocusAfterPress(); + // A secondary button's release is followed by no click. + releasedPress = event.button === 0 ? press : 'none'; + if (press !== 'content') { + configHandlePointerUp(event); + } }, }), // Use capture so we run before other listeners that might move focus.