Skip to content

Fall back to default block styles when a new block is being inserted#22167

Closed
adamziel wants to merge 3 commits into
masterfrom
fix/default-block-variation
Closed

Fall back to default block styles when a new block is being inserted#22167
adamziel wants to merge 3 commits into
masterfrom
fix/default-block-variation

Conversation

@adamziel

@adamziel adamziel commented May 7, 2020

Copy link
Copy Markdown
Contributor

Description

When you call insertBlocks or replaceBlocks, they would add default styles to all the blocks being inserted. At the moment, only preferredStyleVariations are considered, and if those are missing then no style variation is set at all. This PR makes sure we fall back to default style variation from the block definition.

For example, the navigation block has the following style variations:

styles: [
{ name: 'light', label: __( 'Light' ), isDefault: true },
{ name: 'dark', label: __( 'Dark' ) },
],

Without this PR, a new navigation block would get no variation assigned even though light is supposed to be the default one - so the background ends up being transparent.

Before
2020-05-07 12-55-10 2020-05-07 12_55_41

After
2020-05-07 12-56-00 2020-05-07 12_56_45

How has this been tested?

  1. Without this PR: Add a new navigation block with some navigation items. Confirm the background is transparent.
  2. Without this PR: Add a new navigation block with some navigation items. Confirm the background is white.

Types of changes

Bug fix (non-breaking change which fixes an issue)

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.

@adamziel adamziel added [Feature] Inserter The main way to insert blocks using the + button in the editing interface [Package] Block editor /packages/block-editor labels May 7, 2020
@adamziel adamziel self-assigned this May 7, 2020
@adamziel adamziel changed the title Consider default block styles when inserting Fall back to default block styles when a new block is being inserted May 7, 2020
@github-actions

github-actions Bot commented May 7, 2020

Copy link
Copy Markdown

Size Change: +1.96 kB (0%)

Total Size: 824 kB

Filename Size Change
build/api-fetch/index.js 4.08 kB +1 B
build/block-directory/index.js 6.61 kB +7 B (0%)
build/block-editor/index.js 102 kB +682 B (0%)
build/block-editor/style-rtl.css 10.3 kB +124 B (1%)
build/block-editor/style.css 10.3 kB +123 B (1%)
build/block-library/index.js 115 kB -21 B (0%)
build/blocks/index.js 48.2 kB +52 B (0%)
build/components/index.js 180 kB +541 B (0%)
build/components/style-rtl.css 17 kB +42 B (0%)
build/components/style.css 16.9 kB +41 B (0%)
build/compose/index.js 6.66 kB -1 B
build/core-data/index.js 11.4 kB +14 B (0%)
build/data/index.js 8.45 kB +4 B (0%)
build/edit-navigation/index.js 4.4 kB +328 B (7%) 🔍
build/edit-navigation/style-rtl.css 618 B +133 B (21%) 🚨
build/edit-navigation/style.css 617 B +132 B (21%) 🚨
build/edit-post/index.js 28 kB -132 B (0%)
build/edit-post/style-rtl.css 12.2 kB +20 B (0%)
build/edit-post/style.css 12.2 kB +20 B (0%)
build/edit-site/index.js 12.1 kB -177 B (1%)
build/edit-site/style-rtl.css 5.22 kB +25 B (0%)
build/edit-site/style.css 5.22 kB +24 B (0%)
build/edit-widgets/index.js 8.37 kB +1 B
build/edit-widgets/style-rtl.css 4.69 kB +13 B (0%)
build/edit-widgets/style.css 4.69 kB +12 B (0%)
build/editor/editor-styles-rtl.css 425 B -3 B (0%)
build/editor/editor-styles.css 428 B -3 B (0%)
build/editor/index.js 44.3 kB -38 B (0%)
build/element/index.js 4.65 kB +1 B
build/format-library/index.js 7.63 kB -2 B (0%)
build/hooks/index.js 2.14 kB +7 B (0%)
build/keyboard-shortcuts/index.js 2.51 kB -1 B
build/keycodes/index.js 1.94 kB -1 B
build/list-reusable-blocks/index.js 3.12 kB -1 B
build/media-utils/index.js 5.29 kB -1 B
build/notices/index.js 1.79 kB -1 B
build/nux/index.js 3.4 kB +1 B
build/plugins/index.js 2.56 kB -2 B (0%)
build/redux-routine/index.js 2.85 kB -2 B (0%)
build/rich-text/index.js 14.8 kB -1 B
build/server-side-render/index.js 2.67 kB -1 B
build/token-list/index.js 1.28 kB -1 B
build/url/index.js 4.02 kB +1 B
build/viewport/index.js 1.84 kB -1 B
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.02 kB 0 B
build/annotations/index.js 3.62 kB 0 B
build/autop/index.js 2.82 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/style-rtl.css 760 B 0 B
build/block-directory/style.css 761 B 0 B
build/block-library/editor-rtl.css 7.08 kB 0 B
build/block-library/editor.css 7.08 kB 0 B
build/block-library/style-rtl.css 7.28 kB 0 B
build/block-library/style.css 7.29 kB 0 B
build/block-library/theme-rtl.css 683 B 0 B
build/block-library/theme.css 685 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/data-controls/index.js 1.29 kB 0 B
build/date/index.js 5.47 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.1 kB 0 B
build/editor/style-rtl.css 5.07 kB 0 B
build/editor/style.css 5.08 kB 0 B
build/escape-html/index.js 734 B 0 B
build/format-library/style-rtl.css 502 B 0 B
build/format-library/style.css 502 B 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.56 kB 0 B
build/is-shallow-equal/index.js 710 B 0 B
build/list-reusable-blocks/style-rtl.css 226 B 0 B
build/list-reusable-blocks/style.css 226 B 0 B
build/nux/style-rtl.css 616 B 0 B
build/nux/style.css 613 B 0 B
build/primitives/index.js 1.5 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/shortcode/index.js 1.7 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.18 kB 0 B

