Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions packages/lexical-list/src/__tests__/unit/checkList.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
23 changes: 19 additions & 4 deletions packages/lexical-list/src/checkList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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.
Expand Down
Loading