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] Add theme support features from theme.json #7481

Merged
merged 38 commits into from Mar 20, 2023

Conversation

thelovekesh
Copy link
Collaborator

@thelovekesh thelovekesh commented Mar 13, 2023

Summary

Implemented in #6042, reader themes provide support for producing color palettes, gradient presets, and font size styles. This feature leverages get theme support to identify the necessary presets for generating the styles.

Although theme.json can be used by any theme, it serves as a foundation for the new block themes. This PR seeks to enable theme support features styles generation using the theme.json presets to address the block styles breaking issues in reader mode.

Fixes #7471

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).

@thelovekesh thelovekesh force-pushed the fix/reader-themes/block-theme-styles branch from eef4eed to bbe5ac0 Compare March 13, 2023 16:50
@thelovekesh thelovekesh marked this pull request as draft March 13, 2023 16:51
@thelovekesh thelovekesh force-pushed the fix/reader-themes/block-theme-styles branch from bbe5ac0 to b795721 Compare March 13, 2023 16:52
@thelovekesh thelovekesh force-pushed the fix/reader-themes/block-theme-styles branch from b795721 to 2a3d437 Compare March 13, 2023 16:52
@thelovekesh thelovekesh changed the title Add theme support features from theme.json for WP >= 5.9 in Reader Themes [Reader Mode] Add theme support features from theme.json Mar 13, 2023
@thelovekesh
Copy link
Collaborator Author

@westonruter Although the support for theme.json was added in WP 5.8, the required APIs like wp_get_global_settings was added in WP 5.9. Should I look for a workaround to get it worked with WP 5.8 as well or we are good to support it on WP 5.9+?


  1. Right now our CSS unit measurement doesn't look robust as it only handles all values in px

':root .is-%1$s-text, :root .has-%1$s-font-size { font-size: %2$spx; }',

but there might be cases when someone defines them in em or rem.

  1. Also, we will need to calculate values for clamp() to give support for fluid typography.

To solve the above scenarios WP 6.1 has two functions:

but the caveat is they are available prior to WP 6.1.

I'm considering defining them in our own class right now. Please let me know your thoughts?

@westonruter
Copy link
Member

If someone is using block themes, I'd say it's highly unlikely that they would be holding off on updating WordPress core. Therefore, if you want to require WordPress 6.1+ to fully support fluid typography, that's fine with me.

@thelovekesh
Copy link
Collaborator Author

But I think we should update the way of determining measurement units. Someone can use different measurement unit even when fluid typography is not being used.

@thelovekesh
Copy link
Collaborator Author

PR can be tested for fluid typography by disabling CSS tree-shaking till #7291 get fixed.

Comment on lines 437 to 438
? esc_attr( wp_get_typography_font_size_value( $font_size ) )
: esc_attr( $font_size[ self::KEY_SIZE ] )
Copy link
Member

Choose a reason for hiding this comment

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

Minor point, but I don't think esc_attr() is right here. I think strip_tags() is the right choice. See https://github.com/WordPress/wordpress-develop/blob/200868214a1ae0a108dac491677ba82e7541fc8d/src/wp-includes/theme.php#L1891-L1896

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It does make sense, but I am not sure if we will ever get tags here. Or better to never assume and update it to use strip_tags() 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Better to never assume, yeah

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Just realized, we will still need the escaping, as strip_tags is not an escaping utility.

Copy link
Member

Choose a reason for hiding this comment

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

Well, it is somewhat. It's what core uses in wp_custom_css_cb().


// Do not rely on `wp_is_block_theme()` as theme.json can be used in non-block themes.
// See: https://developer.wordpress.org/themes/advanced-topics/theme-json/#a-theme-json-can-be-added-to-any-theme.
return ( is_readable( get_stylesheet_directory() . '/theme.json' ) ? true : is_readable( get_template_directory() . '/theme.json' ) );
Copy link
Member

Choose a reason for hiding this comment

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

This would seem simpler as:

Suggested change
return ( is_readable( get_stylesheet_directory() . '/theme.json' ) ? true : is_readable( get_template_directory() . '/theme.json' ) );
return is_readable( get_stylesheet_directory() . '/theme.json' ) || is_readable( get_template_directory() . '/theme.json' );

Copy link
Member

Choose a reason for hiding this comment

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

Actually, I suggest using wp_theme_has_theme_json() if it exists, or else copy the function as a method into this class:

https://github.com/WordPress/wordpress-develop/blob/200868214a1ae0a108dac491677ba82e7541fc8d/src/wp-includes/global-styles-and-settings.php#L384-L414

Note how it caches the file exists check.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Note how it caches the file exists check.

Oh yes. Bette to copy it since we will need to check for theme.json on WP 5.9+ and this function is added in WP 6.2.

@thelovekesh thelovekesh force-pushed the fix/reader-themes/block-theme-styles branch from 90568f3 to 7487201 Compare March 16, 2023 12:25
@thelovekesh thelovekesh force-pushed the fix/reader-themes/block-theme-styles branch from 5448a45 to 3cc3845 Compare March 16, 2023 14:06
@westonruter
Copy link
Member

westonruter commented Mar 17, 2023

