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

Block Variations Transforms: Display as icon buttons if each block variation has a unique icon #40036

Merged
Show file tree
Hide file tree
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 @@ -2,75 +2,85 @@
* 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 (
<fieldset className={ className }>
<VisuallyHidden as="legend">
{ __( 'Transform to variation' ) }
</VisuallyHidden>
{ variations.map( ( variation ) => (
<Button
key={ variation.name }
icon={ variation.icon }
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>
);
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,
label: title,
info: description,
} )
);
const onSelectVariation = ( variationName ) => {
updateBlockAttributes( blockClientId, {
...variations.find( ( { name } ) => name === variationName )
.attributes,
} );
};
const baseClass = 'block-editor-block-variation-transforms';

return (
<DropdownMenu
className={ baseClass }
className={ className }
label={ __( 'Transform to variation' ) }
text={ __( 'Transform to variation' ) }
popoverProps={ {
position: 'bottom center',
className: `${ baseClass }__popover`,
className: `${ className }__popover`,
} }
icon={ chevronDown }
toggleProps={ { iconPosition: 'right' } }
>
{ () => (
<div className={ `${ baseClass }__container` }>
<div className={ `${ className }__container` }>
<MenuGroup>
<MenuItemsChoice
choices={ selectOptions }
Expand All @@ -84,4 +94,63 @@ function __experimentalBlockVariationTransforms( { blockClientId } ) {
);
}

function __experimentalBlockVariationTransforms( { blockClientId } ) {
const { updateBlockAttributes } = useDispatch( blockEditorStore );
const { activeBlockVariation, variations } = useSelect(
( select ) => {
const { getActiveBlockVariation, getBlockVariations } = select(
blocksStore
);
const { getBlockName, getBlockAttributes } = select(
blockEditorStore
);
const name = blockClientId && getBlockName( blockClientId );
return {
activeBlockVariation: getActiveBlockVariation(
name,
getBlockAttributes( blockClientId )
),
variations: name && getBlockVariations( name, 'transform' ),
};
},
[ blockClientId ]
);
andrewserong marked this conversation as resolved.
Show resolved Hide resolved

const selectedValue = activeBlockVariation?.name;

// Check if each variation has a unique icon.
const hasUniqueIcons = useMemo( () => {
const variationIcons = new Set();
variations.forEach( ( variation ) => {
if ( variation.icon ) {
variationIcons.add( variation.icon );
}
} );
return variationIcons.size === variations.length;
}, [ variations ] );

const onSelectVariation = ( variationName ) => {
updateBlockAttributes( blockClientId, {
...variations.find( ( { name } ) => name === variationName )
.attributes,
} );
};

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

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

export default __experimentalBlockVariationTransforms;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.block-editor-block-variation-transforms {
padding: 0 $grid-unit-20 $grid-unit-20 56px;
padding: 0 $grid-unit-20 $grid-unit-20 52px;
width: 100%;

.components-dropdown-menu__toggle {
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/group/variations.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { row, stack } from '@wordpress/icons';
import { group, row, stack } from '@wordpress/icons';

const variations = [
{
Expand All @@ -14,6 +14,7 @@ const variations = [
isActive: ( blockAttributes ) =>
! blockAttributes.layout ||
blockAttributes.layout?.type === 'default',
icon: group,
},
{
name: 'group-row',
Expand Down