-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Block Editor: Don't memoize callbacks in 'BlockSettingsDropdown' #59397
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -9,7 +9,7 @@ import { | |||
import { DropdownMenu, MenuGroup, MenuItem } from '@wordpress/components'; | ||||
import { useDispatch, useSelect } from '@wordpress/data'; | ||||
import { moreVertical } from '@wordpress/icons'; | ||||
import { Children, cloneElement, useCallback } from '@wordpress/element'; | ||||
import { Children, cloneElement } from '@wordpress/element'; | ||||
import { __ } from '@wordpress/i18n'; | ||||
import { | ||||
store as keyboardShortcutsStore, | ||||
|
@@ -127,41 +127,35 @@ export function BlockSettingsDropdown( { | |||
const isMatch = __unstableUseShortcutEventMatch(); | ||||
const hasSelectedBlocks = selectedBlockClientIds.length > 0; | ||||
|
||||
const updateSelectionAfterDuplicate = useCallback( | ||||
async ( clientIdsPromise ) => { | ||||
if ( __experimentalSelectBlock ) { | ||||
const ids = await clientIdsPromise; | ||||
if ( ids && ids[ 0 ] ) { | ||||
__experimentalSelectBlock( ids[ 0 ], false ); | ||||
} | ||||
} | ||||
}, | ||||
[ __experimentalSelectBlock ] | ||||
); | ||||
async function updateSelectionAfterDuplicate( clientIdsPromise ) { | ||||
if ( ! __experimentalSelectBlock ) { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for reducing the nesting level here 👍 |
||||
return; | ||||
} | ||||
|
||||
const updateSelectionAfterRemove = useCallback( () => { | ||||
if ( __experimentalSelectBlock ) { | ||||
let blockToFocus = previousBlockClientId || firstParentClientId; | ||||
const ids = await clientIdsPromise; | ||||
if ( ids && ids[ 0 ] ) { | ||||
__experimentalSelectBlock( ids[ 0 ], false ); | ||||
} | ||||
} | ||||
|
||||
// Focus the first block if there's no previous block nor parent block. | ||||
if ( ! blockToFocus ) { | ||||
blockToFocus = getBlockOrder()[ 0 ]; | ||||
} | ||||
function updateSelectionAfterRemove() { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only used inline; good to be un-memoized 👍 |
||||
if ( ! __experimentalSelectBlock ) { | ||||
return; | ||||
} | ||||
|
||||
// Only update the selection if the original selection is removed. | ||||
const shouldUpdateSelection = | ||||
hasSelectedBlocks && getSelectedBlockClientIds().length === 0; | ||||
let blockToFocus = previousBlockClientId || firstParentClientId; | ||||
|
||||
__experimentalSelectBlock( blockToFocus, shouldUpdateSelection ); | ||||
// Focus the first block if there's no previous block nor parent block. | ||||
if ( ! blockToFocus ) { | ||||
blockToFocus = getBlockOrder()[ 0 ]; | ||||
} | ||||
}, [ | ||||
__experimentalSelectBlock, | ||||
previousBlockClientId, | ||||
firstParentClientId, | ||||
getBlockOrder, | ||||
hasSelectedBlocks, | ||||
getSelectedBlockClientIds, | ||||
] ); | ||||
|
||||
// Only update the selection if the original selection is removed. | ||||
const shouldUpdateSelection = | ||||
hasSelectedBlocks && getSelectedBlockClientIds().length === 0; | ||||
|
||||
__experimentalSelectBlock( blockToFocus, shouldUpdateSelection ); | ||||
} | ||||
|
||||
// This can occur when the selected block (the parent) | ||||
// displays child blocks within a List View. | ||||
|
@@ -179,20 +173,17 @@ export function BlockSettingsDropdown( { | |||
? undefined | ||||
: openedBlockSettingsMenu === currentClientId || false; | ||||
|
||||
const onToggle = useCallback( | ||||
( localOpen ) => { | ||||
if ( localOpen && openedBlockSettingsMenu !== currentClientId ) { | ||||
setOpenedBlockSettingsMenu( currentClientId ); | ||||
} else if ( | ||||
! localOpen && | ||||
openedBlockSettingsMenu && | ||||
openedBlockSettingsMenu === currentClientId | ||||
) { | ||||
setOpenedBlockSettingsMenu( undefined ); | ||||
} | ||||
}, | ||||
[ currentClientId, openedBlockSettingsMenu, setOpenedBlockSettingsMenu ] | ||||
); | ||||
function onToggle( localOpen ) { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this is passed down, it's passed down to
which is then used for the initial value of Good to be un-memoized as well 👍 |
||||
if ( localOpen && openedBlockSettingsMenu !== currentClientId ) { | ||||
setOpenedBlockSettingsMenu( currentClientId ); | ||||
} else if ( | ||||
! localOpen && | ||||
openedBlockSettingsMenu && | ||||
openedBlockSettingsMenu === currentClientId | ||||
) { | ||||
setOpenedBlockSettingsMenu( undefined ); | ||||
} | ||||
} | ||||
|
||||
return ( | ||||
<BlockActions | ||||
|
@@ -233,7 +224,8 @@ export function BlockSettingsDropdown( { | |||
canRemove | ||||
) { | ||||
event.preventDefault(); | ||||
updateSelectionAfterRemove( onRemove() ); | ||||
onRemove(); | ||||
updateSelectionAfterRemove(); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||
} else if ( | ||||
isMatch( | ||||
'core/block-editor/duplicate', | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only used inline; good to be un-memoized 👍