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

[Create-block] Add --variant flag to allow creation of different block type variants #41289

Merged
merged 66 commits into from
Aug 22, 2022

Conversation

ryanwelcher
Copy link
Contributor

@ryanwelcher ryanwelcher commented May 24, 2022

What?

This PR introduces a new CLI flag to the create-block tool to indicate if the generated block should be dynamic or static. The default state is to create a static block and by passing --variant dynamic a dynamic block would be created.

Why?

There is currently no way to scaffold a dynamic block using create-block without using a custom template. The intent here is to allow any template, including the built-in ones, to support creating either type of block.

How?

I have added the new flag, --variant, and am currently using a combination of Mustache template conditionals and custom-named templates to load the correct files.

Testing Instructions

  1. Run node index.js my-test --variant dynamic from the root of the create-block package
  2. Ensure that there is a template.php file added to the src directory and that no save.js file was added or is referenced in index.js
  3. Run node index.js my-test from the root of the create-block package and confirm that save.js is in-place and references correctly in index.js

@ryanwelcher ryanwelcher added [Type] Enhancement A suggestion for improvement. [Tool] Create Block /packages/create-block Developer Experience Ideas about improving block and theme developer experience labels May 24, 2022
@ryanwelcher ryanwelcher requested a review from gziolo as a code owner May 24, 2022 20:08
@ryanwelcher
Copy link
Contributor Author

Based on a conversation with @gziolo during contributor day at WCEU, this PR now uses the {{isDynamic}} template placeholder to conditionally out parts of the templates.

It also now checks to see if a file is empty before writing it to the filesystem. This allows template authors to handle the use-case of files that should only be included depending on the type of block by allowing them to wrap the content in the conditional placeholders:

{{#isDynamic}}
Only render if --is-dynamic is passed
{{/isDynamic}}

{{^isDynamic}}
Only render if --is-dynamic is NOT passed
{{/isDynamic}}

@ryanwelcher ryanwelcher changed the title [Create-block] Add --is-dynamic flag to allow creation dynamic bocks [Create-block] Add --is-dynamic flag to allow creation of dynamic bocks Jun 8, 2022
@ryanwelcher
Copy link
Contributor Author

@gziolo I've addressed your notes from the code review. I've also made a change to how we use variants. They are now stored in an object. This allows us to define variant-specific values which override the defaults and addresses the issue we were having with the create-block-project-template.

Comment on lines 36 to 39
static: {
slug: 'example-static-es5',
title: 'Example Static Block (ES5)',
},
Copy link
Member

@gziolo gziolo Aug 19, 2022

Choose a reason for hiding this comment

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

The values for the static variant could be set directly in the defaultValues object.

The same applies to the templates related to the standard template.

Copy link
Member

Choose a reason for hiding this comment

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

I was pondering one more optimization from the perspective of the developer writing a scaffolding template. What if we assume that everything that is included in defaultValues is the default variant, so we never really need to pick it explicitly? If there are any variants, then we use the variants option, but we only provide non-default variants:

variants: {
 	dynamic: {
 		slug: 'example-dynamic-block',
 		title: 'Example Dynamic Block',
 	},
 	anotherVariant: {},
},

That would generate 3 vars to use with the template:

  • isDefaultVariant
  • isDynamicVariant
  • isAnotherVariant

If no variants were included, we wouldn't have any variables injected into templates.

This would also remove some checks like whether there is more than one variant provided because we would always have the default one and one or more in the variants field.

The only drawback is that we wouldn't have a way to pass the alternate name for the default variant so users won't see the radio option like:

  • static
  • dynamic
  • anotherVariant

Instead, it would look like

  • default
  • dynamic
  • anotherVariant

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like the idea, perhaps we can iterate on it?

Copy link
Member

Choose a reason for hiding this comment

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

I moved the default values from static to defaultValues in #43481. I didn't refactor further. It's still valuable to have a way to name the default variant. In the future, we could also figure out how to pass some sort of description for the variant. At the moment it's static vs dynamic which might not be clear for someone starting with WP blocks.

@gziolo
Copy link
Member

gziolo commented Aug 19, 2022

I've also made a change to how we use variants. They are now stored in an object. This allows us to define variant-specific values which override the defaults and addresses the issue we were having with the create-block-project-template.

Excellent follow-up @ryanwelcher. Brilliant idea to use an object with overrides for variants 💯

We now only need to settle on the final shape of variants, and we should be good to go. I still need to re-test the plugin after applying all the changes.

@gziolo
Copy link
Member

gziolo commented Aug 19, 2022

Code-wise everything looks good. There is one issue that I flagged in https://github.com/WordPress/gutenberg/pull/41289/files#r949815229 that I consider a blocker so it needs to get resolved.

@ryanwelcher
Copy link
Contributor Author

@gziolo

Code-wise everything looks good. There is one issue that I flagged in https://github.com/WordPress/gutenberg/pull/41289/files#r949815229 that I consider a blocker so it needs to get resolved.

I believe I have addressed this. Please retest when you can.

Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

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

It looks good. Let's get this one in after all those iterations 🎉

I'm running some extensive testing and open follow-up PR with enhancements if necessary.

] ).filter( filterOptionsProvided );

// Get the variant prompt first. This will help get the default values
const variantPrompt =
Copy link
Member

Choose a reason for hiding this comment

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

When I provide the variant from the CLI, it asks again for the variant. We can address it seperately.

Copy link
Member

Choose a reason for hiding this comment

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

Fixed in #43481.

@gziolo gziolo merged commit b4d868f into WordPress:trunk Aug 22, 2022
@github-actions github-actions bot added this to the Gutenberg 14.0 milestone Aug 22, 2022
@bph bph added the Needs Dev Note Requires a developer note for a major WordPress release cycle label Sep 14, 2022
@bph bph mentioned this pull request Sep 14, 2022
89 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Developer Experience Ideas about improving block and theme developer experience Needs Dev Note Requires a developer note for a major WordPress release cycle [Tool] Create Block /packages/create-block [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants