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

Enqueue core and theme colors by using separate structures per origin. #32358

Merged
merged 8 commits into from
Jun 10, 2021

Conversation

jorgefilipecosta
Copy link
Member

This PR refactors our approach to Enqueue core and theme colors, gradients, font sizes, and font families all the time to use a data structure where each origin is saved separately.

This PR essentially reverts what was done in #31669 and tries a different approach.

This approach uses a data structure where the merged color.palette instead of containing an array of colors contains an object with { core, theme, user } each property being an array of colors of that origin. The complexity we have is because theme.json, core-theme.json, and the user json file don't contain origin, there color.palette is an array of colors, which makes the shape resulting from the merge different from the shapes that originated it.

This change opens the path for a future where multiple palettes are shown e.g. useSetting( 'color.pallete' ) may return user, or theme, or core while useSetting( 'color.pallete.theme' ) returns a specific origin.

In terms of simplification, this PR is not probably a win, but it also did not make things more complex than what we had, and it opens the door for the future where multiple origins are shown (now we have the data structure for that).

I did not test everything yet, and this PR still needs some polishing, but the general idea is already implemented, so this PR is open for feedback.

cc: @nosolosw, @youknowriad

@jorgefilipecosta jorgefilipecosta added the Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json label May 31, 2021
@github-actions
Copy link

github-actions bot commented May 31, 2021

Size Change: +106 B (0%)

Total Size: 1.03 MB

