Skip to content
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

Fix missing Replace button in content-locked Image blocks #53410

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* WordPress dependencies
*/
import { __experimentalUseSlotFills as useSlotFills } from '@wordpress/components';
import warning from '@wordpress/warning';

/**
* Internal dependencies
*/
import groups from './groups';

export function useHasAnyBlockControls() {
let hasAnyBlockControls = false;
for ( const group in groups ) {
// It is safe to violate the rules of hooks here as the `groups` object
// is static and will not change length between renders. Do not return
// early as that will cause the hook to be called a different number of
// times between renders.
// eslint-disable-next-line react-hooks/rules-of-hooks
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this too clever? We can avoid this by duplicating the group names here. The con of doing that is that someone might add a new group and forget to update both files.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine!

if ( useHasBlockControls( group ) ) {
hasAnyBlockControls = true;
}
}
return hasAnyBlockControls;
}

export function useHasBlockControls( group = 'default' ) {
const Slot = groups[ group ]?.Slot;
const fills = useSlotFills( Slot?.__unstableName );
if ( ! Slot ) {
warning( `Unknown BlockControls group "${ group }" provided.` );
return null;
}
return !! fills?.length;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import NavigableToolbar from '../navigable-toolbar';
import BlockToolbar from '../block-toolbar';
import { store as blockEditorStore } from '../../store';
import { unlock } from '../../lock-unlock';
import { useHasAnyBlockControls } from '../block-controls/use-has-block-controls';

function BlockContextualToolbar( { focusOnMount, isFixed, ...props } ) {
// When the toolbar is fixed it can be collapsed
Expand All @@ -34,10 +35,10 @@ function BlockContextualToolbar( { focusOnMount, isFixed, ...props } ) {
const isLargeViewport = useViewportMatch( 'medium' );
const {
blockType,
blockEditingMode,
hasParents,
showParentSelector,
selectedBlockClientId,
isContentOnly,
} = useSelect( ( select ) => {
const {
getBlockName,
Expand All @@ -58,9 +59,8 @@ function BlockContextualToolbar( { focusOnMount, isFixed, ...props } ) {
blockType:
_selectedBlockClientId &&
getBlockType( getBlockName( _selectedBlockClientId ) ),
blockEditingMode: getBlockEditingMode( _selectedBlockClientId ),
hasParents: parents.length,
isContentOnly:
getBlockEditingMode( _selectedBlockClientId ) === 'contentOnly',
showParentSelector:
parentBlockType &&
getBlockEditingMode( firstParentClientId ) === 'default' &&
Expand All @@ -78,10 +78,13 @@ function BlockContextualToolbar( { focusOnMount, isFixed, ...props } ) {
setIsCollapsed( false );
}, [ selectedBlockClientId ] );

const isToolbarEnabled =
! blockType ||
hasBlockSupport( blockType, '__experimentalToolbar', true );
const hasAnyBlockControls = useHasAnyBlockControls();
if (
isContentOnly ||
( blockType &&
! hasBlockSupport( blockType, '__experimentalToolbar', true ) )
! isToolbarEnabled ||
( blockEditingMode !== 'default' && ! hasAnyBlockControls )
) {
return null;
}
Expand Down
4 changes: 0 additions & 4 deletions packages/block-editor/src/components/block-tools/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@
}
}

&:has(.block-editor-block-toolbar:empty) {
display: none;
}

// Add a scrim to the right of the collapsed button.
&.is-collapsed::after {
content: "";
Expand Down