UNPKG

@wordpress/block-editor

Version:
64 lines (57 loc) 1.72 kB
/** * External dependencies */ import classnames from 'classnames'; /** * WordPress dependencies */ import { __ } from '@wordpress/i18n'; import { hasBlockSupport, store as blocksStore } from '@wordpress/blocks'; import { useSelect } from '@wordpress/data'; /** * Internal dependencies */ import NavigableToolbar from '../navigable-toolbar'; import { BlockToolbar } from '../'; import { store as blockEditorStore } from '../../store'; function BlockContextualToolbar( { focusOnMount, ...props } ) { const { blockType, hasParents } = useSelect( ( select ) => { const { getBlockName, getBlockParents, getSelectedBlockClientIds, } = select( blockEditorStore ); const { getBlockType } = select( blocksStore ); const selectedBlockClientIds = getSelectedBlockClientIds(); const selectedBlockClientId = selectedBlockClientIds[ 0 ]; return { blockType: selectedBlockClientId && getBlockType( getBlockName( selectedBlockClientId ) ), hasParents: getBlockParents( selectedBlockClientId ).length, }; }, [] ); if ( blockType ) { if ( ! hasBlockSupport( blockType, '__experimentalToolbar', true ) ) { return null; } } // Shifts the toolbar to make room for the parent block selector. const classes = classnames( 'block-editor-block-contextual-toolbar', { 'has-parent': hasParents, } ); return ( <div className="block-editor-block-contextual-toolbar-wrapper"> <NavigableToolbar focusOnMount={ focusOnMount } className={ classes } /* translators: accessibility text for the block toolbar */ aria-label={ __( 'Block tools' ) } { ...props } > <BlockToolbar /> </NavigableToolbar> </div> ); } export default BlockContextualToolbar;