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

Lighter InnerBlocks: allow blocks to render own wrappers #19910

Merged
merged 12 commits into from
Mar 2, 2020

Conversation

ellatrix
Copy link
Member

@ellatrix ellatrix commented Jan 27, 2020

Description

closes #7770

Removes 6 wrapper div elements from the columns and column together.

I created this PR before the merge of #19701 to illustrate with the columns block how it all comes together. This PR really shows the power that blocks will have when they can entirely render themselves.

With this PR, the DOM tree of the columns block is exactly the same as the DOM tree on the front end. This removes the need add any styling for the editor. There's still a few things that ideally should be adjusted (we shouldn't be adding a margin to blocks by default because some blocks need to reset this). But you get the idea and the direction.

I believe this direction will be really important for full site editing themes and performance as well (which is built on InnerBlocks).

It will also allow us to build complex block with inner blocks that need a semantic relationship with the ancestors. Think about a list, table, or definition list built entirely with InnerBlocks.

Please note that I haven't polished the styling of the columns block yet.

Before:

Screenshot 2020-01-27 at 13 49 01

After:

Screenshot 2020-01-27 at 13 45 24

How has this been tested?

Screenshots

Types of changes

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR. .

@ellatrix ellatrix changed the base branch from master to try/block-wrapper January 27, 2020 11:54
@ellatrix ellatrix requested review from jasmussen, mtias and a team January 27, 2020 11:55
@jasmussen
Copy link
Contributor

With this PR, the DOM tree of the columns block is exactly the same as the DOM tree on the front end. This removes the need add any styling for the editor. There's still a few things that ideally should be adjusted (we shouldn't be adding a margin to blocks by default because some blocks need to reset this). But you get the idea and the direction.

Incredible. Let's let that sink in for a moment.

I didn't think this was possible, but it appears you may have done it. Obviously this is a big change, so it needs many eyes and reviews, and there are likely a ton of edgecases and things that will need touching up a a result of such a big refactor. But before diving into that, it seems worth listing why this matters, what the benefits are:

  • The markup becomes the same. This makes the markup predictable for developers developing for the editor and the frontend.
  • With the same markup front and editor, the CSS necessary for the editor style can be vastly simplified, potentially paving the way for a future where we literally load the theme CSS stylesheet into the editor, instead of loading a separate one.
  • With the same markup, front and editor, both the structure and the CSS styles for all existing core blocks can be touched up to reduce a ton of specificity, further benefitting theme developers.

In other words, there are benefits to code quality, to developers, to themers, and to end users who will be more likely to use themes that look like the front-end, increasing usability. That's all to say that this PR is worth reviewing and helping to completion!

@paales
Copy link

paales commented Feb 4, 2020

This is really interesting @ellatrix! I was struggling to get webcomponents working and this might be a big step in the right direction! Maybe you can take a look at the issue (#18778 ) for a second and be able to confirm/deny if your PR would solve the issue?

@github-actions
Copy link

github-actions bot commented Feb 26, 2020

Size Change: -395 B (0%)

Total Size: 865 kB

Filename Size Change
build/block-editor/index.js 105 kB +112 B (0%)
build/block-editor/style-rtl.css 10.5 kB +1 B
build/block-editor/style.css 10.5 kB +1 B
build/block-library/editor-rtl.css 7.4 kB -263 B (3%)
build/block-library/editor.css 7.4 kB -264 B (3%)
build/block-library/index.js 116 kB +7 B (0%)
build/block-library/style-rtl.css 7.5 kB +6 B (0%)
build/block-library/style.css 7.5 kB +5 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.01 kB 0 B
build/annotations/index.js 3.43 kB 0 B
build/api-fetch/index.js 3.39 kB 0 B
build/autop/index.js 2.58 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 6.02 kB 0 B
build/block-directory/style-rtl.css 760 B 0 B
build/block-directory/style.css 760 B 0 B
build/block-library/theme-rtl.css 669 B 0 B
build/block-library/theme.css 671 B 0 B
build/block-serialization-default-parser/index.js 1.65 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/blocks/index.js 57.6 kB 0 B
build/components/index.js 191 kB 0 B
build/components/style-rtl.css 15.5 kB 0 B
build/components/style.css 15.5 kB 0 B
build/compose/index.js 5.76 kB 0 B
build/core-data/index.js 10.5 kB 0 B
build/data-controls/index.js 1.03 kB 0 B
build/data/index.js 8.22 kB 0 B
build/date/index.js 5.37 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 568 B 0 B
build/dom/index.js 3.06 kB 0 B
build/edit-post/index.js 90.9 kB 0 B
build/edit-post/style-rtl.css 8.53 kB 0 B
build/edit-post/style.css 8.53 kB 0 B
build/edit-site/index.js 4.63 kB 0 B
build/edit-site/style-rtl.css 2.51 kB 0 B
build/edit-site/style.css 2.51 kB 0 B
build/edit-widgets/index.js 4.42 kB 0 B
build/edit-widgets/style-rtl.css 2.59 kB 0 B
build/edit-widgets/style.css 2.58 kB 0 B
build/editor/editor-styles-rtl.css 325 B 0 B
build/editor/editor-styles.css 327 B 0 B
build/editor/index.js 44.6 kB 0 B
build/editor/style-rtl.css 4.01 kB 0 B
build/editor/style.css 4 kB 0 B
build/element/index.js 4.45 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/index.js 7.6 kB 0 B
build/format-library/style-rtl.css 502 B 0 B
build/format-library/style.css 502 B 0 B
build/hooks/index.js 1.92 kB 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.48 kB 0 B
build/is-shallow-equal/index.js 710 B 0 B
build/keyboard-shortcuts/index.js 2.3 kB 0 B
build/keycodes/index.js 1.68 kB 0 B
build/list-reusable-blocks/index.js 2.99 kB 0 B
build/list-reusable-blocks/style-rtl.css 226 B 0 B
build/list-reusable-blocks/style.css 226 B 0 B
build/media-utils/index.js 4.85 kB 0 B
build/notices/index.js 1.57 kB 0 B
build/nux/index.js 3.02 kB 0 B
build/nux/style-rtl.css 616 B 0 B
build/nux/style.css 613 B 0 B
build/plugins/index.js 2.54 kB 0 B
build/primitives/index.js 1.49 kB 0 B
build/priority-queue/index.js 780 B 0 B
build/redux-routine/index.js 2.84 kB 0 B
build/rich-text/index.js 14.3 kB 0 B
build/server-side-render/index.js 2.54 kB 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.27 kB 0 B
build/url/index.js 4 kB 0 B
build/viewport/index.js 1.61 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.18 kB 0 B

compressed-size-action

@ellatrix ellatrix changed the base branch from try/block-wrapper to master February 26, 2020 16:02
'__experimentalMoverDirection',
'allowedBlocks',
'template',
] ) }
Copy link
Member Author

Choose a reason for hiding this comment

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

This component could benefit from a hooks rewrite, which will remove the need to omit props used by this component. I'll consider it out of scope for this PR though.

Copy link
Contributor

Choose a reason for hiding this comment

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

So just to clarify here, the extra ...props are needed for the "style" in this PR right?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's correct. Any remaining props on InnerBlocks should be passed down to the block wrapper.

@ellatrix
Copy link
Member Author

Fixed the e2e tests and it's looking good to me now.

@ellatrix
Copy link
Member Author

ellatrix commented Mar 2, 2020

This is working very well. Let's merge and iterate.

@ellatrix ellatrix merged commit b686941 into master Mar 2, 2020
@ellatrix ellatrix deleted the try/inner-block-wrapper branch March 2, 2020 13:36
@github-actions github-actions bot added this to the Gutenberg 7.7 milestone Mar 2, 2020
Comment on lines 78 to +82
export function hasInnerBlocksContext( element ) {
return !! element.querySelector( '.block-editor-block-list__layout' );
return (
element.classList.contains( 'block-editor-block-list__layout' ) ||
!! element.querySelector( '.block-editor-block-list__layout' )
);
Copy link
Member

Choose a reason for hiding this comment

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

There are tests for this function. Probably should have been updated.

: () => <InnerBlocks.ButtonBlockAppender />
}
__experimentalTagName={ Block.div }
Copy link
Member

Choose a reason for hiding this comment

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

It wasn't introduced here, but it's not clear to me why we limit to a predefined set of tag names, vs. some sort of factor function (createBlockComponent( tagName )) or just rendering some element like <BlockComponent tagName="div" /> here. What if I wanted a section block?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's still experimental, but the intention is to add all tags.

'data-has-explicit-width': true,
};
}
lightBlockWrapper: true,
Copy link
Member

Choose a reason for hiding this comment

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

Is this experimental? Where do we document it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, it should have been prefixed as well with __experimental.

&[style] {
&[style*="flex-basis"] {
Copy link
Member

Choose a reason for hiding this comment

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

Are we now applying other styles? This was already pretty fragile in the first place, but was a compromise since existing content wouldn't have any indicators of whether a predefined width was assigned. I think if we expand this, we might want to consider deprecations to include some more durable indicator (like a data- attribute, or class name).

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, there are other inline styles for block animations. Yes, would probably be best to have a separate attribute.

@jasmussen
Copy link
Contributor

This PR had a side-effect that may have been intended, not sure.

Before this PR, the last block in a nesting container would show a big visible plus button, like this:

Screenshot 2020-03-10 at 14 41 38

The heuristic was a bit overthought, perhaps — it would show up at the end of a blocklist if the last block was not a paragraph.

After this PR, that plus is gone. Technically, I find this to be a vastly better experience because that plus should disappear until a block is selected.

However it would be good to find out if there were any ill effects from the removal of these. And it'd also be good to know exactly why it was removed — i.e. was it unintentional and is there dead code somewhere?

CC: @ellatrix

@ellatrix
Copy link
Member Author

@jasmussen It looks like this was an accidental removal:

https://github.com/WordPress/gutenberg/pull/19910/files#diff-3878fabdd6add9b09483511e7ff0d9b3L64

Explicitly setting this to false won't render any appender, and undefined seems to only render it if the last block is not a paragraph. It's been set to false because, even if there was no appender, some invisible DOM would be left that messes up the :last-child rules. I didn't know something would be rendered if the last block was not a paragraph. Should it be restored?

@jasmussen
Copy link
Contributor

Excellent detective work.

We should not restore it until we have a good plan for what to do with it. Knowing how to add it back thanks to you help, I might try my hand at a PR tomorrow.

@cr0ybot
Copy link
Contributor

cr0ybot commented Mar 12, 2020

First of all I think this was a fantastic change!

The side effect that I'm experiencing from this PR is that it is working as intended, but my front-end CSS for overriding column styles does not play well with the editor. This is due to how the editor handles the content width/alignments vs how my front-end works. On the front-end, there is a container element that the blocks are rendered within that has a max width and auto left/right margins, whereas the editor puts a max-width on each block and applies different margins based on the alignment. Due to my override CSS applying margins to the wp-block-columns element, the block looks wrong in the editor.

Screen Shot 2020-03-11 at 20 32 02

I'm not sure what I'm advocating for here, because I like the idea that all blocks are rendered in this manner so that front-end styles should match 1:1... But due to the base alignment layout I wonder if at least the parent wp-block-columns element should actually have a wrapper element so that the wrapper can inherit the default editor max-width and margins.

I think I may try overriding the editor default layout scheme to better match my front-end so that this isn't an issue.

@ZebulanStanphill
Copy link
Member

@cr0ybot #20650, #20656, and #20689 may be relevant to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Columns block styles needs polishing and probably markup changes in the editor
7 participants