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

Replace block variation buttons with ToggleGroupControl #45654

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
DropdownMenu,
MenuGroup,
MenuItemsChoice,
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOptionIcon as ToggleGroupControlOptionIcon,
VisuallyHidden,
} from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
Expand Down Expand Up @@ -95,6 +97,47 @@ function VariationsDropdown( {
);
}

function VariationsToggleGroupControl( {
className,
onSelectVariation,
selectedValue,
variations,
} ) {
return (
<fieldset className={ className }>
Copy link
Member

Choose a reason for hiding this comment

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

Is this fieldset necessary? The ToggleGroupControl alone is semantically grouped and labeled. Maybe swap with a div if it's just for the wrapper classes?

<ToggleGroupControl
label={ __( 'Transform to variation' ) }
value={ selectedValue }
isBlock
hideLabelFromVision
showTooltip
onChange={ ( variation ) => {
onSelectVariation( variation );
} }
size="__unstable-large"
>
{ variations.map( ( variation ) => (
<ToggleGroupControlOptionIcon
key={ variation.name }
icon={ variation.icon }
value={ variation.name }
aria-label={ variation.title }
label={
selectedValue === variation.name
? variation.title
: sprintf(
/* translators: %s: Name of the block variation */
__( 'Transform to %s' ),
variation.title
)
}
/>
) ) }
</ToggleGroupControl>
</fieldset>
);
}

function __experimentalBlockVariationTransforms( { blockClientId } ) {
const { updateBlockAttributes } = useDispatch( blockEditorStore );
const { activeBlockVariation, variations } = useSelect(
Expand Down Expand Up @@ -138,16 +181,25 @@ function __experimentalBlockVariationTransforms( { blockClientId } ) {
} );
};

const baseClass = 'block-editor-block-variation-transforms';

// Skip rendering if there are no variations
if ( ! variations?.length ) return null;

const Component = hasUniqueIcons ? VariationsButtons : VariationsDropdown;
// Show buttons if there are more than 5 variations because the ToggleGroupControl does not wrap
const showButtons = variations.length > 5;

const ButtonComponent = showButtons
? VariationsButtons
: VariationsToggleGroupControl;

const className = showButtons
? 'block-editor-block-variation-transforms buttons'
: 'block-editor-block-variation-transforms toggle-group-control';

const Component = hasUniqueIcons ? ButtonComponent : VariationsDropdown;

return (
<Component
className={ baseClass }
className={ className }
onSelectVariation={ onSelectVariation }
selectedValue={ selectedValue }
variations={ variations }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.block-editor-block-variation-transforms {
padding: 0 $grid-unit-20 $grid-unit-20 52px;
width: 100%;

.components-dropdown-menu__toggle {
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the follow-ups!

Are these style removals intentional? Most of these appear to affect the dropdown list version of the list of variations, which is used when not all of the variations have unique icons.

To test this, with the changes applied to scope the social icons for transform, in packages/block-library/src/social-link/variations.js also set any of the social icon blocks to use the same icon as one of the other variations. E.g. set any of them to use the YouTubeIcon icon.

Here's a quick view of trunk versus this PR:

Trunk This PR
image image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, I need more coffee. I forgot to push the changes 😄 Thanks for your patience!

Copy link
Contributor

Choose a reason for hiding this comment

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

Oops, I need more coffee.

No worries, I have the opposite problem of having drunk too much coffee!

I've re-pulled the changes, and it looks like there are still some differences in the dropdown popover for the transform to variation list. Visually it looks like there's now additional padding / a larger width of the popover, and it overflows the area so there's a horizontal scrollbar:

Trunk This PR
image image

border: 1px solid $gray-700;
Expand Down Expand Up @@ -31,8 +30,12 @@
top: 0;
}
}
}

.block-editor-block-variation-transforms__popover .components-popover__content {
min-width: 230px;
andrewserong marked this conversation as resolved.
Show resolved Hide resolved
&.buttons {
width: 100%;
}

&.toggle-group-control {
display: flex;
}
andrewserong marked this conversation as resolved.
Show resolved Hide resolved
}