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

Cover block: fix flickering when inserted in templates and also fix isDark calculation bugs #53253

Merged
merged 18 commits into from
Aug 7, 2023

Conversation

glendaviesnz
Copy link
Contributor

@glendaviesnz glendaviesnz commented Aug 2, 2023

What?

Fixes: #53211
Also fixes bugs with the calculation if isDark that were identified while working on the above fix:

  • When overly color is removed the color defaults to black but isDark is set to false
  • Changing the dimRatio of the overlay color has no impact on the isDark setting so a black overlay dimRatio of 0 is still considered dark

Why?

The useEffects used in the calculation of isDark are the cause for both the jitter and the above bugs, so easier to fix both at the same time.

How?

Replace the useEffects with setting of isDark in the relevant event handlers.

Testing Instructions

Flickering bug

  1. Create a synced pattern that contains a Cover block with a background image set
  2. In a theme like TT3, add a new template (e.g. my-template.html in the templates directory).
  3. Within my-template.html, add the synced block pattern. To get the markup to add to the template, you can add the synced pattern to a post or page, then view the code editor view and copy + paste to your my-template.html file
  4. Create a page and set its template to my-template.html.
  5. Open the site editor, and navigate to Pages > your newly created page. Click the preview of the page to switch to the edit mode, and check that the Cover block does not flicker.

Example markup from my my-template.html file:

<!-- wp:template-part {"slug":"header","tagName":"header"} /-->

<!-- wp:pattern {"slug":"core/query-standard-posts"} /-->

<!-- wp:post-content /-->

<!-- wp:post-featured-image /-->

<!-- wp:template-part {"slug":"footer","tagName":"footer"} /-->

<!-- wp:block {"ref":65} /-->

isDark bug and verifying all isDark workflows

  • Add a Cover block and set a black overlay color, then add text and ensure the text is white
  • Reduce the dimRatio below 50 and check that the text turns black
  • Increase the dimRatio above 50 again and check that the text turns white again
  • Change the overlay color to a light color and check that the text turns black
  • Change the dimRatio above and below 50 and check that the text remains black
  • Make sure dimRatio is above 50 with the light background and then add a very dark overlay image and check that the text remains black
  • Reduce the dimRatio below 50 and check that the text turns white
  • Replace the image with a very light one and check that the text turns black
  • Remove both the image and the overlay color and check that the text turns white
  • Add a new cover block with black overlay color and dimRation < 50
  • Add light featured image to the post and toggle it on as the overlay image in the Cover block toolbar and check that the text color is black
  • Toggle the feature image back off and check that the text is white again
  • Repeat the above two steps with a dark feature image

In summary, my thinking is that whenever the image/featured image/overlay color/dim ratio is changed the isDark value should be updated in order to prevent text disappearing. N.B. - the only flow that will not reset the isDark setting is if a featured image is removed from the post via the post settings tab. The only way I can see to fix this is by adding a useEffect, but it is a very edge case and I don't think it is worth adding extra complexity to handle this.

Screenshots or screencast

Flicker before:

2023-08-01.11.44.52.mp4

Flicker after:

flicker-after.mp4

Is dark before:

isdark-before.mp4

Is dark after:

isdark-after.mp4

@glendaviesnz glendaviesnz added [Type] Bug An existing feature does not function as intended [Status] In Progress Tracking issues with work in progress [Block] Cover Affects the Cover Block - used to display content laid over a background image labels Aug 2, 2023
@glendaviesnz glendaviesnz self-assigned this Aug 2, 2023
@glendaviesnz glendaviesnz changed the title [WIP] Fix/cover jitter bug [WIP] Cover block: fix jitter bug Aug 2, 2023
@github-actions
Copy link

github-actions bot commented Aug 2, 2023

Size Change: +934 B (0%)

Total Size: 1.44 MB

Filename Size Change
build/block-editor/index.min.js 210 kB +47 B (0%)
build/block-library/blocks/columns/style-rtl.css 421 B +12 B (+3%)
build/block-library/blocks/columns/style.css 421 B +12 B (+3%)
build/block-library/blocks/details/style-rtl.css 98 B -80 B (-45%) 🎉
build/block-library/blocks/details/style.css 98 B -80 B (-45%) 🎉
build/block-library/blocks/file/style-rtl.css 280 B +11 B (+4%)
build/block-library/blocks/file/style.css 281 B +11 B (+4%)
build/block-library/blocks/search/style-rtl.css 608 B +21 B (+4%)
build/block-library/blocks/search/style.css 608 B +24 B (+4%)
build/block-library/index.min.js 202 kB +86 B (0%)
build/block-library/style-rtl.css 13.7 kB -1 B (0%)
build/block-library/style.css 13.8 kB -3 B (0%)
build/components/index.min.js 244 kB +808 B (0%)
build/core-commands/index.min.js 2.44 kB +4 B (0%)
build/edit-post/index.min.js 35.7 kB +59 B (0%)
build/edit-site/index.min.js 90.5 kB +73 B (0%)
build/editor/index.min.js 45.3 kB -64 B (0%)
build/interactivity/index.min.js 10.4 kB -1 B (0%)
build/rich-text/index.min.js 11 kB -5 B (0%)
ℹ️ View Unchanged
Filename Size
build/a11y/index.min.js 955 B
build/annotations/index.min.js 2.69 kB
build/api-fetch/index.min.js 2.28 kB
build/autop/index.min.js 2.1 kB
build/blob/index.min.js 451 B
build/block-directory/index.min.js 6.99 kB
build/block-directory/style-rtl.css 1.02 kB
build/block-directory/style.css 1.02 kB
build/block-editor/content-rtl.css 4.26 kB
build/block-editor/content.css 4.25 kB
build/block-editor/default-editor-styles-rtl.css 381 B
build/block-editor/default-editor-styles.css 381 B
build/block-editor/style-rtl.css 14.8 kB
build/block-editor/style.css 14.8 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 90 B
build/block-library/blocks/archives/style.css 90 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 122 B
build/block-library/blocks/audio/style.css 122 B
build/block-library/blocks/audio/theme-rtl.css 126 B
build/block-library/blocks/audio/theme.css 126 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 104 B
build/block-library/blocks/avatar/style.css 104 B
build/block-library/blocks/block/editor-rtl.css 305 B
build/block-library/blocks/block/editor.css 305 B
build/block-library/blocks/button/editor-rtl.css 584 B
build/block-library/blocks/button/editor.css 582 B
build/block-library/blocks/button/style-rtl.css 624 B
build/block-library/blocks/button/style.css 623 B
build/block-library/blocks/buttons/editor-rtl.css 337 B
build/block-library/blocks/buttons/editor.css 337 B
build/block-library/blocks/buttons/style-rtl.css 332 B
build/block-library/blocks/buttons/style.css 332 B
build/block-library/blocks/calendar/style-rtl.css 239 B
build/block-library/blocks/calendar/style.css 239 B
build/block-library/blocks/categories/editor-rtl.css 113 B
build/block-library/blocks/categories/editor.css 112 B
build/block-library/blocks/categories/style-rtl.css 124 B
build/block-library/blocks/categories/style.css 124 B
build/block-library/blocks/code/editor-rtl.css 53 B
build/block-library/blocks/code/editor.css 53 B
build/block-library/blocks/code/style-rtl.css 121 B
build/block-library/blocks/code/style.css 121 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/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 199 B
build/block-library/blocks/comment-template/style.css 198 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 840 B
build/block-library/blocks/comments/editor.css 839 B
build/block-library/blocks/comments/style-rtl.css 637 B
build/block-library/blocks/comments/style.css 636 B
build/block-library/blocks/cover/editor-rtl.css 647 B
build/block-library/blocks/cover/editor.css 650 B
build/block-library/blocks/cover/style-rtl.css 1.61 kB
build/block-library/blocks/cover/style.css 1.6 kB
build/block-library/blocks/details/editor-rtl.css 65 B
build/block-library/blocks/details/editor.css 65 B
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 410 B
build/block-library/blocks/embed/style.css 410 B
build/block-library/blocks/embed/theme-rtl.css 126 B
build/block-library/blocks/embed/theme.css 126 B
build/block-library/blocks/file/editor-rtl.css 316 B
build/block-library/blocks/file/editor.css 316 B
build/block-library/blocks/file/view-interactivity.min.js 317 B
build/block-library/blocks/file/view.min.js 375 B
build/block-library/blocks/footnotes/style-rtl.css 201 B
build/block-library/blocks/footnotes/style.css 199 B
build/block-library/blocks/freeform/editor-rtl.css 2.58 kB
build/block-library/blocks/freeform/editor.css 2.58 kB
build/block-library/blocks/gallery/editor-rtl.css 947 B
build/block-library/blocks/gallery/editor.css 952 B
build/block-library/blocks/gallery/style-rtl.css 1.53 kB
build/block-library/blocks/gallery/style.css 1.53 kB
build/block-library/blocks/gallery/theme-rtl.css 108 B
build/block-library/blocks/gallery/theme.css 108 B
build/block-library/blocks/group/editor-rtl.css 654 B
build/block-library/blocks/group/editor.css 654 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 336 B
build/block-library/blocks/html/editor.css 337 B
build/block-library/blocks/image/editor-rtl.css 834 B
build/block-library/blocks/image/editor.css 833 B
build/block-library/blocks/image/style-rtl.css 1.42 kB
build/block-library/blocks/image/style.css 1.42 kB
build/block-library/blocks/image/theme-rtl.css 126 B
build/block-library/blocks/image/theme.css 126 B
build/block-library/blocks/image/view-interactivity.min.js 1.46 kB
build/block-library/blocks/latest-comments/style-rtl.css 357 B
build/block-library/blocks/latest-comments/style.css 357 B
build/block-library/blocks/latest-posts/editor-rtl.css 213 B
build/block-library/blocks/latest-posts/editor.css 212 B
build/block-library/blocks/latest-posts/style-rtl.css 478 B
build/block-library/blocks/latest-posts/style.css 478 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 507 B
build/block-library/blocks/media-text/style.css 505 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 712 B
build/block-library/blocks/navigation-link/editor.css 711 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 296 B
build/block-library/blocks/navigation-submenu/editor.css 295 B
build/block-library/blocks/navigation/editor-rtl.css 2.26 kB
build/block-library/blocks/navigation/editor.css 2.26 kB
build/block-library/blocks/navigation/style-rtl.css 2.23 kB
build/block-library/blocks/navigation/style.css 2.22 kB
build/block-library/blocks/navigation/view-interactivity.min.js 988 B
build/block-library/blocks/navigation/view-modal.min.js 2.85 kB
build/block-library/blocks/navigation/view.min.js 469 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 401 B
build/block-library/blocks/page-list/editor.css 401 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 174 B
build/block-library/blocks/paragraph/editor.css 174 B
build/block-library/blocks/paragraph/style-rtl.css 279 B
build/block-library/blocks/paragraph/style.css 281 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 96 B
build/block-library/blocks/post-comments-form/editor.css 96 B
build/block-library/blocks/post-comments-form/style-rtl.css 508 B
build/block-library/blocks/post-comments-form/style.css 508 B
build/block-library/blocks/post-date/style-rtl.css 61 B
build/block-library/blocks/post-date/style.css 61 B
build/block-library/blocks/post-excerpt/editor-rtl.css 71 B
build/block-library/blocks/post-excerpt/editor.css 71 B
build/block-library/blocks/post-excerpt/style-rtl.css 141 B
build/block-library/blocks/post-excerpt/style.css 141 B
build/block-library/blocks/post-featured-image/editor-rtl.css 588 B
build/block-library/blocks/post-featured-image/editor.css 586 B
build/block-library/blocks/post-featured-image/style-rtl.css 319 B
build/block-library/blocks/post-featured-image/style.css 319 B
build/block-library/blocks/post-navigation-link/style-rtl.css 153 B
build/block-library/blocks/post-navigation-link/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 314 B
build/block-library/blocks/post-template/style.css 314 B
build/block-library/blocks/post-terms/style-rtl.css 96 B
build/block-library/blocks/post-terms/style.css 96 B
build/block-library/blocks/post-time-to-read/style-rtl.css 69 B
build/block-library/blocks/post-time-to-read/style.css 69 B
build/block-library/blocks/post-title/style-rtl.css 100 B
build/block-library/blocks/post-title/style.css 100 B
build/block-library/blocks/preformatted/style-rtl.css 125 B
build/block-library/blocks/preformatted/style.css 125 B
build/block-library/blocks/pullquote/editor-rtl.css 135 B
build/block-library/blocks/pullquote/editor.css 135 B
build/block-library/blocks/pullquote/style-rtl.css 335 B
build/block-library/blocks/pullquote/style.css 335 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 302 B
build/block-library/blocks/query-pagination/style.css 299 B
build/block-library/blocks/query-title/style-rtl.css 63 B
build/block-library/blocks/query-title/style.css 63 B
build/block-library/blocks/query/editor-rtl.css 450 B
build/block-library/blocks/query/editor.css 449 B
build/block-library/blocks/quote/style-rtl.css 222 B
build/block-library/blocks/quote/style.css 222 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 149 B
build/block-library/blocks/rss/editor.css 149 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 178 B
build/block-library/blocks/search/editor.css 178 B
build/block-library/blocks/search/theme-rtl.css 114 B
build/block-library/blocks/search/theme.css 114 B
build/block-library/blocks/search/view.min.js 631 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 234 B
build/block-library/blocks/separator/style.css 234 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 323 B
build/block-library/blocks/shortcode/editor.css 323 B
build/block-library/blocks/site-logo/editor-rtl.css 754 B
build/block-library/blocks/site-logo/editor.css 754 B
build/block-library/blocks/site-logo/style-rtl.css 203 B
build/block-library/blocks/site-logo/style.css 203 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 116 B
build/block-library/blocks/site-title/editor.css 116 B
build/block-library/blocks/site-title/style-rtl.css 57 B
build/block-library/blocks/site-title/style.css 57 B
build/block-library/blocks/social-link/editor-rtl.css 184 B
build/block-library/blocks/social-link/editor.css 184 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.44 kB
build/block-library/blocks/social-links/style.css 1.43 kB
build/block-library/blocks/spacer/editor-rtl.css 348 B
build/block-library/blocks/spacer/editor.css 348 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 433 B
build/block-library/blocks/table/editor.css 433 B
build/block-library/blocks/table/style-rtl.css 645 B
build/block-library/blocks/table/style.css 644 B
build/block-library/blocks/table/theme-rtl.css 146 B
build/block-library/blocks/table/theme.css 146 B
build/block-library/blocks/tag-cloud/style-rtl.css 251 B
build/block-library/blocks/tag-cloud/style.css 253 B
build/block-library/blocks/template-part/editor-rtl.css 403 B
build/block-library/blocks/template-part/editor.css 403 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/term-description/style-rtl.css 111 B
build/block-library/blocks/term-description/style.css 111 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 99 B
build/block-library/blocks/verse/style.css 99 B
build/block-library/blocks/video/editor-rtl.css 552 B
build/block-library/blocks/video/editor.css 555 B
build/block-library/blocks/video/style-rtl.css 185 B
build/block-library/blocks/video/style.css 185 B
build/block-library/blocks/video/theme-rtl.css 126 B
build/block-library/blocks/video/theme.css 126 B
build/block-library/classic-rtl.css 179 B
build/block-library/classic.css 179 B
build/block-library/common-rtl.css 1.1 kB
build/block-library/common.css 1.1 kB
build/block-library/editor-elements-rtl.css 75 B
build/block-library/editor-elements.css 75 B
build/block-library/editor-rtl.css 12.1 kB
build/block-library/editor.css 12.1 kB
build/block-library/elements-rtl.css 54 B
build/block-library/elements.css 54 B
build/block-library/reset-rtl.css 478 B
build/block-library/reset.css 478 B
build/block-library/theme-rtl.css 686 B
build/block-library/theme.css 691 B
build/block-serialization-default-parser/index.min.js 1.12 kB
build/block-serialization-spec-parser/index.min.js 2.87 kB
build/blocks/index.min.js 51 kB
build/commands/index.min.js 15.3 kB
build/commands/style-rtl.css 849 B
build/commands/style.css 843 B
build/components/style-rtl.css 11.8 kB
build/components/style.css 11.8 kB
build/compose/index.min.js 12.1 kB
build/core-data/index.min.js 16.4 kB
build/customize-widgets/index.min.js 12 kB
build/customize-widgets/style-rtl.css 1.46 kB
build/customize-widgets/style.css 1.45 kB
build/data-controls/index.min.js 640 B
build/data/index.min.js 8.28 kB
build/date/index.min.js 17.8 kB
build/deprecated/index.min.js 451 B
build/dom-ready/index.min.js 324 B
build/dom/index.min.js 4.63 kB
build/edit-post/classic-rtl.css 544 B
build/edit-post/classic.css 545 B
build/edit-post/style-rtl.css 7.58 kB
build/edit-post/style.css 7.58 kB
build/edit-site/style-rtl.css 13.2 kB
build/edit-site/style.css 13.2 kB
build/edit-widgets/index.min.js 16.9 kB
build/edit-widgets/style-rtl.css 4.52 kB
build/edit-widgets/style.css 4.52 kB
build/editor/style-rtl.css 3.55 kB
build/editor/style.css 3.55 kB
build/element/index.min.js 4.8 kB
build/escape-html/index.min.js 537 B
build/format-library/index.min.js 7.59 kB
build/format-library/style-rtl.css 554 B
build/format-library/style.css 553 B
build/hooks/index.min.js 1.55 kB
build/html-entities/index.min.js 448 B
build/i18n/index.min.js 3.58 kB
build/is-shallow-equal/index.min.js 527 B
build/keyboard-shortcuts/index.min.js 1.64 kB
build/keycodes/index.min.js 1.84 kB
build/list-reusable-blocks/index.min.js 2.18 kB
build/list-reusable-blocks/style-rtl.css 836 B
build/list-reusable-blocks/style.css 836 B
build/media-utils/index.min.js 2.9 kB
build/notices/index.min.js 948 B
build/nux/index.min.js 1.99 kB
build/nux/style-rtl.css 735 B
build/nux/style.css 732 B
build/plugins/index.min.js 1.79 kB
build/preferences-persistence/index.min.js 1.84 kB
build/preferences/index.min.js 1.24 kB
build/primitives/index.min.js 943 B
build/priority-queue/index.min.js 1.52 kB
build/private-apis/index.min.js 951 B
build/react-i18n/index.min.js 615 B
build/react-refresh-entry/index.min.js 9.47 kB
build/react-refresh-runtime/index.min.js 7.31 kB
build/redux-routine/index.min.js 2.7 kB
build/reusable-blocks/index.min.js 2.71 kB
build/reusable-blocks/style-rtl.css 243 B
build/reusable-blocks/style.css 243 B
build/router/index.min.js 1.77 kB
build/server-side-render/index.min.js 1.94 kB
build/shortcode/index.min.js 1.39 kB
build/style-engine/index.min.js 1.83 kB
build/token-list/index.min.js 582 B
build/url/index.min.js 3.57 kB
build/vendors/inert-polyfill.min.js 2.48 kB
build/vendors/react-dom.min.js 41.8 kB
build/vendors/react.min.js 4.02 kB
build/viewport/index.min.js 958 B
build/warning/index.min.js 268 B
build/widgets/index.min.js 7.16 kB
build/widgets/style-rtl.css 1.15 kB
build/widgets/style.css 1.16 kB
build/wordcount/index.min.js 1.02 kB

compressed-size-action

@glendaviesnz glendaviesnz changed the title [WIP] Cover block: fix jitter bug Cover block: fix jitter when inserted in templates and also fix isDark calculation bugs Aug 3, 2023
@glendaviesnz glendaviesnz changed the title Cover block: fix jitter when inserted in templates and also fix isDark calculation bugs Cover block: fix flickering when inserted in templates and also fix isDark calculation bugs Aug 3, 2023
@glendaviesnz glendaviesnz marked this pull request as ready for review August 3, 2023 03:22
@glendaviesnz glendaviesnz requested a review from ajitbohra as a code owner August 3, 2023 03:22
@glendaviesnz glendaviesnz removed the [Status] In Progress Tracking issues with work in progress label Aug 3, 2023
Comment on lines 115 to 117
if ( mediaUrl && useFeaturedImage ) {
setCoverIsDark( mediaUrl, dimRatio, overlayColor.color );
}
Copy link
Member

Choose a reason for hiding this comment

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

Can this logic also be moved inside the event handler? This will cause attribute updates on every render when a condition is true and trigger another re-render.

While it's okay to call setState during the render, I'm not sure if the same applies to the setAttributes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeh, this is the one bit I wasn't sure on. Because the setting of featured image is outside the component we don't have access to the event handle, so maybe this is one case when an effect is better - will have a play.

@@ -103,7 +103,7 @@ function CoverEdit( {
'featured_media',
postId
);

const setCoverIsDark = useCoverIsDark( setAttributes );
Copy link
Member

Choose a reason for hiding this comment

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

This could become getCoverIsDark and let the components update the attribute. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was initially doing that and it just seemed like a lot of replicated:

__unstableMarkNextChangeAsNotPersistent();
setAttributes( { isDark: color.isDark } );

in each of the event handlers so moving to the hook removed the duplication, but I don't have a strong opinion on this. If you think this is better handled in the edit component I can move it back?

Copy link
Member

Choose a reason for hiding this comment

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

If we can "batch" the attribute updates, I think it's okay to include this in the undo history and drop the "non-persistent" action call. But I see that most attribute updaters have wrappers like setMedia; that would mean unwrapping them as well, probably not worth it at the moment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

batching the updates where possible was a much better approach!

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.

This feels like a promising direction @glendaviesnz! It's fixed the flickering issue for me in templates that contain a pattern that has a cover block within it 👍

One subtle issue that I noticed while playing around with this is that if I adjust the overlay opacity very quickly, with this PR applied it's possible for it to not update when expected.

In the following screengrabs, on trunk the is-light class is correctly present when quickly dragging to the right. With this PR applied, it only appears to update when dragging slowly (or at least not quickly):

Trunk This PR
2023-08-03 14 00 05 2023-08-03 13 58 47

Also a couple of the Color panel tests are currently failing — could that be related?

image

const { __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );

const getCoverIsDark = useCallback(
Copy link
Contributor

Choose a reason for hiding this comment

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

In edit.js this is used as setCoverIsDark. Should we call it that here, too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Depends on the answer to this question - what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

George does make a good point! I don't feel too strongly about it, though. Using getCoverIsDark might help neaten things a bit potentially as the isDark value could be updated in the same setAttributes call as other attributes (e.g. in onClearMedia, onUpdateDimRatio, etc)... but I suppose one challenge is that for images the update needs to happen asynchronously via the .then() since we won't immediately know the dark value?

Comment on lines 115 to 117
if ( mediaUrl && useFeaturedImage ) {
setCoverIsDark( mediaUrl, dimRatio, overlayColor.color );
}
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 be called on every render? Would it be worth only calling if mediaUrl, dimRatio, or overlayColor.color changes?

That might wind up re-introducing some form of useEffect, though, which might not be the goal here 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Mamaduka
Copy link
Member

Mamaduka commented Aug 3, 2023

I think this showcases how fragile attribute updates via effects can be in blocks. We should probably try and move similar logic into event handlers for other blocks as well.

@glendaviesnz
Copy link
Contributor Author

Thanks for the great feedback @Mamaduka and @andrewserong, I have made your suggested changes and I think this makes things much more robust - have pushed the changes but have run out of time to fully test. I will give it a better test in the morning, but feel free to give it a quick once over and see if I have missed anything obvious.

@glendaviesnz
Copy link
Contributor Author

One subtle issue that I noticed while playing around with this is that if I adjust the overlay opacity very quickly, with this PR applied it's possible for it to not update when expected.

This seems to have been resolved by batching the update with the dimRatio attribute update.

@glendaviesnz
Copy link
Contributor Author

I will sort out the unit tests tomorrow.

@glendaviesnz
Copy link
Contributor Author

glendaviesnz commented Aug 3, 2023

I also need to add some awaits onto the getCoverIsDark calls I think as this can now return a promise.

@andrewserong
Copy link
Contributor

Thanks for the updates!

This seems to have been resolved by batching the update with the dimRatio attribute update.

That does appear to resolve the issue of dragging quickly not updating correctly. One issue I'm seeing now is that for an image that in its 0 opacity (and that doesn't have an overlay color set) results in dark text on trunk, now dragging the opacity slider never results in dark text. If I set the Overlay to a hard color (e.g. white) then scrolling the slider does update correctly (even very quickly), so it looks like the issue has been resolved in terms of rapidly sliding the control. I'm not too sure why the 0 opacity state isn't updating it to dark, though — but happy to give it a more detailed review again tomorrow!

Here's a quick screengrab demoing the above (note that the text is only dark in this screengrab when the Overlay color is set):

2023-08-03.15.47.56.mp4

// If no overlay color exists the overlay color is black (isDark )
setIsDark( true );
return;
export default function useCoverIsDark() {
Copy link
Member

Choose a reason for hiding this comment

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

I think now this could be just a callback getCoverIsDark, and there's no need for useCallback memoization.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeh, now we are not saving state or setting attributes there isn't any point in it being a hook, have moved it to a standard method in the shared methods folder.

const getCoverIsDark = useCallback(
( url, dimRatio = 50, overlayColor ) => {
// If the dimRatio is less than 50, the image will have the most impact on darkness.
if ( url && dimRatio <= 50 ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Before, when wrapped in an effect, this branch didn’t execute unless url had changed. Now, it may execute needlessly for changes to overlayColor or dimRatio (given dimRatio is less than 50). Since the url hasn't changed the color average and resulting isDark determination will end up the same. Not sure it’s objectionable or worth optimizing but thought it should be noted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing that out. I have done some testing and this doesn't seem to introduce any performance issues, even in the places where this might get called a lot, like dragging the mouse around the custom color picker, so given the extra complexity it would add in order to better track when and when not to run this, and the fact that is now only explicitly run in event handlers, I don't think it is worth trying to optimise it at this stage.

@glendaviesnz
Copy link
Contributor Author

One issue I'm seeing now is that for an image that in its 0 opacity (and that doesn't have an overlay color set) results in dark text on trunk

@andrewserong I think this was due to the missing awaits, which I have now added and it seems to work as expected now.

@@ -88,8 +88,8 @@ exports[`Inserting blocks inserts a block in proper place after having clicked \
<p>First paragraph</p>
<!-- /wp:paragraph -->

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it was a bug in the hook that caused isDark attrib to be set to false when a Cover block was first inserted.

I don't think there are any backwards compat issues to it now behaving correctly and not setting this unless a light image or background is selected after the block is inserted, but let me know if you can see any issues.

I added a Cover block placeholder to a post on trunk and saved, checked out this PR and reloaded the post and there were no errors, and selecting color or image on the placeholder worked as expected.

@glendaviesnz
Copy link
Contributor Author

Switching from hook to a standard method is a reasonably big change so probably needs another thorough review sorry.

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 one @glendaviesnz! This is looking really good, each of the event handlers appears to be updating the isDark value as expected and I haven't run into any issues across all actions (uploading media, using featured images, clearing media, changing overlay colors and dimRatio, etc), and it still fixes the flickering issue 👍

Just left a couple of comments — now that there's an await in getColorIsDark, I was wondering if the other handlers need to be "async" if getColorIsDark never returns a promise? Edit: nevermind, I was misunderstanding how async functions work in this way.

Comment on lines 129 to 130
* getCoverIsDark is a method that specifyies if the cover background is dark or not and
* applies the relevant attribute to help ensure that text is visible by default.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: getCoverIsDark no longer applied the attribute, rather it's handled in the block's code. Shall we update this comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, fixed.

packages/block-library/src/cover/shared.js Outdated Show resolved Hide resolved
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.

This is testing great for me! Might be worth waiting for another review before merging, too, just to make sure we're covered since changing this part of the Cover block can be pretty tricky.

Great work here, though, the approach feels like a big improvement for the block, while resolving the tricky flickering issue! ✨

@stokesman
Copy link
Contributor

👋 Hey folks. I've been following along here (and on the issue before this PR). I'm pleased to see how this progressed. The approach matches where my mind first went on thinking of a solution, i.e. "Try not to derive the attribute in an effect when it could be done with event handlers". Still, out of curiosity, I tried an alternate approach in revamping the hook #53317. It solves the issue in my testing and due to its fewer changes might be more compelling from a maintenance perspective. If there’s time available, I'd like it if it could be considered. Much respect to all the great work and collaboration that's gone into this and I'm offering it up in the same spirit.

@glendaviesnz
Copy link
Contributor Author

I tried an alternate approach in revamping the hook #53317

Thanks for this @stokesman, I ran out of time today to get to this, and it is my end of week here, so will try and take a closer look on Monday if someone else doesn't get to it.

@ajlende
Copy link
Contributor

ajlende commented Aug 4, 2023

It seems my old unmerged PR #45076 that improves the heuristic also seems to fix it. The PR already has one approval, but if you'd like to take a look, it might be another alternative. I explained what I think is going on and how it fixes it in the original issue.

@stokesman
Copy link
Contributor

I ran out of time today to get to this, and it is my end of week here, so will try and take a closer look on Monday

Thanks Glen! Now that Alex has brought up his PR, I'm very much in favor of it. So please disregard the PR I made as an alternate to this and instead have a look at Alex’s to see if you agree we should incorporate it first.

@glendaviesnz
Copy link
Contributor Author

glendaviesnz commented Aug 6, 2023

@ajlende, @stokesman, the heuristic used in #45076 does seem to provide a better outcome in terms of the dark/light setting, but for me it does not fix the flickering issue.

I have copied the updated heuristic to this PR, and this gives the same improved results while still also fixing the flickering.

@stokesman, I understand your preference for a small change over a major refactor, especially in a block as commonly used as the Cover. If this was going into 6.3.1 I would also be very onboard with that. If we are thinking 6.4 for this change, given we have a couple of months until the next release I wonder if we should take the opportunity to make a bigger improvement in the structure of this block. This is not really a good use of an effect, and across the project we should be looking to move from the use of effects to update internal state in preference for using event handlers where possible, and this is a good opportunity to do this.

I don't have a strong opinion on this though, so happy to hear what others think. @andrewserong, @Mamaduka what are your thoughts?

@andrewserong
Copy link
Contributor

does seem to provide a better outcome in terms of the dark/light setting, but for me it does not fix the flickering issue.

Yes, I saw the same.

I don't have a strong opinion on this though, so happy to hear what others think

I usually like the idea of trying out smaller changes, and doing things iteratively where we can. In this case, from my perspective, each of the changes are fairly significant in the sense that we'll need to thoroughly test every permutation of changes to the cover block to make sure that the isDark value and block attributes updates are correct, even if the code change itself is fairly small. For example, in #53375, although it's a smaller change, due to the updates to the useEffect and the dependency array, I think we'd still need to be careful that all the changes are behaving as we'd expect.

So in that respect, I'm currently leaning toward the idea of seeing if we can land an approach that we think is what we'd like in the longer term for the block.

I don't feel very strongly about it, though, so happy to hear what everyone else thinks, too!

@stokesman
Copy link
Contributor

My first instinct is to go for an overhaul like this PR because it should be more efficient. On the other hand, I’d like to avoid introducing complexity to an already complex block while possible. I'm glad it’s being considered and don't mind much what decision is reached.

we should be looking to move from the use of effects to update internal state in preference for using event handlers where possible

I agree, though not without exceptions. Strictly speaking, it has not proven possible for this PR. As is, we have to lean on it for the one case anyway so if we reduce complexity by employing it for all cases it seems justifiable.

For example, in #53375, although it's a smaller change, due to the updates to the useEffect and the dependency array, I think we'd still need to be careful that all the changes are behaving as we'd expect.

I'm glad you saw that PR. I just want to point out the change to the dependency array is only the addition of two dependencies that are stable. They could be left out, just as they are in trunk and as they are in the similar effect in this PR. Anyway, I agree no matter what we do care must be taken!

@glendaviesnz
Copy link
Contributor Author

glendaviesnz commented Aug 7, 2023

Strictly speaking, it has not proven possible for this PR.

Even though one case does still use an effect, in 4 out of the 6 use cases we are now bundling the attribute update with the source event which I think is enough of a potential efficiency gain to still warrant the change.

Good discussion @stokesman, thanks for raising it.

@Mamaduka did you have any additional thoughts to add before we try and make a decision on this?

@Mamaduka
Copy link
Member

Mamaduka commented Aug 7, 2023

I like the direction of this PR. The only time the effect is used is with the featured image.

Every time we refactor away from using effects, it would require a lot of changes, but that's good, IMO.

Copy link
Contributor

@ajlende ajlende 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 all the listed scenarios:

  • When an overlay image is added, changed or removed
  • When the featured image is selected as the overlay image, or removed from the overlay
  • When the overlay color is changed
  • When the overlay color is removed
  • When the dimRatio is changed

They all seem to be working for me. I was never able to reproduce the original flickering issue, but things do still work as I expect here.

@glendaviesnz glendaviesnz merged commit e22e7be into trunk Aug 7, 2023
@glendaviesnz glendaviesnz deleted the fix/cover-jitter-bug branch August 7, 2023 21:46
@github-actions github-actions bot added this to the Gutenberg 16.5 milestone Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Cover Affects the Cover Block - used to display content laid over a background image [Type] Bug An existing feature does not function as intended
Projects
Development

Successfully merging this pull request may close these issues.

Cover Block: flickering in site editor page view when used within a synced pattern
5 participants