Skip to content

feat(list): add disableTakeFocusOnClick option to CheckListPlugin (#7689) #7696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 17 additions & 5 deletions packages/lexical-list/src/checkList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@ export const INSERT_CHECK_LIST_COMMAND: LexicalCommand<void> = createCommand(
'INSERT_CHECK_LIST_COMMAND',
);

export function registerCheckList(editor: LexicalEditor) {
/**
* Registers the checklist plugin with the editor.
* @param editor The LexicalEditor instance.
* @param options Optional configuration.
* - disableTakeFocusOnClick: If true, clicking a checklist item will not focus the editor (useful for mobile).
*/
export function registerCheckList(
editor: LexicalEditor,
options?: { disableTakeFocusOnClick?: boolean }
) {
const disableTakeFocusOnClick = options?.disableTakeFocusOnClick ?? false;
return mergeRegister(
editor.registerCommand(
INSERT_CHECK_LIST_COMMAND,
Expand Down Expand Up @@ -144,12 +154,12 @@ export function registerCheckList(editor: LexicalEditor) {
),
editor.registerRootListener((rootElement, prevElement) => {
if (rootElement !== null) {
rootElement.addEventListener('click', handleClick);
rootElement.addEventListener('click', (event) => handleClick(event, disableTakeFocusOnClick));
rootElement.addEventListener('pointerdown', handlePointerDown);
}

if (prevElement !== null) {
prevElement.removeEventListener('click', handleClick);
prevElement.removeEventListener('click', (event) => handleClick(event, disableTakeFocusOnClick));
prevElement.removeEventListener('pointerdown', handlePointerDown);
}
}),
Expand Down Expand Up @@ -205,7 +215,7 @@ function handleCheckItemEvent(event: PointerEvent, callback: () => void) {
}
}

function handleClick(event: Event) {
function handleClick(event: Event, disableTakeFocusOnClick = false) {
handleCheckItemEvent(event as PointerEvent, () => {
if (isHTMLElement(event.target)) {
const domNode = event.target;
Expand All @@ -216,7 +226,9 @@ function handleClick(event: Event) {
const node = $getNearestNodeFromDOMNode(domNode);

if ($isListItemNode(node)) {
domNode.focus();
if (!disableTakeFocusOnClick) {
domNode.focus();
}
node.toggleChecked();
}
});
Expand Down
5 changes: 5 additions & 0 deletions packages/lexical-list/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
TextNode,
} from 'lexical';

import {INSERT_CHECK_LIST_COMMAND, registerCheckList} from './checkList';

Check failure on line 29 in packages/lexical-list/src/index.ts

View workflow job for this annotation

GitHub Actions / core-tests / integrity (20.11.0)

Import declaration conflicts with local declaration of 'registerCheckList'.
import {
$handleListInsertParagraph,
$insertList,
Expand Down Expand Up @@ -55,7 +55,7 @@
ListNode,
ListNodeTagType,
ListType,
registerCheckList,

Check failure on line 58 in packages/lexical-list/src/index.ts

View workflow job for this annotation

GitHub Actions / core-tests / integrity (20.11.0)

Export declaration conflicts with exported declaration of 'registerCheckList'.
SerializedListItemNode,
SerializedListNode,
};
Expand Down Expand Up @@ -278,3 +278,8 @@
export function removeList(editor: LexicalEditor): void {
editor.update(() => $removeList());
}

export function registerCheckList(

Check failure on line 282 in packages/lexical-list/src/index.ts

View workflow job for this annotation

GitHub Actions / core-tests / integrity (20.11.0)

Function implementation is missing or not immediately following the declaration.
editor: LexicalEditor,
options?: { disableTakeFocusOnClick?: boolean }
): () => void;
Comment on lines +281 to +285
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be here

Suggested change
export function registerCheckList(
editor: LexicalEditor,
options?: { disableTakeFocusOnClick?: boolean }
): () => void;

6 changes: 3 additions & 3 deletions packages/lexical-react/src/LexicalCheckListPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import {registerCheckList} from '@lexical/list';
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {useEffect} from 'react';

export function CheckListPlugin(): null {
export function CheckListPlugin({ disableTakeFocusOnClick = false }: { disableTakeFocusOnClick?: boolean } = {}): null {
const [editor] = useLexicalComposerContext();

useEffect(() => {
return registerCheckList(editor);
}, [editor]);
return registerCheckList(editor, { disableTakeFocusOnClick });
}, [editor, disableTakeFocusOnClick]);
return null;
}
Loading