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

Style Engine: add typography and color to frontend #40665

Merged
merged 22 commits into from
May 18, 2022

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented Apr 28, 2022

What?

Experimental PR to see if and how we can port backend features added in #40332 to the frontend in relation to color and typography

Why?

To unify the frontend and backend.

It might not work out since styles and classnames aren't added in the same callback 🤷

How?

Adding color and typography CSS rules to the existing ruleset (spacing).

The addition of a new method getClassnames to generate block classnames based on preset attribute values passed to the style engine something similar to var:preset|color|burnedToast

Testing Instructions

Insert a group block into the post editor and tweak the typography and color styles.

Here's some test HTML

Block code
<!-- wp:group {"style":{"color":{"text":"#dd1d1d"},"typography":{"fontSize":"2.08rem","fontStyle":"normal","fontWeight":"500","textTransform":"uppercase","lineHeight":2,"letterSpacing":"23px"},"spacing":{"padding":{"top":"45px","right":"45px","bottom":"45px","left":"45px"},"margin":{"top":"58px","bottom":"58px"}}},"backgroundColor":"secondary"} -->
<div class="wp-block-group has-secondary-background-color has-text-color has-background" style="font-style:normal;color:#dd1d1d;margin-top:58px;margin-bottom:58px;padding-top:45px;padding-right:45px;padding-bottom:45px;padding-left:45px;font-size:2.08rem;font-weight:500;letter-spacing:23px;line-height:2;text-transform:uppercase"><!-- wp:paragraph -->
<p>I love chocolate raisins</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->

Check that the block appears as it should in the editor and the frontend.

Unit tests

# style engine tests
npm run test-unit packages/style-engine/src/test/index.js 

# global styles output
npm run test-unit components/global-styles/test/use-global-styles-output.js

More to come

@ramonjd ramonjd added [Status] In Progress Tracking issues with work in progress [Type] Experimental Experimental feature or API. [Package] Style Engine /packages/style-engine labels Apr 28, 2022
@ramonjd ramonjd requested a review from ellatrix as a code owner April 28, 2022 02:12
@ramonjd ramonjd self-assigned this Apr 28, 2022
@ramonjd ramonjd removed the request for review from ellatrix April 28, 2022 02:12
@github-actions
Copy link

github-actions bot commented Apr 28, 2022

Size Change: +437 B (0%)

Total Size: 1.24 MB

Filename Size Change
build/block-editor/index.min.js 151 kB +218 B (0%)
build/block-library/index.min.js 180 kB +18 B (0%)
build/blocks/index.min.js 47.1 kB +7 B (0%)
build/components/index.min.js 227 kB +1 B (0%)
build/components/style-rtl.css 15 kB +7 B (0%)
build/components/style.css 15.1 kB +6 B (0%)
build/edit-navigation/style-rtl.css 4.05 kB +2 B (0%)
build/edit-navigation/style.css 4.06 kB +2 B (0%)
build/edit-post/style-rtl.css 7.08 kB +4 B (0%)
build/edit-post/style.css 7.09 kB +4 B (0%)
build/edit-site/index.min.js 47.8 kB +165 B (0%)
build/edit-site/style-rtl.css 7.97 kB +1 B (0%)
build/edit-widgets/style-rtl.css 4.41 kB +2 B (0%)
build/edit-widgets/style.css 4.41 kB +2 B (0%)
build/editor/index.min.js 38.4 kB -2 B (0%)
ℹ️ View Unchanged
Filename Size
build/a11y/index.min.js 993 B
build/annotations/index.min.js 2.77 kB
build/api-fetch/index.min.js 2.27 kB
build/autop/index.min.js 2.15 kB
build/blob/index.min.js 487 B
build/block-directory/index.min.js 6.51 kB
build/block-directory/style-rtl.css 1.01 kB
build/block-directory/style.css 1.01 kB
build/block-editor/default-editor-styles-rtl.css 378 B
build/block-editor/default-editor-styles.css 378 B
build/block-editor/style-rtl.css 14.9 kB
build/block-editor/style.css 14.9 kB
build/block-library/blocks/archives/editor-rtl.css 61 B
build/block-library/blocks/archives/editor.css 60 B
build/block-library/blocks/archives/style-rtl.css 65 B
build/block-library/blocks/archives/style.css 65 B
build/block-library/blocks/audio/editor-rtl.css 150 B
build/block-library/blocks/audio/editor.css 150 B
build/block-library/blocks/audio/style-rtl.css 111 B
build/block-library/blocks/audio/style.css 111 B
build/block-library/blocks/audio/theme-rtl.css 125 B
build/block-library/blocks/audio/theme.css 125 B
build/block-library/blocks/avatar/editor-rtl.css 116 B
build/block-library/blocks/avatar/editor.css 116 B
build/block-library/blocks/avatar/style-rtl.css 59 B
build/block-library/blocks/avatar/style.css 59 B
build/block-library/blocks/block/editor-rtl.css 161 B
build/block-library/blocks/block/editor.css 161 B
build/block-library/blocks/button/editor-rtl.css 445 B
build/block-library/blocks/button/editor.css 445 B
build/block-library/blocks/button/style-rtl.css 560 B
build/block-library/blocks/button/style.css 560 B
build/block-library/blocks/buttons/editor-rtl.css 292 B
build/block-library/blocks/buttons/editor.css 292 B
build/block-library/blocks/buttons/style-rtl.css 275 B
build/block-library/blocks/buttons/style.css 275 B
build/block-library/blocks/calendar/style-rtl.css 207 B
build/block-library/blocks/calendar/style.css 207 B
build/block-library/blocks/categories/editor-rtl.css 84 B
build/block-library/blocks/categories/editor.css 83 B
build/block-library/blocks/categories/style-rtl.css 79 B
build/block-library/blocks/categories/style.css 79 B
build/block-library/blocks/code/style-rtl.css 103 B
build/block-library/blocks/code/style.css 103 B
build/block-library/blocks/code/theme-rtl.css 124 B
build/block-library/blocks/code/theme.css 124 B
build/block-library/blocks/columns/editor-rtl.css 108 B
build/block-library/blocks/columns/editor.css 108 B
build/block-library/blocks/columns/style-rtl.css 406 B
build/block-library/blocks/columns/style.css 406 B
build/block-library/blocks/comment-author-avatar/editor-rtl.css 125 B
build/block-library/blocks/comment-author-avatar/editor.css 125 B
build/block-library/blocks/comment-content/style-rtl.css 92 B
build/block-library/blocks/comment-content/style.css 92 B
build/block-library/blocks/comment-template/style-rtl.css 127 B
build/block-library/blocks/comment-template/style.css 127 B
build/block-library/blocks/comments-pagination-numbers/editor-rtl.css 123 B
build/block-library/blocks/comments-pagination-numbers/editor.css 121 B
build/block-library/blocks/comments-pagination/editor-rtl.css 222 B
build/block-library/blocks/comments-pagination/editor.css 209 B
build/block-library/blocks/comments-pagination/style-rtl.css 235 B
build/block-library/blocks/comments-pagination/style.css 231 B
build/block-library/blocks/comments-title/editor-rtl.css 75 B
build/block-library/blocks/comments-title/editor.css 75 B
build/block-library/blocks/comments/editor-rtl.css 95 B
build/block-library/blocks/comments/editor.css 95 B
build/block-library/blocks/cover/editor-rtl.css 546 B
build/block-library/blocks/cover/editor.css 547 B
build/block-library/blocks/cover/style-rtl.css 1.53 kB
build/block-library/blocks/cover/style.css 1.53 kB
build/block-library/blocks/embed/editor-rtl.css 293 B
build/block-library/blocks/embed/editor.css 293 B
build/block-library/blocks/embed/style-rtl.css 417 B
build/block-library/blocks/embed/style.css 417 B
build/block-library/blocks/embed/theme-rtl.css 124 B
build/block-library/blocks/embed/theme.css 124 B
build/block-library/blocks/file/editor-rtl.css 300 B
build/block-library/blocks/file/editor.css 300 B
build/block-library/blocks/file/style-rtl.css 255 B
build/block-library/blocks/file/style.css 255 B
build/block-library/blocks/file/view.min.js 353 B
build/block-library/blocks/freeform/editor-rtl.css 2.44 kB
build/block-library/blocks/freeform/editor.css 2.44 kB
build/block-library/blocks/gallery/editor-rtl.css 961 B
build/block-library/blocks/gallery/editor.css 964 B
build/block-library/blocks/gallery/style-rtl.css 1.51 kB
build/block-library/blocks/gallery/style.css 1.51 kB
build/block-library/blocks/gallery/theme-rtl.css 122 B
build/block-library/blocks/gallery/theme.css 122 B
build/block-library/blocks/group/editor-rtl.css 333 B
build/block-library/blocks/group/editor.css 333 B
build/block-library/blocks/group/style-rtl.css 57 B
build/block-library/blocks/group/style.css 57 B
build/block-library/blocks/group/theme-rtl.css 78 B
build/block-library/blocks/group/theme.css 78 B
build/block-library/blocks/heading/style-rtl.css 76 B
build/block-library/blocks/heading/style.css 76 B
build/block-library/blocks/html/editor-rtl.css 332 B
build/block-library/blocks/html/editor.css 333 B
build/block-library/blocks/image/editor-rtl.css 731 B
build/block-library/blocks/image/editor.css 730 B
build/block-library/blocks/image/style-rtl.css 529 B
build/block-library/blocks/image/style.css 535 B
build/block-library/blocks/image/theme-rtl.css 124 B
build/block-library/blocks/image/theme.css 124 B
build/block-library/blocks/latest-comments/style-rtl.css 284 B
build/block-library/blocks/latest-comments/style.css 284 B
build/block-library/blocks/latest-posts/editor-rtl.css 199 B
build/block-library/blocks/latest-posts/editor.css 198 B
build/block-library/blocks/latest-posts/style-rtl.css 463 B
build/block-library/blocks/latest-posts/style.css 462 B
build/block-library/blocks/list/style-rtl.css 88 B
build/block-library/blocks/list/style.css 88 B
build/block-library/blocks/media-text/editor-rtl.css 266 B
build/block-library/blocks/media-text/editor.css 263 B
build/block-library/blocks/media-text/style-rtl.css 493 B
build/block-library/blocks/media-text/style.css 490 B
build/block-library/blocks/more/editor-rtl.css 431 B
build/block-library/blocks/more/editor.css 431 B
build/block-library/blocks/navigation-link/editor-rtl.css 708 B
build/block-library/blocks/navigation-link/editor.css 706 B
build/block-library/blocks/navigation-link/style-rtl.css 115 B
build/block-library/blocks/navigation-link/style.css 115 B
build/block-library/blocks/navigation-submenu/editor-rtl.css 299 B
build/block-library/blocks/navigation-submenu/editor.css 299 B
build/block-library/blocks/navigation-submenu/view.min.js 375 B
build/block-library/blocks/navigation/editor-rtl.css 2.03 kB
build/block-library/blocks/navigation/editor.css 2.04 kB
build/block-library/blocks/navigation/style-rtl.css 1.95 kB
build/block-library/blocks/navigation/style.css 1.94 kB
build/block-library/blocks/navigation/view-modal.min.js 2.78 kB
build/block-library/blocks/navigation/view.min.js 395 B
build/block-library/blocks/nextpage/editor-rtl.css 395 B
build/block-library/blocks/nextpage/editor.css 395 B
build/block-library/blocks/page-list/editor-rtl.css 363 B
build/block-library/blocks/page-list/editor.css 363 B
build/block-library/blocks/page-list/style-rtl.css 175 B
build/block-library/blocks/page-list/style.css 175 B
build/block-library/blocks/paragraph/editor-rtl.css 157 B
build/block-library/blocks/paragraph/editor.css 157 B
build/block-library/blocks/paragraph/style-rtl.css 260 B
build/block-library/blocks/paragraph/style.css 260 B
build/block-library/blocks/post-author/style-rtl.css 175 B
build/block-library/blocks/post-author/style.css 176 B
build/block-library/blocks/post-comments-form/editor-rtl.css 69 B
build/block-library/blocks/post-comments-form/editor.css 69 B
build/block-library/blocks/post-comments-form/style-rtl.css 495 B
build/block-library/blocks/post-comments-form/style.css 495 B
build/block-library/blocks/post-comments/editor-rtl.css 77 B
build/block-library/blocks/post-comments/editor.css 77 B
build/block-library/blocks/post-comments/style-rtl.css 628 B
build/block-library/blocks/post-comments/style.css 628 B
build/block-library/blocks/post-excerpt/editor-rtl.css 73 B
build/block-library/blocks/post-excerpt/editor.css 73 B
build/block-library/blocks/post-excerpt/style-rtl.css 69 B
build/block-library/blocks/post-excerpt/style.css 69 B
build/block-library/blocks/post-featured-image/editor-rtl.css 721 B
build/block-library/blocks/post-featured-image/editor.css 721 B
build/block-library/blocks/post-featured-image/style-rtl.css 153 B
build/block-library/blocks/post-featured-image/style.css 153 B
build/block-library/blocks/post-template/editor-rtl.css 99 B
build/block-library/blocks/post-template/editor.css 98 B
build/block-library/blocks/post-template/style-rtl.css 323 B
build/block-library/blocks/post-template/style.css 323 B
build/block-library/blocks/post-terms/style-rtl.css 73 B
build/block-library/blocks/post-terms/style.css 73 B
build/block-library/blocks/post-title/style-rtl.css 80 B
build/block-library/blocks/post-title/style.css 80 B
build/block-library/blocks/preformatted/style-rtl.css 103 B
build/block-library/blocks/preformatted/style.css 103 B
build/block-library/blocks/pullquote/editor-rtl.css 198 B
build/block-library/blocks/pullquote/editor.css 198 B
build/block-library/blocks/pullquote/style-rtl.css 370 B
build/block-library/blocks/pullquote/style.css 370 B
build/block-library/blocks/pullquote/theme-rtl.css 167 B
build/block-library/blocks/pullquote/theme.css 167 B
build/block-library/blocks/query-pagination-numbers/editor-rtl.css 122 B
build/block-library/blocks/query-pagination-numbers/editor.css 121 B
build/block-library/blocks/query-pagination/editor-rtl.css 221 B
build/block-library/blocks/query-pagination/editor.css 211 B
build/block-library/blocks/query-pagination/style-rtl.css 234 B
build/block-library/blocks/query-pagination/style.css 231 B
build/block-library/blocks/query/editor-rtl.css 369 B
build/block-library/blocks/query/editor.css 369 B
build/block-library/blocks/quote/style-rtl.css 213 B
build/block-library/blocks/quote/style.css 213 B
build/block-library/blocks/quote/theme-rtl.css 223 B
build/block-library/blocks/quote/theme.css 226 B
build/block-library/blocks/read-more/style-rtl.css 132 B
build/block-library/blocks/read-more/style.css 132 B
build/block-library/blocks/rss/editor-rtl.css 202 B
build/block-library/blocks/rss/editor.css 204 B
build/block-library/blocks/rss/style-rtl.css 289 B
build/block-library/blocks/rss/style.css 288 B
build/block-library/blocks/search/editor-rtl.css 165 B
build/block-library/blocks/search/editor.css 165 B
build/block-library/blocks/search/style-rtl.css 397 B
build/block-library/blocks/search/style.css 398 B
build/block-library/blocks/search/theme-rtl.css 64 B
build/block-library/blocks/search/theme.css 64 B
build/block-library/blocks/separator/editor-rtl.css 146 B
build/block-library/blocks/separator/editor.css 146 B
build/block-library/blocks/separator/style-rtl.css 233 B
build/block-library/blocks/separator/style.css 233 B
build/block-library/blocks/separator/theme-rtl.css 194 B
build/block-library/blocks/separator/theme.css 194 B
build/block-library/blocks/shortcode/editor-rtl.css 474 B
build/block-library/blocks/shortcode/editor.css 474 B
build/block-library/blocks/site-logo/editor-rtl.css 759 B
build/block-library/blocks/site-logo/editor.css 759 B
build/block-library/blocks/site-logo/style-rtl.css 181 B
build/block-library/blocks/site-logo/style.css 181 B
build/block-library/blocks/site-tagline/editor-rtl.css 86 B
build/block-library/blocks/site-tagline/editor.css 86 B
build/block-library/blocks/site-title/editor-rtl.css 84 B
build/block-library/blocks/site-title/editor.css 84 B
build/block-library/blocks/social-link/editor-rtl.css 177 B
build/block-library/blocks/social-link/editor.css 177 B
build/block-library/blocks/social-links/editor-rtl.css 674 B
build/block-library/blocks/social-links/editor.css 673 B
build/block-library/blocks/social-links/style-rtl.css 1.37 kB
build/block-library/blocks/social-links/style.css 1.36 kB
build/block-library/blocks/spacer/editor-rtl.css 332 B
build/block-library/blocks/spacer/editor.css 332 B
build/block-library/blocks/spacer/style-rtl.css 48 B
build/block-library/blocks/spacer/style.css 48 B
build/block-library/blocks/table/editor-rtl.css 504 B
build/block-library/blocks/table/editor.css 504 B
build/block-library/blocks/table/style-rtl.css 625 B
build/block-library/blocks/table/style.css 625 B
build/block-library/blocks/table/theme-rtl.css 188 B
build/block-library/blocks/table/theme.css 188 B
build/block-library/blocks/tag-cloud/style-rtl.css 226 B
build/block-library/blocks/tag-cloud/style.css 227 B
build/block-library/blocks/template-part/editor-rtl.css 149 B
build/block-library/blocks/template-part/editor.css 149 B
build/block-library/blocks/template-part/theme-rtl.css 101 B
build/block-library/blocks/template-part/theme.css 101 B
build/block-library/blocks/text-columns/editor-rtl.css 95 B
build/block-library/blocks/text-columns/editor.css 95 B
build/block-library/blocks/text-columns/style-rtl.css 166 B
build/block-library/blocks/text-columns/style.css 166 B
build/block-library/blocks/verse/style-rtl.css 87 B
build/block-library/blocks/verse/style.css 87 B
build/block-library/blocks/video/editor-rtl.css 571 B
build/block-library/blocks/video/editor.css 572 B
build/block-library/blocks/video/style-rtl.css 173 B
build/block-library/blocks/video/style.css 173 B
build/block-library/blocks/video/theme-rtl.css 124 B
build/block-library/blocks/video/theme.css 124 B
build/block-library/common-rtl.css 993 B
build/block-library/common.css 990 B
build/block-library/editor-rtl.css 10.3 kB
build/block-library/editor.css 10.3 kB
build/block-library/reset-rtl.css 478 B
build/block-library/reset.css 478 B
build/block-library/style-rtl.css 11.5 kB
build/block-library/style.css 11.6 kB
build/block-library/theme-rtl.css 689 B
build/block-library/theme.css 694 B
build/block-serialization-default-parser/index.min.js 1.12 kB
build/block-serialization-spec-parser/index.min.js 2.83 kB
build/compose/index.min.js 11.7 kB
build/core-data/index.min.js 14.6 kB
build/customize-widgets/index.min.js 11.2 kB
build/customize-widgets/style-rtl.css 1.39 kB
build/customize-widgets/style.css 1.39 kB
build/data-controls/index.min.js 663 B
build/data/index.min.js 7.98 kB
build/date/index.min.js 32 kB
build/deprecated/index.min.js 518 B
build/dom-ready/index.min.js 336 B
build/dom/index.min.js 4.69 kB
build/edit-navigation/index.min.js 16 kB
build/edit-post/classic-rtl.css 546 B
build/edit-post/classic.css 547 B
build/edit-post/index.min.js 30.4 kB
build/edit-site/style.css 7.95 kB
build/edit-widgets/index.min.js 16.4 kB
build/editor/style-rtl.css 3.71 kB
build/editor/style.css 3.71 kB
build/element/index.min.js 4.3 kB
build/escape-html/index.min.js 548 B
build/format-library/index.min.js 6.62 kB
build/format-library/style-rtl.css 571 B
build/format-library/style.css 571 B
build/hooks/index.min.js 1.66 kB
build/html-entities/index.min.js 454 B
build/i18n/index.min.js 3.79 kB
build/is-shallow-equal/index.min.js 535 B
build/keyboard-shortcuts/index.min.js 1.83 kB
build/keycodes/index.min.js 1.41 kB
build/list-reusable-blocks/index.min.js 1.75 kB
build/list-reusable-blocks/style-rtl.css 838 B
build/list-reusable-blocks/style.css 838 B
build/media-utils/index.min.js 2.94 kB
build/notices/index.min.js 957 B
build/nux/index.min.js 2.1 kB
build/nux/style-rtl.css 751 B
build/nux/style.css 749 B
build/plugins/index.min.js 1.98 kB
build/preferences-persistence/index.min.js 2.23 kB
build/preferences/index.min.js 1.32 kB
build/primitives/index.min.js 949 B
build/priority-queue/index.min.js 628 B
build/react-i18n/index.min.js 704 B
build/react-refresh-entry/index.min.js 8.44 kB
build/react-refresh-runtime/index.min.js 7.31 kB
build/redux-routine/index.min.js 2.69 kB
build/reusable-blocks/index.min.js 2.24 kB
build/reusable-blocks/style-rtl.css 256 B
build/reusable-blocks/style.css 256 B
build/rich-text/index.min.js 11.2 kB
build/server-side-render/index.min.js 1.61 kB
build/shortcode/index.min.js 1.52 kB
build/token-list/index.min.js 668 B
build/url/index.min.js 1.99 kB
build/vendors/react-dom.min.js 38.5 kB
build/vendors/react.min.js 4.34 kB
build/viewport/index.min.js 1.08 kB
build/warning/index.min.js 280 B
build/widgets/index.min.js 7.21 kB
build/widgets/style-rtl.css 1.16 kB
build/widgets/style.css 1.16 kB
build/wordcount/index.min.js 1.07 kB

compressed-size-action

@ramonjd ramonjd force-pushed the update/style-engine-frontend-color-typography branch from ba35a2f to be71be4 Compare May 2, 2022 04:22
@ramonjd ramonjd force-pushed the update/style-engine-frontend-color-typography branch from 85065b1 to 978da19 Compare May 2, 2022 05:16
*
* @return string[] An array of classnames.
*/
export function getClassnames( style: Style ): string[] {
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 we could probably think about/ship this functionality separately.

Just adding it here to evaluate the concept.

Copy link
Contributor

Choose a reason for hiding this comment

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

I like the concept! I think I'd lean toward including it in this PR, it's looking like a good direction to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

Who's responsible for providing the styles for these classnames, while I think having a function that return "state" classnames is fine, I might think that these classnames shouldn't be necessary to style things properly and if we can have all the logic in generate function it's a bit better.

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we can't replace classnames with CSS variables yet in generate because that would require block deprecations for now. It might be fine to keep these out for now with a caviat that we may change these APIs later (to fully fulfill the style engine's potential :P)

Also, I don't see this function used in the "style" hook, should we replace the custom logic there with this function (like we did with the inline styles)?

For Global Styles generation though, it seems that we'll have to implement the CSS variable based styles in generate anyway right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Who's responsible for providing the styles for these classnames,

In the post editor, the styles linked to the has-* classnames are ultimately pulled from global settings/presents (generated by class-wp-theme-json-gutenberg.php).

An inline JS script hydrates the editor with settings. The styles are passed down from and transformed/output in .

Then in hooks/color.js the classnames for the block wrapper are compiled and added to the block props.

Is that what you meant by "responsible"?

The last step is what I had in mind for the style engine to take over initially, only because we're doing it in the backend. I was playing around with generating the has-* classnames in hooks/color.js via the style engine this morning. It works okay, but I get the feeling I'm building "around" existing logic, so not sure yet.

while I think having a function that return "state" classnames is fine, I might think that these classnames shouldn't be necessary to style things properly and if we can have all the logic in generate function it's a bit better.

Now I'm writing about it, it might be a better approach to have the style engine generate the CSS styles at the transformed/output stage mentioned above and forget about block classnames for now.

I guess we can't replace classnames with CSS variables yet in generate because that would require block deprecations for now.

Yeah, I did have a shot at putting the CSS vars inline... and it worked 🎉 but the console exploded in my face 😄

Also, I don't see this function used in the "style" hook, should we replace the custom logic there with this function (like we did with the inline styles)?

I tested it out this morning, and have things half-working. There are some inexplicable fixture-related errors, but I'll push anyway so can eyeball the code.

For Global Styles generation though, it seems that we'll have to implement the CSS variable based styles in generate anyway right?

Yeah for sure. That will be its own challenge. It makes me think that I should leave the block classname alone for now and rather devote the time into centralizing style generation.

Copy link
Member Author

Choose a reason for hiding this comment

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

It makes me think that I should leave the block classname alone for now and rather devote the time into centralizing style generation.

I've pushed my work so far in 393fac4 so we can look.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I did have a shot at putting the CSS vars inline... and it worked 🎉 but the console exploded in my face 😄

But isn't this the right thing to do anyway. It will solve both "block style generation" and "global styles generation" at the same time with the same style engine.

I agree that we need to find a way to either:

1- Don't use the style engine yet for colors and such in the hooks/color.js for now and keep the existing logic there
2- Have some kind of flag per block to say: this block is ready for full style engine style generation which "for me" means:
- do not serialize inline styles in the saved content
- rely on the JS style engine for the "edit" function (injected somewhere in hooks/color)
- rely on the PHP style engine for the frontend.

Maybe it's too soon for 2, I don't know.


393fac4 is fine if we really want to keep getClassnames to centralize the "classname" generation for blocks based on styles, but for me, this is not really the style engine, or more precisely, not the main purpose of the style engine.

I hope that makes a bit of sense :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Don't use the style engine yet for colors and such in the hooks/color.js for now and keep the existing logic there

Yeah, what this PR is attempting might be too much too soon. There's probably more value in trying to get style/css generation right across blocks/global styles.

rely on the PHP style engine for the frontend.

It would be fabulous for the PHP to take care of all frontend styles. Not sure about inline CSS saved in the editor though... We could maybe start over at layout.php for now.

want to keep getClassnames to centralize the "classname" generation for blocks based on styles, but for me, this is not really the style engine, or more precisely, not the main purpose of the style engine.

I get you. My idea was to take as much decision making about classnames and styles away from the editor as possible. Classname generation might be a bit premature I'd admit that.

I'd be fine with skipping this functionality in the JS in this PR. Maybe even revisiting the backend functionality too.

I'm still holding onto the hope that it'll all be clearer after we experiment with things. 🤞 Am I crazy? 😄

I hope that makes a bit of sense :)

Yes, it's given us a lot to think about, thanks a lot!

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe it's too soon for 2, I don't know.

Thanks for laying out a bit of how that could work — I think that sounds like something to pursue in a separate PR (perhaps once we've got all the block supports implemented server-side?) but I really like the idea of a per-block flag to opt-in for the style engine to render out the styles server-side on the site frontend. We could also use that flag to conditionally ignore style validation errors as a gentle way of gradually introducing the work from #37942 (your PR to ignore style validation errors) so we can avoid issues with deprecations.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for clarifying 👍 I'm confident this is going in the right direction ❤️

@ramonjd ramonjd requested a review from andrewserong May 4, 2022 03:52
@ramonjd
Copy link
Member Author

ramonjd commented May 4, 2022

Will fix the failing JS tests at some point 😇

Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

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

Nice work @ramonjd! This is looking like a really solid direction to me, there's some good parallels with the server-side implementation and I think it's starting to really highlight the value of us having the style engine as a separate package with common patterns for how each of the rules are implemented.

I've left a bunch of comments (mostly just some nits), but it's looking like a good path forward to me. Let me know if there's anything in particular you'd like me to test in further detail!

packages/style-engine/src/index.ts Outdated Show resolved Hide resolved
packages/style-engine/src/styles/color/background.ts Outdated Show resolved Hide resolved
packages/style-engine/src/styles/color/gradient.ts Outdated Show resolved Hide resolved
packages/style-engine/src/styles/color/text.ts Outdated Show resolved Hide resolved
packages/style-engine/src/styles/color/background.ts Outdated Show resolved Hide resolved
packages/style-engine/src/styles/color/gradient.ts Outdated Show resolved Hide resolved
packages/style-engine/src/index.ts Outdated Show resolved Hide resolved
*
* @return string[] An array of classnames.
*/
export function getClassnames( style: Style ): string[] {
Copy link
Contributor

Choose a reason for hiding this comment

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

I like the concept! I think I'd lean toward including it in this PR, it's looking like a good direction to me.

packages/block-editor/src/hooks/style.js Outdated Show resolved Hide resolved
@ramonjd
Copy link
Member Author

ramonjd commented May 5, 2022

Just to note that this PR breaks link color style output. The presets aren't parsed.

So a block like this,

<!-- wp:paragraph {"style":{"elements":{"link":{"color":{"text":"var:preset|color|vivid-red"}}}}} -->
    <p class="has-link-color"><a href="https://test.com,">Test</a></p>
<!-- /wp:paragraph -->

The output is:

.editor-styles-wrapper .wp-elements-3 a{
    color: var:preset|color|vivid-red; /* Should be var(--wp--preset--color--vivid-red)*/
}

Looking at this now 👀

@ramonjd ramonjd force-pushed the update/style-engine-frontend-color-typography branch from 95ab733 to d051c9f Compare May 6, 2022 02:21
@ramonjd
Copy link
Member Author

ramonjd commented May 6, 2022

Just to note that this PR breaks link color style output. The presets aren't parsed.

@andrewserong I've removed support for styles.color.text in this PR until we work out how to deal with element styles.

The link text color value needs to be converted from var:preset|color|slug to var(--wp--preset--color--slug) .

This is fine in itself however the style objects for links and text colors, at least the ones passed to getInlineStyles(), are the same.

At this line of compileElementsStyles() we pass { color: { text: 'value' } }, which is extracted from style.elements further down here.

The full object would be { elements: { link: { color: { text: 'value' } } } }

Since the link text color and regular text color objects are the same at this point, the style engine doesn't know that it should parse var:preset|styleContext|slug values for the link only.

One possibility is to parse all style values for var:-like values and return CSS var style rules the time.

We can do that and it will work (I tested it).

However if we do that, we might need to rethink the way we generate classnames, given that the current "rule" for background colors and other styles is use var:preset|color|slug to generate and return a classname, e.g., has-$slug-color, only and not return CSS var style rules.

Long story short, the style engine would need to know when to return a CSS var and when not to for a particular style value.

Given it's the style hook that's giving the style object a haircut, I think a neater way would be to pass the entire style object, including the elements object, to the style engine so we can do it explicitly for each style.

This means we might need to concentrate and/or refactor hooks/style.js, which would entail a bit of faffing around in another PR.

What do you reckon? Sorry if that doesn't make sense. I've been thinking about it for too long.

Can you think of a way of dealing with var: values that need to be converted into CSS vars?

@ramonjd
Copy link
Member Author

ramonjd commented May 6, 2022

One possibility is to parse all style values for var:-like values and return CSS var style rules the time.

We can do that and it will work (I tested it).

I've done that in 2cf8698

The consequence is that we'll parse all style values for var:... strings and return a CSS var.

Maybe it's a better way to start off and we can deal with classname + element stuff later, the backend notwithstanding 🤷

@andrewserong
Copy link
Contributor

However if we do that, we might need to rethink the way we generate classnames, given that the current "rule" for background colors and other styles is use var:preset|color|slug to generate and return a classname, e.g., has-$slug-color, only and not return CSS var style rules.

For the other colors that might reference a variable (e.g. background and gradient), they're currently handled outside of the style object, so that appears to be how they skip outputting a CSS var style rule, unless I'm missing something?

One possibility is to parse all style values for var:-like values and return CSS var style rules the time.

That seems like the most straightforward way to handle it to me, since it looks like that's the current behaviour in getInlineStyles where each value is run through compileStyleValue?

Apologies, I very well might be missing quite a bit of context here!

@ramonjd
Copy link
Member Author

ramonjd commented May 6, 2022

That seems like the most straightforward way to handle it to me, since it looks like that's the current behaviour in getInlineStyles where each value is run through compileStyleValue?

Thanks for trying to decipher my gibberish @andrewserong

I ended up doing this anyway.

What I was trying to express with all the Github comment anguish was this: in the backend var:preset|* values are used to build classnames only.

So

    'color' => array(
       'background' => 'var:preset|color|burned-toast' 
   )

Tells the style engine to return has-burned-toast-background-color and not background-color: var(--wp--preset--color--burned-toast).

See #40332 (comment) for some context.

This logic doesn't exist in the frontend right now. Maybe it doesn't have to, and all my hand-wringing is for nothing.

Besides, we don't really have to deal with it yet since we're not using the getClassNames method.

My guess is that it will come out in the wash as we progress.

@ramonjd ramonjd requested a review from youknowriad May 6, 2022 05:18
@andrewserong
Copy link
Contributor

What I was trying to express with all the Github comment anguish was this: in the backend var:preset|* values are used to build classnames only.

Ah, I see now! Thanks for the explanation, this is the line where you're skipping generating the CSS rule if it starts with var 👍

My guess is that it will come out in the wash as we progress.

Yes, it's a little tricky to work out which way we should go here. I think I'd lean toward seeing how far we can get with the current approach, even if it creates a slight gap between frontend and backend, and then revisit when we explicitly go to incorporate the Elements block support — that'll wind up involving dealing with non-inline styles, too, so it becomes a bit of a rabbithole to figure out.

One of the other differences we still have between front and backend is that in the frontend code we're also calling the style engine from the global styles output, which we haven't started doing in the backend yet. So probably good to see if we can handball some of these questions to follow-up PRs where we can dig in, in isolation?

@ramonjd
Copy link
Member Author

ramonjd commented May 6, 2022

So probably good to see if we can handball some of these questions to follow-up PRs where we can dig in, in isolation?

Agree. Once again, thanks for helping out with the thinking here. 🙇

@ramonjd ramonjd force-pushed the update/style-engine-frontend-color-typography branch from cb36e10 to c726f9b Compare May 10, 2022 03:00
ramonjd and others added 18 commits May 16, 2022 19:37
Fixing camel case style prop name
… It's my PR and I'll fail if I want to.

Preferencing `uniq` for new Set() spread
Updating tests.
Updated inline docs
Making the generateRule signature the same as generateBoxRules
Removing styles color text support because we haven't yet come up a with a way to deal with link color styles for elements. We need to reconcile the way we generate text colors with link css var colors and the logic in hooks/style.js
Add constants
Updated inline docs
Making the generateRule signature the same as generateBoxRules
…the generic class name if required.

Regenerating fixtures due to the re-ordering of color classnames.
@ramonjd ramonjd force-pushed the update/style-engine-frontend-color-typography branch from 07bafe3 to 41dbe14 Compare May 16, 2022 09:37
Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

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

Thanks for tidying this up @ramonjd, I think this is looking very close to merge to me.

Tested with the following markup (and making lots of tweaks in the editor):

Block markup
<!-- wp:group {"style":{"color":{"gradient":"linear-gradient(135deg,rgb(252,185,0) 18%,rgb(255,105,0) 100%)"}}} -->
<div class="wp-block-group has-background" style="background:linear-gradient(135deg,rgb(252,185,0) 18%,rgb(255,105,0) 100%)"><!-- wp:heading {"fontFamily":"system-font"} -->
<h2 class="has-system-font-font-family">Heading</h2>
<!-- /wp:heading -->

<!-- wp:paragraph {"style":{"typography":{"fontSize":"0.53rem"}}} -->
<p style="font-size:0.53rem">A paragraph</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"style":{"typography":{"fontStyle":"italic","fontWeight":"800","textTransform":"capitalize","lineHeight":"5","letterSpacing":"13px"}},"fontSize":"medium"} -->
<p class="has-medium-font-size" style="font-style:italic;font-weight:800;letter-spacing:13px;line-height:5;text-transform:capitalize">This is a paragraph</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"style":{"elements":{"link":{"color":{"text":"#f66d6d"}}},"color":{"background":"#6d0e0e","text":"#f0f0f0"}},"fontSize":"large"} -->
<p class="has-text-color has-background has-link-color has-large-font-size" style="color:#f0f0f0;background-color:#6d0e0e">Another paragraph with <a href="http://www.imdb.com">a link</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->

<!-- wp:read-more {"content":"Read more","style":{"typography":{"textDecoration":"underline"}}} /-->

image

✅ Color is output correctly
✅ Background color is output correctly
✅ Font size is output correctly
❓ Font style is output correctly, but it looks like we need to add the useEngine flag to it (tests well manually if I change that flag)
✅ Font weight is output correctly
✅ Line height is output correctly
✅ Text decoration is output correctly
✅ Text transform is output correctly
✅ Letter spacing is output correctly
❓ Font family doesn't appear to be opted-in — was that intentional? I assume so, as it's currently implemented via class names in the editor for e.g. Heading block? I left a comment to discuss further, I ran out of time today to look it up myself 😅

Happy to do more testing / a final review tomorrow. Thanks again! 🙇

packages/blocks/src/api/constants.js Show resolved Hide resolved
packages/style-engine/src/styles/utils.ts Outdated Show resolved Hide resolved
packages/style-engine/src/styles/typography/index.ts Outdated Show resolved Hide resolved
Remove the handler for fontFamily to reflect the current situation in the editor whereby no custom fontFamily style values are expected.
Removed unused function `getSlugFromPreset`
Added tests for elements.link.color.text preset style values.
Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

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

Thanks for making the updates @ramonjd! I've just re-tested and that's looking good now.

✅ Font style is output correctly
✅ Font family is no longer included
✅ Included utils functions are used, and no strays left behind, and logic in getCSSVarFromStyleValue matches that used in computeStyleValue in hooks/style.js 👍

This LGTM now! 🎉

@ramonjd ramonjd merged commit 1eee465 into trunk May 18, 2022
@ramonjd ramonjd deleted the update/style-engine-frontend-color-typography branch May 18, 2022 02:58
@github-actions github-actions bot added this to the Gutenberg 13.3 milestone May 18, 2022
@priethor priethor removed the [Status] In Progress Tracking issues with work in progress label May 25, 2022
@bph bph mentioned this pull request Sep 21, 2022
89 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Style Engine /packages/style-engine [Type] Experimental Experimental feature or API.
Projects
Status: 🏆 Done
Development

Successfully merging this pull request may close these issues.

4 participants