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 bcb1ccb5bd6374..b8099e46c025b0 100644 --- a/packages/block-editor/src/components/block-variation-transforms/index.js +++ b/packages/block-editor/src/components/block-variation-transforms/index.js @@ -2,47 +2,63 @@ * WordPress dependencies */ import { store as blocksStore } from '@wordpress/blocks'; -import { __ } from '@wordpress/i18n'; +import { __, sprintf } from '@wordpress/i18n'; import { + Button, DropdownMenu, MenuGroup, MenuItemsChoice, + VisuallyHidden, } from '@wordpress/components'; import { useSelect, useDispatch } from '@wordpress/data'; -import { useState, useEffect } from '@wordpress/element'; +import { useMemo } from '@wordpress/element'; import { chevronDown } from '@wordpress/icons'; /** * Internal dependencies */ -import { __experimentalGetMatchingVariation as getMatchingVariation } from '../../utils'; import { store as blockEditorStore } from '../../store'; -function __experimentalBlockVariationTransforms( { blockClientId } ) { - const [ selectedValue, setSelectedValue ] = useState(); - const { updateBlockAttributes } = useDispatch( blockEditorStore ); - const { variations, blockAttributes } = useSelect( - ( select ) => { - const { getBlockVariations } = select( blocksStore ); - const { getBlockName, getBlockAttributes } = select( - blockEditorStore - ); - const blockName = blockClientId && getBlockName( blockClientId ); - return { - variations: - blockName && getBlockVariations( blockName, 'transform' ), - blockAttributes: getBlockAttributes( blockClientId ), - }; - }, - [ blockClientId ] +function VariationsButtons( { + className, + onSelectVariation, + selectedValue, + variations, +} ) { + return ( +
); - useEffect( () => { - setSelectedValue( - getMatchingVariation( blockAttributes, variations )?.name - ); - }, [ blockAttributes, variations ] ); - if ( ! variations?.length ) return null; +} +function VariationsDropdown( { + className, + onSelectVariation, + selectedValue, + variations, +} ) { const selectOptions = variations.map( ( { name, title, description } ) => ( { value: name, @@ -50,27 +66,21 @@ function __experimentalBlockVariationTransforms( { blockClientId } ) { info: description, } ) ); - const onSelectVariation = ( variationName ) => { - updateBlockAttributes( blockClientId, { - ...variations.find( ( { name } ) => name === variationName ) - .attributes, - } ); - }; - const baseClass = 'block-editor-block-variation-transforms'; + return (