-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Block supports: add fluid typography #39529
Conversation
6f27bb7
to
4be344a
Compare
4be344a
to
d016da6
Compare
c129ab8
to
a6942b2
Compare
Just curious... :root { /* Just an example */
--root-size: 14;
--fluid-ratio: 0.8;
font-size: calc(var(--root-size) * 1px + var(--fluid-ratio) * 1vw);
} In this scenario there is no upper limit, because if the user is on an 80-inch 16K screen, we may not want to limit the font-size to something like 30px... In their case that would be too small. The I don't have anything against using |
One of the trickiest challenges to solve is to add fluid typography with as little UI as possible, while at the same time accommodating both themes that go edge to edge in the viewport, and themes that have a max-width. One approach I've explored to this leverages clamp and calc both to accomplish a max-width being built into the single formula that lets it stop scaling up when reaching the max-width value. I use this on my site at the moment, and it's working well: The key bit here is that the font stops scaling upwards when the site max-width is reached. If it continued scaling to the viewport width, it would break the layout. |
Yeah, it's a challenge. |
Definitely, but it breaks apart still if you add a max-width for the main column that isn't em-based, right? |
I had even those defined in relation to the root font-size... (see theme.json). TBH I haven't checked if it still works 'cause it was using an older format for the theme.json file and I haven't updated it in a while. Maybe it does break now, but I don't think it should 🤔 |
Thanks for the ideas and for kicking off the discussion @aristath and @jasmussen 💟 I admit, I hadn't read very widely on this topic before starting this work, so your input is valuable. I hope it elucidates some of the reasoning that got me this far. I'm not wedded to any particular point.
This is an experiment and therefore doesn't attempt to dictate a direction. The variables I was considering were:
The reason I chose to experiment with
To expand on the last point, currently If fluid typography is enabled however, I tried a couple of clamp approaches already (1, 2). The current version has the advantage of working (mostly) well with varying units, allows specific font sizes to opt-out of fluidness, and doesn't introduce any esoteric
Thanks for this example, it's one I hadn't considered. To be honest, I'm all for something simpler. A real challenge has been to make it generic enough to tweak, but not specific enough so that, if we ever make changes to the algorithm it will break sites. ❓ Would you expect designers to want to have a fixed fluid ratio across all font variations?
Are there any other cases aside from the one you mentioned we should be taking into account? CSS clamp, at least in my recent reading, has become popular since CSS Tricks https://css-tricks.com/simplified-fluid-typography/ wrote about it in 2019 and there are consequently quite of lot implementations, for example https://fluid-typography.netlify.app/, https://fluidtypography.com/ and https://www.fluid-type-scale.com/. Each come with their own constraints and assumptions, e.g., some assume Given the complexity of some of the formulas, I'm still skeptical about whether we should commit to one over the other. Testing over various themes and layouts will hopefully yield the answer!
If we offer fluid typography as an activation option, with Gutenberg taking care of the implementation, there's nothing preventing theme authors from keeping it disabled and defining whichever fluid typography values they desire in I'm not defending I'm open to ideas and advice about how we can offer a scalable and easy-to-use responsive typography to theme authors, especially since, whatever we decide now, may affect any future editor implementations such as #33543. Before I even started this PR, and with nary a foggy clue about how to implement it, I had envisaged a scalable path to maturity, whereby we initially exposed a single theme.json property that theme authors could toggle e.g., After some testing I dismissed this idea mainly due to the fact that it created too many system constraints: I had to tweak my font sizes to accommodate a hidden formula, and as soon as the system changed, whether via disabling fluid type or changing the algorithm, they became invalid. That's still true of
I like this idea. Having a way to bypass the built-in fluid formula is a great suggestion, and one I think might mitigate my concerns about system constraints above. ❓ What would be a neat way to represent this option? {
"size": "3.125rem",
"maxSize": "5rem",
"fluidSize": "3.464vw + 2.229rem",
"slug": "walloping",
"name": "Walloping"
}, |
Thank you @ramonjd for expanding on your thinking! Posts like these are definitely helpful and will help us find an optimal solution to these issues ❤️
I think adding a |
Regardless of the implementation (and absolutely, the simpler that works, the better!) it seems a useful constraint to consider is to start with the least user interface we can. As we develop and explore, it will become clearer what has to be surfaced, whereas it's always harder to move in the other direction. |
+100 |
I concur! I was running with the premise that, at first, there would be no user-facing controls at all, with fluid typography being enabled via theme.json properties. The rationale would be that we could test our API choices first, then let any lessons guide how we express fluid typography in the UI. This is particularly important because we'd have to create a JS implementation for the editor, so my hope is that it'd create less work. It might be an on/off representation of the existing theme.json "toggle": It's also one of the reasons why I thought targeting
Good one. I'll definitely play around with that. 🍺 There's nothing preventing theme authors from entering any type of fluid font size values for any element or block right now. The advantage I see of extending the typography theme.json API is that we would allow themes to define font-size values for when fluid typography is enabled or disabled (eventually) in the UI. So even something like this would work: {
"size": "3.125rem",
"fluidSize": "clamp(3.125rem, calc(7 / 80 * 100vw), 7rem )",
"slug": "walloping",
"name": "Walloping"
}, When fluid font sizes is enabled we'd use Or we could look at a more deterministic model {
"size": "3.125rem",
"fluidSizes": [ "3.125rem", "calc(7 / 80 * 100vw)", "7rem" ],
"slug": "walloping",
"name": "Walloping"
}, Such an approach would offer full control over // $minSize
// $maxSize
// $fluidFormula
// $fluidFormula could be something like calc(var(--root-size) * 1px + var(--fluid-ratio) * 1vw)
if ( $fluidFormula && $! maxSize && ! $minSize ) {
return "$fluidFormula;"
}
if ( $minSize && $fluidFormula && $maxSize ) {
return "clamp($minSize, $fluidFormula, $maxSize)";
}
// Next iteration...
if ( $minViewportWidth && $maxViewportWidth ) {
// Return a clamp with a formula that constrains fluid between min and max.
}
// We could even play around with returning `min()` and `max()` depending on the combination of available parameters
// 🤷 YOLO
if ( $maxViewportWidth && $maxSize && ! $minSize && ! $minViewportWidth ) {
return "min(maxViewportWidth, maxSize);"
} |
Just dropping a link to some related discussion: #34641 (comment) |
eb2f0e0
to
e7d4648
Compare
@ramonjd What's the next steps on this one? |
e7d4648
to
89dcbbf
Compare
I think the direction needs some input in general. Given that it's an extension, albeit an experimental one, of the theme.json there might be more canonical way of introducing this feature. At this stage we've swung the pendulum both ways: I've catered for maximum flexibility (ability to input any fluid formula) and an internal implementation based on The latter allows theme authors to input mix and max values and have Gutenberg take care of the implementation. It serves all tastes, but maybe it goes too far in one direction? |
Is it worth taking this out of draft to get more exposure and feedback? Also get some theme folks involved, I've pinged a few. |
This commit introduces fluid typography block supports and switches to use the Style Engine for typography and colors. The motivation for fluid typography block supports: >"Fluid typography" describes how a site's font sizes adapt to every change in screen size, for example, growing larger as the viewport width increases, or smaller as it decreases. > >Font sizes can smoothly scale between minimum and maximum viewport widths. Typography changes introduced from Gutenberg: * Uses the Style Engine to generate the CSS and classnames in `wp_apply_typography_support()`. * Introduces `wp_typography_get_preset_inline_style_value()` for backwards-compatibility. * Introduces a private internal function called `wp_get_typography_value_and_unit()`, for checking and getting typography unit and value. * Introduces a private internal function called `wp_get_computed_fluid_typography_value()`, for an internal implementation of CSS `clamp()`. * Deprecates `wp_typography_get_css_variable_inline_style()`. References: * [WordPress/gutenberg#40332 WordPress/gutenberg PR 40332] Style Engine: add typography and color to backend * [WordPress/gutenberg#39529 WordPress/gutenberg PR 39529] Block supports: add fluid typography Follow-up to [53076], [52302], [52069], [51089], [50761], [49226]. Props ramonopoly, youknowriad, aristath, oandregal, aaronrobertshaw, cbirdsong, jorgefilipecosta, ironprogrammer, hellofromTonya. See #56467. git-svn-id: https://develop.svn.wordpress.org/trunk@54260 602fd350-edb4-49c9-b593-d223f7449a82
This commit introduces fluid typography block supports and switches to use the Style Engine for typography and colors. The motivation for fluid typography block supports: >"Fluid typography" describes how a site's font sizes adapt to every change in screen size, for example, growing larger as the viewport width increases, or smaller as it decreases. > >Font sizes can smoothly scale between minimum and maximum viewport widths. Typography changes introduced from Gutenberg: * Uses the Style Engine to generate the CSS and classnames in `wp_apply_typography_support()`. * Introduces `wp_typography_get_preset_inline_style_value()` for backwards-compatibility. * Introduces a private internal function called `wp_get_typography_value_and_unit()`, for checking and getting typography unit and value. * Introduces a private internal function called `wp_get_computed_fluid_typography_value()`, for an internal implementation of CSS `clamp()`. * Deprecates `wp_typography_get_css_variable_inline_style()`. References: * [WordPress/gutenberg#40332 WordPress/gutenberg PR 40332] Style Engine: add typography and color to backend * [WordPress/gutenberg#39529 WordPress/gutenberg PR 39529] Block supports: add fluid typography Follow-up to [53076], [52302], [52069], [51089], [50761], [49226]. Props ramonopoly, youknowriad, aristath, oandregal, aaronrobertshaw, cbirdsong, jorgefilipecosta, ironprogrammer, hellofromTonya. See #56467. Built from https://develop.svn.wordpress.org/trunk@54260 git-svn-id: http://core.svn.wordpress.org/trunk@53819 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit introduces fluid typography block supports and switches to use the Style Engine for typography and colors. The motivation for fluid typography block supports: >"Fluid typography" describes how a site's font sizes adapt to every change in screen size, for example, growing larger as the viewport width increases, or smaller as it decreases. > >Font sizes can smoothly scale between minimum and maximum viewport widths. Typography changes introduced from Gutenberg: * Uses the Style Engine to generate the CSS and classnames in `wp_apply_typography_support()`. * Introduces `wp_typography_get_preset_inline_style_value()` for backwards-compatibility. * Introduces a private internal function called `wp_get_typography_value_and_unit()`, for checking and getting typography unit and value. * Introduces a private internal function called `wp_get_computed_fluid_typography_value()`, for an internal implementation of CSS `clamp()`. * Deprecates `wp_typography_get_css_variable_inline_style()`. References: * [WordPress/gutenberg#40332 WordPress/gutenberg PR 40332] Style Engine: add typography and color to backend * [WordPress/gutenberg#39529 WordPress/gutenberg PR 39529] Block supports: add fluid typography Follow-up to [53076], [52302], [52069], [51089], [50761], [49226]. Props ramonopoly, youknowriad, aristath, oandregal, aaronrobertshaw, cbirdsong, jorgefilipecosta, ironprogrammer, hellofromTonya. See #56467. Built from https://develop.svn.wordpress.org/trunk@54260 git-svn-id: https://core.svn.wordpress.org/trunk@53819 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit introduces fluid typography block supports and switches to use the Style Engine for typography and colors. The motivation for fluid typography block supports: >"Fluid typography" describes how a site's font sizes adapt to every change in screen size, for example, growing larger as the viewport width increases, or smaller as it decreases. > >Font sizes can smoothly scale between minimum and maximum viewport widths. Typography changes introduced from Gutenberg: * Uses the Style Engine to generate the CSS and classnames in `wp_apply_typography_support()`. * Introduces `wp_typography_get_preset_inline_style_value()` for backwards-compatibility. * Introduces a private internal function called `wp_get_typography_value_and_unit()`, for checking and getting typography unit and value. * Introduces a private internal function called `wp_get_computed_fluid_typography_value()`, for an internal implementation of CSS `clamp()`. * Deprecates `wp_typography_get_css_variable_inline_style()`. References: * [WordPress/gutenberg#40332 WordPress/gutenberg PR 40332] Style Engine: add typography and color to backend * [WordPress/gutenberg#39529 WordPress/gutenberg PR 39529] Block supports: add fluid typography Follow-up to [53076], [52302], [52069], [51089], [50761], [49226]. Props ramonopoly, youknowriad, aristath, oandregal, aaronrobertshaw, cbirdsong, jorgefilipecosta, ironprogrammer, hellofromTonya. See #56467. git-svn-id: https://develop.svn.wordpress.org/trunk@54260 602fd350-edb4-49c9-b593-d223f7449a82
Resolves #24480
What?
This is an experimental PR that implements fluid typography via opinionated calc/clamp formulae.
It adds new properties to theme.json settings and filters the
settings.typography.fontSizes
presets intheme.json
.Skip to testing instructions
Motivation
"Fluid typography" describes how a site's font sizes adapt to every change in screen size, for example, growing larger as the viewport width increases, or smaller as it decreases.
Font sizes can smoothly scale between minimum and maximum viewport widths.
2022-07-15.09.32.52.mp4
Contrast that idea with font sizes that respond to specific viewport sizes, such as those defined by media queries, but do nothing in between those sizes.
Theme.json already allows authors to insert their own fluid font size values. This won't change, but this PR offers it to folks who don't want to worry about the implementation details.
Alternatives
This PR is an alternative to #33543
Design
The gist is that, by opting into fluid typography, theme.json authors can generate
--wp--preset--font-size--*
CSS vars that they can use throughout the theme.json styles.To opt into fluid typography
typography.fluid
should betrue
:For example:
For now I'm working with
px
,em
andrem
units for widths and font sizes to make things 'relatively' easier 😄❗ Furthermore, it's important to support (and ultimately recommend) the use of relative sizes such as
rem
to encourage accessibility. The reason being is that we're setting the font size to scale proportionally with whichever base size the browser sets.Fluid font sizes are constrained by static min and max viewport width sizes
1600px
and768px
.Later we'll add configurable viewport width sizes.
Min and max font size values are first taken from
typography.fontSizes
. Where either max or min or both are not found, we'll create some dynamically using min and max font sizes factors. Later we'll add configurable factors.What about custom font-sizes set by the user in the editor?
The current assumption is that when a user sets a font-size, it'll overwrite any theme preset anyway. Plus, that leads us down the path of how and where to generate the styles in the JS.
Drawbacks
Testing Instructions
Empty theme is a good one to test with. Here are some test HTML + theme.json settings.
sample editor code
Sample theme.json
☑️ Then give a few elements this style either in
theme.json
or via the editor using the Typography font size control.☑️ Check out the frontend.
☑️ Change the font size in your browser, and make sure the font sizes that use
rem
orem
scale proportionately.☑️ Run the unit tests
Adoption Strategy
Initially we'd release this to theme authors via the theme.json interface only.
The intention is to garner feedback on the formulae and API, before we think about any editor UI and, beyond that, introducing fluidity to other properties such as spacing.
Since #34334 classic theme stylesheets contain core presets. I'm not yet sure of the implications, but it's something to keep in mind.
Later editions
Based on feedback we might like to enable configurable settings such as fluid scale rate and viewport size boundaries, e.g.,
How do we teach this
A reference alone under Presets would be inadequate.
I think we'd need an explanation and demonstration of the options as well.
Related issues/PRs