From 2c566750b5232ffcc7af97ca914bd6a425ac8eee Mon Sep 17 00:00:00 2001 From: Dale du Preez Date: Thu, 10 Nov 2022 00:55:45 +0200 Subject: [PATCH] Update block variations transforms to use ToggleGroupControl --- .../block-variation-transforms/index.js | 133 ++++++------------ 1 file changed, 42 insertions(+), 91 deletions(-) diff --git a/packages/block-editor/src/components/block-variation-transforms/index.js b/packages/block-editor/src/components/block-variation-transforms/index.js index 4c90009ea00006..23d7eed3f2e69f 100644 --- a/packages/block-editor/src/components/block-variation-transforms/index.js +++ b/packages/block-editor/src/components/block-variation-transforms/index.js @@ -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 ( -
- - { __( 'Transform to variation' ) } - - { variations.map( ( variation ) => ( -
- ); -} - -function VariationsDropdown( { - className, - onSelectVariation, - selectedValue, - variations, -} ) { - const selectOptions = variations.map( - ( { name, title, description } ) => ( { - value: name, - label: title, - info: description, - } ) - ); - - return ( - - { () => ( -
- - - -
- ) } -
- ); -} - function __experimentalBlockVariationTransforms( { blockClientId } ) { const { updateBlockAttributes } = useDispatch( blockEditorStore ); const { activeBlockVariation, variations } = useSelect( @@ -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( () => { @@ -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 ( - +
+ + { 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 ( + + ); + } + + return ( + + ); + } ) } + +
); }