Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Deafault settings for container spacing conficits with new accordion

  • Hello,

    I have my default padding and margins for gb-container blocks set to:

    function apuri_generateblocks_spacing( $defaults ) {
    	$defaults['container']['marginUnit'] = 'em';
    	$defaults['container']['paddingUnit'] = 'em';
    	$defaults['container']['paddingTop'] = '0';
    	$defaults['container']['paddingRight'] = '0';
    	$defaults['container']['paddingBottom'] = '0';
    	$defaults['container']['paddingLeft'] = '0';
    	$defaults['container']['marginTop'] = '0';
    	$defaults['container']['marginRight'] = '0';
    	$defaults['container']['marginBottom'] = '0';
    	$defaults['container']['marginLeft'] = '0';

    This works like a charm, I can override them where I want, but by default they are flat. U use global styles or local templates, or css to keep things consistent.

    However, I have issues with specific variants of container blocks, I would say it is a bug:
    – Query Loop’s “Post Template”
    – Accordion’s “Accordion Content”
    – Tab’s “Tab Item”

    They inherit the $defaults['container']['marginUnit'] = 'em'; and $defaults['container']['paddingUnit'] = 'em'; while do not inherit the actual values passed:

    `$defaults[‘container’][‘paddingTop’] = ‘0’;
    $defaults[‘container’][‘paddingRight’] = ‘0’;
    $defaults[‘container’][‘paddingBottom’] = ‘0’;
    $defaults[‘container’][‘paddingLeft’] = ‘0’;
    $defaults[‘container’][‘marginTop’] = ‘0’;
    $defaults[‘container’][‘marginRight’] = ‘0’;
    $defaults[‘container’][‘marginBottom’] = ‘0’;
    $defaults[‘container’][‘marginLeft’] = ‘0’;`

    The Result?
    These container variants have these as default padding and margin values: 20em.
    I am able to kinda override this via custom css, but it’s not really good.

    The reason why I would say this is a bug is that I can set the units, but cannot influence the values, and I can do it for all other blocks, but not these, and I have actually set this value for another block, the container, but the setting is passed down to these variants, but only partially.

    I need advice, bugfix, something. This is a large site and it is based on rems, and ems for 96% of things.
    Thanks

  • Hi there,

    I wouldn’t recommend setting defaults like this anymore. We’re moving away from these attributes in GB 1.8 anyways, so those filters will no longer work (there will be dev notes on this in the release post).

    You can test the latest version here: https://github.com/tomusborne/generateblocks/tree/release/1.8.0

    It’s pre-alpha, so no live sites for now.

    We’ve made it so the unit and value are one value (no separate units), which prevents issues like what you’re describing. However, setting the default values is no advised, it’s much better to just create a Global Style.

    Let me know if you need more info πŸ™‚

  • Damn,
    the problem is that site is mostly done, and all blocks use ems by default and if I turn this off it is all going to break. πŸ™

    I rarely use px, so I used this to make it more efficient to build.
    And the site in question is huge. πŸ™

    Any chance this keeps working but you advise not using it in the future?

    PS If one could choose preffered units per block using some UI, I wouldn’t need this.

  • We’re moving away from these attributes in GB 1.8 anyways, so those filters will no longer work

    Will there be some migrations made. Right now, if I remove the filter, all values convert from em to px.
    So if my container has 1.256em it turns to 1.256px which as you can imagine will completely wreck the design.

    This announcement is now waaay more important then my initial quesion. πŸ™

  • Yes, the migration will happen automatically when you load the editor, and the old attributes will automatically map to the new attributes on the frontend before you load the editor. Users shouldn’t notice anything at all (except for the new options).

    The only thing you’ll notice is that your defaults no longer work when you add new blocks (even those may migrate automatically, haven’t tested that yet).

    This is a good thing, it means you can now mix and match units across all 4 sides and across different devices πŸ™‚

  • Music to my ears. πŸ™‚
    Thanks for letting me know

  • No problem! Let me know what you think if you end up testing it πŸ™‚

  • @tom

    I have a, somewhat related question.
    I want to set my accordions to look a certain way by default.
    I could do that using custom css, but it works much better when I use global styles for this.
    However this requires the client to remember and select the global styles every time they add an accordion.

    this is what I gather we used to use add_filter( 'generateblocks_defaults', 'apuri_generateblocks_spacing' ); for.
    Is this still recomended? What would be the latest method to set the defaults for a block?

  • I actually recommend against using the generateblocks_defaults filter, as it can cause too many compatibility issues as we progress the plugin.

    I think in this case it would be better to create Local Patterns for the user that come pre-filled with the Global Styles. That way they don’t need to remember to set anything.

  • Thanks,

    I use local patterns a lot, but for this example it is a bit counter intuitive for the user that they should not use accordion that UI suggests to them when they need one, but have to go ahead and pick local patterns and then accordion there.

    For now I am going to do it using custom css, but I would suggest this as a feature. A way for the user/site dev to be able to set default style/settings for a block.

    That way it becomes a part of a design system for a particular project.

    Thanks again for all the great stuff you are doing.

  • Another alternative is for you to create your own block variation. You could register it as a custom accordion and include all of the attributes you want to be added by default.

    Actual block defaults are a nightmare – we won’t be going that route anymore. However, there are various ways of doing what you’re after. I think a block variation is probably the cleanest with the local pattern coming second.

  • Thanks, I will look into that

  • Hello @tom

    I am trying to play with the variants. So, it is easy to do this with simple blocks, but Accordion is a variant itself on the container block, and it is a composite. So I went inside the pluginc code to see how you guys have done this. So I managed to pull this off with this code:

    const template = [ [ 'generateblocks/container',
    	{
    		variantRole: 'thisbit-accordion-item',
    		accordionItemOpen: true,
    	},
    	[
    		[ 'generateblocks/button',
    			Object.assign( {},
    				generateBlocksPro?.blockStyles?.button,
    				iconAttributes,
    				{
    					text: 'Accordion title',
    					variantRole: 'thisbit-accordion-toggle',
    					buttonType: 'button',
    					backgroundColor: '#ffffff',
    					textColor: '#000000',
    					backgroundColorHover: '#ffffff',
    					textColorHover: '#222222',
    					backgroundColorCurrent: '#fafafa',
    					textColorCurrent: '#000000',
    					alignment: 'left',
    				}
    			),
    		],
    		[ 'generateblocks/container',
    			{
    				variantRole: 'thisbit-accordion-content',
    				paddingTop: '20',
    				paddingRight: '20',
    				paddingBottom: '20',
    				paddingLeft: '20',
    				marginBottom: '20',
    				backgroundColor: '#fafafa',
    			},
    			[
    				[ 'core/paragraph',
    					{
    						content: 'Accordion content.',
    					},
    				],
    			],
    		],
    	],
    ] ];
    
    wp.domReady( () => {
    		wp.blocks.registerBlockVariation(
    			'generateblocks/container',
    			{
    				title: 'Thisbit Accordion',
    				name: 'thisbit-accordion',
    				icon: 'list-view',
    				attributes: {
    					variantRole: 'thisbit-accordion',
    					accordionTransition: 'slide',
    				},
    				innerBlocks: template,
    				isActive: ( attrs ) => 'thisbit-accordion' === attrs.variantRole,
    			}
    		);
    
    		wp.blocks.registerBlockVariation(
    			'generateblocks/container',
    			{
    				title: 'Thisbit Accordion Item',
    				name: 'thisbit-accordion-item',
    				scope: [ 'block' ],
    				attributes: {
    					variantRole: 'thisbit-accordion-item',
    				},
    				isActive: ( attrs ) => 'thisbit-accordion-item' === attrs.variantRole,
    			}
    		);
    
    		wp.blocks.registerBlockVariation(
    			'generateblocks/container',
    			{
    				title: 'Thisbit Accordion Content',
    				name: 'thisbit-accordion-content',
    				scope: [ 'block' ],
    				attributes: {
    					variantRole: 'thisbit-accordion-content',
    				},
    				isActive: ( attrs ) => 'thisbit-accordion-content' === attrs.variantRole,
    			}
    		);
    
    		wp.blocks.registerBlockVariation(
    			'generateblocks/button',
    			{
    				title: 'Thisbit Accordion Title',
    				name: 'thisbit-accordion-title',
    				scope: [ 'block' ],
    				attributes: {
    					variantRole: 'thisbit-accordion-toggle',
    				},
    				isActive: ( attrs ) => 'thisbit-accordion-title' === attrs.variantRole,
    			}
    		);
    
    		wp.blocks.registerBlockVariation(
    			'generateblocks/container',
    			{
    				title: 'Thisbit Accordion Title',
    				name: 'thisbit-container-accordion-toggle',
    				scope: [ 'block' ],
    				attributes: {
    					variantRole: 'thisbit-accordion-toggle',
    				},
    				isActive: ( attrs ) => 'thisbit-accordion-toggle' === attrs.variantRole,
    			}
    		);
    	
    } );

    This seems to do it. Hoever the interactivity does not work. What would be the way to make sure that my variant pulls the appropriate js that handles clicking and all.

    Thanks very much

  • You need to keep the variantRole identical to what we use in our Accordion variation. This allows GenerateBlocks to know that it is indeed an accordion.

    Other than that you should be good to go πŸ™‚

  • Thanks @tom this did it. πŸ™‚
    So far this looks pretty elegant.

    Now I have to explore the documentation to see how to set the settings to the defaults and add my custom styles to it as well. That should be fun.

    One drawback of this approach that I see for now is that if I change CSS that styles the block after I have used it allready. And then later change some styling it applyies only to new blocks I add to the pages because it is part of the block itself. Easy way out of this would be to do that via custom css in a sheet, but then that loads on each page reardless if I use the block or not.

    Perhaps a better way would be to use a block.json?

    Anyways, I can take it from here. Thanks.

Viewing 15 posts - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.