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

Docs: Add tutorial explaining how to use block variations API #20581

Closed
gziolo opened this issue Mar 2, 2020 · 7 comments
Closed

Docs: Add tutorial explaining how to use block variations API #20581

gziolo opened this issue Mar 2, 2020 · 7 comments
Labels
[Feature] Block API API that allows to express the block paradigm. Needs Dev Ready for, and needs developer efforts [Type] Developer Documentation Documentation for developers

Comments

@gziolo
Copy link
Member

gziolo commented Mar 2, 2020

Description

Originally raised in #16283.

Based on the examples shared in #20068 and e2e tests created in #20286 we could create tutorials presenting how to make the most using the block variations API.

Examples

Recorded screencasts contain unstable API but included code examples are up to date.

  1. Registers the variation with the inner blocks to apply from the block variation picker using scope limited to the block:
wp.blocks.registerBlockVariation( 'core/columns', { name: 'custom', title: 'Smiley', isDefault: true, innerBlocks: [ [ 'core/column' ], [ 'core/column' ], [ 'core/column' ], [ 'core/column' ] ], icon: 'smiley', scope: [ 'block' ] } );

patterns-api-inserter-1

  1. Registers the variation with the inner blocks to apply from the inserter using scope limited to the inserter:
wp.blocks.registerBlockVariation( 'core/columns', { name: 'custom', title: 'Smiley', isDefault: true, innerBlocks: [ [ 'core/column' ], [ 'core/column' ], [ 'core/column' ], [ 'core/column' ] ], icon: 'smiley', scope: [ 'inserter' ] } );

patterns-api-inserter-2

  1. Registers two variations with initial attributes to apply from the inserter directly:
wp.blocks.registerBlockVariation( 'core/heading', { name: 'green-text', title: 'Green Text', description: 'This block has green text. It overrides the default description.',  attributes: { content: 'Green Text', textColor: 'vivid-green-cyan' }, icon: 'palmtree', scope: [ 'inserter' ] } );
wp.blocks.registerBlockVariation( 'core/heading', { name: 'red-text', title: 'Red Text', attributes: { content: 'Red Text', level: 3, textColor: 'vivid-red' }, icon: 'smiley', scope: [ 'inserter' ] } );

patterns-api-inserter-3

  1. Registers a variation which allows changing the default style variation:
wp.blocks.registerBlockVariation( 'core/quote', { name: 'large', title: 'Large Quote', isDefault: true, attributes: { className: 'is-style-large' }, icon: 'palmtree', scope: [ 'inserter' ] } );
@gziolo gziolo added [Feature] Block API API that allows to express the block paradigm. [Type] Developer Documentation Documentation for developers labels Mar 2, 2020
@gziolo gziolo added the Needs Dev Ready for, and needs developer efforts label Mar 2, 2020
@gziolo
Copy link
Member Author

gziolo commented Jul 2, 2020

It's currently documented in the following places:

There is also a very helpful post from @richtabor on his blog: https://richtabor.com/block-variations/

@jorgefilipecosta
Copy link
Member

I'm closing the issue for now as it seems variations are already documented.

@aacassandra
Copy link

aacassandra commented Jan 3, 2021

Hi everyone, im trying to implement the code as above

  1. scope as block, not work
  2. scope as inserter is work
wp.blocks.registerBlockVariation( 'core/columns', {
	name: 'custom',
	title: 'Smiley',
	isDefault: true,
	innerBlocks: [ [ 'core/column' ], [ 'core/column' ], [ 'core/column' ], [ 'core/column' ] ],
	icon: 'smiley',
	scope: [ 'block' ],
} );

why not work? is there something wrong with this code?

For variation like social-link, how do we get into sidebar blocks? any examples for this or documentation?
@gziolo

@coreyworrell
Copy link
Contributor

@aacassandra it looks like there is no a scope option for transform which adds the sidebar option there. Not sure what block scope is supposed to do.

block – Used by blocks to filter specific block variations. Mostly used in Placeholder patterns like Columns block.

Not very clear what that means.

I also noticed though that if you specify { attributes: { className: 'is-style-whatever' } } that you can no longer add additional classes via the Advanced sidebar panel, or else it "deactivates" the variation.

@Humanify-nl
Copy link

Sorry to add this to a closed issue. But I found it is very elusive in the documentation how to use the variations picker if block scope is chosen. I had to find it digging into the core src, comparing groups & columns.

For example, when creating variations on the core group block, the variations picker is not found. Yet, it allows me to choose block scope, while this doesnt do anything (because there is no <__experimentalBlockVariationPicker /> in core/group).

Would it be helpful if this is mentioned somewhere in the docs? (if you choose block scope, make sure there is a variation picker)

@gziolo
Copy link
Member Author

gziolo commented Aug 31, 2021

Would it be helpful if this is mentioned somewhere in the docs? (if you choose block scope, make sure there is a variation picker)

Yes, that sounds like a good clarification to include. It looks like there are only 2 core blocks that have it integrated:

  • Columns
  • Query Loop

@ntsekouras, can you confirm?

@ntsekouras
Copy link
Contributor

Yes, that sounds like a good clarification to include. It looks like there are only 2 core blocks that have it integrated:
Columns
Query Loop
@ntsekouras, can you confirm?

I can confirm that. I'll open a PR to update the docs a bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm. Needs Dev Ready for, and needs developer efforts [Type] Developer Documentation Documentation for developers
Projects
None yet
Development

No branches or pull requests

6 participants