-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Changes from all commits
be6575d
19579ca
e00268d
ce78698
a710eca
763b269
574656f
d263ff8
345233b
f5630e1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,18 +6,17 @@ | |
|
||
// Defaults. | ||
const DEFAULT_MAXIMUM_VIEWPORT_WIDTH = '1600px'; | ||
const DEFAULT_MINIMUM_VIEWPORT_WIDTH = '768px'; | ||
const DEFAULT_MINIMUM_VIEWPORT_WIDTH = '320px'; | ||
const DEFAULT_SCALE_FACTOR = 1; | ||
const DEFAULT_MINIMUM_FONT_SIZE_FACTOR = 0.75; | ||
const DEFAULT_MINIMUM_FONT_SIZE_FACTOR_MIN = 0.25; | ||
const DEFAULT_MINIMUM_FONT_SIZE_FACTOR_MAX = 0.75; | ||
const DEFAULT_MINIMUM_FONT_SIZE_LIMIT = '14px'; | ||
|
||
/** | ||
* Computes a fluid font-size value that uses clamp(). A minimum and maximum | ||
* font size OR a single font size can be specified. | ||
* | ||
* If a single font size is specified, it is scaled up and down by | ||
* minimumFontSizeFactor and maximumFontSizeFactor to arrive at the minimum and | ||
* maximum sizes. | ||
* If a single font size is specified, it is scaled up and down using a logarithmic scale. | ||
* | ||
* @example | ||
* ```js | ||
|
@@ -33,14 +32,13 @@ const DEFAULT_MINIMUM_FONT_SIZE_LIMIT = '14px'; | |
* ``` | ||
* | ||
* @param {Object} args | ||
* @param {?string} args.minimumViewPortWidth Minimum viewport size from which type will have fluidity. Optional if fontSize is specified. | ||
* @param {?string} args.maximumViewPortWidth Maximum size up to which type will have fluidity. Optional if fontSize is specified. | ||
* @param {string|number} [args.fontSize] Size to derive maximumFontSize and minimumFontSize from, if necessary. Optional if minimumFontSize and maximumFontSize are specified. | ||
* @param {?string} args.maximumFontSize Maximum font size for any clamp() calculation. Optional. | ||
* @param {?string} args.minimumFontSize Minimum font size for any clamp() calculation. Optional. | ||
* @param {?number} args.scaleFactor A scale factor to determine how fast a font scales within boundaries. Optional. | ||
* @param {?number} args.minimumFontSizeFactor How much to scale defaultFontSize by to derive minimumFontSize. Optional. | ||
* @param {?string} args.minimumFontSizeLimit The smallest a calculated font size may be. Optional. | ||
* @param {?string} args.minimumViewPortWidth Minimum viewport size from which type will have fluidity. Optional if fontSize is specified. | ||
* @param {?string} args.maximumViewPortWidth Maximum size up to which type will have fluidity. Optional if fontSize is specified. | ||
* @param {string|number} [args.fontSize] Size to derive maximumFontSize and minimumFontSize from, if necessary. Optional if minimumFontSize and maximumFontSize are specified. | ||
* @param {?string} args.maximumFontSize Maximum font size for any clamp() calculation. Optional. | ||
* @param {?string} args.minimumFontSize Minimum font size for any clamp() calculation. Optional. | ||
* @param {?number} args.scaleFactor A scale factor to determine how fast a font scales within boundaries. Optional. | ||
* @param {?string} args.minimumFontSizeLimit The smallest a calculated font size may be. Optional. | ||
* | ||
* @return {string|null} A font-size value using clamp(). | ||
*/ | ||
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will removing this parameter cause any back compat issues, given that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for raising that. Yeah, it's possible. The 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 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. |
||
minimumFontSizeLimit, | ||
} ) { | ||
// Validate incoming settings and set defaults. | ||
|
@@ -106,6 +103,26 @@ export function getComputedFluidTypographyValue( { | |
* the given font size multiplied by the min font size scale factor. | ||
*/ | ||
if ( ! minimumFontSize ) { | ||
const fontSizeValueInPx = | ||
fontSizeParsed.unit === 'px' | ||
? fontSizeParsed.value | ||
: fontSizeParsed.value * 16; | ||
|
||
/* | ||
* 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 | ||
); | ||
|
||
// Calculates the minimum font size. | ||
const calculatedMinimumFontSize = roundToPrecision( | ||
fontSizeParsed.value * minimumFontSizeFactor, | ||
3 | ||
|
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
👍