{"id":6010,"date":"2020-08-06T08:06:00","date_gmt":"2020-08-06T06:06:00","guid":{"rendered":"https:\/\/florianbrinkmann.com\/en\/?p=6010"},"modified":"2020-08-05T16:07:31","modified_gmt":"2020-08-05T14:07:31","slug":"block-variations-gutenberg","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/block-variations-gutenberg-6010\/","title":{"rendered":"Create block variations in Gutenberg"},"content":{"rendered":"\n
WordPress 5.4 introduced the Block Variations API<\/a><\/em>. The API can be used to create variations of blocks, which get listed as own blocks in the block inserter. An example from core are the social media buttons, that are all variations of the social link block<\/a>.<\/p>\n\n\n\n A variation can predefine attributes of the block, in case of the social link block, for example, each variation defines the Like shown in the dev post on WordPress.org, variations are created like that:<\/p>\n\n\n Additionally, an icon can be set, and via With service<\/code> attribute with the identifier of the specific social network.<\/p>\n\n\n\n
variations: [\n {\n name<\/span>: 'wordpress'<\/span>,\n isDefault<\/span>: true<\/span>,\n title<\/span>: __( 'WordPress'<\/span> ),\n description<\/span>: __( 'Code is poetry!'<\/span> ),\n icon<\/span>: WordPressIcon,\n attributes<\/span>: { service<\/span>: 'wordpress'<\/span> },\n },\n {\n name<\/span>: 'google'<\/span>,\n title<\/span>: __( 'Google'<\/span> ),\n icon<\/span>: GoogleIcon,\n attributes<\/span>: { service<\/span>: 'google'<\/span> },\n },\n {\n name<\/span>: 'twitter'<\/span>,\n title<\/span>: __( 'Twitter'<\/span> ),\n icon<\/span>: TwitterIcon,\n attributes<\/span>: { service<\/span>: 'twitter'<\/span> },\n },\n],<\/code><\/div>Code language:<\/span> JavaScript<\/span> (<\/span>javascript<\/span>)<\/span><\/small><\/pre>\n\n\n
variations<\/code> is a key in the object that is passed to
registerBlockType()<\/code>, like
title<\/code>,
edit<\/code>,
attributes<\/code>, et cetera. Each variation gets a
name<\/code> and a
title<\/code>, which is displayed to the user.<\/p>\n\n\n\n
attributes<\/code> we can set values for one or more block attributes.
isDefault<\/code> specifies with variation should be the default. Moreover, the
innerBlocks<\/code> key can be used to fill an
InnerBlocks<\/code> component of the block with a set of blocks. That is used, for example, in the columns block<\/a>, to create the different predefined column layouts.<\/p>\n\n\n\n
scope<\/code>, we can define in which area the variation should be available, but I did not quite understand what the options
block<\/code> and
inserter<\/code> do. Maybe
block<\/code> means that the variation can only be used as part of another block. When leaving that option, both areas are set.<\/p>\n\n\n\n