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

Expose layout values from theme.json as css-variables #30081

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,18 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
if ( $content_size || $wide_size ) {
ob_start();
?>
<?php echo '.wp-container-' . $id; ?> {
--wp--layout--content-size: <?php echo $content_size ? $content_size : $wide_size; ?>;
--wp--layout--wide-size: <?php echo $wide_size ? $wide_size : $content_size; ?>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last remark I have is about the naming. I don't have any opinion on that, but I just want to make sure this does make sense and is consistent with what we do elsewhere cc @nosolosw

The other thing I wanted to mention is that "content size" and "wide size" are only defined and useful if the layout is "default" (which is the only layout we support right now, but I'm thinking about flex, absolute, grid layouts later. These have different kind of configs and styles). I guess this last point diminishes the need for these CSS variables because there's no guarantee that they exist.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as naming goes, what we have is:

  • presets: --wp--preset--<preset-category>--<preset-slug>
  • custom vars by the theme: --wp--custom--<variable-name>
  • styles: --wp--style--color--link

I'm fine with whatever is consistent and clear for folks, but perhaps a suggestion would be to follow the styles naming --wp--style--layout-*.

These styles aren't generated by the theme.json engine, though, but by the block ― so I'm not sure if it's worth making that difference clear or aligning names. One thing I've noticed is that if we do this, they become public API, which so far we've tried to avoid. I haven't been able to follow the layout work closely but by skimming quickly at this code it looks like we attach this code only if a block supports the layout and has the layout attribute. I suppose then that there may be situations in which these aren't present (the user didn't select any layout).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm personally still hesitant about this PR specially because the layout config can change depending on the "type" of the layout, while right now we do support just one type, I expect we'll introduce others pretty soon (horizontal flex, grid, absolute...) and in all of these these two sizes don't make sense.

}
<?php echo '.wp-container-' . $id; ?> > * {
max-width: <?php echo $content_size ? $content_size : $wide_size; ?>;
max-width: var(--wp--layout--content-size);
margin-left: auto;
margin-right: auto;
}

<?php echo '.wp-container-' . $id; ?> > .alignwide {
max-width: <?php echo $wide_size ? $wide_size : $content_size; ?>;
max-width: var(--wp-layout--wide-size);
}

<?php echo '.wp-container-' . $id; ?> .alignfull {
Expand Down
11 changes: 8 additions & 3 deletions packages/block-editor/src/components/block-list/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ export function LayoutStyle( { selector, layout = {} } ) {
let style =
!! contentSize || !! wideSize
? `
${ selector } {
--wp--layout--content-size: ${ contentSize ?? wideSize };
--wp--layout--wide-size: ${ wideSize ?? contentSize };
}

${ appendSelectors( selector, '> *' ) } {
max-width: ${ contentSize ?? wideSize };
max-width: var(--wp--layout--content-size);
margin-left: auto;
margin-right: auto;
}

${ appendSelectors( selector, '> [data-align="wide"]' ) } {
max-width: ${ wideSize ?? contentSize };
${ appendSelectors( selector, '> [data-align="wide"]' ) } {
max-width: var(--wp--layout--wide-size);
}

${ appendSelectors( selector, '> [data-align="full"]' ) } {
Expand Down