gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Can't create a card Block Variation for the Group Block

Open silaskoehler opened this issue 2 years ago • 3 comments

Description

I try to register a BlockVariation like so:

registerBlockVariation( 'core/group', {
	name: 'card',
	title: 'Karte',
	description: 'Style Group as a Card',
	attributes: {
		anchor: 'is-card',
	},
	isActive: [ 'anchor' ],
} );

I can insert the variation in the editor, but it is not active after I insert it.

My guess was that there might be a conflict with the default group:variation.https://github.com/WordPress/gutenberg/blob/b54d1fe5fe96b00d7f6455b4aaf40bb21ec43967/packages/block-library/src/group/variations.js#L62-L73

So i try to give a stronger isActive specification, but it does not work for me.

registerBlockVariation( 'core/group', {
	name: 'card',
	title: 'Card' ,
	description:  'Style Group as a Card',
	attributes: { layout: { type: 'constrained', className: 'is-card' } },
	isActive: ( blockAttributes ) =>
		blockAttributes.className === 'is-card' &&
		( ! blockAttributes.layout ||
			! blockAttributes.layout?.type ||
			blockAttributes.layout?.type === 'default' ||
			blockAttributes.layout?.type === 'constrained' ),
} );

Do i miss something?

Step-by-step reproduction instructions

  1. register a block variation with registerBlockVariation and try to override the default group block variation.

Screenshots, screen recording, code snippet

Image

Environment info

  • WordPress Studio
  • WordPress 6.7 with default theme
  • Chrome Browser

Please confirm that you have searched existing issues in the repo.

  • [x] Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • [x] Yes

Please confirm which theme type you used for testing.

  • [x] Block
  • [ ] Classic
  • [ ] Hybrid (e.g. classic with theme.json)
  • [ ] Not sure

silaskoehler avatar Nov 28 '24 09:11 silaskoehler

So i try to give a stronger isActive specification, but it does not work for me.

I think your hunch is right given the variation works, but it doesn't show as active.

It sounds like the same issue as https://github.com/WordPress/gutenberg/issues/41303, which was partially fixed I think, but I don't know much about it. It might be worth reading through the issue and the related PR - https://github.com/WordPress/gutenberg/pull/62031.

Let me know if it's the same and I can close this issue as a duplicate.

talldan avatar Nov 28 '24 09:11 talldan

Thank you for your quick response, and apologies for not finding the other one. #41303 is exactly my issue. Perhaps we could add the "variations" label to the other one. Please feel free to close this one as a duplicate.

silaskoehler avatar Nov 28 '24 10:11 silaskoehler

Thanks for the report. Let's close this issue and continue the discussion in #41303.

t-hamano avatar Nov 28 '24 12:11 t-hamano