Skip to content
Closed
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
Expand Up @@ -4,97 +4,18 @@
import { store as blocksStore } from '@wordpress/blocks';
import { __, sprintf } from '@wordpress/i18n';
import {
Button,
DropdownMenu,
MenuGroup,
MenuItemsChoice,
VisuallyHidden,
__experimentalToggleGroupControl as ToggleGroupControl,
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
__experimentalToggleGroupControlOptionIcon as ToggleGroupControlOptionIcon,
} from '@wordpress/components';
import { useSelect, useDispatch } from '@wordpress/data';
import { useMemo } from '@wordpress/element';
import { chevronDown } from '@wordpress/icons';

/**
* Internal dependencies
*/
import BlockIcon from '../block-icon';
import { store as blockEditorStore } from '../../store';

function VariationsButtons( {
className,
onSelectVariation,
selectedValue,
variations,
} ) {
return (
<fieldset className={ className }>
<VisuallyHidden as="legend">
{ __( 'Transform to variation' ) }
</VisuallyHidden>
{ variations.map( ( variation ) => (
<Button
key={ variation.name }
icon={ <BlockIcon icon={ variation.icon } showColors /> }
isPressed={ selectedValue === variation.name }
label={
selectedValue === variation.name
? variation.title
: sprintf(
/* translators: %s: Name of the block variation */
__( 'Transform to %s' ),
variation.title
)
}
onClick={ () => onSelectVariation( variation.name ) }
aria-label={ variation.title }
showTooltip
/>
) ) }
</fieldset>
);
}

function VariationsDropdown( {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

One question/concern I have is around whether we want to keep the existing UI for cases where we don't have unique icons. I would be happy to reduce the scope of the PR and reinstate the VariationsDropdown logic if we decide that would be a better UX.

className,
onSelectVariation,
selectedValue,
variations,
} ) {
const selectOptions = variations.map(
( { name, title, description } ) => ( {
value: name,
label: title,
info: description,
} )
);

return (
<DropdownMenu
className={ className }
label={ __( 'Transform to variation' ) }
text={ __( 'Transform to variation' ) }
popoverProps={ {
position: 'bottom center',
className: `${ className }__popover`,
} }
icon={ chevronDown }
toggleProps={ { iconPosition: 'right' } }
>
{ () => (
<div className={ `${ className }__container` }>
<MenuGroup>
<MenuItemsChoice
choices={ selectOptions }
value={ selectedValue }
onSelect={ onSelectVariation }
/>
</MenuGroup>
</div>
) }
</DropdownMenu>
);
}

function __experimentalBlockVariationTransforms( { blockClientId } ) {
const { updateBlockAttributes } = useDispatch( blockEditorStore );
const { activeBlockVariation, variations } = useSelect(
Expand All @@ -115,7 +36,7 @@ function __experimentalBlockVariationTransforms( { blockClientId } ) {
[ blockClientId ]
);

const selectedValue = activeBlockVariation?.name;
const selectedVariationName = activeBlockVariation?.name;

// Check if each variation has a unique icon.
const hasUniqueIcons = useMemo( () => {
Expand Down Expand Up @@ -143,15 +64,45 @@ function __experimentalBlockVariationTransforms( { blockClientId } ) {
// Skip rendering if there are no variations
if ( ! variations?.length ) return null;

const Component = hasUniqueIcons ? VariationsButtons : VariationsDropdown;

return (
<Component
className={ baseClass }
onSelectVariation={ onSelectVariation }
selectedValue={ selectedValue }
variations={ variations }
/>
<div className={ baseClass }>
<ToggleGroupControl
label={ __( 'Transform to variation' ) }
hideLabelFromVision={ true }
onChange={ onSelectVariation }
value={ selectedVariationName }
>
{ variations.map( ( variation ) => {
if ( hasUniqueIcons ) {
const variationLabel =
selectedVariationName === variation.name
? variation.title
: sprintf(
/* translators: %s: Name of the block variation */
__( 'Transform to %s' ),
variation.title
);
return (
<ToggleGroupControlOptionIcon
key={ variation.name }
icon={ variation.icon }
label={ variationLabel }
value={ variation.name }
/>
);
}

return (
<ToggleGroupControlOption
key={ variation.name }
label={ variation.title }
showTooltip={ true }
value={ variation.name }
/>
);
} ) }
</ToggleGroupControl>
</div>
);
}

Expand Down