compressed-size-action

@gziolo

gziolo commented May 7, 2020

Copy link
Copy Markdown
Member

Can you give a more detailed example of the issue you are trying to solve? It would help to see the list of style variations set and the generated output before and after.

Comment thread packages/blocks/src/store/selectors.js Outdated
* @param {Object} state Data state.
* @param {string} name Block type name.
*
* @return {Array?} Block Styles.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's an object here.

Comment thread packages/blocks/src/store/selectors.js Outdated
* @return {Array?} Block Styles.
*/
export function getDefaultBlockStyle( state, name ) {
return getBlockStyles( state, name ).filter(

@gziolo gziolo May 7, 2020

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

find from Lodash might be safer because of filter on undefined (@return {Array?} Block Styles.) will throw an error.

Well, optional chaining might work :)

@mapk

mapk commented May 7, 2020

Copy link
Copy Markdown
Contributor

I'm a bit unclear why we're adding a white background to the Navigation block. One common user experience with Navs is to add them on top of other backgrounds in a Header. Here's an example: https://cloudup.com/cfkiaDLkbij

If we do default a background color, we should allow that background to be removed. This doesn't seem to happen currently.

background-color

@karmatosed

Copy link
Copy Markdown
Member

@mapk we need a background color otherwise we get the transparency issues in the navigation block. I would expect that the 'clear' would do just that and it highlights the background color here:

image

That should solve the unexpected situation.

@mapk

mapk commented May 7, 2020

Copy link
Copy Markdown
Contributor

The only transparency issues I've noticed in the Nav block have to do with the subnav items. Are there others? I'm sure there's a way we can include a background color on the subnav items, but not on the main block.

I tried clicking "clear" in my video and it didn't do anything. 😄

@adamziel

adamziel commented May 8, 2020

Copy link
Copy Markdown
Contributor Author

@gziolo The culprit is the following line:

const blockStyle = styleVariations[ blockName ];

Without this PR, it only considers preferredStyleVariations which is empty by default. With this PR, it also considers the default style variations. For example, the navigation block has the following style variations:

styles: [
{ name: 'light', label: __( 'Light' ), isDefault: true },
{ name: 'dark', label: __( 'Dark' ) },
],

Without this PR, a new navigation block would get no variation assigned even though light is supposed to be the default one - so the background ends up being transparent. When inspecting the block, the default variation would be selected as the active one, even though it wouldn't really be active. See the "Before" and "After" gifs in the PR description.

I'm a bit unclear why we're adding a white background to the Navigation block

@karmatosed @mapk good note! I think this is consideration is separate from this PR - IMHO the default style variation should be assigned to newly created blocks by default. Whether or not the default menu variation is light is something we can figure out independently.

For example maybe we should have a third variation? As in transparent background with white sub-menus.

Also the clear button seems to be broken entirely, I was able to set the background to pink, but even then it did not clear it.

The clear button seems to be removing the background CSS rule so the default one from style variation kicks in. Maybe it should be setting it to transparent instead?

@gziolo

gziolo commented May 8, 2020

Copy link
Copy Markdown
Member

Do you mean that the class name is not injected into generated HTML? If yes, it’s a design decision that was made and it’s expected behavior. Only non-default style variations cause new class name to be added. @youknowriad or @mtias know the full story.

@mtias

mtias commented May 8, 2020

Copy link
Copy Markdown
Member

I feel dark and white should not affect the background of the main navigation container, only the submenus.

@adamziel

adamziel commented May 8, 2020

Copy link
Copy Markdown
Contributor Author

Let's move the background color discussion to a dedicated PR here: #22228

Do you mean that the class name is not injected into generated HTML?

That's what I meant, yes! Hm, but if I switch to a dark variation and then switch back to the light style, the class is going to be added - should it be not added in that case then?

@adamziel

adamziel commented May 8, 2020

Copy link
Copy Markdown
Contributor Author

Closing these PR as per last @gziolo comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Inserter The main way to insert blocks using the + button in the editing interface [Package] Block editor /packages/block-editor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants