core/post-content should support spacing (blockGap, margin, padding) controls
The core/post-content block is given .has-global-padding by default when useRootPaddingAwareAlignments is enabled. This makes it so that all groups that are direct children of the content-block can not use global padding because of following CSS coming from core:
.has-global-padding :where(.has-global-padding) {
padding-right: 0;
padding-left: 0;
}
I'm not really seeing what the purpose of of this would be in this scenario and therefore my best guess is that this is not intended or has been overlooked.
Obviously we want to build our pages without worrying about such things and we definitely do not want to manually set padding for each section on a page just because the parent template forces default padding. Also, because .has-global-padding styles have higher specificity than block-level padding settings, there is not much left to do than working around this issue which is not desirable.
My own take on the solution is to add spacing (blockGap, margin, padding) support to the post-content block so it can have the same spacing controls as a group block. Then we can just remove the padding for the post content block and even the spacing for child blocks so that we can build pages that have full width sections, with default padding and no spacing between.
Of course if there is some misunderstanding on my part regarding all this I would much appreciate if anyone could explain how to properly achieve such a setup.
Experienced on: WP 6.1.1 Gutenberg 15.2.4
Thanks for reporting this @wwdes !
The rule you mentioned above has the purpose of preventing the duplication of root padding in nested containers. The has-global-padding classname is applied to all blocks with a constrained layout when useRootPaddingAwareAlignments is enabled, because there is no way of telling where a block may be in the nesting order, but the actual root padding value should only be applied to the outermost block with that classname.
If I understand your report correctly, the problem is that the rule is overriding any global padding styles applied to blocks with the has-global-padding classname. I can reproduce that locally with TT3 by nesting a Cover block inside a Group, both with constrained layout, and adding padding to the Cover block in global styles.
Fixing this will probably require reducing the specificity of those rules.
the rule is overriding any global padding styles applied to blocks with the
has-global-paddingclassname
Yes, but I think that as it is only padding-left/right and not top/bottom, it leads me to believe that it was intentional but for reasons that I do not know.
Never the less, I think that the post-content block is in essence a container block (like group, cover, columns, etc) but has not got such treatment and therefore it is inconsistent with other container blocks. I think adding support for spacing controls to it will definitely be welcomed by creators, however only doing that will not resolve the issue because has-global-padding class is applied to blocks that have Inner blocks use content width setting enabled regardless. The class is therefore removed only when that setting is disabled, but then we can not control width of inner blocks anymore which is not what we are after.
Perhaps the useRootPaddingAwareAlignments rule is flawed because the block editor does not recognize when a container has padding set to 0. The zero padding step is added to the spacing control by default regardless of what developers set in theme.json. Maybe the solution is to not only implement support for spacing controls to post content block but also to make the block editor aware of when container blocks have their spacing for left and right set to 0 and in that case refrain from adding the has-global-padding class to them.
There is also a WYSIWYG issue that will need to be taken care of. If the post content block in a template would have a control for block spacing, a user creating a post will not be aware that the template has a setting for it as it will not be apparent in the editor and thus diverting from the WYSIWYG concept. But then the post content block already has typography settings and these just as described above are not apparent in the current post editor. In any case an implementation will require the post editor being aware of the template for the currently edited post so it reflects template settings accordingly. Interestingly the post editor is already partially aware of settings in the post content block of the template, namely the Inner blocks use content width setting which clearly impacts the block width control in the post editor. So I imagine that this behavior should just include all settings from a templates post content block in order to consistently maintain the WYSIWYG experience.
Honestly, this is way more complex than I first imagined so at this point I am not really confident about what the best approach for this would be but never the less wanted to throw in some ideas for consideration. As for my issue in the meantime I have resolved to a workaround where I simply remove the has-global-padding class from post content block with this filter:
add_filter('render_block_core/post-content', 'page_post_content_block_fix', 10, 2);
function page_post_content_block_fix($block_content, $block) {
if (strpos('page-post-content-block-fix', $block['attrs']['className']) === false)
return $block_content;
preg_match('/(?<=class=").*(?=")/', $block_content, $classes);
$classes = explode(' ', $classes[0]);
$classes = array_diff($classes, ['has-global-padding']);
$block_content = preg_replace('/(?<=class=").*(?=")/', implode(' ', $classes), $block_content, 1);
return $block_content;
}
With this I just add the page-post-content-block-fix class to my post content block and the code takes core of the rest.