This seems very close. I'm testing with 2023 with the following content:

<!-- wp:paragraph {"fontSize":"xx-large"} -->
<p class="has-xx-large-font-size">Hello!</p>
<!-- /wp:paragraph -->
Primary Theme (2023) Reader Theme (2021) Before Reader Theme (2021) After
image image image

So that's great. What's not quite there yet is the Legacy theme:

image

Something's not right with margin or line-height or something.

@thelovekesh
Copy link
Collaborator Author

Seems like on legacy themes line-height is causing issues.

With line-height: 1.75em

image

With line-height: 1.5

image

@westonruter
Copy link
Member

In TT3, there is this rule applying to the content container:

body .is-layout-flow > * + * {
    margin-block-start: 1.5rem;
    margin-block-end: 0;
}

Applying these styles to .amp-wp-article-content mostly fixes the issue:

image

Not perfectly, but better.

@westonruter
Copy link
Member

OK, let's change this:

line-height: 1.75em;

To just 1.75.

@westonruter
Copy link
Member

westonruter commented Mar 17, 2023

line-height: 1.75em line-height: 1.75
image image

@westonruter
Copy link
Member

Something else that isn't quite right, even with line-height:1.75. Adding a background color to a paragraph that has XXL font size:

<!-- wp:paragraph {"backgroundColor":"primary","fontSize":"xx-large"} -->
<p class="has-primary-background-color has-background has-xx-large-font-size">Hello!</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent interdum, elit non cursus tincidunt, nibh nibh congue metus, ac gravida nunc ipsum ut augue. Donec tempor pulvinar maximus. Sed et tellus nec sapien vulputate sollicitudin. Nam auctor nulla imperdiet, sodales leo ultricies, cursus metus. Aenean ullamcorper dui vel neque luctus faucibus non vel odio. Aenean convallis mattis nisi, nec fringilla sem sagittis eget. Duis ut quam venenatis, blandit elit id, iaculis ipsum. Curabitur tristique euismod nisl, a finibus massa dignissim ac. Nullam nisl orci, imperdiet at libero sed, pulvinar vehicula lacus. Duis eleifend sollicitudin pharetra. Maecenas sapien nunc, mollis a velit sed, ultricies accumsan tortor. Ut accumsan sapien id turpis eleifend, at eleifend orci volutpat. Etiam massa arcu, congue sit amet sem vitae, gravida efficitur sem. Curabitur ornare velit velit, ut accumsan tellus convallis at. Nunc sagittis nisl mauris, nec hendrerit leo sagittis et.</p>
<!-- /wp:paragraph -->
TT3 TT2 Reader Legacy Reader
image image image

@thelovekesh
Copy link
Collaborator Author

Seems like the xx-large font size requires more width on the screen but we have restricted it to 840px in .amp-wp-article which wraps the whole article in it.

max-width: 840px;

@westonruter
Copy link
Member

Oh, if I actually look at Legacy in a mobile viewport it renders fine:

Screen.recording.2023-03-17.13.20.20.webm

We could fix this by adding negative margins:

p.has-background {
    margin-left: -2.375em;
    margin-right: -2.375em;
}
Screen.recording.2023-03-17.13.24.26.webm

@westonruter
Copy link
Member

westonruter commented Mar 17, 2023

Another case to check for: groups with backgrounds.

<!-- wp:group {"style":{"spacing":{"padding":{"top":"var:preset|spacing|40","right":"var:preset|spacing|40","bottom":"var:preset|spacing|40","left":"var:preset|spacing|40"}}},"backgroundColor":"primary","layout":{"type":"constrained"}} -->
<div class="wp-block-group has-primary-background-color has-background" style="padding-top:var(--wp--preset--spacing--40);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--40);padding-left:var(--wp--preset--spacing--40)"><!-- wp:paragraph {"backgroundColor":"luminous-vivid-amber"} -->
<p class="has-luminous-vivid-amber-background-color has-background">First</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"backgroundColor":"vivid-red"} -->
<p class="has-vivid-red-background-color has-background">Second</p>
<!-- /wp:paragraph --></div>
<!-- /wp:group -->

Should look like this:

image

@thelovekesh thelovekesh self-assigned this Mar 20, 2023
@thelovekesh thelovekesh added this to Ready for QA in Ongoing Mar 20, 2023
@thelovekesh thelovekesh moved this from Ready for QA to Ready for Review in Ongoing Mar 20, 2023
@thelovekesh thelovekesh removed their assignment Mar 20, 2023
@thelovekesh thelovekesh removed this from Ready for Review in Ongoing Mar 20, 2023
@westonruter
Copy link
Member

Looking good!

Non-AMP Desktop Legacy AMP Mobile Legacy AMP
image image image

@westonruter westonruter merged commit 5c20e77 into develop Mar 20, 2023
38 of 39 checks passed
@westonruter westonruter deleted the fix/reader-themes/block-theme-styles branch March 20, 2023 19:07
@westonruter westonruter added the Changelogged Whether the issue/PR has been added to release notes. label Mar 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelogged Whether the issue/PR has been added to release notes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invisible cover block background colors
2 participants