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

Reader mode: Generate styles for color palette, gradient presets, and font sizes from the theme support features of the primary theme #6042

Merged

Conversation

pierlon
Copy link
Contributor

@pierlon pierlon commented Apr 5, 2021

Summary

Fixes #5229

There are various theme support features that impact how blocks are rendered on the frontend, including:

  • editor-color-palette: “A default set of colors is provided, but themes can register their own and optionally lock users into picking from the defined palette.”
  • editor-gradient-presets: Default set of gradients for block backgrounds, e.g. Cover block.
  • editor-font-sizes: “A default set of sizes is provided, but themes can register their own and optionally lock users into picking from preselected sizes.”

When editing content in the block editor and you select one of the colors from the theme's color palette, the slug of that color is used in a class name on the block. In order for the color to then show up, the stylesheet on the page must have a style rule for that color. This was broken for AMP pages in in Reader mode:

  • In legacy Reader mode, the theme's stylesheet is not loaded so the styles are not present.
  • In new Reader mode, where a separate AMP-compatible theme is used for AMP pages, a completely different theme stylesheet is used which may not have a completely different color palette or which may define completely different color values.

The issue is the same for the theme's font sizes and gradient presets: the style rules are defined in the primary theme's stylesheet which is not loaded in Reader mode.

This issue is fixed by ensuring that the required styles are included in Reader mode:

  • In legacy Reader mode, styles are dynamically computed based on the active theme's support features.
  • When a Reader theme is selected, the theme support features from the active theme are cached in an option so that they can then be retrieved to then write as dynamically-generated style rules.

The cache of the primary theme's theme support features are updated whenever:

  • AMP options are updated.
  • The primary theme is switched.
  • A theme is updated.

Demo 1

With the Twenty Twenty theme active and the following post content:

<!-- wp:paragraph {"backgroundColor":"primary"} -->
<p class="has-primary-background-color has-background">Foobar</p>
<!-- /wp:paragraph -->

<!-- wp:cover {"overlayColor":"accent"} -->
<div class="wp-block-cover has-accent-background-color has-background-dim"><div class="wp-block-cover__inner-container">
<!-- wp:paragraph {"align":"center","placeholder":"Write title…","textColor":"primary","fontSize":"large"} -->
<p class="has-text-align-center has-primary-color has-text-color has-large-font-size">Accent background</p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:cover -->

Here's the before and after with the changes from this PR applied:

Before After
image image

Demo 2

Non-AMP Legacy Reader Before Legacy Reader After 2020 Reader Before 2020 Reader After
image image image image image

Given this block markup with the Twenty Twenty-One theme active:

<!-- wp:cover {"overlayColor":"red"} -->
<div class="wp-block-cover has-red-background-color has-background-dim"><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","backgroundColor":"dark-gray","textColor":"purple","fontSize":"large"} -->
<p class="has-text-align-center has-purple-color has-dark-gray-background-color has-text-color has-background has-large-font-size">Cover background is “Red”, paragraph background is “Dark Gray”, and text is “Purple”.</p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:cover -->

<!-- wp:paragraph {"backgroundColor":"dark-gray"} -->
<p class="has-dark-gray-background-color has-background">Color background is Dark Gray and text color is default!</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"backgroundColor":"dark-gray","textColor":"red"} -->
<p class="has-red-color has-dark-gray-background-color has-text-color has-background">Color background is Dark Gray and text color is Red!</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"backgroundColor":"yellow"} -->
<p class="has-yellow-background-color has-background">Color background is Yellow and text color is default!</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"backgroundColor":"yellow","textColor":"gray"} -->
<p class="has-gray-color has-yellow-background-color has-text-color has-background">Color background is Yellow and text color is Gray!</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Text colors:</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
<ul><li><span class="has-inline-color has-black-color">Black</span></li><li><span class="has-inline-color has-dark-gray-color">Dark Gray</span></li><li><span class="has-inline-color has-gray-color">Gray</span></li><li><span class="has-inline-color has-green-color">Green</span></li><li><span class="has-inline-color has-blue-color">Blue</span></li><li><span class="has-inline-color has-purple-color">Purple</span></li><li><span class="has-inline-color has-red-color">Red</span></li><li><span class="has-inline-color has-orange-color">Orange</span></li><li><span class="has-inline-color has-yellow-color">Yellow</span></li><li><span class="has-inline-color has-white-color">White</span></li></ul>
<!-- /wp:list -->

<!-- wp:paragraph {"backgroundColor":"black","textColor":"white"} -->
<p class="has-white-color has-black-background-color has-text-color has-background">Black background, white text</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"backgroundColor":"dark-gray","textColor":"yellow"} -->
<p class="has-yellow-color has-dark-gray-background-color has-text-color has-background">Dark gray background, yellow text</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"backgroundColor":"gray","textColor":"orange"} -->
<p class="has-orange-color has-gray-background-color has-text-color has-background">Gray background, orange text</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"backgroundColor":"green","textColor":"black"} -->
<p class="has-black-color has-green-background-color has-text-color has-background">Green background, black text</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"backgroundColor":"blue","textColor":"dark-gray"} -->
<p class="has-dark-gray-color has-blue-background-color has-text-color has-background">Blue background, dark gray text</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"backgroundColor":"purple","textColor":"gray"} -->
<p class="has-gray-color has-purple-background-color has-text-color has-background">Purple background, gray text</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"backgroundColor":"red","textColor":"black"} -->
<p class="has-black-color has-red-background-color has-text-color has-background">Red background, black text</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"backgroundColor":"orange","textColor":"dark-gray"} -->
<p class="has-dark-gray-color has-orange-background-color has-text-color has-background">Orange background, dark gray text</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"backgroundColor":"yellow","textColor":"gray"} -->
<p class="has-gray-color has-yellow-background-color has-text-color has-background">Yellow background, gray text</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"backgroundColor":"white","textColor":"black"} -->
<p class="has-black-color has-white-background-color has-text-color has-background">White background, black text</p>
<!-- /wp:paragraph -->

<!-- wp:cover {"gradient":"purple-to-yellow"} -->
<div class="wp-block-cover has-background-dim has-background-gradient has-purple-to-yellow-gradient-background"><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} -->
<p class="has-text-align-center has-large-font-size">Purple To Yellow Gradient</p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:cover -->

<!-- wp:cover {"gradient":"green-to-yellow"} -->
<div class="wp-block-cover has-background-dim has-background-gradient has-green-to-yellow-gradient-background"><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} -->
<p class="has-text-align-center has-large-font-size">Green To Yellow Gradient</p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:cover -->

<!-- wp:cover {"gradient":"red-to-yellow"} -->
<div class="wp-block-cover has-background-dim has-background-gradient has-red-to-yellow-gradient-background"><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} -->
<p class="has-text-align-center has-large-font-size">Red To Yellow Gradient</p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:cover -->

<!-- wp:cover {"gradient":"purple-to-red"} -->
<div class="wp-block-cover has-background-dim has-background-gradient has-purple-to-red-gradient-background"><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} -->
<p class="has-text-align-center has-large-font-size">Purple to Red Gradient</p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:cover -->

<!-- wp:cover {"customGradient":"linear-gradient(160deg,rgb(0,255,0) 0%,rgb(255,0,0) 100%)"} -->
<div class="wp-block-cover has-background-dim has-background-gradient" style="background:linear-gradient(160deg,rgb(0,255,0) 0%,rgb(255,0,0) 100%)"><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} -->
<p class="has-text-align-center has-large-font-size">Custom: Green to Red Gradient</p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:cover -->

<!-- wp:paragraph -->
<p><em>Font sizes:</em></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"fontSize":"extra-small"} -->
<p class="has-extra-small-font-size">Extra small</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"fontSize":"small"} -->
<p class="has-small-font-size">Small</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"fontSize":"normal"} -->
<p class="has-normal-font-size">Normal</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"fontSize":"large"} -->
<p class="has-large-font-size">Large</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"fontSize":"extra-large"} -->
<p class="has-extra-large-font-size">Extra Large</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"fontSize":"huge"} -->
<p class="has-huge-font-size">Huge</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"fontSize":"gigantic"} -->
<p class="has-gigantic-font-size">Gigantic</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"style":{"typography":{"fontSize":10}}} -->
<p style="font-size:10px">Custom (10px)</p>
<!-- /wp:paragraph -->

Checklist

  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@pierlon pierlon requested a review from westonruter April 5, 2021 20:03
@codecov
Copy link

codecov bot commented Apr 5, 2021

Codecov Report

Merging #6042 (8462653) into develop (b89c1a1) will increase coverage by 0.13%.
The diff coverage is 93.93%.

Impacted file tree graph

@@              Coverage Diff              @@
##             develop    #6042      +/-   ##
=============================================
+ Coverage      74.89%   75.03%   +0.13%     
- Complexity      5775     5829      +54     
=============================================
  Files            231      232       +1     
  Lines          17491    17618     +127     
=============================================
+ Hits           13100    13219     +119     
- Misses          4391     4399       +8     
Flag Coverage Δ Complexity Δ
javascript 79.84% <ø> (ø) 0.00 <ø> (ø)
php 74.81% <93.93%> (+0.14%) 5829.00 <59.00> (+54.00)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ Complexity Δ
src/AmpWpPlugin.php 100.00% <ø> (ø) 9.00 <0.00> (ø)
src/ReaderThemeSupportFeatures.php 93.44% <93.44%> (ø) 51.00 <51.00> (?)
includes/amp-helper-functions.php 84.93% <100.00%> (ø) 0.00 <0.00> (ø)
includes/options/class-amp-options-manager.php 92.82% <100.00%> (ø) 75.00 <0.00> (ø)
src/ReaderThemeLoader.php 91.15% <100.00%> (+0.40%) 33.00 <8.00> (+3.00)

@pierlon pierlon self-assigned this Apr 5, 2021
@pierlon pierlon added the WS:Core Work stream for Plugin core label Apr 5, 2021
@pierlon pierlon added this to the v2.1 milestone Apr 6, 2021
@westonruter
Copy link
Member

I've also tried creating a cover block in Twenty Twenty and giving its background color the accent color:

Editor Non-AMP Legacy AMP Twenty Twenty-One Reader
image image image image

The color chosen for Twenty Twenty-One doesn't seem right?

I then made Twenty Twenty-One the active theme, and I opened the editor and I was surprised to see that the Cover block prompted me to select a new color. Without doing so, I and looking at the non-AMP frontend, I then saw the same dark background color as above as when Twenty Twenty-One was the Reader theme and Twenty Twenty was the active theme. In Legacy AMP, no background color is shown at all, whereas when Twenty Twenty is the Reader theme, the pink background shows up again as expected:

Editor Non-AMP Legacy AMP Twenty Twenty Reader
image image image image

The black background in Twenty Twenty-One is coming from the default style rule for the Cover block, whereas in Twenty Twenty there is a style rule for .has-accent-background-color which is missing in Twenty Twenty-One:

Twenty Twenty One Twenty Twenty
image image

If I go ahead and make Twenty Twenty-One the active theme and set the new background color according to the theme, the results are as follows:

Editor Non-AMP Legacy AMP Twenty Twenty Reader
image image image image

(Note the Legacy AMP text color is white when it should be black.)

Twenty Twenty One Twenty Twenty
image (None)

This is a challenge! The lack of standardization across themes poses a key problem. This problem is acknowledged even in the block editor after switching a theme, since the user has to re-pick a theme color if the new theme lacks the same color name from the previous theme.

@westonruter
Copy link
Member

The issue is not limited to the background color either. If I select one theme color “Red” for the background and another theme color “Dark Gray” as the text color, in a Reader theme I get no styling:

Twenty Twenty-One Editor Twenty Twenty AMP Reader Theme
image image

The cover block has no styling because Twenty Twenty has no style rules for .has-red-background-color nor .has-dark-gray-color.

@westonruter
Copy link
Member

I have what may be an acceptable fallback for the Reader theme case in 2347340, where if a Reader theme is being served it will give Cover blocks a default background color of black and text color of white:

Non-AMP AMP
image image

@westonruter westonruter force-pushed the fix/5229-theme-color-not-avail-in-legacy-reader-template branch from 2347340 to a500206 Compare April 17, 2021 04:25
@westonruter
Copy link
Member

I have an idea for how to solve the Reader theme problem. We can store the primary theme's editor-color-palette theme support flag in the AMP options so that they are then available to us when the Reader theme is active. Gonna play around with that idea and push it up.

@westonruter
Copy link
Member

@pierlon Here's what it looks like now with my changes:

Non-AMP Legacy AMP Reader Template 2020 as Reader Theme 2019 as Reader Theme 2013 as Reader Theme
image image image image image

Two observations:

  1. The line spacing in the legacy Reader theme is not quite right.
  2. The 2019 (e.g. dark-gray) and 2013 (e.g. red and yellow) themes have color palettes with some of the same names, which is resulting in the colors in its stylesheet overriding what I'm adding here. When the primary theme and the reader theme assign different colors to the same palette name, which one should win?

…theme-color-not-avail-in-legacy-reader-template

* 'develop' of github.com:ampproject/amp-wp: (35 commits)
  Bump `codecov/codecov-action` to v1.4.1
  Fix chevron for "Context" column not expanding
  Ensure the `source` class is always used
  Remove legacy styles for single error detail summary
  Extract details column to separate SCSS partial
  Extract error details toggle button to separate SCSS partial
  Position stylesheets expand arrow correctly and clean up CSS
  Keep DETAILS closed initially so that expand arrows stay in sync
  Remove obsolete Icon::get_color()
  Preserve hover color for row-title
  Use SCSS stylelint preset and fix SCSS lint issues
  Convert `amp-icons` to SCSS and use alt icons in admin bar
  Ensure expand arrow is positioned relative to stylesheet details button
  Ensure rows with unreviewed errors are styled on AMP Validated URLs page
  Reverse boldness of new-vs-reviewed error rows in block editor
  Fix stylelint error
  Ensure SUMMARY marker is rendered correctly on Safari
  Prevent minor bottom border gap in error details cell
  Revers boldness of new-vs-reviewed error rows
  Be more specific in jsdoc param type definition
  ...
…theme-color-not-avail-in-legacy-reader-template

* 'develop' of github.com:ampproject/amp-wp:
  Make expression reduction clearer
  Ignore directories in CSS source directory
  Map expression in single pass
  Add rel="nofollow noreferrer noopener" to amp-wp.org link in form submission message
  Define a logger for WP CLI in PHPUnit bootstrap
  Scope CSS entries to current Webpack compilation
  Add Webpack plugin to ignore JS and PHP asset files when stylesheet used as entrypoint
  Bump @wordpress/scripts from 14.0.1 to 14.1.0
  Bump @wordpress/escape-html from 1.12.0 to 1.12.1
  Bump @wordpress/is-shallow-equal from 3.1.0 to 3.1.1
  Bump @wordpress/autop from 2.12.0 to 2.12.1
  Bump @wordpress/data from 4.27.0 to 4.27.1
  Bump @wordpress/compose from 3.25.0 to 3.25.1
@westonruter westonruter marked this pull request as ready for review April 21, 2021 23:05
@github-actions
Copy link
Contributor

github-actions bot commented Apr 21, 2021

Plugin builds for 8462653 are ready 🛎️!

@westonruter westonruter changed the title Legacy Reader template: Generate color and background color styles from theme's editor color palette Reader mode: Generate styles for color palette, gradient presets, and font sizes from the theme support features of the primary theme Apr 21, 2021
private function get_theme_support_features() {
$features = [];
foreach ( self::SUPPORTED_FEATURES as $feature_key ) {
$features[ $feature_key ] = current( (array) get_theme_support( $feature_key ) );
Copy link
Member

Choose a reason for hiding this comment

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

We can limit the keys that we store to only include slug, size, color, and gradient.

}
foreach ( $theme_support_features as $support => $feature ) {
if ( is_array( $feature ) ) {
add_theme_support( $support, $feature ); // @todo Problem: $feature is now a subset.
Copy link
Member

Choose a reason for hiding this comment

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

I thought this was a good idea at first, but now it may be problematic. Consider the case where a user has Twenty Thirteen as the primary theme and they select Twenty Twenty-One as the Reader theme. The TT1 theme actually reads from the editor-color-palette to populate a Customizer control for the background color, and the result here is that the color palette from Twenty Thirteen is showing up in the Customizer for Twenty Twenty-One:

image

On one hand that seems kinda cool, but it seems eventually it could cause problems, especially here in the case where I've reduced the theme support props to omit anything but the slug and the other prop that is needed to generate the styles. It could be that a theme wants to use the color values in the Customizer or some other UI as well, and they'd be missing here.

I think we should consider not adding the theme support features from the primary theme. They're not actually used in the generation of the page styles since they're (currently) defined in the theme's style.css. But that could change in the future. There's nothing stopping someone from trying to be be more DRY and configurable and only define the color palette in PHP to then render out the style rules like we're doing here.

The bottom line is that we are printing out the styles from the primary theme which are needed for rendering the blocks. Actually copying the theme support features from the primary theme would extend the scope to beyond the block styles to then affect other aspects of the theme design. As such we can revisit that later.

*/
public function register() {
add_filter( 'amp_options_updating', [ $this, 'filter_amp_options_updating' ] );
add_action( 'after_switch_theme', [ $this, 'update_cached_theme_support' ] );
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't work when switching the theme with WP-CLI.

Copy link
Member

Choose a reason for hiding this comment

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

Now it works with b62a8c8.

Copy link
Member

Choose a reason for hiding this comment

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

Note that when you switch a theme with WP-CLI, the after_switch_theme action will only fire the next time that a user accesses the site.

…theme-color-not-avail-in-legacy-reader-template

* 'develop' of github.com:ampproject/amp-wp:
  Improve JS test coverage
  Remove code incorrectly added in merge conflict resolution
  Pass missing backend props to settings page JS app
  Improve the notice microcopy
  Use info notice and toggle for showing unavailable reader themes
  Ensure carousel nav state gets updated when items count changes
  Add unit test to increase coverage
  Fix indentation issues
  Hide carousel nav if there is just one page
  Remove unused namespace imports
  Remove unused namespace import
  Categorize themes by availability in ReaderThemes context provider
Copy link
Collaborator

@delawski delawski left a comment

Choose a reason for hiding this comment

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

@westonruter @pierlon I've reviewed the code and tested it out locally. Everything seems to be working as expected.

tests/php/test-amp-helper-functions.php Show resolved Hide resolved
tests/php/test-amp-helper-functions.php Show resolved Hide resolved
@westonruter westonruter merged commit bd9f563 into develop Apr 23, 2021
@westonruter westonruter deleted the fix/5229-theme-color-not-avail-in-legacy-reader-template branch April 23, 2021 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Integration: Gutenberg WS:Core Work stream for Plugin core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Paragraph Block with Custom Background Color Not Displaying Text in Contrasting Color
3 participants