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

Make core colors classes and custom properties always available #31669

Merged
merged 10 commits into from
May 20, 2021

Conversation

oandregal
Copy link
Member

@oandregal oandregal commented May 10, 2021

Implements first iteration of #29568

This PR makes it so the CSS styles (classes and custom properties) related to the core palette and gradients are always enqueued.

At the UI level, it doesn't change anything, and only the theme colors are shown.

Changes in this PR

  • It updates the merge algorithm of theme.json so that the color palette and the gradients are not merged. Instead, colors are appended.
  • It updates the useSetting hook to filter colors based on a key. If the color has the key origin: "core" it'll be filtered out, hence not used in the UI controls. Alternatives I've considered for this key are __experimentalNotUI. However, given that the final goal is to show them all at once, the origin key seems more future-proof.

What happens if a theme uses the same slug as core

If a theme provides the same slug as a core color uses (for example, both core and theme declare black) the theme's color classes and CSS Custom Properties are enqueued after the core's, so the theme's styles will override core's.

At the UI level, there's no change: only the theme colors are shown.

How to test

Front-end:

  • Using the TT1-blocks theme, visit the front-end. Verify that the #global-styles-inline-css inline stylesheet has the corresponding CSS Custom Properties and classes for both core and theme colors.

Post editor:

  • Using the TT1-blocks theme, visit the post editor.
  • Add a block with support for colors. Verify the UI color control only contains the theme colors.
  • Check that the CSS Custom Properties and classes for core and theme colors are present in the inline stylesheet. Tip: search for --wp--preset--color--purple and .has-purple-color to find the stylesheets.

Site editor:

  • Using the TT1-blocks theme, visit the site editor.
  • Add a block with support for colors. Verify the UI color control only contains the theme colors.
  • Go to the global styles sidebar. Verify the UI color control only contains the theme colors.
  • Check that the CSS Custom Properties and classes for core and theme colors are present in the inline stylesheet. Tip: search for --wp--preset--color--purple and .has-purple-color to find the stylesheets.

@oandregal oandregal requested a review from spacedmonkey as a code owner May 10, 2021 16:47
@oandregal oandregal self-assigned this May 10, 2021
@oandregal oandregal added the Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json label May 10, 2021
@oandregal
Copy link
Member Author

oandregal commented May 10, 2021

We can also make it so the core colors are not shown in the UI.

Regarding both core and theme declaring the same color slug: what do we think of this? We can consolidate both into one (either keeping the name / value of core or theme), etc.

Wanted to confirm direction before doing more work.

@github-actions
Copy link

github-actions bot commented May 10, 2021

Size Change: +236 B (0%)

Total Size: 1.62 MB

Filename Size Change
build/block-editor/index.js 119 kB +48 B (0%)
build/edit-site/index.js 25.8 kB +188 B (+1%)
ℹ️ 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 673 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 12.9 kB 0 B
build/block-editor/style.css 12.9 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/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 771 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 129 B 0 B
build/block-library/blocks/heading/editor.css 129 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 717 B 0 B
build/block-library/blocks/image/editor.css 716 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 523 B 0 B
build/block-library/blocks/latest-posts/style.css 522 B 0 B
build/block-library/blocks/legacy-widget/editor-rtl.css 557 B 0 B
build/block-library/blocks/legacy-widget/editor.css 557 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.54 kB 0 B
build/block-library/blocks/navigation/editor.css 1.54 kB 0 B
build/block-library/blocks/navigation/style-rtl.css 1.78 kB 0 B
build/block-library/blocks/navigation/style.css 1.78 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 233 B 0 B
build/block-library/blocks/page-list/style.css 233 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 119 B 0 B
build/block-library/blocks/post-featured-image/style.css 119 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-loop/editor-rtl.css 98 B 0 B
build/block-library/blocks/query-loop/editor.css 97 B 0 B
build/block-library/blocks/query-loop/style-rtl.css 315 B 0 B
build/block-library/blocks/query-loop/style.css 317 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 189 B 0 B
build/block-library/blocks/search/editor.css 189 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.93 kB 0 B
build/block-library/editor.css 9.91 kB 0 B
build/block-library/index.js 147 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.3 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.29 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 188 kB 0 B
build/components/style-rtl.css 16.2 kB 0 B
build/components/style.css 16.2 kB 0 B
build/compose/index.js 9.95 kB 0 B
build/core-data/index.js 12.1 kB 0 B
build/customize-widgets/index.js 43.2 kB 0 B
build/customize-widgets/style-rtl.css 1.38 kB 0 B
build/customize-widgets/style.css 1.38 kB 0 B
build/data-controls/index.js 829 B 0 B
build/data/index.js 7.23 kB 0 B
build/date/index.js 31.8 kB 0 B
build/deprecated/index.js 739 B 0 B
build/dom-ready/index.js 577 B 0 B
build/dom/index.js 4.62 kB 0 B
build/edit-navigation/index.js 13.9 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 335 kB 0 B
build/edit-post/style-rtl.css 6.8 kB 0 B
build/edit-post/style.css 6.8 kB 0 B
build/edit-site/style-rtl.css 4.76 kB 0 B
build/edit-site/style.css 4.75 kB 0 B
build/edit-widgets/index.js 292 kB 0 B
build/edit-widgets/style-rtl.css 3.46 kB 0 B
build/edit-widgets/style.css 3.47 kB 0 B
build/editor/index.js 38.4 kB 0 B
build/editor/style-rtl.css 3.92 kB 0 B
build/editor/style.css 3.91 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 627 B 0 B
build/i18n/index.js 3.73 kB 0 B
build/is-shallow-equal/index.js 710 B 0 B
build/keyboard-shortcuts/index.js 1.65 kB 0 B
build/keycodes/index.js 1.43 kB 0 B
build/list-reusable-blocks/index.js 2.06 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/navigation/index.js 2.85 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 923 B 0 B
build/redux-routine/index.js 2.82 kB 0 B
build/reusable-blocks/index.js 2.54 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.7 kB 0 B
build/server-side-render/index.js 1.64 kB 0 B
build/shortcode/index.js 1.68 kB 0 B
build/token-list/index.js 846 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.66 kB 0 B
build/wordcount/index.js 1.24 kB 0 B

compressed-size-action

@jasmussen
Copy link
Contributor

Love it. I think there's some design work in #27473 (comment) by @pablohoneyhoney that I think we can leverage here (see the theme/default/custom bits in the first screenshot). Or if not here, then in a followup PR.

@oandregal
Copy link
Member Author

Added a before / after screenshot for the UI color panel, to have more context on the impact of this PR.

I'm not sure we'll have time to update how the UI control works for 5.8 so I'm looking for low-hanging fruit that can be shipped. Is this, as it is, good for 5.8? An evolution of this PR I can think of include: do not show the core colors in the UI controls (they work as today: only show theme colors) but do enqueue core color classes and CSS Custom Properties. What do we think of both approaches?

@jasmussen
Copy link
Contributor

I'm not sure we'll have time to update how the UI control works for 5.8 so I'm looking for low-hanging fruit that can be shipped. Is this, as it is, good for 5.8?

How does this affect the basic block editor interface, say this paragraph color panel:

Screenshot 2021-05-11 at 10 00 20

If it does affect that, it's a bit more challenging as it adds a ton of colors to a UI that already has low compression, in which case I'd personally prefer the enqueuing of classes even if not surfacing of UI would be the preferable low hanging fruit solution.

If it's mostly a global styles change, it's probably fine?

Key to both is that we still consider and steer towards the design shown at the link, meaning we should avoid going too far down a path that takes us away from it.

@MaggieCabrera
Copy link
Contributor

I just checked this on TT1 and emptytheme and I found out that the post editor is no longer showing colors for blocks. In the screenshot for the button block:

Screenshot 2021-05-11 at 10 05 20

It shows the correct colors when I switch back to trunk.

I was trying to find out what happens if the theme defines a color that already exists on the default palette (such as black and white) and if we should show the duplicates or not.

@oandregal
Copy link
Member Author

How does this affect the basic block editor interface, say this paragraph color panel:

Exactly, this change makes all colors show up in all the places we use the colors UI control: the block sidebar (both in post editor and site editor) as well as global styles sidebar (site editor).

I was trying to find out what happens if the theme defines a color that already exists on the default palette (such as black and white) and if we should show the duplicates or not.

Themes are not supposed to update core colors. However, if they provide the same slug as a core color (for example, both core and theme declare 'black') this is what happens with this PR:

  • both colors will be shown in the UI
  • in the stylesheet, the theme's color classes and CSS Custom Properties are enqueued after the core's, so the theme's styles will override core's.

@jasmussen
Copy link
Contributor

I do worry about all colors showing up in all places with the current color picker UI we have. It just isn't good enough, we need something that doesn't explored in length like this does.

It seems if we want a low hanging fruit option it would seem best to simply ship the CSS classes for now. Otherwise we need some sort of palette picker or collapsing interface to handle it.

@oandregal
Copy link
Member Author

oandregal commented May 11, 2021

If it does affect that, it's a bit more challenging as it adds a ton of colors to a UI that already has low compression, in which case I'd personally prefer the enqueuing of classes even if not surfacing of UI would be the preferable low hanging fruit solution.

Nice. So my question would be: what is the expected result when some theme color slug is the same as the core's (see issue description)? Let's go with the example Maggie shared: both core and theme palettes provide a color with slug black. For the system, the only important bit is the slug, as that is what is used to generate the classes and Custom Properties. This is the result of processing colors:

body {
  --wp--preset--color--black: <value>;
}

.has-black-color { color: <value> !important; }
.has-black-background-color { color: <value> !important; }
.has-black-border-color { color: <value> !important; }

What <value> should be in that case? The one provided by core or the one provided by the theme?

@MaggieCabrera
Copy link
Contributor

What happens if black doesn't mean the same for the theme and the default palette? A theme may decide that black means #2d2d2d. Also, is the screenshot I shared on my comment before a bug? cause it only happens with this PR applied

@oandregal
Copy link
Member Author

What happens if black doesn't mean the same for the theme and the default palette? A theme may decide that black means #2d2d2d.

The only bit the system uses to care about the "semantics" of the color is the slug. That's why it's problematic that a theme can provide the same color slug.

With the current approach, the theme can re-write the meaning of black by providing a color with that same slug but a different value, such as #2d2d2d. Is this a good idea? Do we want this or do we want to keep the core color value?

Also, is the screenshot I shared on my comment before a bug? cause it only happens with this PR applied

I can't repro 😢 It works for me fine with emptytheme, TT1-blocks, TT1.

@MaggieCabrera
Copy link
Contributor

I can't repro 😢 It works for me fine with emptytheme, TT1-blocks, TT1.

Looks like it only happens for the button block weirdly enough. The cover block, paragraph, pullquote... were showing the correct colors but not the buttons.

@MaggieCabrera
Copy link
Contributor

With the current approach, the theme can re-write the meaning of black by providing a color with that same slug but a different value, such as #2d2d2d. Is this a good idea? Do we want this or do we want to keep the core color value?

I had to handle a similar case while working with the color annotation plugin and what we did was we checked if the value of the colors were the same, and if they were different we'd have them both, but we did rename them with a "custom" label. I wonder if it makes sense to have .has-black-color and .has-black-color-themeslug in such cases or if that's not desirable at all.

@jasmussen
Copy link
Contributor

jasmussen commented May 11, 2021

That's an excellent question to discuss and an important one to solve. Here are some paths forward with pros and cons:

Core colors override theme colors

  • Black is core black and predictably so, themes and patterns can be designed towards those.
  • Themes will likely learn of this structure and either retire or rename their overlapping colors, such as "theme-black".
  • A theme can't affect/enhance default patterns, as colors that might be used there are off limits.
  • Palettes that surface both core and theme colors with the same slug will be broken.

Theme colors override core colors

  • Themes can intentionally override core colors, thereby allowing a fuller integration with patterns.
  • Colors you perhaps did not expect to change when you switch themes, might change.
  • Palettes that surface both core and theme colors with the same slug will be broken.

Core and theme colors coexist

  • Palettes won't be broken if the same slug is used.

Of the above three, while I appreciate that themes can integrate with patterns that leverage core colors, I can't help but feel like that is a bit of a hack. Perhaps we should reserve the slug of core colors and disallow themes using them? If we refine, polish and expand the core palette to compensate, that might still allow a wide range of expression.

I keep coming back to the challenge outlined in this PR #30831 — how if you switch themes, a theme color you defined will go missing and has-background becomes meaningless. Is there an additional path, kind of like a CSS font-stack where a theme can both extend a core color (overwrite it through a blessed API), but also provide a fallback for when switched away from? I.e. (pseudo code) color: theme-black, core-black;?

@oandregal
Copy link
Member Author

Is there an additional path, kind of like a CSS font-stack where a theme can both extend a core color (overwrite it through a blessed API), but also provide a fallback for when switched away from? I.e. (pseudo code) color: theme-black, core-black;?

theme.json 🙂

If we go with the "theme color values override core color values" that's what we have. We do that dynamically with theme.json only enqueuing the necessary bytes. Example:

  • user activates a theme that overrides the black color defined by core to #2D2D2D, we output:
body {
  --wp--preset--color--black: #2D2D2D;
}

.has-black-color { color: #2D2D2D !important; }
.has-black-background-color { color: #2D2D2D !important; }
.has-black-border-color { color: #2D2D2D !important; }
  • user switches to a them that doesn't override the core value of #000000, the output is:
body {
  --wp--preset--color--black: #000000;
}

.has-black-color { color: #000000 !important; }
.has-black-background-color { color: #000000 !important; }
.has-black-border-color { color: #000000 !important; }

So that's backed in this proposal.

However, a different issue this PR doesn't touch is what happens when the color is use is a color theme, such as primary-velvet-underground, and so switching themes you'll run into the situation described at #30831 This PR doesn't address that issue directly, but, hopefully, by exposing core colors we'll see more themes to adopt them, hence that issue would be less prominent.

@oandregal
Copy link
Member Author

I wonder if it makes sense to have .has-black-color and .has-black-color-themeslug in such cases or if that's not desirable at all.

The issue with that approach is that classes are attached to the post content when the user selects a color from the preset. We can't rewrite existing posts, so, if the user did something that attached the class .has-black-color-themeslug to a particular post and then switches to a theme that doesn't override the black color provided by the theme, the .has-black-color-themeslug class is no longer present. The new theme doesn't have it. It's the same issue as having a theme color with slug arrival-grey, see #30831

@carolinan
Copy link
Contributor

carolinan commented May 12, 2021

I tested the PR with Twenty Twenty-One and was able to reproduce the problem with the missing palette for the buttons.

Twenty Twenty-One registers the slug "black", and when black is selected in the editor, both colors have checkmarks.
For some reason, this only happens with black, not white.

I see the following JS warning:

react-dom.development.js:82 Warning: 
Encountered two children with the same key, `#000000`. 
Keys should be unique so that components maintain their identity across updates. 
Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.

Windows 10, Chrome Version 90.0.4430.212, WP 5.7.1.

@oandregal oandregal mentioned this pull request May 12, 2021
82 tasks
@oandregal
Copy link
Member Author

Given the feedback here, I believe this needs to be reworked to not show the core colors in the UI control.

There's the issue of what happens when the theme provides a color with the same slug as core's. Given that core colors will no be shown in the UI, I presume the behavior that makes more sense is that the theme's color overrides core's.

@oandregal oandregal force-pushed the try/colors-are-not-merged branch from b6ea487 to a03cfc2 Compare May 14, 2021 15:37
@oandregal oandregal requested a review from ellatrix as a code owner May 14, 2021 15:37
@oandregal oandregal force-pushed the try/colors-are-not-merged branch from dc66b85 to 594e6c5 Compare May 18, 2021 14:37
@@ -6,124 +6,148 @@
{
"name": "Black",
"slug": "black",
"color": "#000000"
"color": "#000000",
"origin": "core"
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 redundant to have "origin": "core", in all of the colors. Here they are defined in the core theme.json so our code should automatically assume all these colors of core 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.

Yeah, thought about that. These are the trade-offs I've considered:

  1. We only write this once and themes don't have to, it's something internal to core so it's transparent to the consumers of the API.
  2. If our code needs to augment that data, that needs to run for every request and makes the logic more complex.
  3. We don't know how the UI control that uses this data is going to work.

With these trade-offs, I leaned towards implementing the simplest thing that works and that can scale as our needs evolve.


Alternatives I've considered:

  • What if core colors (or colors not to show in the UI) were stored in a different part? Too complex to introduce at the stage we're in. We need to account for every level (top-level, blocks) and have access to both colors in the client for the case the theme doesn't provide any color.

  • What if we add a showInUi flag instead of origin? Will work the same, not super-opinionated. Although I think origin is more future proof.

  • What if we add the origin to all the colors? It's data we pass to the client that we don't need at the moment.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we expect others to use this property? Something more descriptive like an array of which contexts to display the colors in UI may make sense.

My intuition would lean toward removing this property in the definition especially if we're unsure of usages by other consumers. Folks will copy paste examples, so I'd be a little careful here. We could do something like adding it in the in-memory representation or alternatively picking a different data structure like "core": { "settings": { "color": "pallete" : {. (I don't know this problem space very well, so just my quick takes, it may not make sense for this problem.)

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've already shared my rationale about why I'd think going forward with the simplest approach that works is fine. However, one thing I didn't consider was the copy/paste rationale. That's a good point and makes me having second thoughts about this, to be honest.

// Some others should be appended to the existing.
$to_append = array();
$to_append[] = array( 'color', 'palette' );
$to_append[] = array( 'color', 'gradients' );
Copy link
Member

Choose a reason for hiding this comment

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

Already existing issue: It seems we need to include array( 'color', 'duotone' ) in either
to_append or to_replace.

Copy link
Member Author

Choose a reason for hiding this comment

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

Did the same as the other color palettes: core is always enqueued and the theme is appended.

packages/block-editor/src/components/use-setting/index.js Outdated Show resolved Hide resolved
packages/block-editor/src/components/use-setting/index.js Outdated Show resolved Hide resolved
@oandregal oandregal force-pushed the try/colors-are-not-merged branch from 49dd85d to 172af8d Compare May 19, 2021 08:19
@youknowriad youknowriad added this to the Gutenberg 10.7 milestone May 19, 2021
@oandregal oandregal force-pushed the try/colors-are-not-merged branch from 172af8d to b7050eb Compare May 19, 2021 10:14
@@ -103,13 +103,28 @@ function getPresetMetadataFromStyleProperty( styleProperty ) {
export const LINK_COLOR = '--wp--style--color--link';
export const LINK_COLOR_DECLARATION = `a { color: var(${ LINK_COLOR }, #00e); }`;

const filterColorsFromCoreOrigin = ( path, setting ) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Will this get out of sync with the implementation in block editor?

Copy link
Contributor

Choose a reason for hiding this comment

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

I feel ultimately here, we'd need a way to get both

useSetting( 'color.palette' ) // return theme colors
useSetting( 'color.defaultPalette' ) // return default palette 

and same for other settings. It's not important now, but we should make sure it's something we can achieve later.

array(
'slug' => 'green',
'color' => 'green',
),
Copy link
Contributor

@gwwar gwwar May 19, 2021

Choose a reason for hiding this comment

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

Non-blocking, but it looks like we caught several cases with manual testing in the refactor rather than with tests. With that in mind are there any test cases that make sense to add?

I also wanted to note that as a reader with little context on the theme/color system, I don't know what to expect in behavior from a test case named "test_merge_incoming_data"

@@ -6,124 +6,148 @@
{
"name": "Black",
"slug": "black",
"color": "#000000"
"color": "#000000",
"origin": "core"
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we expect others to use this property? Something more descriptive like an array of which contexts to display the colors in UI may make sense.

My intuition would lean toward removing this property in the definition especially if we're unsure of usages by other consumers. Folks will copy paste examples, so I'd be a little careful here. We could do something like adding it in the in-memory representation or alternatively picking a different data structure like "core": { "settings": { "color": "pallete" : {. (I don't know this problem space very well, so just my quick takes, it may not make sense for this problem.)

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.

I guess we can merge so we include this feature this Gutenberg release, we can include the edge fixes after the release is done and cherry pick them.

@oandregal oandregal force-pushed the try/colors-are-not-merged branch from b7050eb to 81df19e Compare May 20, 2021 07:42
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.

7 participants