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 Variations: Support dot notation in isActive string array #62068

Closed
ockham opened this issue May 28, 2024 · 3 comments · Fixed by #62088
Closed

Block Variations: Support dot notation in isActive string array #62068

ockham opened this issue May 28, 2024 · 3 comments · Fixed by #62088
Assignees
Labels
[Feature] Block Variations Block variations, including introducing new variations and variations as a feature [Status] In Progress Tracking issues with work in progress [Type] Enhancement A suggestion for improvement.

Comments

@ockham
Copy link
Contributor

ockham commented May 28, 2024

What problem does this address?

Block variations support an isActive property which can be either a function, or an array of strings (string[]), documented here. tl;dr: Given a block instance, that variation is used to determine which variation is currently active: If isActive is a function, it is given the block instance's attributes and the variation's attributes as arguments. It will return true if the block instance matches the variation. The string[] version of isActive is thought of as a shorthand of the above, where all items in the array are attribute names that will be compared.

As documented here and here, it's possible that multiple variations' isActive criteria match a given block instance, which can lead to the wrong variation being determined to be active.

There's a potential fix that solves this problem by choosing the matching variation with the highest specificity (i.e. number of items in the isActive array), but that only works if all matching variations have isActive arrays rather than functions.

One notable case where it's impossible to use an array for isActive is when an attribute is an object, and only one of its properties must be compared to determine if its active, for example:

attributes: {
namespace: 'core/posts-list',
query: {
perPage: 4,
pages: 1,
offset: 0,
postType: 'post',
order: 'desc',
orderBy: 'date',
author: '',
search: '',
sticky: 'exclude',
inherit: false,
},
},
scope: [ 'inserter' ],
isActive: ( { namespace, query } ) => {
return namespace === 'core/posts-list' && query.postType === 'post';
},

What is your proposed solution?

If we allow dots as "property accessors" in isActive array items, we wouldn't need to resort to isArray functions in cases like the above. The example could instead be rewritten as

isActive: [ 'namespace', 'query.postType' ]
@ockham ockham added [Type] Enhancement A suggestion for improvement. [Feature] Block Variations Block variations, including introducing new variations and variations as a feature labels May 28, 2024
@ockham
Copy link
Contributor Author

ockham commented May 28, 2024

FYI @ntsekouras @tjcafferkey @gziolo

@ockham
Copy link
Contributor Author

ockham commented May 28, 2024

We have the getValueFromObjectPath util function that should help with this.

@ockham ockham added the Good First Issue An issue that's suitable for someone looking to contribute for the first time label May 28, 2024
@ntsekouras
Copy link
Contributor

I think this is an enhancement that could be done independently from the specificity issue you mention. The isActive API as a function is still useful and can accommodate more complex cases (example in template parts).

@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label May 29, 2024
@ockham ockham removed the Good First Issue An issue that's suitable for someone looking to contribute for the first time label May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block Variations Block variations, including introducing new variations and variations as a feature [Status] In Progress Tracking issues with work in progress [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants