-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Blocks: Make it more flexible to match the currently applied block variation #30739
Comments
One thing I found a bit awkward was needing to define
Do you mean that if we're passed an array of This could definitely work and it keeps logic grouped together. One of the tradeoffs would be that it's a bit limiting if someone needs a custom check. I'm not aware of such a use case yet, so I'm not sure how much we need to worry about this.
This one also works. One thing to be mindful of is making sure folks understand what the function ties to. These are block variations, but we also have style variations, and I think folks might also mix this up with patterns. If it's a concern to group things together in settings and keep options minimal have we considered an object instead of an array and another top level function? This makes things a bit more difficult for the server definition, but I was curious if this was considered already. For example: {
//...
variations: { items:[
{
name: 'twitter',
title: 'Twitter',
icon: embedTwitterIcon,
keywords: [ 'tweet', __( 'social' ) ],
description: __( 'Embed a tweet.' ),
patterns: [ /^https?:\/\/(www\.)?twitter\.com\/.+/i ],
attributes: { providerNameSlug: 'twitter', responsive: true },
},
//...
], getActiveVariation: ( blockAttributes, blockVariations ) => { ... } }` Sidenote: To fully remove the hooks.js file from navigation-link, 🤔 thinking through icons would be another one. One straightforward approach would be passing a string matching the icon name from the package. Is there any background on why we may have avoided this? |
Yes, by providing an array of attributes is proposed to make equality checks for each of them.
A custom check is used in
Correct. @gziolo had noted that a layer which translates strings into @wordpress/icons should be provided. |
Yes, it would be great to add a way to be able to pass a string that resolves into a matching icon from
Yes, in general, it seems disconnected when put as a top-level setting. It was one of the reasons I was advocating against it initially.
I don't remember if we discussed this option, but it's definitely a good option to consider. It doesn't solve the issue with the need for the filter for the Navigation Link block, unless we allow also an array representation as a |
If folks don't have strong feelings, I'd say option 1 is likely most viable since it's the least amount of work + would match well if we added a string icon mapping. |
Yes, I agree it is a relatively simple change. We can always do both options discussed 😄 |
Taking this 😄 |
@gwwar and @ntsekouras, should we open a new issue that makes variations: {
items,
getActiveVariation,
}, |
We could but I don't think that is needed (at least for now) and this involves lots of changes and keeping backwards compatibility. Do you think it would provide value if we implemented this now? |
It's mostly optimization as developers wouldn't have to inject |
@gziolo I think it'd be worthwhile to create a new issue, if it's something we'd still like. (fine to mark as low priority). It can be a decent amount of work for someone to reason about what still needs to be done from a closed issue discussion |
We can keep it closed and see how quickly we run into a blocker that requires changes to the shape of |
What problem does this address?
It is possible to define multiple variations of the same block. Users can see in the UI a title, description, or icon if the currently applied block variation implements
isActive
callback and it returnstrue
.See more in: https://github.com/WordPress/gutenberg/blob/trunk/docs/reference-guides/block-api/block-variations.md
However, it looks like the majority of current usage loops over the list of variations and dynamically injects the
isActive
callback. Some examples:gutenberg/packages/block-library/src/embed/variations.js
Lines 342 to 352 in bbd3333
gutenberg/packages/block-library/src/social-link/variations.js
Lines 307 to 316 in bbd3333
With #29095, it is also possible to register block variations on the server. It is impossible to define
isActive
in PHP as long as it needs to be represented as a JavaScript callback. It ends up being dynamically injected in JavaScript with the WordPress hook that creates another level of complexity.gutenberg/packages/block-library/src/navigation-link/hooks.js
Lines 47 to 66 in b620c92
What is your proposed solution?
Option 1:
isActive
allows arraysThis is a simple idea where
isActive
could be represented as an array in the case where block variations are registered on the server, e.g.:type
is the value of a block attribute in the editor and thetype
set as an attribute for the variation. You can think of it as the following condition:Options 2: top-level function on the client
Another option is that we allow defining the top-level API method that returns the name of the currently applied variation. In fact, this is what @ntsekouras originally proposed in #27469 when working on the API, but I convinced him to start with a callback per block variation so we keep the list of top-level settings small.
We could ensure backward compatibility by giving the higher priority the
isActive
matches defined on individual block variations.The text was updated successfully, but these errors were encountered: