-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from 4 commits
b7d247a
cf95be2
72646cb
f3d8af3
df227da
18842fc
8bce5b8
63db9f0
21b2f5e
89bcb62
73fc56b
523f7b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -95,6 +98,49 @@ function VariationsDropdown( { | |
); | ||
} | ||
|
||
function VariationsToggleGroupControl( { | ||
className, | ||
onSelectVariation, | ||
selectedValue, | ||
variations, | ||
} ) { | ||
return ( | ||
<fieldset className={ className }> | ||
<Flex> | ||
<ToggleGroupControl | ||
label={ __( 'Transform to variation' ) } | ||
value={ selectedValue } | ||
isBlock | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding @jasmussen point about the border: The |
||
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( | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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?