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

BlockSwitcher: Refactor to use Button layout properly #67502

Merged
merged 1 commit into from
Dec 3, 2024
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
40 changes: 19 additions & 21 deletions packages/block-editor/src/components/block-switcher/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from '@wordpress/blocks';
import { useSelect, useDispatch } from '@wordpress/data';
import { copy } from '@wordpress/icons';
import { store as preferencesStore } from '@wordpress/preferences';

/**
* Internal dependencies
Expand Down Expand Up @@ -185,21 +186,6 @@ function BlockSwitcherDropdownMenuContents( {
);
}

const BlockIndicator = ( { icon, showTitle, blockTitle } ) => (
<>
<BlockIcon
className="block-editor-block-switcher__toggle"
icon={ icon }
showColors
/>
{ showTitle && blockTitle && (
<span className="block-editor-block-switcher__toggle-text">
{ blockTitle }
</span>
) }
</>
);

export const BlockSwitcher = ( { clientIds } ) => {
const {
hasContentOnlyLocking,
Expand Down Expand Up @@ -272,6 +258,11 @@ export const BlockSwitcher = ( { clientIds } ) => {
clientId: clientIds?.[ 0 ],
maximumLength: 35,
} );
const showIconLabels = useSelect(
( select ) =>
select( preferencesStore ).get( 'core', 'showIconLabels' ),
[]
);

if ( invalidBlocks ) {
return null;
Expand All @@ -282,6 +273,11 @@ export const BlockSwitcher = ( { clientIds } ) => {
? blockTitle
: __( 'Multiple blocks selected' );

const blockIndicatorText =
( isReusable || isTemplate ) && ! showIconLabels && blockTitle
? blockTitle
: undefined;

const hideDropdown =
isDisabled ||
( ! hasBlockStyles && ! canRemove ) ||
Expand All @@ -295,12 +291,13 @@ export const BlockSwitcher = ( { clientIds } ) => {
className="block-editor-block-switcher__no-switcher-icon"
title={ blockSwitcherLabel }
Copy link
Member Author

Choose a reason for hiding this comment

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

This existing title implementation can show redundant tooltips not only in showIconLabels mode, but also for patterns. I will address this separately.

icon={
<BlockIndicator
<BlockIcon
className="block-editor-block-switcher__toggle"
icon={ icon }
showTitle={ isReusable || isTemplate }
blockTitle={ blockTitle }
showColors
/>
}
text={ blockIndicatorText }
/>
</ToolbarGroup>
);
Expand Down Expand Up @@ -329,12 +326,13 @@ export const BlockSwitcher = ( { clientIds } ) => {
className: 'block-editor-block-switcher__popover',
} }
icon={
<BlockIndicator
<BlockIcon
className="block-editor-block-switcher__toggle"
icon={ icon }
showTitle={ isReusable || isTemplate }
blockTitle={ blockTitle }
showColors
/>
}
text={ blockIndicatorText }
toggleProps={ {
description: blockSwitcherDescription,
...toggleProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@
}
}

.block-editor-block-switcher__toggle-text {
margin-left: $grid-unit-10;
Copy link
Member Author

Choose a reason for hiding this comment

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

Unnecessary due to built-in Button styles.


// Account for double label when show-text-buttons is set.
.show-icon-labels & {
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the cleanup!

display: none;
}
Comment on lines -33 to -35
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved to JS logic (see showIconLabels).

}

Copy link
Member Author

Choose a reason for hiding this comment

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

It looks like there are more custom styles that can be removed, but out of scope for this PR.

.components-button.block-editor-block-switcher__no-switcher-icon {
display: flex;

Expand Down
Loading