Skip to content

Commit 334f940

Browse files
authored
fix(richtext-lexical): unnecessary isEnabled computations on toolbar items (#8176)
Fixes #8170 - wrapped onActiveChange in useCallback - removed an unnecessary mouseUp event Note: I put the console.log for debugging in the useCallback called updateStates inside packages/richtext-lexical/src/features/toolbars/shared/ToolbarDropdown/index.tsx. ## Before https://github.com/user-attachments/assets/07d715d4-f6c7-4a4a-91ab-5de418c909d6 ## After https://github.com/user-attachments/assets/2d404d1c-d1a7-46fd-a5b6-7d01c5c16959
1 parent dd5a9ac commit 334f940

File tree

3 files changed

+40
-41
lines changed
  • packages/richtext-lexical/src/features/toolbars

3 files changed

+40
-41
lines changed

packages/richtext-lexical/src/features/toolbars/fixed/client/Toolbar/index.tsx

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,31 +69,34 @@ function ToolbarGroupComponent({
6969
}
7070
}, [group])
7171

72-
const onActiveChange = ({ activeItems }: { activeItems: ToolbarGroupItem[] }) => {
73-
if (!activeItems.length) {
74-
if (group?.type === 'dropdown' && group.items.length && group.ChildComponent) {
75-
setDropdownIcon(() => group.ChildComponent)
76-
setDropdownLabel(null)
77-
} else {
78-
setDropdownIcon(null)
79-
setDropdownLabel(null)
72+
const onActiveChange = React.useCallback(
73+
({ activeItems }: { activeItems: ToolbarGroupItem[] }) => {
74+
if (!activeItems.length) {
75+
if (group?.type === 'dropdown' && group.items.length && group.ChildComponent) {
76+
setDropdownIcon(() => group.ChildComponent)
77+
setDropdownLabel(null)
78+
} else {
79+
setDropdownIcon(null)
80+
setDropdownLabel(null)
81+
}
82+
return
8083
}
81-
return
82-
}
83-
const item = activeItems[0]
84+
const item = activeItems[0]
8485

85-
let label = item.key
86-
if (item.label) {
87-
label =
88-
typeof item.label === 'function' ? item.label({ i18n, richTextComponentMap }) : item.label
89-
}
90-
// Crop title to max. 25 characters
91-
if (label.length > 25) {
92-
label = label.substring(0, 25) + '...'
93-
}
94-
setDropdownLabel(label)
95-
setDropdownIcon(() => item.ChildComponent)
96-
}
86+
let label = item.key
87+
if (item.label) {
88+
label =
89+
typeof item.label === 'function' ? item.label({ i18n, richTextComponentMap }) : item.label
90+
}
91+
// Crop title to max. 25 characters
92+
if (label.length > 25) {
93+
label = label.substring(0, 25) + '...'
94+
}
95+
setDropdownLabel(label)
96+
setDropdownIcon(() => item.ChildComponent)
97+
},
98+
[group, i18n, richTextComponentMap],
99+
)
97100

98101
return (
99102
<div className={`fixed-toolbar__group fixed-toolbar__group-${group.key}`} key={group.key}>

packages/richtext-lexical/src/features/toolbars/inline/client/Toolbar/index.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,21 @@ function ToolbarGroupComponent({
7171
}
7272
}, [group])
7373

74-
const onActiveChange = ({ activeItems }: { activeItems: ToolbarGroupItem[] }) => {
75-
if (!activeItems.length) {
76-
if (group?.type === 'dropdown' && group.items.length && group.ChildComponent) {
77-
setDropdownIcon(() => group.ChildComponent)
78-
} else {
79-
setDropdownIcon(null)
74+
const onActiveChange = useCallback(
75+
({ activeItems }: { activeItems: ToolbarGroupItem[] }) => {
76+
if (!activeItems.length) {
77+
if (group?.type === 'dropdown' && group.items.length && group.ChildComponent) {
78+
setDropdownIcon(() => group.ChildComponent)
79+
} else {
80+
setDropdownIcon(null)
81+
}
82+
return
8083
}
81-
return
82-
}
83-
const item = activeItems[0]
84-
setDropdownIcon(() => item.ChildComponent)
85-
}
84+
const item = activeItems[0]
85+
setDropdownIcon(() => item.ChildComponent)
86+
},
87+
[group],
88+
)
8689

8790
return (
8891
<div

packages/richtext-lexical/src/features/toolbars/shared/ToolbarDropdown/index.tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,6 @@ export const ToolbarDropdown = ({
135135
updateStates()
136136
}, [updateStates])
137137

138-
useEffect(() => {
139-
document.addEventListener('mouseup', updateStates)
140-
return () => {
141-
document.removeEventListener('mouseup', updateStates)
142-
}
143-
}, [updateStates])
144-
145138
useEffect(() => {
146139
return mergeRegister(
147140
editor.registerUpdateListener(() => {

0 commit comments

Comments
 (0)