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

Centralize style & support mappings for blocks #25185

Merged
merged 13 commits into from
Sep 10, 2020
Merged

Conversation

oandregal
Copy link
Member

Fixes #25051
Alternative to #25145

This Pull Request seeks to clarify what info we take from which source. Specifically, we extract info from the block (support keys and the style attribute) as well as from theme.json (style properties that follow the same paths as the style attribute).

@@ -385,17 +409,9 @@ function gutenberg_experimental_global_styles_get_block_data() {
* @return array Containing a set of css rules.
*/
function gutenberg_experimental_global_styles_flatten_styles_tree( $styles ) {
$mappings = array(
Copy link
Member Author

Choose a reason for hiding this comment

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

No changes here, only extracted to a separate function.

'--wp--style--color--link' => array( '__experimentalColor', 'linkColor' ),
'background-color' => array( '__experimentalColor' ),
Copy link
Member Author

Choose a reason for hiding this comment

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

These keys were renamed to use the same conventions as client-side (camelCase over kebab-case), making everything more coherent and less computation-demanding.

! supports.includes( TEXT_COLOR ) &&
! supports.includes( BACKGROUND_COLOR ) &&
! supports.includes( GRADIENT_COLOR ) &&
! supports.includes( 'color' ) &&
Copy link
Member Author

Choose a reason for hiding this comment

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

These keys are the ones that were updated in the server (gutenberg_experimental_global_styles_get_support_keys changes). This way we use the common convention for the styles properties everywhere.

@github-actions
Copy link

github-actions bot commented Sep 9, 2020

Size Change: -240 B (0%)

Total Size: 1.2 MB

Filename Size Change
build/a11y/index.js 1.14 kB -1 B
build/annotations/index.js 3.67 kB +2 B (0%)
build/block-directory/index.js 8.5 kB +1 B
build/block-editor/index.js 128 kB -254 B (0%)
build/block-library/index.js 139 kB -110 B (0%)
build/blocks/index.js 47.8 kB +131 B (0%)
build/components/index.js 200 kB -9 B (0%)
build/compose/index.js 9.68 kB +1 B
build/core-data/index.js 12.3 kB +21 B (0%)
build/data-controls/index.js 1.29 kB +2 B (0%)
build/data/index.js 8.54 kB -2 B (0%)
build/date/index.js 31.9 kB -2 B (0%)
build/dom/index.js 4.47 kB -2 B (0%)
build/edit-navigation/index.js 11.7 kB -4 B (0%)
build/edit-post/index.js 305 kB +1 B
build/edit-site/index.js 19.3 kB -16 B (0%)
build/edit-widgets/index.js 12.1 kB +7 B (0%)
build/editor/index.js 45.6 kB -5 B (0%)
build/element/index.js 4.65 kB +4 B (0%)
build/html-entities/index.js 621 B -1 B
build/i18n/index.js 3.56 kB -2 B (0%)
build/is-shallow-equal/index.js 710 B -1 B
build/keyboard-shortcuts/index.js 2.52 kB -1 B
build/keycodes/index.js 1.94 kB -2 B (0%)
build/list-reusable-blocks/index.js 3.12 kB -1 B
build/media-utils/index.js 5.32 kB +2 B (0%)
build/notices/index.js 1.79 kB +1 B
build/plugins/index.js 2.56 kB -1 B
build/redux-routine/index.js 2.85 kB -1 B
build/rich-text/index.js 13.9 kB -1 B
build/server-side-render/index.js 2.77 kB -2 B (0%)
build/url/index.js 4.06 kB +4 B (0%)
build/warning/index.js 1.14 kB +1 B
ℹ️ View Unchanged
Filename Size Change
build/api-fetch/index.js 3.41 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 953 B 0 B
build/block-directory/style.css 952 B 0 B
build/block-editor/style-rtl.css 11.1 kB 0 B
build/block-editor/style.css 11.1 kB 0 B
build/block-library/editor-rtl.css 8.68 kB 0 B
build/block-library/editor.css 8.68 kB 0 B
build/block-library/style-rtl.css 7.59 kB 0 B
build/block-library/style.css 7.58 kB 0 B
build/block-library/theme-rtl.css 754 B 0 B
build/block-library/theme.css 754 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/components/style-rtl.css 15.5 kB 0 B
build/components/style.css 15.5 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 568 B 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 6.26 kB 0 B
build/edit-post/style.css 6.25 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/edit-widgets/style-rtl.css 2.46 kB 0 B
build/edit-widgets/style.css 2.45 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/index.js 7.72 kB 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/list-reusable-blocks/style-rtl.css 476 B 0 B
build/list-reusable-blocks/style.css 476 B 0 B
build/nux/index.js 3.4 kB 0 B
build/nux/style-rtl.css 671 B 0 B
build/nux/style.css 668 B 0 B
build/primitives/index.js 1.41 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.27 kB 0 B
build/viewport/index.js 1.85 kB 0 B
build/wordcount/index.js 1.17 kB 0 B

compressed-size-action

settings.push( {
colorValue: getProperty( name, [ 'color', 'text' ] ),
colorValue: getProperty( name, STYLE_PROPERTY.color ),
Copy link
Member Author

Choose a reason for hiding this comment

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

Personally, this is the major advantage of centralization, and what pushed me to create this PR. Controls don't need to know what's the path of the property they want to access.

Copy link
Member

Choose a reason for hiding this comment

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

If the idea is to let control not need to know the shape of the property they want to access, I guess getProperty could receive the property instead of the path, and be responsible for resolving the property.

Of course, we may need to change something that does not contain a property e.g: a preset. So I guess in the future we may have a get and setter that receives the property directly and one that receives the paths.

But for now, what we have is ok.

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! You're so right. At 6b635ad and 7a44eaa I've renamed the getter & setter to getStyleProperty and setStyleProperty and they take the property name now. This clean-up makes everything much better :)

@oandregal oandregal self-assigned this Sep 9, 2020

export const LINK_COLOR = '--wp--style--color--link';

export const STYLE_PROPERTY = {
Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is a good place for this kind of info, as it speaks about how the block attributes are structured. However, I don't feel super strongly about this. If this is a blocker I'm fine moving it to any other place.

LINK_COLOR,
PADDING_SUPPORT_KEY,
STYLE_PROPERTY,
} from './constants';
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 concerned about this personally, it makes it an API and I don't really know whether these keys will remain as is, be combined... Why do we need this to be an API? (specially the _key ones)

Copy link
Member Author

Choose a reason for hiding this comment

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

mmm, my rationale was that the support keys are actually part of the API. This is why: if we had specific accessors (hasLineHeightSupport, hasFontSizeSupport, etc), consumers wouldn't need anything else to use them; however, by having general accessors (such as hasBlockSupport( feature )) we also need to provide the consumer with a list of the available keys they can use. So far, they inspect the block.json for this. I'd think providing constants for them would make it easier for us to modify their names later on if we need to. Said this, I realize we don't expose any other feature, so I won't hold this opinion too strongly and would be fine to move these back to block-editor, if that's preferred.

For the style keys, I agree with Jorge that we need a single source of truth, as they're both used in block-editor and edit-site. Exporting them here makes more sense from my POV.

Copy link
Contributor

Choose a reason for hiding this comment

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

in which situation we need to access these keys outside the hooks themselves which are part of block-editor package?

Copy link
Member Author

Choose a reason for hiding this comment

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

I reverted the support keys to block-editor. I still think that we should offer a feature list as I mentioned, but I realized that it doesn't seem a good idea to start with the experimental keys.

settings.push( {
colorValue: getProperty( name, [ 'color', 'text' ] ),
colorValue: getProperty( name, STYLE_PROPERTY.color ),
Copy link
Member

Choose a reason for hiding this comment

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

If the idea is to let control not need to know the shape of the property they want to access, I guess getProperty could receive the property instead of the path, and be responsible for resolving the property.

Of course, we may need to change something that does not contain a property e.g: a preset. So I guess in the future we may have a get and setter that receives the property directly and one that receives the paths.

But for now, what we have is ok.


export const LINK_COLOR = '--wp--style--color--link';

export const STYLE_PROPERTY = {
Copy link
Member

Choose a reason for hiding this comment

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

I guess we should prefix the constant with experimental.

@oandregal
Copy link
Member Author

@jorgefilipecosta @youknowriad thanks for the feedback! I'm reasonably happy with this clean-up. It helped to clarify the global styles provider API and consolidate the data structure we use in both places. 💪

getProperty: ( context, path ) =>
get( userStyles?.[ context ]?.styles, path ),
setProperty: ( context, path, newValue ) => {
getStyleProperty: ( context, propertyName ) =>
Copy link
Member

Choose a reason for hiding this comment

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

I think we probably also need the old setters, otherwise for example to configure presets using the UI how are we going to do it? We also need setters and getters in global styles that don't map to a CSS property.

Copy link
Member Author

@oandregal oandregal Sep 10, 2020

Choose a reason for hiding this comment

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

So far, I don't believe we need anything else. Note that with the old format you could only change properties within styles anyway.

My understanding is that we'll need to create new presets for users, but I'm not sure how that's going to work because we're going to need access to both theme and user presets at the same time (unlike the current styles or features where we merge both). Perhaps in that case we could have a getPreset( global, color, user )/setPreset( global, color, newValue )? I guess we can cross that bridge when we get to it.

Copy link
Member

Choose a reason for hiding this comment

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

Yes true we could only change style paths anyway. So I guess the change is positive :)

Copy link
Member

@jorgefilipecosta jorgefilipecosta left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@oandregal oandregal merged commit 75ab85b into master Sep 10, 2020
@oandregal oandregal deleted the update/style-mappings branch September 10, 2020 16:43
Copy link
Member

@aristath aristath left a comment

Choose a reason for hiding this comment

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

This is great... ❤️ LGTM.

@github-actions github-actions bot added this to the Gutenberg 9.0 milestone Sep 10, 2020
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.

Centralize style mappings declaration
4 participants