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

Fluid typography: use logarithmic scale factor to calculate a min font size #49707

Merged
merged 10 commits into from
Apr 20, 2023

Conversation

ramonjd
Copy link
Member

@ramonjd ramonjd commented Apr 11, 2023

Tracking issue:

This is an experimental PR that serves as an alternative to #49247, whose commits serve as a base (thanks @andrewserong !)

Maybe resolves #49162

What?

This PR introduces a logarithmic scale factor to calculate a minimum font scale that tapers out as the font size increases.

Screenshot 2023-04-14 at 12 55 43 pm

The calculation is only performed where there no min font size is passed to the fluid font size methods.

		/*
		 * The scale factor is a multiplier that affects how quickly the curve will move towards the minimum,
		 * that is, how quickly the size factor reaches 0 given increasing font size values.
		 * For a - b * log2(), lower values of b will make the curve move towards the minimum faster.
		 * The scale factor is constrained between min and max values.
		 */
		const minimumFontSizeFactor = Math.min(
			Math.max(
				1 - 0.075 * Math.log2( fontSizeValueInPx ),
				DEFAULT_MINIMUM_FONT_SIZE_FACTOR_MIN
			),
			DEFAULT_MINIMUM_FONT_SIZE_FACTOR_MAX
		);

The min font scale factor is constrained by min and max values.

There is currently no "ceiling" for minimum font sizes. Rather the scale factor will grow smaller, down to 0.25, as the font size increases.

Why?

The calculation that derives minimum font sizes from font sizes is linear, which means that very large font sizes won't be scaled down to a readable level.

Testing Instructions

I've tested this using the 2023 theme.

In a new post, insert the following example block code. Play around with sizes and values.

Example block code
<!-- wp:paragraph {"style":{"typography":{"fontSize":"8rem","lineHeight":"1.1"}}} -->
<p style="font-size:8rem;line-height:1.1">Loco brings you family recipes, fresh, simple, tasteful ingredients and a nice vibe.</p>
<!-- /wp:paragraph -->

<!-- wp:separator -->
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<!-- /wp:separator -->

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

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

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

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

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

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

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

<!-- wp:separator -->
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<!-- /wp:separator -->

<!-- wp:paragraph {"style":{"typography":{"fontSize":"256px"}}} -->
<p style="font-size:256px">A paragraph at 256px</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"style":{"typography":{"fontSize":"128px"}}} -->
<p style="font-size:128px">A paragraph at 128px</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"style":{"typography":{"fontSize":"96px"}}} -->
<p style="font-size:96px">A paragraph at 96px</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"style":{"typography":{"fontSize":"64px"}}} -->
<p style="font-size:64px">A paragraph at 64px</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"style":{"typography":{"fontSize":"48px"}}} -->
<p style="font-size:48px">A paragraph at 48px</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"style":{"typography":{"fontSize":"32px"}}} -->
<p style="font-size:32px">A paragraph at 32px</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"style":{"typography":{"fontSize":"16px"}}} -->
<p style="font-size:16px">A paragraph at 16px</p>
<!-- /wp:paragraph -->

Run the tests!!

npm run test:unit:php -- --filter WP_Block_Supports_Typography_Test // PHP
npm run test:unit // JS

Screenshots or screencast

2023-04-14.12.57.30.mp4

@ramonjd ramonjd self-assigned this Apr 11, 2023
@ramonjd ramonjd added [Type] Enhancement A suggestion for improvement. [Type] Experimental Experimental feature or API. Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Feature] Typography Font and typography-related issues and PRs labels Apr 11, 2023
@ramonjd ramonjd force-pushed the update/fluid-typography-calculate-min-font-size branch from 349bc5a to 430b09f Compare April 11, 2023 03:39
@github-actions
Copy link

github-actions bot commented Apr 11, 2023

Size Change: +56 B (0%)

Total Size: 1.37 MB

Filename Size Change
build/block-editor/index.min.js 203 kB +56 B (0%)
ℹ️ View Unchanged
Filename Size
build/a11y/index.min.js 993 B
build/annotations/index.min.js 2.78 kB
build/api-fetch/index.min.js 2.27 kB
build/autop/index.min.js 2.15 kB
build/blob/index.min.js 483 B
build/block-directory/index.min.js 7.2 kB
build/block-directory/style-rtl.css 1.04 kB
build/block-directory/style.css 1.04 kB
build/block-editor/content-rtl.css 4.17 kB
build/block-editor/content.css 4.17 kB
build/block-editor/default-editor-styles-rtl.css 403 B
build/block-editor/default-editor-styles.css 403 B
build/block-editor/style-rtl.css 14.6 kB
build/block-editor/style.css 14.6 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 138 B
build/block-library/blocks/audio/theme.css 138 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 91 B
build/block-library/blocks/avatar/style.css 91 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 587 B
build/block-library/blocks/button/editor.css 587 B
build/block-library/blocks/button/style-rtl.css 628 B
build/block-library/blocks/button/style.css 627 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/columns/style-rtl.css 409 B
build/block-library/blocks/columns/style.css 409 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 649 B
build/block-library/blocks/cover/editor.css 651 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-summary/editor-rtl.css 65 B
build/block-library/blocks/details-summary/editor.css 65 B
build/block-library/blocks/details-summary/style-rtl.css 61 B
build/block-library/blocks/details-summary/style.css 61 B
build/block-library/blocks/details/style-rtl.css 54 B
build/block-library/blocks/details/style.css 54 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 138 B
build/block-library/blocks/embed/theme.css 138 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 269 B
build/block-library/blocks/file/style.css 270 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 984 B
build/block-library/blocks/gallery/editor.css 988 B
build/block-library/blocks/gallery/style-rtl.css 1.55 kB
build/block-library/blocks/gallery/style.css 1.55 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 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 340 B
build/block-library/blocks/html/editor.css 341 B
build/block-library/blocks/image/editor-rtl.css 830 B
build/block-library/blocks/image/editor.css 829 B
build/block-library/blocks/image/style-rtl.css 652 B
build/block-library/blocks/image/style.css 652 B
build/block-library/blocks/image/theme-rtl.css 137 B
build/block-library/blocks/image/theme.css 137 B
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 716 B
build/block-library/blocks/navigation-link/editor.css 715 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/editor-rtl.css 2.13 kB
build/block-library/blocks/navigation/editor.css 2.14 kB
build/block-library/blocks/navigation/style-rtl.css 2.22 kB
build/block-library/blocks/navigation/style.css 2.21 kB
build/block-library/blocks/navigation/view-modal.min.js 2.81 kB
build/block-library/blocks/navigation/view.min.js 447 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 501 B
build/block-library/blocks/post-comments-form/style.css 501 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 322 B
build/block-library/blocks/post-featured-image/style.css 322 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 281 B
build/block-library/blocks/post-template/style.css 281 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 103 B
build/block-library/blocks/preformatted/style.css 103 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 288 B
build/block-library/blocks/query-pagination/style.css 284 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 463 B
build/block-library/blocks/query/editor.css 463 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 165 B
build/block-library/blocks/search/editor.css 165 B
build/block-library/blocks/search/style-rtl.css 408 B
build/block-library/blocks/search/style.css 406 B
build/block-library/blocks/search/theme-rtl.css 114 B
build/block-library/blocks/search/theme.css 114 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 329 B
build/block-library/blocks/shortcode/editor.css 329 B
build/block-library/blocks/site-logo/editor-rtl.css 489 B
build/block-library/blocks/site-logo/editor.css 489 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.4 kB
build/block-library/blocks/social-links/style.css 1.39 kB
build/block-library/blocks/spacer/editor-rtl.css 359 B
build/block-library/blocks/spacer/editor.css 359 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 651 B
build/block-library/blocks/table/style.css 650 B
build/block-library/blocks/table/theme-rtl.css 157 B
build/block-library/blocks/table/theme.css 157 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 404 B
build/block-library/blocks/template-part/editor.css 404 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 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 179 B
build/block-library/blocks/video/style.css 179 B
build/block-library/blocks/video/theme-rtl.css 139 B
build/block-library/blocks/video/theme.css 139 B
build/block-library/classic-rtl.css 179 B
build/block-library/classic.css 179 B
build/block-library/common-rtl.css 1.12 kB
build/block-library/common.css 1.12 kB
build/block-library/editor-elements-rtl.css 75 B
build/block-library/editor-elements.css 75 B
build/block-library/editor-rtl.css 11.6 kB
build/block-library/editor.css 11.6 kB
build/block-library/elements-rtl.css 54 B
build/block-library/elements.css 54 B
build/block-library/index.min.js 204 kB
build/block-library/reset-rtl.css 478 B
build/block-library/reset.css 478 B
build/block-library/style-rtl.css 12.8 kB
build/block-library/style.css 12.8 kB
build/block-library/theme-rtl.css 698 B
build/block-library/theme.css 703 B
build/block-serialization-default-parser/index.min.js 1.13 kB
build/block-serialization-spec-parser/index.min.js 2.83 kB
build/blocks/index.min.js 51.1 kB
build/commands/index.min.js 14.8 kB
build/commands/style-rtl.css 1.1 kB
build/commands/style.css 1.09 kB
build/components/index.min.js 208 kB
build/components/style-rtl.css 11.7 kB
build/components/style.css 11.7 kB
build/compose/index.min.js 12.4 kB
build/core-data/index.min.js 16.3 kB
build/customize-widgets/index.min.js 12.2 kB
build/customize-widgets/style-rtl.css 1.41 kB
build/customize-widgets/style.css 1.41 kB
build/data-controls/index.min.js 718 B
build/data/index.min.js 8.68 kB
build/date/index.min.js 40.4 kB
build/deprecated/index.min.js 518 B
build/dom-ready/index.min.js 336 B
build/dom/index.min.js 4.76 kB
build/edit-post/classic-rtl.css 571 B
build/edit-post/classic.css 571 B
build/edit-post/index.min.js 35 kB
build/edit-post/style-rtl.css 7.59 kB
build/edit-post/style.css 7.58 kB
build/edit-site/index.min.js 64.3 kB
build/edit-site/style-rtl.css 10.1 kB
build/edit-site/style.css 10.1 kB
build/edit-widgets/index.min.js 17.3 kB
build/edit-widgets/style-rtl.css 4.56 kB
build/edit-widgets/style.css 4.56 kB
build/editor/index.min.js 45.9 kB
build/editor/style-rtl.css 3.49 kB
build/editor/style.css 3.48 kB
build/element/index.min.js 4.95 kB
build/escape-html/index.min.js 548 B
build/format-library/index.min.js 7.26 kB
build/format-library/style-rtl.css 557 B
build/format-library/style.css 556 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.79 kB
build/keycodes/index.min.js 1.94 kB
build/list-reusable-blocks/index.min.js 2.14 kB
build/list-reusable-blocks/style-rtl.css 865 B
build/list-reusable-blocks/style.css 865 B
build/media-utils/index.min.js 2.99 kB
build/notices/index.min.js 977 B
build/plugins/index.min.js 1.94 kB
build/preferences-persistence/index.min.js 2.23 kB
build/preferences/index.min.js 1.35 kB
build/primitives/index.min.js 960 B
build/priority-queue/index.min.js 1.52 kB
build/private-apis/index.min.js 942 B
build/react-i18n/index.min.js 702 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.75 kB
build/reusable-blocks/index.min.js 2.26 kB
build/reusable-blocks/style-rtl.css 265 B
build/reusable-blocks/style.css 265 B
build/rich-text/index.min.js 11.1 kB
build/server-side-render/index.min.js 2.09 kB
build/shortcode/index.min.js 1.52 kB
build/style-engine/index.min.js 1.55 kB
build/token-list/index.min.js 650 B
build/url/index.min.js 3.74 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 1.09 kB
build/warning/index.min.js 280 B
build/widgets/index.min.js 7.3 kB
build/widgets/style-rtl.css 1.18 kB
build/widgets/style.css 1.18 kB
build/wordcount/index.min.js 1.06 kB

compressed-size-action

@github-actions
Copy link

github-actions bot commented Apr 11, 2023

Flaky tests detected in f5630e1.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4728168606
📝 Reported issues:

Copy link
Contributor

@tellthemachines tellthemachines 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 trying this out! It's working well in testing with a range of different font sizes; the sizes scale very nicely while keeping proportionality between them.

The approach seems sound, my only question below is about changing the function parameters.

@@ -51,7 +49,6 @@ export function getComputedFluidTypographyValue( {
minimumViewPortWidth = DEFAULT_MINIMUM_VIEWPORT_WIDTH,
maximumViewPortWidth = DEFAULT_MAXIMUM_VIEWPORT_WIDTH,
scaleFactor = DEFAULT_SCALE_FACTOR,
minimumFontSizeFactor = DEFAULT_MINIMUM_FONT_SIZE_FACTOR,
Copy link
Contributor

Choose a reason for hiding this comment

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

Will removing this parameter cause any back compat issues, given that getComputedFluidTypographyValue is part of the public block editor API?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for raising that. Yeah, it's possible.

The minimumFontSizeFactor was a value only configurable in the JS function getComputedFluidTypographyValue()

In the PHP, the size factor was hardcoded, and was never an option that could be passed: https://github.com/WordPress/gutenberg/pull/49707/files#diff-2c54d3c8305590cf826f83511a9fd85d5b405470addd62edc3183ff9118177fbL486

In that way, this PR harmonizes the front and backend code 😅 I guess the question is whether there are JS usages out there where one would expect it work as before (in the editor at least).

But my guess is that it'd be relatively safe given that the backend code never allowed customization of this value.

Furthermore, I've removed minimumFontSizeFactor from the JS completely in this PR so passing it won't cause an error.

I can't find any explicit usages on https://wpdirectory.net/, but happy to follow folks' advice if there are backwards compat alarm bells.

Besides this, in general, this PR does represent a change in the way the min values are calculated and therefore has the potential to change the way a site's typography behaves. Also it changes the default min viewport, which will do the same.

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.

Great work @ramonjd, as @tellthemachines mentioned, I love the way this preserves the proportionality between font-sizes — this feels way closer to a magic "it just works" fluidity 👍

One thing I noticed in testing is that the scale in medium viewport sizes feels a little smaller to me than on trunk. I quite like how it feels, but it's noticeable enough that I wonder if it'll potentially read as an unexpected change for sites that already have specific font-sizes selected?

Here's a couple of screenshots to compare:

Trunk vs this PR with the windows set to be half the width of my monitor, with the list view open:

image

As above, but with the list view closed:

image

In terms of technical differences in computed font-size, I observed the following with a browser window that is 1265px wide, with the block inspector open:

  • Trunk without list view showing: 104.307px
  • This PR without list view showing: 92.0719px
  • Trunk with list view showing: 96px
  • This PR with list view showing: 71.6366px

With how aggressively the log scale is applied, I was wondering what the difference in scaling across these mid-range viewports might be if we were to either use a different log base (e.g. Math.log2 instead of a natural logarithm), or if we were to adjust the minimum viewport width?

Overall I love the direction, though, this feels like a good path forward 🎉

$calculated_minimum_font_size = round( $preferred_size['value'] * $default_minimum_font_size_factor, 3 );
$preferred_font_size_in_px = 'px' === $preferred_size['unit'] ? $preferred_size['value'] : $preferred_size['value'] * 16;
// Logarithmic scale factor: Min font scale that tapers out as the font size increases.
$minimum_font_size_factor = 1 - 0.12 * log( $preferred_font_size_in_px );
Copy link
Contributor

Choose a reason for hiding this comment

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

Please excuse my naivety here: where does the 0.12 value come from, is it just a pleasing value, or is it more significant?

Copy link
Member Author

@ramonjd ramonjd Apr 11, 2023

Choose a reason for hiding this comment

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

I agree, it feels a bit magic 😄

It affects how quickly the curve will move towards the minimum, that is, how quickly the size factor reaches 0 given increasing font size values.

For example, switching it to 1 - 0.13 * log( $preferred_font_size_in_px ); results in the following curve:

Screenshot 2023-04-11 at 5 06 24 pm

Compare it with 0.12

Screenshot 2023-04-11 at 1 32 30 pm

@ramonjd
Copy link
Member Author

ramonjd commented Apr 11, 2023

Thanks for the help with this @andrewserong and @tellthemachines

Great suggestions. I play around with it during the week and hopefully arrive at a better balance.

I wonder if it'll potentially read as an unexpected change for sites that already have specific font-sizes selected?

🤔 This is something I'm also concerned about. I'm not sure I can get the algorithm to match its linear counterpart 1:1, but we can try to tweak it to get it close.

$default_scale_factor = 1;
$has_min_font_size = isset( $fluid_settings['minFontSize'] ) && ! empty( gutenberg_get_typography_value_and_unit( $fluid_settings['minFontSize'] ) );
$default_minimum_font_size_limit = $has_min_font_size ? $fluid_settings['minFontSize'] : '14px';
$default_maximum_viewport_width = '1600px';
Copy link
Member

Choose a reason for hiding this comment

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

This may be more useful referencing the layout.wideSize value from theme.json, so that fonts stops growing when wide width elements are maxed out—as the wideSize size is used as the page width in many cases.

Copy link
Member

Choose a reason for hiding this comment

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

Notice how the layout keeps shifting (as the fontSize is still growing), even when the max size is reached:

CleanShot.2023-04-13.at.15.37.49.mp4

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! I agree it's definitely something we can and probably should do. It was slated for the original incarnation of fluid font sizes, but I think it fell off radar since we wanted to test the fallback value. From memory it tested pretty well.

I think a manageable approach would be to throw up a new PR using this PR as the base.

👍

@ramonjd
Copy link
Member Author

ramonjd commented Apr 14, 2023

if we were to either use a different log base (e.g. Math.log2 instead of a natural logarithm), or if we were to adjust the minimum viewport width?

I tried out base 2 log and I think it does match more closely what we have, thanks for the suggestion @andrewserong

I'm going to push I pushed a new PR with @richtabor's suggestion that pulls in the values from settings.layout in order to set viewport defaults.

"settings": {
    "layout": {
        "contentSize": "840px",
        "wideSize": "1100px"
    }
}

ramonjd added a commit that referenced this pull request Apr 14, 2023
Testing out using `settings.layout.wideSize` as the maximum viewport width
Updating tests to come
andrewserong and others added 10 commits April 18, 2023 13:10
…mum font scale that tapers out as the font size increases.

The calculation is only performed where there no min font size is passed to the fluid font size methods.
The min font scale factor is constrained by min and max values.
Tests are updated to reflect the new clamp values.
Docs are updated to reflect API change in JS function (removing minimumFontSizeFactor arg)
@ramonjd ramonjd force-pushed the update/fluid-typography-calculate-min-font-size branch from 8c5b1be to f5630e1 Compare April 18, 2023 03:10
@ramonjd
Copy link
Member Author

ramonjd commented Apr 18, 2023

Just been testing to see if this PR will wildly change the font size behaviour....

Trunk 468px wide This PR 468px wide
Screenshot 2023-04-18 at 2 03 04 pm Screenshot 2023-04-18 at 2 02 35 pm

Note that this PR moves away from a linear scale to calculate minimum font, and also sets the default minimum viewport to 320px which means that larger fonts will scale down further as the viewport narrows.

For example, the minimum font size for "Heading 1 – Colossal" (set at 5.75rem) is calculated as 4.313rem (`* 0.75) in trunk:

2023-04-18.14.00.27.mp4

In this PR it's calculated at 2.937rem:

2023-04-18.14.01.11.mp4

Meaning the font size will appear smaller in narrower viewports.

At 768px however, which is the current, default viewport minimum size, the computed font size of "Heading 1 – Colossal" is 69.008px in trunk and 62.7408px in this PR. So I think the curved scale is a pretty good match.

Interested to see what others can come up with, and opinions on whether this PR will cause problems for themes. I've tested so far on 2023 and some contrived emptytheme examples.

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 feeling quite nice to me with log2.

Referencing my earlier observations looking at a block set to 128px and in a browser window that is 1265px wide, with the block inspector open:

  • Trunk without list view showing: 104.307px
  • Earlier in this PR (log) without list view showing: 92.0719px
  • This PR (log2) without list view showing: 95.66px
  • Trunk with list view showing: 96px
  • This PR (log) with list view showing: 71.6366px
  • This PR (log2) with list view showing: 77.2325px

So log2 is looking really pretty close to ideal to me. At iPhone XR size in Chrome, the font-size winds up being 65.735px which looks about right to me:

image

This PR does still differ from trunk of course, but then that is the intention. I think it's a cool idea to try out tying the max viewport width to the layout wide size, but again I wonder if that could be an unexpected change for some themes? E.g. if wide size is being used for cover or image blocks to be slightly larger than content size in a theme that has a narrower post content area, but might have larger text in headers and footers. I think I'd probably lean toward seeing if we can look at customising the max viewport width in follow-ups if we can?

Also very curious to hear what designers / theme builders might think of this change — it's looking close to me!

@ramonjd
Copy link
Member Author

ramonjd commented Apr 18, 2023

I think I'd probably lean toward seeing if we can look at customising the max viewport width in follow-ups if we can?

Definitely a worthy follow-up. Thanks for testing @andrewserong !

@andrewserong andrewserong requested a review from a team April 18, 2023 05:56
@jasmussen
Copy link
Contributor

jasmussen commented Apr 18, 2023

This is a great one, thanks so much for staying on top of this. Once again I can think of few better than @beafialho or @henriqueiamarino or @iamtakashi to test this. If they are available, an easy way to test would be through http://gutenberg.run/?pull=49707

🙏

@henriqueiamarino
Copy link

Thanks for all the work put into this task fellows.
I believe this works just fine for my particular use, and here's an example of it:

Apr20_FluidType

This is good enough to go IMO. Congrats!

@beafialho
Copy link

Thank you for your work! I think this works well for most cases.

For really huge sizes, in case someone wishes to build something like the example below, it doesn't scale well. But I don't think this is a blocker, as cases like this are exceptions.

huge.mp4
word.mp4

Copy link
Contributor

@jasmussen jasmussen left a comment

Choose a reason for hiding this comment

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

I count two solid sanity checks with potential tweaks or followups. For that reason it can get a green star for the behavior. I'll trust others to double check the code. Should we land this one and tweak the huge headings separately, or amend this one? Thanks for all the work!

@ramonjd
Copy link
Member Author

ramonjd commented Apr 20, 2023

Thanks so much for the speedy reviews everyone!!! 🍺

I'll get the following PR into shape as well:

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.

Great to see folks are liking this change! Just giving this a ✅ from the code side of things, the changes look good to me! ✨

@ramonjd ramonjd added this to the Gutenberg 15.7 milestone Apr 20, 2023
@ramonjd
Copy link
Member Author

ramonjd commented Apr 20, 2023

For really huge sizes, in case someone wishes to build something like the example below, it doesn't scale well. But I don't think this is a blocker, as cases like this are exceptions.

Good point. This is something I'll keep in mind while testing #49815. If folks can reign this sort of behaviour in by adjusting layout values in the editor, even if it is an edge case, I think we'll have won the battle 😆

@andrewserong
Copy link
Contributor

If folks can reign this sort of behaviour in by adjusting layout values in the editor, even if it is an edge case, I think we'll have won the battle

Totally. And I suppose there's also the option of re-introducing a ceiling on the minimum font size as in #49247 if we need to, which could also in theory be a configurable value in theme.json if it came to that?

@ramonjd
Copy link
Member Author

ramonjd commented Apr 20, 2023

And I suppose there's also the option of re-introducing a ceiling on the minimum font size as in #49247 if we need to, which could also in theory be a configurable value in theme.json if it came to that?

Definitely!!!

Also, do folks think this change is worth a make.wordpress.org shoutout given that it changes behaviour slightly?

@andrewserong
Copy link
Contributor

andrewserong commented Apr 20, 2023

Also, do folks think this change is worth a make.wordpress.org shoutout given that it changes behaviour slightly?

Yes, it'd make a good dev note update. Not sure how urgent it would be for a post, though — perhaps a general fluid typography dev note before 6.3 where we'd include each of the updates if we wind up making further tweaks, too?

@ramonjd ramonjd merged commit 52c529f into trunk Apr 20, 2023
@ramonjd ramonjd deleted the update/fluid-typography-calculate-min-font-size branch April 20, 2023 23:34
@annezazu
Copy link
Contributor

@bph for consideration in future "What's new for developers?" monthly updates on the dev blog!

ramonjd added a commit that referenced this pull request Apr 21, 2023
Testing out using `settings.layout.wideSize` as the maximum viewport width
Updating tests to come
ramonjd added a commit that referenced this pull request May 8, 2023
* Fluid Typography: Try adding a ceiling to the calculated minimum font size

* Add test to ensure ceiling is skipped when min font size is provided

* In this commit, we use a logarithmic scale factor to calculate a minimum font scale that tapers out as the font size increases.
The calculation is only performed where there no min font size is passed to the fluid font size methods.
The min font scale factor is constrained by min and max values.
Tests are updated to reflect the new clamp values.
Docs are updated to reflect API change in JS function (removing minimumFontSizeFactor arg)

* Fix lint and JS tests

* EL LINTO DEL DIABLO!

* Based on #49707
Testing out using `settings.layout.wideSize` as the maximum viewport width
Updating tests to come

* Updating tests and generally cleaning up after a bizarre rebase session.

* This commit:
- elaborates on the purpose of layout.wideSize in the theme.json schema

---------

Co-authored-by: Andrew Serong <[email protected]>
@strarsis
Copy link
Contributor

strarsis commented Oct 7, 2023

What if I want to keep the linear factor? It works with a theme design and has the advantage of keeping the same text breaking on different viewport widths. For edge cases I use additional styling, if necessary. Is there an additional option for the factor? Currently I have to disable fluid font size and use the previous CSS clamp values of the earlier Gutenberg as size to achieve the same scaling.

@ramonjd
Copy link
Member Author

ramonjd commented Oct 9, 2023

Thanks for the comment @strarsis

Is there an additional option for the factor? Currently I have to disable fluid font size and use the previous CSS clamp values of the earlier Gutenberg as size to achieve the same scaling.

No there's currently not an option to disable the log scale factor. Note that it only affects fonts that have no defined minimum font size. For example font sizes set manually in the editor, or preset font sizes with no "min" - like "Smallish" below.

"settings": {
    ....
    "typography": {
        "fluid": true,
        "fontSizes": [
            {
                "size": "1rem",
                "slug": "smallish",
                "name": "Smallish"
            },
            {
                "size": "2rem",
                "fluid": {
                    "min": "2rem",
                    "max": "2.5rem"
                },
                "slug": "mediumish",
                "name": "Mediumish"
            },
        ]
}

I guess it would be possible to allow an override to allow for scale factors, but rather than adding another property or flag, we could think about exposing a current default setting.

I haven't tested it much, but perhaps if there's a theme-defined default min font size factor, or min font size then we skip the log stuff. 🤔 I'm not sure.

@strarsis
Copy link
Contributor

strarsis commented Oct 9, 2023

@ramonjd:

Note that it only affects fonts that have no defined minimum font size

🤔 But exactly that was the case for the theme: The font-sizes all have a min value, but the clamp values still all changed after the update. I had to put the previous, linear-factor-using getComputedFluidTypographyValue into my build script.

@ramonjd
Copy link
Member Author

ramonjd commented Oct 9, 2023

But exactly that was the case for the theme: The font-sizes all have a min value, but the clamp values still all changed after the update. I had to put the previous, linear-factor-using getComputedFluidTypographyValue into my build script

I tested with theme.json presets with a min value. I bypassed the changes in this PR and logged out to make sure, and can't see that fonts with a defined min size are even making into the condition block.

Would you mind posting a few of the affected presets here so I could test in a theme.json file?

Also, which version of Gutenberg and WordPress are you using?

Thanks!

@strarsis
Copy link
Contributor

strarsis commented Oct 9, 2023

@ramonjd:

which version of Gutenberg and WordPress are you using

Well, that probably is an explanation: I uninstalled the Gutenberg plugin and use the WordPress core Gutenberg functionality instead, as now WordPress core supports all necessary FSE and other features.
But WordPress core Gutenberg generated different clamp values as with the older Gutenberg plugin that was previously used.

@ramonjd
Copy link
Member Author

ramonjd commented Oct 9, 2023

But WordPress core Gutenberg generated different clamp values as with the older Gutenberg plugin that was previously used.

Aha! I believe that WordPress 6.3 and above should reflect the current behavior in Gutenberg.

6.4, which will be released in a few weeks, for sure 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Typography Font and typography-related issues and PRs Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Enhancement A suggestion for improvement. [Type] Experimental Experimental feature or API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fluid typography: Custom font sizes are not responsive
9 participants