-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Group block variations not recognized as active #41303
Comments
Hi, @Humanify-nl Your I would recommend updating gutenberg/packages/block-library/src/group/variations.js Lines 14 to 17 in 90995b6
|
Thanks @Mamaduka , I didnt realize that Sadly, I can't get the logic to work since the group attributes are mostly the same, and adding below code doesn't seem to work.
I think I'll have to change the core default group
But I rather not change the default block code. It seems a bit complicated that a thing like a variation selector needs this much logic, just to make a new custom variation happen. Similar as to why there is no css class placed on a variation. On the front-end we have no way to know what kind of group block variation we are dealing with, while there are so many use-cases where this is needed. You're just adding a class. Why don't you use a block style? We use this with for things like:
It's possible to do all this with CSS but it needs a lot of code, and is not great, especially when template-parts come into play. A simple class would solve this: What to do? |
I have the same issue, the custom class is being added, but theres no way to trigger isActive on my custom variation, even though I've made it specific for my variation. |
Another attempt at this, since wp6.0.1: This is my new variation:
This overrides the default group, and updates the
I've tried a few ways to do this. Maybe my logic is wrong, but in the current state of variation switcher, it seems way too complicated to add a new variation. Is there a way to simplify this? Like, is there a way to register the button press without logic, and instead use a js event? @youknowriad , I see you've done a lot of work on these variations, do you have an idea how to do this properly? |
The issue for the group block is that the logic for determining Here is the default variation: gutenberg/packages/block-library/src/group/variations.js Lines 7 to 20 in 05381d2
See what I mean? If you're checking for a custom classname or literally anything else, if your variation also happens to use the default or constrained layout value, or doesn't even define EDIT: Some additional information on how variations are considered "active": When you register a block variation via gutenberg/packages/blocks/src/store/selectors.js Lines 245 to 269 in 44731b9
|
I'm running into this on a project now— trying to make a group container with some default content, and I'd like it to be a block variation (rather than pattern or style) so that it can be separate in the List View, but it's never considered "selected". My code: registerBlockVariation( 'core/group', {
name: 'sidebar-container',
title: __( 'Sidebar Container', 'wporg' ),
description: __( 'Put blocks in a sticky sidebar alongside content.', 'wporg' ),
scope: [ 'inserter' ],
attributes: { className: 'sidebar-container' },
isActive: [ 'className' ],
innerBlocks: [ /* default sidebar content */ ]
} ); Changing my attributes to I'm going to remove the "needs technical feedback" label because I think we have plenty ^, and add "Bug" because this seems fairly unexpected.
I tried this very quickly, and it seems like that prevents the icon from appearing selected in the sidebar. Given that doesn't quite work, I think the better solution here would be to fix this in the Block API, to check all variations & allow for some solution when multiple |
I bumped into this problem a few days ago and came looking for an existing issue before opening one of my own. I can confirm that the hack/work-around @ryelle mentions does work, but as she says, it is far from ideal...as it requires added additional CSS when the group needs to be constrained to style correctly.
As I started to look into this problem my first thought for a solution was to try to change getActiveBlockVariation() so that the isActive property from core were always checked after any variations defined outside of core (in plugin/theme). But I quickly realized that while that may work in some cases, it isn't general enough to handle all cases. So, Kelly's point about a resolution mechanism when multiple variations are matched is needed. Exactly what the resolution mechanism is, I don't have any bright ideas at this time ;-) |
what @ryelle mentioned only works until you save, once saved the active state will not be presistent. However, using the same logic I found another workaround: instead of relying on const GROUP_CARD_VARIATION = 'my-card-group-variation';
registerBlockVariation('core/group', {
name: GROUP_CARD_VARIATION,
title: 'Card Group',
description: 'Give card attributes.',
icon: 'calendar-alt',
attributes: {
layout: { type: 'flex', orientation: 'nonsense' },
className: GROUP_CARD_VARIATION,
roundedCorner: {
type: 'boolean',
default: false,
}
},
isActive: ({ className }) => {
return (
className.includes(GROUP_CARD_VARIATION) // check if className contains GROUP_CARD_VARIATION and not equals. incase you add additional css classes it will still work
);
},
allowedControls: [],
scope: ['inserter', 'transform'],
}); and if you are adding filters, like my case, dont forget to use the same logic as above: const withBookQueryControls = (BlockEdit) => (props) => {
if (!props?.attributes?.className?.includes(GROUP_CARD_VARIATION)) {
return <BlockEdit {...props} />
}
const { attributes, setAttributes } = props;
const roundedCss = ' rounded-md overflow-hidden';
return <>
<BlockEdit {...props} />
<InspectorControls>
<PanelBody title="Card Settings">
<ToggleControl
label="Rounded Corner"
checked={attributes.className.includes(roundedCss)}
onChange={(v) => setAttributes({ className: v ? attributes.className + roundedCss : attributes.className.replace(roundedCss, '') })}
/>
</PanelBody>
</InspectorControls>
</>
};
addFilter('editor.BlockEdit', 'core/group', withBookQueryControls); |
Is there any known workaround for getting |
+1 to resolving this issue. I'd like to trigger isActive in a group block variation that uses the default layout attributes. To reiterate the above: the default group block variations are consuming isActive (via layout attributes) before it has a chance to check any custom group block variations. In the meantime I've come up with the following workaround. This is something of a hack, and replaces the entire class attribute of the group on the front end. Use the flex layout attribute in the block variation to trigger isActive (I'm using "smc-quiz" as my key):
Then filter the block to replace the class attribute server side (here using flow layout):
You may want to change the returned class and/or edit the conditional checks for your instance. The code above can probably be improved (can a block variation be targeted by the "render_block" filter?) Please feel to do so. |
I think I have an idea how to fix this issue. It consists of two parts:
|
That's a hard problem indeed. We had some explorations in the past for this to see how it would be best to extend a core block and its variations. We ended up adding a 'dumb' attribute( Maybe something like that could be a reliable solution, by injecting in all blocks an attribute(like the namespace) with a |
I replied to this part in a comment on my PR. Quoting the most relevant part:
|
Description
Using a custom block variation and selecting it in the block inspector does not show the 'is-pressed' css class.
The selected varation does in fact get shown in the editor, it is just the button press that does not get registered somehow.
Step-by-step reproduction instructions
Step 1: add a custom variation to the group block.
Step 2: select the variation in the editor.
Step 3: notice that the first button shows is-pressed, instead of the custom variation.
Screenshots, screen recording, code snippet
Environment info
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes
The text was updated successfully, but these errors were encountered: