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

Block Library: Update Columns block to use Patterns API #18283

Merged
merged 7 commits into from
Nov 29, 2019

Conversation

gziolo
Copy link
Member

@gziolo gziolo commented Nov 5, 2019

Description

Part 2 of #16283.

Fixes #18738.

Continuation of #18270. This branch is based on the add/patterns-api master branch.

This PR refactors Columns block to use this new API making the placeholder open for customization.

Selector

This PR also introduces a new selector which lets to find the default pattern:

/**
 * Returns the default block pattern for the given block type.
 * If there is no default pattern set, it returns the first item.
 *
 * @param {Object} state      Data state.
 * @param {string} blockName  Block type name.
 *
 * @return {?WPBlockPattern} The default block pattern.
 */
function __experimentalGetDefaultBlockPattern( state, blockName );

To ensure that icons have a shared size in the layout selection, this PR also adds a new prop to the IconButton component: size to make it possible to enforce one value for icons passed in various formats.

Components

__experimental BlockPatternPicker

I refactored an internal component InnerBlocksTemplatePicker from the InnerBlocks implementation. It's now called BlockPatternPicker and is exposed as an experimental feature __experimental BlockPatternPicker from @wordpress/block-editor package.

In addition, I slightly tweaked wordings in the UI to use the pattern keyword. It's also reflected in the name of props.

Screen Shot 2019-11-06 at 18 47 28

InnerBlocksTemplatePicker is no longer included in InnerBlocks as discussed in this PR. I updated all the corresponding documentation. This API was experimental so we don't need any further actions.

IconButton

I added a new size prop to IconButton component to make it possible to scale all icons to the same size in the block pattern picker.

How has this been tested?

npm run test-unit – new unit tests were added.

Columns block UI

  1. Create a new post or open an existing one.
  2. Add the Column block.
  3. Click one of the layouts and double-check it was properly initialized.
  4. Add another Column block.
  5. Click the skip link and double-check that the default pattern was applied – 2 columns.

Extensibility

To add a new pattern to the Columns block:

wp.blocks.__experimentalRegisterBlockPattern( 'core/columns', { name: 'custom', label: 'Custom', isDefault: true, innerBlocks: [ [ 'core/column' ], [ 'core/column' ], [ 'core/column' ], [ 'core/column' ] ], icon: 'smiley' } ); 

To remove a pattern from the Columns block:

wp.blocks.__experimentalUnregisterBlockPattern( 'core/columns', 'two-columns-equal' ); 

When those two commands get executed, then the custom pattern becomes the default one. In effect, when you add a Column block and click the skip link, then 4 columns will be inserted.

Screen Shot 2019-11-05 at 13 51 54

IconButton

You can test in Storybook the IconButton and its new size prop. To do it run npm run design-system:dev and navigate to the proper page. You can use knobs to play with sizes:

Screen Shot 2019-11-05 at 13 51 36

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.

@gziolo gziolo self-assigned this Nov 5, 2019
@gziolo gziolo added [Package] Block library /packages/block-library [Feature] Block API API that allows to express the block paradigm. [Type] Technical Prototype Offers a technical exploration into an idea as an example of what's possible labels Nov 5, 2019
@gziolo gziolo requested review from mtias, aduth and mcsf November 5, 2019 12:25
@gziolo gziolo force-pushed the update/columns-block-patterns-api branch from 582aaaf to 3d385da Compare November 5, 2019 12:38
@gziolo gziolo changed the base branch from add/patterns-api to master November 8, 2019 06:59
@gziolo gziolo force-pushed the update/columns-block-patterns-api branch from c659e2e to 5744f19 Compare November 8, 2019 07:02
@gziolo
Copy link
Member Author

gziolo commented Nov 27, 2019

@youknowriad, I applied some of the changes which were included in #18343. In particular, I added the wrapper for edit implementation which handles the process of the pattern selection automatically. This is the same approach as you could see in #18343 for Media & Text block which we decided to abandon because the UX wasn't good enough.

I really think we should build this as an implementation detail of BlockEdit (or other block editor components) from the start. Registering patterns for blocks, should be enough to tweak the behavior of the block (like styles, transforms)

This is very close to the vision shared. I'd like to refactor one more block to identify some potential issues with the current proposal. At the moment the wrapper which handles the pattern selection is presented when there are no nested blocks added to the block. In the case of Columns block, it's perfectly fine as it always needs to have at least 2 nested blocks. However, this might not fit all use cases as some blocks might assume that there might be no blocks provided when the block gets created. In addition, it doesn't cover also blocks that don't have inner blocks where we want to use Patterns API to offer some initial setup.

@gziolo gziolo force-pushed the update/columns-block-patterns-api branch 2 times, most recently from 435f520 to 356703a Compare November 27, 2019 15:29
Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking very good in my testing

@gziolo gziolo force-pushed the update/columns-block-patterns-api branch from 356703a to 13b6cae Compare November 28, 2019 12:16
@gziolo
Copy link
Member Author

gziolo commented Nov 28, 2019

It looks like 13b6cae solves all the issues discussed. Icon component seems to work great. Let's see what Travis thinks.

@gziolo
Copy link
Member Author

gziolo commented Nov 28, 2019

Nice, Travis likes the changes proposed. Thanks @youknowriad for the great tip 🥇

The only question remaining is whether we should remove TemplatePicker integration with InnerBlocks in this PR.

@youknowriad
Copy link
Contributor

Using Icon in the IconButton can cause some breakage. Notably I see that DropIt icon is broken in the more menu, that said I'm fine if we can consider this a plugin issue but we should check quickly how many issues this could introduce.

@gziolo
Copy link
Member Author

gziolo commented Nov 28, 2019

Using Icon in the IconButton can cause some breakage. Notably I see that DropIt icon is broken in the more menu, that said I'm fine if we can consider this a plugin issue but we should check quickly how many issues this could introduce.

I see why it can happen, it's the Inception issue 😄 Can we add the case in the Icon to merge props when it happens? I can give it a try.

@gziolo
Copy link
Member Author

gziolo commented Nov 28, 2019

Using Icon in the IconButton can cause some breakage. Notably I see that DropIt icon is broken in the more menu, that said I'm fine if we can consider this a plugin issue but we should check quickly how many issues this could introduce.

I see why it can happen, it's the Inception issue 😄 Can we add the case in the Icon to merge props when it happens? I can give it a try.

It looks like the issue is different, there is size prop which gets overriden:

function Icon({ size = "small", borderless = false, color = "unsplash" }) {

https://github.com/youknowriad/dropit/blob/b0bd499ba85e9795fbb88199fff776123f60bdd5/scripts/sidebar/components/icon/index.js#L71-L83

@youknowriad
Copy link
Contributor

yeah! I'm fine for this being a Dropit issue :)

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@gziolo gziolo force-pushed the update/columns-block-patterns-api branch from 13b6cae to e522171 Compare November 29, 2019 08:43
@gziolo gziolo merged commit ae327b2 into master Nov 29, 2019
@gziolo gziolo deleted the update/columns-block-patterns-api branch November 29, 2019 09:08
@youknowriad youknowriad added this to the Gutenberg 7.1 milestone Dec 9, 2019
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. [Package] Block library /packages/block-library [Type] Technical Prototype Offers a technical exploration into an idea as an example of what's possible
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add props for InnerBlocksTemplatePicker icon, label and instructions
2 participants