Filename Size Change
build/block-editor/index.js 118 kB +10 B (0%)
build/edit-site/index.js 25.9 kB +96 B (0%)
ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.12 kB 0 B
build/annotations/index.js 2.93 kB 0 B
build/api-fetch/index.js 2.42 kB 0 B
build/autop/index.js 2.28 kB 0 B
build/blob/index.js 672 B 0 B
build/block-directory/index.js 6.61 kB 0 B
build/block-directory/style-rtl.css 989 B 0 B
build/block-directory/style.css 990 B 0 B
build/block-editor/style-rtl.css 13 kB 0 B
build/block-editor/style.css 13 kB 0 B
build/block-library/blocks/archives/editor-rtl.css 61 B 0 B
build/block-library/blocks/archives/editor.css 60 B 0 B
build/block-library/blocks/archives/style-rtl.css 65 B 0 B
build/block-library/blocks/archives/style.css 65 B 0 B
build/block-library/blocks/audio/editor-rtl.css 58 B 0 B
build/block-library/blocks/audio/editor.css 58 B 0 B
build/block-library/blocks/audio/style-rtl.css 112 B 0 B
build/block-library/blocks/audio/style.css 112 B 0 B
build/block-library/blocks/block/editor-rtl.css 161 B 0 B
build/block-library/blocks/block/editor.css 161 B 0 B
build/block-library/blocks/button/editor-rtl.css 475 B 0 B
build/block-library/blocks/button/editor.css 474 B 0 B
build/block-library/blocks/button/style-rtl.css 603 B 0 B
build/block-library/blocks/button/style.css 602 B 0 B
build/block-library/blocks/buttons/editor-rtl.css 315 B 0 B
build/block-library/blocks/buttons/editor.css 315 B 0 B
build/block-library/blocks/buttons/style-rtl.css 375 B 0 B
build/block-library/blocks/buttons/style.css 375 B 0 B
build/block-library/blocks/calendar/style-rtl.css 208 B 0 B
build/block-library/blocks/calendar/style.css 208 B 0 B
build/block-library/blocks/categories/editor-rtl.css 84 B 0 B
build/block-library/blocks/categories/editor.css 83 B 0 B
build/block-library/blocks/categories/style-rtl.css 79 B 0 B
build/block-library/blocks/categories/style.css 79 B 0 B
build/block-library/blocks/code/style-rtl.css 90 B 0 B
build/block-library/blocks/code/style.css 90 B 0 B
build/block-library/blocks/columns/editor-rtl.css 190 B 0 B
build/block-library/blocks/columns/editor.css 190 B 0 B
build/block-library/blocks/columns/style-rtl.css 422 B 0 B
build/block-library/blocks/columns/style.css 422 B 0 B
build/block-library/blocks/cover/editor-rtl.css 644 B 0 B
build/block-library/blocks/cover/editor.css 646 B 0 B
build/block-library/blocks/cover/style-rtl.css 1.22 kB 0 B
build/block-library/blocks/cover/style.css 1.23 kB 0 B
build/block-library/blocks/embed/editor-rtl.css 486 B 0 B
build/block-library/blocks/embed/editor.css 486 B 0 B
build/block-library/blocks/embed/style-rtl.css 401 B 0 B
build/block-library/blocks/embed/style.css 400 B 0 B
build/block-library/blocks/file/editor-rtl.css 301 B 0 B
build/block-library/blocks/file/editor.css 300 B 0 B
build/block-library/blocks/file/frontend.js 773 B 0 B
build/block-library/blocks/file/style-rtl.css 255 B 0 B
build/block-library/blocks/file/style.css 255 B 0 B
build/block-library/blocks/freeform/editor-rtl.css 2.44 kB 0 B
build/block-library/blocks/freeform/editor.css 2.44 kB 0 B
build/block-library/blocks/gallery/editor-rtl.css 704 B 0 B
build/block-library/blocks/gallery/editor.css 705 B 0 B
build/block-library/blocks/gallery/style-rtl.css 1.06 kB 0 B
build/block-library/blocks/gallery/style.css 1.06 kB 0 B
build/block-library/blocks/group/editor-rtl.css 160 B 0 B
build/block-library/blocks/group/editor.css 160 B 0 B
build/block-library/blocks/group/style-rtl.css 57 B 0 B
build/block-library/blocks/group/style.css 57 B 0 B
build/block-library/blocks/heading/editor-rtl.css 152 B 0 B
build/block-library/blocks/heading/editor.css 152 B 0 B
build/block-library/blocks/heading/style-rtl.css 76 B 0 B
build/block-library/blocks/heading/style.css 76 B 0 B
build/block-library/blocks/home-link/style-rtl.css 259 B 0 B
build/block-library/blocks/home-link/style.css 259 B 0 B
build/block-library/blocks/html/editor-rtl.css 281 B 0 B
build/block-library/blocks/html/editor.css 281 B 0 B
build/block-library/blocks/image/editor-rtl.css 729 B 0 B
build/block-library/blocks/image/editor.css 727 B 0 B
build/block-library/blocks/image/style-rtl.css 481 B 0 B
build/block-library/blocks/image/style.css 485 B 0 B
build/block-library/blocks/latest-comments/style-rtl.css 281 B 0 B
build/block-library/blocks/latest-comments/style.css 282 B 0 B
build/block-library/blocks/latest-posts/editor-rtl.css 137 B 0 B
build/block-library/blocks/latest-posts/editor.css 137 B 0 B
build/block-library/blocks/latest-posts/style-rtl.css 529 B 0 B
build/block-library/blocks/latest-posts/style.css 529 B 0 B
build/block-library/blocks/legacy-widget/editor-rtl.css 582 B 0 B
build/block-library/blocks/legacy-widget/editor.css 583 B 0 B
build/block-library/blocks/list/style-rtl.css 63 B 0 B
build/block-library/blocks/list/style.css 63 B 0 B
build/block-library/blocks/media-text/editor-rtl.css 176 B 0 B
build/block-library/blocks/media-text/editor.css 176 B 0 B
build/block-library/blocks/media-text/style-rtl.css 492 B 0 B
build/block-library/blocks/media-text/style.css 489 B 0 B
build/block-library/blocks/more/editor-rtl.css 434 B 0 B
build/block-library/blocks/more/editor.css 434 B 0 B
build/block-library/blocks/navigation-link/editor-rtl.css 633 B 0 B
build/block-library/blocks/navigation-link/editor.css 634 B 0 B
build/block-library/blocks/navigation-link/style-rtl.css 94 B 0 B
build/block-library/blocks/navigation-link/style.css 94 B 0 B
build/block-library/blocks/navigation/editor-rtl.css 1.55 kB 0 B
build/block-library/blocks/navigation/editor.css 1.55 kB 0 B
build/block-library/blocks/navigation/frontend.js 2.86 kB 0 B
build/block-library/blocks/navigation/style-rtl.css 1.63 kB 0 B
build/block-library/blocks/navigation/style.css 1.63 kB 0 B
build/block-library/blocks/nextpage/editor-rtl.css 395 B 0 B
build/block-library/blocks/nextpage/editor.css 395 B 0 B
build/block-library/blocks/page-list/editor-rtl.css 310 B 0 B
build/block-library/blocks/page-list/editor.css 311 B 0 B
build/block-library/blocks/page-list/style-rtl.css 240 B 0 B
build/block-library/blocks/page-list/style.css 240 B 0 B
build/block-library/blocks/paragraph/editor-rtl.css 157 B 0 B
build/block-library/blocks/paragraph/editor.css 157 B 0 B
build/block-library/blocks/paragraph/style-rtl.css 247 B 0 B
build/block-library/blocks/paragraph/style.css 248 B 0 B
build/block-library/blocks/post-author/editor-rtl.css 209 B 0 B
build/block-library/blocks/post-author/editor.css 209 B 0 B
build/block-library/blocks/post-author/style-rtl.css 183 B 0 B
build/block-library/blocks/post-author/style.css 184 B 0 B
build/block-library/blocks/post-comments-form/style-rtl.css 140 B 0 B
build/block-library/blocks/post-comments-form/style.css 140 B 0 B
build/block-library/blocks/post-comments/style-rtl.css 360 B 0 B
build/block-library/blocks/post-comments/style.css 359 B 0 B
build/block-library/blocks/post-content/editor-rtl.css 139 B 0 B
build/block-library/blocks/post-content/editor.css 139 B 0 B
build/block-library/blocks/post-excerpt/editor-rtl.css 73 B 0 B
build/block-library/blocks/post-excerpt/editor.css 73 B 0 B
build/block-library/blocks/post-excerpt/style-rtl.css 69 B 0 B
build/block-library/blocks/post-excerpt/style.css 69 B 0 B
build/block-library/blocks/post-featured-image/editor-rtl.css 338 B 0 B
build/block-library/blocks/post-featured-image/editor.css 338 B 0 B
build/block-library/blocks/post-featured-image/style-rtl.css 141 B 0 B
build/block-library/blocks/post-featured-image/style.css 141 B 0 B
build/block-library/blocks/post-template/editor-rtl.css 100 B 0 B
build/block-library/blocks/post-template/editor.css 99 B 0 B
build/block-library/blocks/post-template/style-rtl.css 379 B 0 B
build/block-library/blocks/post-template/style.css 380 B 0 B
build/block-library/blocks/post-title/style-rtl.css 60 B 0 B
build/block-library/blocks/post-title/style.css 60 B 0 B
build/block-library/blocks/preformatted/style-rtl.css 103 B 0 B
build/block-library/blocks/preformatted/style.css 103 B 0 B
build/block-library/blocks/pullquote/editor-rtl.css 183 B 0 B
build/block-library/blocks/pullquote/editor.css 183 B 0 B
build/block-library/blocks/pullquote/style-rtl.css 318 B 0 B
build/block-library/blocks/pullquote/style.css 318 B 0 B
build/block-library/blocks/query-pagination-numbers/editor-rtl.css 122 B 0 B
build/block-library/blocks/query-pagination-numbers/editor.css 121 B 0 B
build/block-library/blocks/query-pagination/editor-rtl.css 270 B 0 B
build/block-library/blocks/query-pagination/editor.css 262 B 0 B
build/block-library/blocks/query-pagination/style-rtl.css 168 B 0 B
build/block-library/blocks/query-pagination/style.css 168 B 0 B
build/block-library/blocks/query-title/editor-rtl.css 86 B 0 B
build/block-library/blocks/query-title/editor.css 86 B 0 B
build/block-library/blocks/query/editor-rtl.css 131 B 0 B
build/block-library/blocks/query/editor.css 132 B 0 B
build/block-library/blocks/quote/style-rtl.css 169 B 0 B
build/block-library/blocks/quote/style.css 169 B 0 B
build/block-library/blocks/rss/editor-rtl.css 201 B 0 B
build/block-library/blocks/rss/editor.css 202 B 0 B
build/block-library/blocks/rss/style-rtl.css 290 B 0 B
build/block-library/blocks/rss/style.css 290 B 0 B
build/block-library/blocks/search/editor-rtl.css 211 B 0 B
build/block-library/blocks/search/editor.css 211 B 0 B
build/block-library/blocks/search/style-rtl.css 359 B 0 B
build/block-library/blocks/search/style.css 362 B 0 B
build/block-library/blocks/separator/editor-rtl.css 99 B 0 B
build/block-library/blocks/separator/editor.css 99 B 0 B
build/block-library/blocks/separator/style-rtl.css 251 B 0 B
build/block-library/blocks/separator/style.css 251 B 0 B
build/block-library/blocks/shortcode/editor-rtl.css 512 B 0 B
build/block-library/blocks/shortcode/editor.css 512 B 0 B
build/block-library/blocks/site-logo/editor-rtl.css 440 B 0 B
build/block-library/blocks/site-logo/editor.css 441 B 0 B
build/block-library/blocks/site-logo/style-rtl.css 154 B 0 B
build/block-library/blocks/site-logo/style.css 154 B 0 B
build/block-library/blocks/social-link/editor-rtl.css 164 B 0 B
build/block-library/blocks/social-link/editor.css 165 B 0 B
build/block-library/blocks/social-links/editor-rtl.css 800 B 0 B
build/block-library/blocks/social-links/editor.css 799 B 0 B
build/block-library/blocks/social-links/style-rtl.css 1.32 kB 0 B
build/block-library/blocks/social-links/style.css 1.33 kB 0 B
build/block-library/blocks/spacer/editor-rtl.css 308 B 0 B
build/block-library/blocks/spacer/editor.css 308 B 0 B
build/block-library/blocks/spacer/style-rtl.css 48 B 0 B
build/block-library/blocks/spacer/style.css 48 B 0 B
build/block-library/blocks/table/editor-rtl.css 478 B 0 B
build/block-library/blocks/table/editor.css 478 B 0 B
build/block-library/blocks/table/style-rtl.css 480 B 0 B
build/block-library/blocks/table/style.css 480 B 0 B
build/block-library/blocks/tag-cloud/editor-rtl.css 118 B 0 B
build/block-library/blocks/tag-cloud/editor.css 118 B 0 B
build/block-library/blocks/tag-cloud/style-rtl.css 94 B 0 B
build/block-library/blocks/tag-cloud/style.css 94 B 0 B
build/block-library/blocks/template-part/editor-rtl.css 551 B 0 B
build/block-library/blocks/template-part/editor.css 550 B 0 B
build/block-library/blocks/term-description/editor-rtl.css 90 B 0 B
build/block-library/blocks/term-description/editor.css 90 B 0 B
build/block-library/blocks/text-columns/editor-rtl.css 95 B 0 B
build/block-library/blocks/text-columns/editor.css 95 B 0 B
build/block-library/blocks/text-columns/style-rtl.css 166 B 0 B
build/block-library/blocks/text-columns/style.css 166 B 0 B
build/block-library/blocks/verse/style-rtl.css 87 B 0 B
build/block-library/blocks/verse/style.css 87 B 0 B
build/block-library/blocks/video/editor-rtl.css 569 B 0 B
build/block-library/blocks/video/editor.css 570 B 0 B
build/block-library/blocks/video/style-rtl.css 173 B 0 B
build/block-library/blocks/video/style.css 173 B 0 B
build/block-library/common-rtl.css 1.26 kB 0 B
build/block-library/common.css 1.26 kB 0 B
build/block-library/editor-rtl.css 9.98 kB 0 B
build/block-library/editor.css 9.97 kB 0 B
build/block-library/index.js 148 kB 0 B
build/block-library/reset-rtl.css 506 B 0 B
build/block-library/reset.css 507 B 0 B
build/block-library/style-rtl.css 10.2 kB 0 B
build/block-library/style.css 10.2 kB 0 B
build/block-library/theme-rtl.css 692 B 0 B
build/block-library/theme.css 693 B 0 B
build/block-serialization-default-parser/index.js 1.3 kB 0 B
build/block-serialization-spec-parser/index.js 3.06 kB 0 B
build/blocks/index.js 47.2 kB 0 B
build/components/index.js 180 kB 0 B
build/components/style-rtl.css 16.2 kB 0 B
build/components/style.css 16.1 kB 0 B
build/compose/index.js 10 kB 0 B
build/core-data/index.js 12.1 kB 0 B
build/customize-widgets/index.js 9.96 kB 0 B
build/customize-widgets/style-rtl.css 1.45 kB 0 B
build/customize-widgets/style.css 1.44 kB 0 B
build/data-controls/index.js 828 B 0 B
build/data/index.js 7.22 kB 0 B
build/date/index.js 31.8 kB 0 B
build/deprecated/index.js 738 B 0 B
build/dom-ready/index.js 576 B 0 B
build/dom/index.js 4.62 kB 0 B
build/edit-navigation/index.js 14 kB 0 B
build/edit-navigation/style-rtl.css 3.08 kB 0 B
build/edit-navigation/style.css 3.08 kB 0 B
build/edit-post/classic-rtl.css 454 B 0 B
build/edit-post/classic.css 454 B 0 B
build/edit-post/index.js 58.5 kB 0 B
build/edit-post/style-rtl.css 6.92 kB 0 B
build/edit-post/style.css 6.91 kB 0 B
build/edit-site/style-rtl.css 4.75 kB 0 B
build/edit-site/style.css 4.75 kB 0 B
build/edit-widgets/index.js 15.7 kB 0 B
build/edit-widgets/style-rtl.css 3.42 kB 0 B
build/edit-widgets/style.css 3.42 kB 0 B
build/editor/index.js 38.2 kB 0 B
build/editor/style-rtl.css 3.91 kB 0 B
build/editor/style.css 3.9 kB 0 B
build/element/index.js 3.44 kB 0 B
build/escape-html/index.js 739 B 0 B
build/format-library/index.js 5.66 kB 0 B
build/format-library/style-rtl.css 637 B 0 B
build/format-library/style.css 639 B 0 B
build/hooks/index.js 1.76 kB 0 B
build/html-entities/index.js 628 B 0 B
build/i18n/index.js 3.73 kB 0 B
build/is-shallow-equal/index.js 709 B 0 B
build/keyboard-shortcuts/index.js 1.74 kB 0 B
build/keycodes/index.js 1.43 kB 0 B
build/list-reusable-blocks/index.js 2.07 kB 0 B
build/list-reusable-blocks/style-rtl.css 629 B 0 B
build/list-reusable-blocks/style.css 628 B 0 B
build/media-utils/index.js 3.08 kB 0 B
build/notices/index.js 1.07 kB 0 B
build/nux/index.js 2.31 kB 0 B
build/nux/style-rtl.css 718 B 0 B
build/nux/style.css 716 B 0 B
build/plugins/index.js 1.99 kB 0 B
build/primitives/index.js 1.03 kB 0 B
build/priority-queue/index.js 791 B 0 B
build/react-i18n/index.js 924 B 0 B
build/redux-routine/index.js 2.82 kB 0 B
build/reusable-blocks/index.js 2.56 kB 0 B
build/reusable-blocks/style-rtl.css 225 B 0 B
build/reusable-blocks/style.css 225 B 0 B
build/rich-text/index.js 10.6 kB 0 B
build/server-side-render/index.js 1.63 kB 0 B
build/shortcode/index.js 1.68 kB 0 B
build/token-list/index.js 847 B 0 B
build/url/index.js 1.95 kB 0 B
build/viewport/index.js 1.28 kB 0 B
build/warning/index.js 1.13 kB 0 B
build/widgets/index.js 1.67 kB 0 B
build/wordcount/index.js 1.24 kB 0 B

compressed-size-action

@youknowriad
Copy link
Contributor

Separate { core, theme, user }

Thanks for trying this PR @jorgefilipecosta

Where I think I have a different opinion than you is that I think from the block-editor package perspective, separating "core", "theme", "user" settings doesn't make sense, The block editor only has settings no matter their sources.

That said, I do think the "default color palette" or the "default presets" are just other settings. In other words:

  • For any random setting like "disableCustomColor" or "disableDropcap"... it doesn't make sense to split by source, it's just a regular setting,
  • For presets, we may need to separate "theme", "user" and "default" settings.

What do you think?

@jorgefilipecosta
Copy link
Member Author

jorgefilipecosta commented Jun 1, 2021

Hi @youknowriad,

For any random setting like "disableCustomColor" or "disableDropcap"... it doesn't make sense to split by source, it's just a regular setting,

Exactly that's what this PR does, for normal settings there is no source separation.

For presets, we may need to separate "theme", "user" and "default" settings.

That is also the approach this PR is doing with the exception we call "core" and not "default" but I will change.

I think this approach is doing what you are describing we just have a settings object and for presets that object contains subobjects each one defining the preset source.

So this PR contains a single settings object, every setting excluding the four presets continues exactly as is. For presets instead of color.palette being an array of colors it is an object where each property "core", "theme", and "user" is an array of colors coming from a given source.

Is there anything I'm missing that you think we should change?

@youknowriad
Copy link
Contributor

Actually, it sounds great :)

It's a breaking change in useSetting that said, that thing is not in Core yet, so we're good but we need to make sure this breaking change lands in 5.8 then :)

@youknowriad
Copy link
Contributor

If I'm not wrong, there's some code to be remove from useSetting (the one that targets the origin).

@jorgefilipecosta jorgefilipecosta force-pushed the update/way-presets-are-merged branch from 44690d5 to 447af09 Compare June 1, 2021 15:55
@jorgefilipecosta
Copy link
Member Author

It's a breaking change in useSetting that said, that thing is not in Core yet, so we're good but we need to make sure this breaking change lands in 5.8 then :)

Hi @youknowriad,

It may or may not be a breaking change on useSetting. My initial plan was to on useSetting('color.palette') return the "merged" palette as we do today (if there are user colors return user colors if not return theme colors if not return default colors.
Basically, on useSetting we would check for some paths e.g: color.palette and have a special logic.
useSetting('color.palette.theme') for example would return the theme colors.
If we do this we would not cause any breaking change. Another option that would be breaking change but does not require any logic would be to make useSetting('color.palette') return the object with all origins. If we do this and to avoid having merge logic on the components I think we would still need a way to return the "merged" colors independently of the origin.

Let me know what you prefer useSetting('color.palette') returns the "merged" palette as we do today or seSetting('color.palette') returns the object with all origins.

@youknowriad
Copy link
Contributor

If we do this we would not cause any breaking change. Another option that would be breaking change but does not require any logic would be to make useSetting('color.palette') return the object with all origins. If we do this and to avoid having merge logic on the components I think we would still need a way to return the "merged" colors independently of the origin.

This has my preference but I don't think we need a way to return the "merged" colors since in the UI these are going to be shown separately if I'm not wrong. I guess this means we'd probably need to do things like this in the color hooks:

const themeColors = useSetting('color.palette.theme')
const defaultColors = useSetting('color.palette.default')

const usedColors = themeColors || defaultColors;

@oandregal
Copy link
Member

👋 Want to make sure the direction here is clear to me. Would this summarize what this PR is trying to achieve?

  1. The theme.json data coming from the different origins stays the same. For example, themes still do:
{
  "version": 1,
  "settings": {
    "color": {
      "palette": [
        { "slug": "color-slug-1", "value": "color-value-2", "name": "color name 1" },
        { "slug": "color-slug-2", "value": "color-value-2", "name": "color name 2" },
      ]
    }
  }
}
  1. Internally, we hold the following structure instead (both in the client at the store core/block-editor.settings.__experimentalFeatures.color.palette and in the server within WP_Theme_JSON.$theme_json:
{
  "color": {
    "palette": {
      "core": [
        { "slug": "color-slug-1", "value": "color-value-2", "name": "color name 1" },
        { "slug": "color-slug-2", "value": "color-value-2", "name": "color name 2" },
      ],
      "theme": [
        { "slug": "color-slug-1", "value": "color-value-2", "name": "color name 1" },
        { "slug": "color-slug-2", "value": "color-value-2", "name": "color name 2" },
      ],
      "user": [
        { "slug": "color-slug-1", "value": "color-value-2", "name": "color name 1" },
        { "slug": "color-slug-2", "value": "color-value-2", "name": "color name 2" },
      ]
    }
  }
}
  1. The legacy settings at core/block-editor.settings.colors still hold only the colors in use by the UI controrls (either core or theme colors).

  2. We do this for all presets: color, gradients, duotone, font sizes, font families.

  3. We use useSetting( 'color.palette.origin' ) to get access to each origin independently (at the components that use this are responsible to merge the data themselves).

// doesn't contain spaces.
self::append_to_selector( $selector, '.has-' . preg_replace( '/\s+/', '-', $value['slug'] ) . '-' . $class['class_suffix'] ),
array(
$values_per_origin = _wp_array_get( $settings, $preset['path'], array() );
Copy link
Member

@oandregal oandregal Jun 2, 2021

Choose a reason for hiding this comment

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

If I understood properly this PR's direction, it seems that we'll always enqueue all the preset values (classes + custom properties) for every origin.

This makes me think of what happens when different origins have the same slug for the preset value. For example, if both core and a theme provides a color with yellow slug. Then, the user modifies the value of that yellow and we end up with:

body {
  --wp--preset--color--yellow: <core_value>;
  --wp--preset--color--yellow: <theme_value>;
  --wp--preset--color--yellow: <user_value>;
}

.has-yellow-color { <core_value> !important; };
.has-yellow-color { <theme_value> !important; };
.has-yellow-color { <user_value> !important; };

I'm wondering if a different implementation for compute_preset_classes and compute_preset_vars would make the code clearer and remove the duplicates. What if we:

  1. Reduced the settings.color.palette to a single object with slug and value. This step already takes care of the duplicates (the later origin to be processed wins). Something like:
{
  <slug-1>: <value-1>,
  <slug-2>: <value-2>,
  ...
}
  1. Maped this object to the functions's output (either classes or custom properties).

Copy link
Member

Choose a reason for hiding this comment

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

Actually. More thoughts for when two yellows are defined in different origins. Overwriting the previous origin works for now with the use case we have in mind: make those classes and custom properties available to patterns, independently from the theme in use.

However, when we want to show the colors in separate slots in the UI, how does that work if they have the same slug? Both when themes provide a color with the same slug than core's and when the user edits the value of a core or theme color. (Not that we need to implement it in this PR, but, at least, we should understand how feasible it is using this approach.)

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 applied your feedback! In the future, when implementing the UI, we may need to do something smart, and if the a color with the same slug existis in a higher priority origin, the UI stops showing the slug with lowest priority

Copy link
Member

Choose a reason for hiding this comment

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

A braindump of the options I see:

  1. If theme colors use an existing core slug, the core color is overwritten. The current proposal. Gives us a way to update core colors, we'd need something separate if we wanted to remove them (perhaps a coreColors: false or a list of allowed colors).
  2. If theme colors use an existing core slug, the theme color is removed. The way themes update/remove core colors is by updating a specific key in theme.json, such as coreColors: [ ... ].

Given that the reason we want core colors available is to have consistent CSS for use in patterns, removing them without alternative doesn't seem like something we need. So, it boils down to optimize for how we want updating to work. The option 2 is more intentional and straightforward (doesn't require we do anything smart for the UI).

If we went with option 2, we could do it in a follow up.

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 what we have right now is enough we can overwrite the previous color slugs. There is now a way to remove them and that's our objective, in previous versions of WordPress there is also no way to remove core color classes one can just overwrite, we are doing the same behavior.

isset( $colors_by_origin['theme'] ) ?
$colors_by_origin['theme'] :
$colors_by_origin['core']
);
unset( $settings['__experimentalFeatures']['color']['palette'] );
Copy link
Member

Choose a reason for hiding this comment

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

It seems we want this data in the client, to be accessed by useSetting( 'color.palette.origin' ), correct?

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 in the future we want it but for now, we can not add it. The way we are back-compatible with plugins changing the old color settings is by using unset( $settings['__experimentalFeatures']['color']['palette'] );. If we don't do this $settings['__experimentalFeatures']['color']['palette'] will take priority over the old settings and these plugins break. That was the reason we were already doing unset( $settings['__experimentalFeatures']['color']['palette'] ); and I don't think we can stop doing that here. I'm not sure how we are going to address this in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi @nosolosw I ended up removing these unsets I tested with the plugin Block Editor Colors and a theme without theme.json and the plugin still works as expected.

Copy link
Member

Choose a reason for hiding this comment

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

According to the direction #32358 (comment):

The legacy settings at core/block-editor.settings.colors still hold only the colors in use by the UI controrls (either core or theme colors).

In addition to no longer unset the settings.__experimentalFeatures.colors we still need to retrofit the values to the old ones. Otherwise, what happens is that settings.colors holds incorrect data. Steps to reproduce with colors (same for gradients and font sizes):

  • use WordPress 5.7, TT1-blocks, and activate this branch as the plugin
  • go and see the values in the core/block-editor store under settings.colors
  • the expected result was that it held the theme values but instead it had the default core colors

Copy link
Member Author

Choose a reason for hiding this comment

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

We are now retrofitting the values to the old ones.

@jorgefilipecosta
Copy link
Member Author

👋 Want to make sure the direction here is clear to me. Would this summarize what this PR is trying to achieve?

  1. The theme.json data coming from the different origins stays the same. For example, themes still do:
{
  "version": 1,
  "settings": {
    "color": {
      "palette": [
        { "slug": "color-slug-1", "value": "color-value-2", "name": "color name 1" },
        { "slug": "color-slug-2", "value": "color-value-2", "name": "color name 2" },
      ]
    }
  }
}
  1. Internally, we hold the following structure instead (both in the client at the store core/block-editor.settings.__experimentalFeatures.color.palette and in the server within WP_Theme_JSON.$theme_json:
{
  "color": {
    "palette": {
      "core": [
        { "slug": "color-slug-1", "value": "color-value-2", "name": "color name 1" },
        { "slug": "color-slug-2", "value": "color-value-2", "name": "color name 2" },
      ],
      "theme": [
        { "slug": "color-slug-1", "value": "color-value-2", "name": "color name 1" },
        { "slug": "color-slug-2", "value": "color-value-2", "name": "color name 2" },
      ],
      "user": [
        { "slug": "color-slug-1", "value": "color-value-2", "name": "color name 1" },
        { "slug": "color-slug-2", "value": "color-value-2", "name": "color name 2" },
      ]
    }
  }
}
  1. The legacy settings at core/block-editor.settings.colors still hold only the colors in use by the UI controrls (either core or theme colors).
  2. We do this for all presets: color, gradients, duotone, font sizes, font families.
  3. We use useSetting( 'color.palette.origin' ) to get access to each origin independently (at the components that use this are responsible to merge the data themselves).

Exactly that's the direction this PR is going.

@jorgefilipecosta
Copy link
Member Author

Hi @youknowriad I applied the logic we discussed. useSetting( color.palette' ) returns an object with all origins, useSetting( color.palette.theme' ) returns a specific origin. We have a util __experimentalGetHighestPriorityPreset that returns the higher priority origin to avoid repeating colorPalette.user ?? colorPalette.theme ?? colorPalette.core everywhere.

@@ -3,6 +3,16 @@
*/
import { SETTINGS_DEFAULTS } from '../store/defaults';

export function __experimentalGetHighestPriorityPreset(
Copy link
Member

@oandregal oandregal Jun 7, 2021

Choose a reason for hiding this comment

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

Can this be absorbed by the useSetting hook? I imagine this could work this way:

  • useSetting( 'color.palette' ): returns the consolidated / merged palette.
  • useSetting( 'color.palette.core' ): returns the core colors.
  • useSetting( 'color.palette.theme' ): returns the theme colors.
  • useSetting( 'color.palette.user' ): returs the user colors.

Similarly for the other presets.

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 suggested this option in #32358 (comment).
@youknowriad seemed to prefer to avoid this logic inside useSetting #32358 (comment).

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 hesitant :) it feels like we shouldn't allow color.palette entirely

Copy link
Member

Choose a reason for hiding this comment

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

Is your concern it won't serve any purpose with the upcoming UI changes? Mine is similar: introducing a temporary __experimentalGetHighestProiorityPreset that will be deprecated, with the additional burden it poses in the consumers (they need to know the inner details of how this works).

Thinking out loud: the settings.colors should still have the proper values for the UI #32358 (comment) so I wonder if a way to avoid introducing this temporary utility would be to access settings.colors directly (same for gradients and font sizes)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Is your concern it won't serve any purpose with the upcoming UI changes?

Yes, it's not entirely clear how the global color.palette is going to be used. __experimentalGetHighestProiorityPreset is experimental so it's fine but remains the question about color.palette

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you should trust your instinct here, I personally don't have the right answer :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thinking out loud: the settings.colors should still have the proper values for the UI #32358 (comment) so I wonder if a way to avoid introducing this temporary utility would be to access settings.colors directly (same for gradients and font sizes)?

using settings.colors is not an option because now settings depend on the block and the old settings structure does not allow that.

// either remove it and append the incoming,
// or update it with the incoming.
$to_append = array();
$to_append[] = array( 'color', 'duotone' );
Copy link
Member

Choose a reason for hiding this comment

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

We're missing duotone from the list.

Naming: with the new behavior for merge to_replace / to_append no longer make sense, we could look for better names to both these. Perhaps $properties and $propertis_per_origin?

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 don't think duotone should be part of this list. For the duotone case, we don't have classes or CSS variables it is a server-side dynamic mechanism.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, my point is that we're missing it: it's not in any list at the moment (either $properties or $to_append).

@@ -140,14 +140,7 @@ function_exists( 'gutenberg_is_edit_site_page' ) &&

// Copied from get_block_editor_settings() at wordpress-develop/block-editor.php.
$settings['__experimentalFeatures'] = $consolidated->get_settings();
if ( isset( $settings['__experimentalFeatures']['color']['palette'] ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

Leaving a comment here to point to this thread.

@@ -70,6 +70,11 @@ export const PRESET_METADATA = [
},
];

const presetPaths = {};
Copy link
Member

Choose a reason for hiding this comment

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

I don't see this in use anywhere?

@oandregal
Copy link
Member

Ok, I've reviewed this all.

We need a couple of fixes: the legacy settings (settings.colors, settings.fontSizes) don't have the proper values (thread), duotone is missing (thread). Otherwise, it works.

My main concern, though, is how consumers access the data (useSetting): I'm not stoked by this experience. I played a bit with the idea of a different data model in which we don't store the origins within the key (color.palette.core, color.palette.theme, etc) but have different keys instead (color.palette, color.paletteDefault) to see if things would improve for consumers. While it has some advantages -access to typography settings is improved because they don't have the same requirements than colors (font families aren't defined by core, sizes and families can't be added by the user, no plans to show them in the UI differently, etc)- it's not super great either. Happy to hear further thoughts on how to improve this.

@jorgefilipecosta jorgefilipecosta force-pushed the update/way-presets-are-merged branch from f094717 to 4bfddb3 Compare June 8, 2021 13:08
@jorgefilipecosta jorgefilipecosta force-pushed the update/way-presets-are-merged branch 4 times, most recently from b310de7 to d9bdb5b Compare June 8, 2021 20:52
@jorgefilipecosta jorgefilipecosta force-pushed the update/way-presets-are-merged branch from d9bdb5b to f5c1f87 Compare June 8, 2021 21:30
@jorgefilipecosta
Copy link
Member Author

The feedback on this PR was addressed. useSetting('color.palette') now returns the merged colors, useSetting('color.palette.theme') now returns theme colors.

Copy link
Member

@oandregal oandregal left a comment

Choose a reason for hiding this comment

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

I've tested a number of scenarios in the post & site editors using TwentyTwentyOne and TT1-blocks:

  • a theme that does not define any color palette => core colors are shown
  • a theme with an empty array as color palette => no colors are shown
  • a theme with a color palette => the theme colors are shown
  • add user colors via the site editor => the modifications are shown everywhere

I've also verified that the classes and custom properties for core are present in addition to theme CSS.

Everything is fine with 5.7. In WordPress 5.8 the list of colors shown include both core and theme, but this is expected because this reverts a change we landed in 5.8 and we still need to port this PR to core. Once we do, it'll work as 5.7.

@oandregal oandregal merged commit f3d6bbf into trunk Jun 10, 2021
@oandregal oandregal deleted the update/way-presets-are-merged branch June 10, 2021 09:40
@github-actions github-actions bot added this to the Gutenberg 10.9 milestone Jun 10, 2021
@youknowriad youknowriad added the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Jun 10, 2021
@oandregal
Copy link
Member

@youknowriad youknowriad removed the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Jun 14, 2021
pento pushed a commit to WordPress/wordpress-develop that referenced this pull request Jun 15, 2021
…ta 2.

This includes:

**Various**

 - Fix multi selection for nested blocks WordPress/gutenberg#32536
 - Consistently show the drop indicator while dragging blocks WordPress/gutenberg#31896
 - Fix horizontal drop indicator WordPress/gutenberg#32589
 - Fix Safari flickering issue WordPress/gutenberg#32581
 - Silence useSelect zombie bug errors WordPress/gutenberg#32088

**Template Editor**

 - Clarify the template creation modal WordPress/gutenberg#32427
 - Only add skip links for block templates WordPress/gutenberg#32451

**Widgets Editor**

 - Add block breadcrumb WordPress/gutenberg#32498 WordPress/gutenberg#32528 WordPress/gutenberg#32569
 - Saved deleted and restored widgets. WordPress/gutenberg#32534
 - Fix unsaved changes detection WordPress/gutenberg#32573
 - Fix button spacing in the header WordPress/gutenberg#32585
 - Avoid extra undo levels WordPress/gutenberg#32572
 - Move Legacy Widget block to the `@wordpress/widgets` package WordPress/gutenberg#32501
 - Fix Social Links color inheritance WordPress/gutenberg#32625
 - Use Button appender WordPress/gutenberg#32580

**Global Styles (theme.json)**
 
 - Separate the presets per origin in the block editor settings WordPress/gutenberg#32358 WordPress/gutenberg#32622
 - Use CSS Custom Properties for the preset styles WordPress/gutenberg#32627

**Performance**

 - Remove is-typing classname to improve typing performance WordPress/gutenberg#32567

Props nosolosw, jorgefilipecosta, aristath, ntsekouras, peterwilsoncc, mcsf.
See #53397.


git-svn-id: https://develop.svn.wordpress.org/trunk@51149 602fd350-edb4-49c9-b593-d223f7449a82
nylen pushed a commit to nylen/wordpress-develop-svn that referenced this pull request Jun 15, 2021
…ta 2.

This includes:

**Various**

 - Fix multi selection for nested blocks WordPress/gutenberg#32536
 - Consistently show the drop indicator while dragging blocks WordPress/gutenberg#31896
 - Fix horizontal drop indicator WordPress/gutenberg#32589
 - Fix Safari flickering issue WordPress/gutenberg#32581
 - Silence useSelect zombie bug errors WordPress/gutenberg#32088

**Template Editor**

 - Clarify the template creation modal WordPress/gutenberg#32427
 - Only add skip links for block templates WordPress/gutenberg#32451

**Widgets Editor**

 - Add block breadcrumb WordPress/gutenberg#32498 WordPress/gutenberg#32528 WordPress/gutenberg#32569
 - Saved deleted and restored widgets. WordPress/gutenberg#32534
 - Fix unsaved changes detection WordPress/gutenberg#32573
 - Fix button spacing in the header WordPress/gutenberg#32585
 - Avoid extra undo levels WordPress/gutenberg#32572
 - Move Legacy Widget block to the `@wordpress/widgets` package WordPress/gutenberg#32501
 - Fix Social Links color inheritance WordPress/gutenberg#32625
 - Use Button appender WordPress/gutenberg#32580

**Global Styles (theme.json)**
 
 - Separate the presets per origin in the block editor settings WordPress/gutenberg#32358 WordPress/gutenberg#32622
 - Use CSS Custom Properties for the preset styles WordPress/gutenberg#32627

**Performance**

 - Remove is-typing classname to improve typing performance WordPress/gutenberg#32567

Props nosolosw, jorgefilipecosta, aristath, ntsekouras, peterwilsoncc, mcsf.
See #53397.


git-svn-id: https://develop.svn.wordpress.org/trunk@51149 602fd350-edb4-49c9-b593-d223f7449a82
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Jun 15, 2021
…ta 2.

This includes:

**Various**

 - Fix multi selection for nested blocks WordPress/gutenberg#32536
 - Consistently show the drop indicator while dragging blocks WordPress/gutenberg#31896
 - Fix horizontal drop indicator WordPress/gutenberg#32589
 - Fix Safari flickering issue WordPress/gutenberg#32581
 - Silence useSelect zombie bug errors WordPress/gutenberg#32088

**Template Editor**

 - Clarify the template creation modal WordPress/gutenberg#32427
 - Only add skip links for block templates WordPress/gutenberg#32451

**Widgets Editor**

 - Add block breadcrumb WordPress/gutenberg#32498 WordPress/gutenberg#32528 WordPress/gutenberg#32569
 - Saved deleted and restored widgets. WordPress/gutenberg#32534
 - Fix unsaved changes detection WordPress/gutenberg#32573
 - Fix button spacing in the header WordPress/gutenberg#32585
 - Avoid extra undo levels WordPress/gutenberg#32572
 - Move Legacy Widget block to the `@wordpress/widgets` package WordPress/gutenberg#32501
 - Fix Social Links color inheritance WordPress/gutenberg#32625
 - Use Button appender WordPress/gutenberg#32580

**Global Styles (theme.json)**
 
 - Separate the presets per origin in the block editor settings WordPress/gutenberg#32358 WordPress/gutenberg#32622
 - Use CSS Custom Properties for the preset styles WordPress/gutenberg#32627

**Performance**

 - Remove is-typing classname to improve typing performance WordPress/gutenberg#32567

Props nosolosw, jorgefilipecosta, aristath, ntsekouras, peterwilsoncc, mcsf.
See #53397.

Built from https://develop.svn.wordpress.org/trunk@51149


git-svn-id: http://core.svn.wordpress.org/trunk@50758 1a063a9b-81f0-0310-95a4-ce76da25c4cd
gMagicScott pushed a commit to gMagicScott/core.wordpress-mirror that referenced this pull request Jun 15, 2021
…ta 2.

This includes:

**Various**

 - Fix multi selection for nested blocks WordPress/gutenberg#32536
 - Consistently show the drop indicator while dragging blocks WordPress/gutenberg#31896
 - Fix horizontal drop indicator WordPress/gutenberg#32589
 - Fix Safari flickering issue WordPress/gutenberg#32581
 - Silence useSelect zombie bug errors WordPress/gutenberg#32088

**Template Editor**

 - Clarify the template creation modal WordPress/gutenberg#32427
 - Only add skip links for block templates WordPress/gutenberg#32451

**Widgets Editor**

 - Add block breadcrumb WordPress/gutenberg#32498 WordPress/gutenberg#32528 WordPress/gutenberg#32569
 - Saved deleted and restored widgets. WordPress/gutenberg#32534
 - Fix unsaved changes detection WordPress/gutenberg#32573
 - Fix button spacing in the header WordPress/gutenberg#32585
 - Avoid extra undo levels WordPress/gutenberg#32572
 - Move Legacy Widget block to the `@wordpress/widgets` package WordPress/gutenberg#32501
 - Fix Social Links color inheritance WordPress/gutenberg#32625
 - Use Button appender WordPress/gutenberg#32580

**Global Styles (theme.json)**
 
 - Separate the presets per origin in the block editor settings WordPress/gutenberg#32358 WordPress/gutenberg#32622
 - Use CSS Custom Properties for the preset styles WordPress/gutenberg#32627

**Performance**

 - Remove is-typing classname to improve typing performance WordPress/gutenberg#32567

Props nosolosw, jorgefilipecosta, aristath, ntsekouras, peterwilsoncc, mcsf.
See #53397.

Built from https://develop.svn.wordpress.org/trunk@51149


git-svn-id: https://core.svn.wordpress.org/trunk@50758 1a063a9b-81f0-0310-95a4-ce76da25c4cd
F-Wilke pushed a commit to FiliagoDev/WordPress that referenced this pull request Jul 31, 2021
…ta 2.

This includes:

**Various**

 - Fix multi selection for nested blocks WordPress/gutenberg#32536
 - Consistently show the drop indicator while dragging blocks WordPress/gutenberg#31896
 - Fix horizontal drop indicator WordPress/gutenberg#32589
 - Fix Safari flickering issue WordPress/gutenberg#32581
 - Silence useSelect zombie bug errors WordPress/gutenberg#32088

**Template Editor**

 - Clarify the template creation modal WordPress/gutenberg#32427
 - Only add skip links for block templates WordPress/gutenberg#32451

**Widgets Editor**

 - Add block breadcrumb WordPress/gutenberg#32498 WordPress/gutenberg#32528 WordPress/gutenberg#32569
 - Saved deleted and restored widgets. WordPress/gutenberg#32534
 - Fix unsaved changes detection WordPress/gutenberg#32573
 - Fix button spacing in the header WordPress/gutenberg#32585
 - Avoid extra undo levels WordPress/gutenberg#32572
 - Move Legacy Widget block to the `@wordpress/widgets` package WordPress/gutenberg#32501
 - Fix Social Links color inheritance WordPress/gutenberg#32625
 - Use Button appender WordPress/gutenberg#32580

**Global Styles (theme.json)**
 
 - Separate the presets per origin in the block editor settings WordPress/gutenberg#32358 WordPress/gutenberg#32622
 - Use CSS Custom Properties for the preset styles WordPress/gutenberg#32627

**Performance**

 - Remove is-typing classname to improve typing performance WordPress/gutenberg#32567

Props nosolosw, jorgefilipecosta, aristath, ntsekouras, peterwilsoncc, mcsf.
See #53397.

Built from https://develop.svn.wordpress.org/trunk@51149


git-svn-id: http://core.svn.wordpress.org/trunk@50758 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants