Block variations transformations only going in one direction
Description
I have two variations of core/paragraph. For the sake of this issue, I'll call them Blue Paragraph and Red Paragraph. I can, via scope (in erach variation), add the select in the block setting pallet (in the right sidebar) for picking a variation. Via that select, I can transform from core/paragraph, Blue paragraph or Red paragraph to Blue or Red, but not to core. That is, for example, I can't transform from Blue Paragraph back to the core/paragraph.
I've read the following looking for a solution:
https://github.com/WordPress/gutenberg/pull/26687
https://gutenberg.10up.com/reference/Blocks/block-variations/
https://developer.wordpress.org/block-editor/reference-guides/block-api/block-variations/
https://richtabor.com/block-variations/
And didn't see any mention of how to do this. Maybe it's not a code bug, but a documentation bug in that it's not explained how to achieve this?
It may help to know, my hope is to setup very specify blocks & variations that reflect the client's style guide. Yes, I understand I can define a color pallet, but there's currently no way to control color combos within that set of colors; specific combos that are defined in the brand style guide. In addition, this enables the client's team to focus on content and layout and not have to worry - at all - about maintaining the style guide. The blocks and block variations limit them, in a good and desired way.
Step-by-step reproduction instructions
Here's a quick & dirty of my two (test) variations:
wp.blocks.registerBlockVariation( 'core/paragraph', {
name: 'custom',
title: __( 'Blue Paragraph' ),
description: __( 'My new Blue Paragraph' ),
attributes: { providerNameSlug: 'custom', className: 'is-style-blue-p4' },
scope: [ 'inserter', 'transform' ],
});
wp.blocks.registerBlockVariation( 'core/paragraph', {
name: 'custom2',
title: __( 'Red Paragraph' ),
description: __( 'My new Red Paragraph' ),
attributes: { providerNameSlug: 'custom2', className: 'is-style-red-p1' },
scope: [ 'block', 'inserter', 'transform' ],
});
Screenshots, screen recording, code snippet
No response
Environment info
Latest version of WP, not using the Gutenberg plugin.
Firefox on Win 10 Pro, tho' I don't feel these are part of the problem.
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes
Hi, This issue has gone 30 days without any activity. This means it is time for a check-in to make sure it is still relevant. If you are still experiencing this issue with the latest versions, you can help the project by responding to confirm the problem and by providing any updated reproduction steps. Thanks for helping out.
I tested this and can confirm that by default, there doesn’t seem to be any way to get back to the default core block once a variation has been selected.
Environment Details:
- WordPress: 6.8.1
- Gutenberg Plugin: 20.9.0
- Tested using TT5 theme
Screencast:
https://github.com/user-attachments/assets/d165fcd2-6b01-4250-971f-ac5f075a7e44
One way I found to restore the default behaviour is to manually register a "default" variation and set isDefault: true. In this variation, you can unset the customised values used in other variations.
Here's the code that I used for it:
wp.blocks.registerBlockVariation( 'core/paragraph', {
name: 'default',
title: 'Paragraph',
description: 'Default Paragraph block',
attributes: {
providerNameSlug: undefined,
className: ''
},
scope: [ 'transform' ],
isDefault: true,
isActive: ( blockAttributes ) =>
!blockAttributes.className || blockAttributes.className === ''
});
Using default variation to reset other variation changes:
https://github.com/user-attachments/assets/8a7ab418-471a-477f-9605-96f3d8aed120
One way I found to restore the default behaviour is to manually register a "default" variation and set
isDefault: true.
I beleive this is the ideal approach. For example, the Group block registers the "Group" block itself as the default variation.
Below is an example where you can add variation to a paragraph block and then revert back to the default block:
wp.blocks.registerBlockVariation( 'core/paragraph', {
name: 'default',
title: 'Paragraph',
scope: [ 'transform' ],
isDefault: true,
attributes: { className: '' },
isActive: ( { className } ) => ! className,
} );
wp.blocks.registerBlockVariation( 'core/paragraph', {
name: 'custom',
title: 'Blue Paragraph',
description: 'My new Blue Paragraph',
attributes: { className: 'is-style-blue' },
scope: [ 'transform' ],
isActive: ( { className } ) => className === 'is-style-blue',
} );
wp.blocks.registerBlockVariation( 'core/paragraph', {
name: 'custom2',
title: 'Red Paragraph',
description: 'My new Red Paragraph',
attributes: { className: 'is-style-red' },
scope: [ 'transform' ],
isActive: ( { className } ) => className === 'is-style-red',
} );
@AlchemyUnited I would like to close this as "not a bug", but feel free to comment if I've missed something.