-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix navigation editor missing appender not showing appender when no b…
…locks selected (#34678) * Support custom appender in the nav block * Implement the toggle style appender in the nav editor * Update rendering logic for appender * Fix comment * Add e2e test
- Loading branch information
Showing
7 changed files
with
129 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
packages/edit-navigation/src/filters/add-navigation-editor-custom-appender.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { addFilter } from '@wordpress/hooks'; | ||
import { createHigherOrderComponent } from '@wordpress/compose'; | ||
import { | ||
InnerBlocks, | ||
store as blockEditorStore, | ||
} from '@wordpress/block-editor'; | ||
|
||
function CustomAppender() { | ||
return <InnerBlocks.ButtonBlockAppender isToggle />; | ||
} | ||
|
||
function EnhancedNavigationBlock( { blockEdit: BlockEdit, ...props } ) { | ||
const clientId = props.clientId; | ||
const { | ||
noBlockSelected, | ||
isSelected, | ||
isImmediateParentOfSelectedBlock, | ||
selectedBlockHasDescendants, | ||
} = useSelect( | ||
( select ) => { | ||
const { | ||
getClientIdsOfDescendants, | ||
hasSelectedInnerBlock, | ||
getSelectedBlockClientId, | ||
} = select( blockEditorStore ); | ||
const _isImmediateParentOfSelectedBlock = hasSelectedInnerBlock( | ||
clientId, | ||
false | ||
); | ||
const selectedBlockId = getSelectedBlockClientId(); | ||
const _selectedBlockHasDescendants = !! getClientIdsOfDescendants( [ | ||
selectedBlockId, | ||
] )?.length; | ||
|
||
return { | ||
isSelected: selectedBlockId === clientId, | ||
noBlockSelected: ! selectedBlockId, | ||
isImmediateParentOfSelectedBlock: _isImmediateParentOfSelectedBlock, | ||
selectedBlockHasDescendants: _selectedBlockHasDescendants, | ||
}; | ||
}, | ||
[ clientId ] | ||
); | ||
|
||
const customAppender = | ||
noBlockSelected || | ||
isSelected || | ||
( isImmediateParentOfSelectedBlock && ! selectedBlockHasDescendants ) | ||
? CustomAppender | ||
: false; | ||
|
||
return <BlockEdit { ...props } customAppender={ customAppender } />; | ||
} | ||
|
||
const addNavigationEditorCustomAppender = createHigherOrderComponent( | ||
( BlockEdit ) => ( props ) => { | ||
if ( props.name !== 'core/navigation' ) { | ||
return <BlockEdit { ...props } />; | ||
} | ||
|
||
// Use a separate component so that `useSelect` only run on the navigation block. | ||
return <EnhancedNavigationBlock blockEdit={ BlockEdit } { ...props } />; | ||
}, | ||
'withNavigationEditorCustomAppender' | ||
); | ||
|
||
export default () => | ||
addFilter( | ||
'editor.BlockEdit', | ||
'core/edit-navigation/with-navigation-editor-custom-appender', | ||
addNavigationEditorCustomAppender | ||
); |
5 changes: 3 additions & 2 deletions
5
packages/edit-navigation/src/filters/add-navigation-editor-placeholder.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters