-
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.
Add navigator for nav block (#17265)
* Add icon for block navigator * Extract block navigation list into its own file and export from block-editor package root * Refactor navigator into own component and hook up to store * Switch to using a hook to implement the block navigator toolbar * Return elements from hook instead of components to avoid redeclaration of components
- Loading branch information
Showing
6 changed files
with
165 additions
and
53 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
55 changes: 3 additions & 52 deletions
55
packages/block-editor/src/components/block-navigation/index.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
64 changes: 64 additions & 0 deletions
64
packages/block-editor/src/components/block-navigation/list.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,64 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { map } from 'lodash'; | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Button } from '@wordpress/components'; | ||
import { getBlockType } from '@wordpress/blocks'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import BlockIcon from '../block-icon'; | ||
|
||
export default function BlockNavigationList( { | ||
blocks, | ||
selectedBlockClientId, | ||
selectBlock, | ||
showNestedBlocks, | ||
} ) { | ||
return ( | ||
/* | ||
* Disable reason: The `list` ARIA role is redundant but | ||
* Safari+VoiceOver won't announce the list otherwise. | ||
*/ | ||
/* eslint-disable jsx-a11y/no-redundant-roles */ | ||
<ul className="editor-block-navigation__list block-editor-block-navigation__list" role="list"> | ||
{ map( blocks, ( block ) => { | ||
const blockType = getBlockType( block.name ); | ||
const isSelected = block.clientId === selectedBlockClientId; | ||
|
||
return ( | ||
<li key={ block.clientId }> | ||
<div className="editor-block-navigation__item block-editor-block-navigation__item"> | ||
<Button | ||
className={ classnames( 'editor-block-navigation__item-button block-editor-block-navigation__item-button', { | ||
'is-selected': isSelected, | ||
} ) } | ||
onClick={ () => selectBlock( block.clientId ) } | ||
> | ||
<BlockIcon icon={ blockType.icon } showColors /> | ||
{ blockType.title } | ||
{ isSelected && <span className="screen-reader-text">{ __( '(selected block)' ) }</span> } | ||
</Button> | ||
</div> | ||
{ showNestedBlocks && !! block.innerBlocks && !! block.innerBlocks.length && ( | ||
<BlockNavigationList | ||
blocks={ block.innerBlocks } | ||
selectedBlockClientId={ selectedBlockClientId } | ||
selectBlock={ selectBlock } | ||
showNestedBlocks | ||
/> | ||
) } | ||
</li> | ||
); | ||
} ) } | ||
</ul> | ||
/* eslint-enable jsx-a11y/no-redundant-roles */ | ||
); | ||
} |
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
80 changes: 80 additions & 0 deletions
80
packages/block-library/src/navigation-menu/use-block-navigator.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,80 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
useState, | ||
} from '@wordpress/element'; | ||
import { | ||
useSelect, | ||
useDispatch, | ||
} from '@wordpress/data'; | ||
import { | ||
BlockNavigationList, | ||
} from '@wordpress/block-editor'; | ||
import { | ||
IconButton, | ||
SVG, | ||
Path, | ||
Modal, | ||
} from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
const NavigatorIcon = ( | ||
<SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20"> | ||
<Path d="M5 5H3v2h2V5zm3 8h11v-2H8v2zm9-8H6v2h11V5zM7 11H5v2h2v-2zm0 8h2v-2H7v2zm3-2v2h11v-2H10z" /> | ||
</SVG> | ||
); | ||
|
||
export default function useBlockNavigator( clientId ) { | ||
const [ isNavigationListOpen, setIsNavigationListOpen ] = useState( false ); | ||
|
||
const { | ||
block, | ||
selectedBlockClientId, | ||
} = useSelect( ( select ) => { | ||
const { | ||
getSelectedBlockClientId, | ||
getBlock, | ||
} = select( 'core/block-editor' ); | ||
|
||
return { | ||
block: getBlock( clientId ), | ||
selectedBlockClientId: getSelectedBlockClientId(), | ||
}; | ||
}, [ clientId ] ); | ||
|
||
const { | ||
selectBlock, | ||
} = useDispatch( 'core/block-editor' ); | ||
|
||
const navigatorToolbarButton = ( | ||
<IconButton | ||
className="components-toolbar__control" | ||
label={ __( 'Open block navigator' ) } | ||
onClick={ () => setIsNavigationListOpen( true ) } | ||
icon={ NavigatorIcon } | ||
/> | ||
); | ||
|
||
const navigatorModal = isNavigationListOpen && ( | ||
<Modal | ||
title={ __( 'Block Navigator' ) } | ||
closeLabel={ __( 'Close' ) } | ||
onRequestClose={ () => { | ||
setIsNavigationListOpen( false ); | ||
} } | ||
> | ||
<BlockNavigationList | ||
blocks={ [ block ] } | ||
selectedBlockClientId={ selectedBlockClientId } | ||
selectBlock={ selectBlock } | ||
showNestedBlocks | ||
/> | ||
</Modal> | ||
); | ||
|
||
return { | ||
navigatorToolbarButton, | ||
navigatorModal, | ||
}; | ||
} |