Skip to content

Commit

Permalink
Fix block toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Oct 18, 2024
1 parent c9b7d1d commit 811818b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 50 deletions.
33 changes: 12 additions & 21 deletions packages/block-editor/src/components/block-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export function PrivateBlockToolbar( {
const {
blockClientId,
blockClientIds,
isContentOnlyEditingMode,
isDefaultEditingMode,
blockType,
toolbarKey,
Expand Down Expand Up @@ -116,7 +115,6 @@ export function PrivateBlockToolbar( {
return {
blockClientId: selectedBlockClientId,
blockClientIds: selectedBlockClientIds,
isContentOnlyEditingMode: editingMode === 'contentOnly',
isDefaultEditingMode: _isDefaultEditingMode,
blockType: selectedBlockClientId && getBlockType( _blockName ),
shouldShowVisualToolbar: isValid && isVisual,
Expand Down Expand Up @@ -185,14 +183,11 @@ export function PrivateBlockToolbar( {
key={ toolbarKey }
>
<div ref={ toolbarWrapperRef } className={ innerClasses }>
{ showParentSelector &&
! isMultiToolbar &&
isLargeViewport &&
isDefaultEditingMode && <BlockParentSelector /> }
{ showParentSelector && ! isMultiToolbar && isLargeViewport && (
<BlockParentSelector />
) }
{ ( shouldShowVisualToolbar || isMultiToolbar ) &&
( isDefaultEditingMode ||
( isContentOnlyEditingMode && ! hasParentPattern ) ||
isSynced ) && (
! hasParentPattern && (
<div
ref={ nodeRef }
{ ...showHoveredOrFocusedGestures }
Expand All @@ -203,19 +198,15 @@ export function PrivateBlockToolbar( {
disabled={ ! isDefaultEditingMode }
isUsingBindings={ isUsingBindings }
/>
{ isDefaultEditingMode && (
<>
{ ! isMultiToolbar && (
<BlockLockToolbar
clientId={ blockClientId }
/>
) }
<BlockMover
clientIds={ blockClientIds }
hideDragHandle={ hideDragHandle }
/>
</>
{ ! isMultiToolbar && (
<BlockLockToolbar
clientId={ blockClientId }
/>
) }
<BlockMover
clientIds={ blockClientIds }
hideDragHandle={ hideDragHandle }
/>
</ToolbarGroup>
</div>
) }
Expand Down
11 changes: 10 additions & 1 deletion packages/block-editor/src/components/block-toolbar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,18 @@

> :last-child,
> :last-child .components-toolbar-group,
> :last-child .components-toolbar {
> :last-child .components-toolbar,
// If the last toolbar group is empty,
// we need to remove the double border from the penultimate one.
&:has(> :last-child:empty) > :nth-last-child(2),
&:has(> :last-child:empty) > :nth-last-child(2) .components-toolbar-group,
&:has(> :last-child:empty) > :nth-last-child(2) .components-toolbar {
border-right: none;
}

.components-toolbar-group:empty {
display: none;
}
}

.block-editor-block-contextual-toolbar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,31 @@ import { useHasAnyBlockControls } from '../block-controls/use-has-block-controls
* @return {boolean} Whether the block toolbar component will be rendered.
*/
export function useHasBlockToolbar() {
const { isToolbarEnabled, isDefaultEditingMode } = useSelect(
( select ) => {
const {
getBlockEditingMode,
getBlockName,
getBlockSelectionStart,
} = select( blockEditorStore );
const { isToolbarEnabled, isBlockDisabled } = useSelect( ( select ) => {
const { getBlockEditingMode, getBlockName, getBlockSelectionStart } =
select( blockEditorStore );

// we only care about the 1st selected block
// for the toolbar, so we use getBlockSelectionStart
// instead of getSelectedBlockClientIds
const selectedBlockClientId = getBlockSelectionStart();
// we only care about the 1st selected block
// for the toolbar, so we use getBlockSelectionStart
// instead of getSelectedBlockClientIds
const selectedBlockClientId = getBlockSelectionStart();

const blockType =
selectedBlockClientId &&
getBlockType( getBlockName( selectedBlockClientId ) );
const blockType =
selectedBlockClientId &&
getBlockType( getBlockName( selectedBlockClientId ) );

return {
isToolbarEnabled:
blockType &&
hasBlockSupport( blockType, '__experimentalToolbar', true ),
isDefaultEditingMode:
getBlockEditingMode( selectedBlockClientId ) === 'default',
};
},
[]
);
return {
isToolbarEnabled:
blockType &&
hasBlockSupport( blockType, '__experimentalToolbar', true ),
isBlockDisabled:
getBlockEditingMode( selectedBlockClientId ) === 'disabled',
};
}, [] );

const hasAnyBlockControls = useHasAnyBlockControls();

if (
! isToolbarEnabled ||
( ! isDefaultEditingMode && ! hasAnyBlockControls )
) {
if ( ! isToolbarEnabled || ( isBlockDisabled && ! hasAnyBlockControls ) ) {
return false;
}

Expand Down

0 comments on commit 811818b

Please sign in to comment.