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 @@ -6,8 +6,11 @@ import { __, sprintf } from '@wordpress/i18n';
import {
Button,
DropdownMenu,
Flex,
MenuGroup,
MenuItemsChoice,
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOptionIcon as ToggleGroupControlOptionIcon,
VisuallyHidden,
} from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
Expand Down Expand Up @@ -95,6 +98,49 @@ 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?

<Flex>
<ToggleGroupControl
label={ __( 'Transform to variation' ) }
value={ selectedValue }
isBlock
Copy link
Member

Choose a reason for hiding this comment

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

Regarding @jasmussen point about the border: The isBlock prop is what controls the border visibility for the moment. It looks like the Flex component is being used here to undo what the isBlock prop is for anyway, so I think all we need to do is remove both the Flex and isBlock prop 🙂

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>
</Flex>
</fieldset>
);
}

function __experimentalBlockVariationTransforms( { blockClientId } ) {
const { updateBlockAttributes } = useDispatch( blockEditorStore );
const { activeBlockVariation, variations } = useSelect(
Expand Down Expand Up @@ -138,12 +184,19 @@ 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;
const baseClass = 'block-editor-block-variation-transforms';

// 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 Component = hasUniqueIcons ? ButtonComponent : VariationsDropdown;

return (
<Component
Expand Down