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

Add default attribute values on server side. #24909

Closed
wants to merge 4 commits into from

Conversation

Addison-Stavlo
Copy link
Contributor

@Addison-Stavlo Addison-Stavlo commented Aug 29, 2020

Description

Alternative approach to #24300: Following up on the style support added for server side blocks in #23007 - I noticed these default values are already picked up by the corresponding hooks in the editor, but in the case of server side blocks are never applied on the front end.

This PR adds a function that can be used to apply default attribute values to a block array. This function is to be called before the attributes are used, and is currently called as a first step in gutenberg_apply_block_supports.


@mcsf / @youknowriad - We talked about running a function like this on the render_block filter higher up in the chain than where we apply the styles. But I am noticing that filter only filters the content string and not the block object itself. So it seems changes applied to the block object passed in that filter are not persistent to the next step in the filter (unless there is a way to do this I may be missing?).

So for now we are just calling this function as a first step in the apply_styles function to filter the block and apply the default values where necessary. Perhaps this generic function could be available for use by developers when default attributes need to be applied for dynamic blocks. 🤔

A few questions:

  • Is there a way to do this with the render_block filter (or is there a more appropriate filter that exists for the block object) so we don't have to call this function everywhere it is needed?

If we go with this approach:

  • should this type of reusable function live elsewhere (and should it be documented)?

if not:

  • Are there other ideas for an approach?

How has this been tested?

There are not currently any server side blocks in Gutenberg that define defaults for these values. In my testing I have added default values (and their corresponding support flags if necessary) to current blocks (such as Post Author). After verifying all default value styles are applied as expected to the front-end, I added a test in class-block-supported-styles-test.php to make this easier to see.

To test this manually for a server side block, you can add default values (and their corresponding support flags) to its block.json file under attributes. (see below)

For named default values you can add the following attributes (note - backgroundColor and gradient to be added and tested separately since they are both for the background):

"textColor": {
	"type": "string",
	"default": "secondary"
},
"backgroundColor": {
	"type": "string",
	"default": "foreground"
},
"gradient": {
	"type": "string",
	"default": "diagonal"
},
"fontSize": {
	"type": "string",
	"default": "large"
},
"align": {
	"type": "string",
	"default": "wide"
}

For custom default values you can add the following to attributes (same note with background and gradient to be tested separately):

"style": {
	"type": "object",
	"default": {
		"color": {
			"background": "#00F",
			"gradient": "linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(0,212,255,1) 100%)",
			"text": "#0F0",
			"link": "#F00"
		},
		"typography": {
			"fontSize": "33",
			"lineHeight": "2.0"
		}
	}
}

also, be sure the required flags are added under supports in block.json:

"__experimentalFontSize": true,
"__experimentalColor": {
	"gradients": true,
	"linkColor": true
},
"__experimentalLineHeight": true,
"align": true

Add the block to a post in the editor, save, and view it on the front-end. You should see the styles visually applied, and their corresponding classes and/or inline styles present on the wrapping element.

Screenshots

Before - A server side block with hook style support with default values defined but not applied:
Screen Shot 2020-07-30 at 6 45 07 PM
After - Default values applied as expected:
Screen Shot 2020-07-30 at 6 41 39 PM

Types of changes

Bug fix (non-breaking change which fixes an issue)
New feature (non-breaking change which adds functionality)

(Kind of both of the above. It is a known 'bug' or shortcoming in the current implementation.)

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.

@Addison-Stavlo Addison-Stavlo self-assigned this Aug 29, 2020
@github-actions
Copy link

github-actions bot commented Aug 29, 2020

Size Change: +652 B (0%)

Total Size: 1.17 MB

Filename Size Change
build/block-editor/index.js 126 kB +8 B (0%)
build/block-editor/style-rtl.css 10.8 kB +19 B (0%)
build/block-editor/style.css 10.8 kB +19 B (0%)
build/block-library/editor-rtl.css 8.55 kB +29 B (0%)
build/block-library/editor.css 8.55 kB +28 B (0%)
build/block-library/index.js 136 kB +162 B (0%)
build/blocks/index.js 47.7 kB +3 B (0%)
build/components/index.js 200 kB +712 B (0%)
build/components/style-rtl.css 15.5 kB -203 B (1%)
build/components/style.css 15.5 kB -208 B (1%)
build/data/index.js 8.55 kB +2 B (0%)
build/edit-navigation/index.js 11.6 kB -1 B
build/edit-post/index.js 304 kB +4 B (0%)
build/edit-site/index.js 17 kB +2 B (0%)
build/edit-widgets/index.js 12 kB +61 B (0%)
build/edit-widgets/style-rtl.css 2.46 kB +5 B (0%)
build/edit-widgets/style.css 2.45 kB +5 B (0%)
build/editor/index.js 45.3 kB +8 B (0%)
build/element/index.js 4.65 kB +1 B
build/format-library/index.js 7.71 kB -2 B (0%)
build/keyboard-shortcuts/index.js 2.52 kB +1 B
build/nux/index.js 3.4 kB -3 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.14 kB 0 B
build/annotations/index.js 3.67 kB 0 B
build/api-fetch/index.js 3.44 kB 0 B
build/autop/index.js 2.82 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 7.99 kB 0 B
build/block-directory/style-rtl.css 953 B 0 B
build/block-directory/style.css 952 B 0 B
build/block-library/style-rtl.css 7.47 kB 0 B
build/block-library/style.css 7.47 kB 0 B
build/block-library/theme-rtl.css 741 B 0 B
build/block-library/theme.css 742 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/compose/index.js 9.67 kB 0 B
build/core-data/index.js 12.3 kB 0 B
build/data-controls/index.js 1.29 kB 0 B
build/date/index.js 5.38 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 568 B 0 B
build/dom/index.js 4.48 kB 0 B
build/edit-navigation/style-rtl.css 1.16 kB 0 B
build/edit-navigation/style.css 1.16 kB 0 B
build/edit-post/style-rtl.css 5.61 kB 0 B
build/edit-post/style.css 5.61 kB 0 B
build/edit-site/style-rtl.css 3.06 kB 0 B
build/edit-site/style.css 3.06 kB 0 B
build/editor/editor-styles-rtl.css 492 B 0 B
build/editor/editor-styles.css 493 B 0 B
build/editor/style-rtl.css 3.81 kB 0 B
build/editor/style.css 3.81 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/style-rtl.css 547 B 0 B
build/format-library/style.css 548 B 0 B
build/hooks/index.js 2.13 kB 0 B
build/html-entities/index.js 621 B 0 B
build/i18n/index.js 3.57 kB 0 B
build/is-shallow-equal/index.js 710 B 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/index.js 3.12 kB 0 B
build/list-reusable-blocks/style-rtl.css 476 B 0 B
build/list-reusable-blocks/style.css 476 B 0 B
build/media-utils/index.js 5.32 kB 0 B
build/notices/index.js 1.79 kB 0 B
build/nux/style-rtl.css 671 B 0 B
build/nux/style.css 668 B 0 B
build/plugins/index.js 2.56 kB 0 B
build/primitives/index.js 1.41 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/redux-routine/index.js 2.85 kB 0 B
build/rich-text/index.js 13.9 kB 0 B
build/server-side-render/index.js 2.77 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.06 kB 0 B
build/viewport/index.js 1.85 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.17 kB 0 B

compressed-size-action

@Addison-Stavlo Addison-Stavlo changed the title WIP - Try adding default attribute values on server side. Add default attribute values on server side. Sep 1, 2020
@mcsf
Copy link
Contributor

mcsf commented Sep 8, 2020

👋

The implementation looks totally correct, but I'm at a bit of a crossroads thinking about #24300 (comment). I'm curious on your thoughts.

Also on my mind: how do we invert control so that block authors are the ones (trivially but explicitly) invoking the features they need for their blocks? By features I mean resolving attributes' default values, but also the actual rendering of specific transformations (e.g. injecting GS styles, classes into the serialised markup).

@Addison-Stavlo
Copy link
Contributor Author

Summarizing a slack conversation I had with @mcsf for visibility (please feel free to add more if I have missed any important points) -

On one hand - we cannot support sourced attributes on the back-end, and don't currently have any good ideas of how to accomplish this. Adding this partial fix to apply defaults for these style hook attributes may send mixed signals, and add confusion regarding that these things magically work on the backend as well as they do on the client, when in actuality they do not as there are gaps and limitations. Since there is not a wide spread necessity for these defaults anyways, it may be more worthwhile to manage this in the block types themselves where they happen to be needed.

On the other hand - This is a rather simple fix that allows default attribute values for style hooks to be supported for dynamic blocks. And while it may reinforce the confusion of what automatically works on the back end, that confusion may still exist regardless of this change. While there may not be many dynamic blocks that need default attribute support, not going with this change and applying these styles individually on the block type itself is part of what we were trying to avoid when adding the gutenberg_apply_block_supports function in the first place.

So what is the less bad? Potentially adding more confusion of what does and doesn't work on the back-end or requiring style applications done in gutenberg_apply_block_supports to be re-written for individual dynamic blocks where default values may be needed? 🤔 🤷‍♀️ It is hard to say.

@Addison-Stavlo
Copy link
Contributor Author

Closing this as we favor rethinking how we handle supports on this side in general.

@youknowriad youknowriad deleted the try/default-values-server-side branch October 12, 2020 09:50
@mcsf mcsf mentioned this pull request Oct 14, 2020
11 tasks
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.

2 participants