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

Try fixing post editor layout for wide/full Post Content blocks #49565

Merged
merged 2 commits into from
Apr 11, 2023
Merged
Changes from all 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
14 changes: 12 additions & 2 deletions packages/edit-post/src/components/visual-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export default function VisualEditor( { styles } ) {
postContentAttributes,
] );

const layout = newestPostContentAttributes?.layout || {};
const { layout = {}, align = '' } = newestPostContentAttributes || {};

const postContentLayoutClasses = useLayoutClasses(
newestPostContentAttributes,
Expand All @@ -272,7 +272,8 @@ export default function VisualEditor( { styles } ) {
{
'is-layout-flow': ! themeSupportsLayout,
},
themeSupportsLayout && postContentLayoutClasses
themeSupportsLayout && postContentLayoutClasses,
align && `align${ align }`
Copy link
Contributor

Choose a reason for hiding this comment

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

I know this is likely pretty edge-casey, but what about the case where the wide post content block has the constrained layout type (or inherit: true)? In that case, in the site editor, the preview of the post content block will visually look like the content is constrained to the contentSize, but when opening up the page editor, the alignwide rules take precedence.

To avoid that, I was wondering if we could add a variable to use before outputting the classname and styles. E.g. something along the lines of:

const { layout = {}, align = '' } = newestPostContentAttributes || {};
const outputAlignmentStyles = !! ( align && ( ! layout.type || layout.type === 'default' ) );

And then use that in this line and before outputting <LayoutStyle... below?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well spotted! I think we should still output the alignment styles though, because if the Post Content is alignwide none of its children will ever be wider than that, so it helps to have the content max width set.

Instead, I'm going to try scoping the rules affecting Post Content children to when Post Content itself is is-layout-flow.

Copy link
Contributor

Choose a reason for hiding this comment

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

Instead, I'm going to try scoping the rules affecting Post Content children to when Post Content itself is is-layout-flow.

Nice, that helps keep the code simpler, too 👍

);

const postContentLayoutStyles = useLayoutStyles(
Expand Down Expand Up @@ -327,6 +328,12 @@ export default function VisualEditor( { styles } ) {
[ styles ]
);

// Add some styles for alignwide/alignfull Post Content and its children.
const alignCSS = `.is-root-container.alignwide { max-width: var(--wp--style--global--wide-size); margin-left: auto; margin-right: auto;}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is so weird, I don't really understand why we need this code, any explanations?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That was added to address this comment. Post Content layout in the front end is influenced by both its layout and alignment; this was an attempt make the post editor accurately reflect them.

Copy link
Contributor

Choose a reason for hiding this comment

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

So are you saying that we try to apply the "alignment" of the post content block in the post editor? For me that seems a bit random because we don't really know the layout of its parent block, maybe it's within a small sidebar, and wide there means something else. Anyway, I'm not touching that at the moment but I feel like it's just complexity for nothing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, we're not looking at the complete nesting tree. It would be possible to grab any custom wide align value from the parent (which we're not doing) and that might make things a bit more accurate, but in terms of effort vs effect the current solution will work for the majority of cases. Many themes nest Post Content inside one or more Group blocks; even those that nest it inside Columns will most likely have it in a responsive column that takes up most of the page. Having Post Content display in a narrow column on the single post template is a very unlikely scenario.

With these layout problems there are always many variables at play: nesting structure, parent block styles, theme styles, root padding, alignments... and often no approach works for all cases; I think if there's no universal solution it's best to try for a solution that works for most cases 😅

.is-root-container.alignwide:where(.is-layout-flow) > :not(.alignleft):not(.alignright) { max-width: var(--wp--style--global--wide-size);}
.is-root-container.alignfull { max-width: none; margin-left: auto; margin-right: auto;}
.is-root-container.alignfull:where(.is-layout-flow) > :not(.alignleft):not(.alignright) { max-width: none;}`;

return (
<BlockTools
__unstableContentRef={ ref }
Expand Down Expand Up @@ -382,6 +389,9 @@ export default function VisualEditor( { styles } ) {
globalLayoutSettings?.definitions
}
/>
{ align && (
<LayoutStyle css={ alignCSS } />
) }
{ postContentLayoutStyles && (
<LayoutStyle
layout={ postContentLayout }
Expand Down