-
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
Allow responsive styles via theme.json #27107
Comments
My 2c is that we should not strive for pixel-perfect themes but instead we should move towards a pixel-agnostic way of thinking. Responsive typography can be achieved without breakpoints and one way to do that would be something like this: {
"global": {
"settings": {
"custom": {
"typo": {
"rootSize": 16,
"adaptiveRatio": 0.8,
"scale": 1.333,
}
},
"typography": {
"fontSizes": [
{
"name": "small",
"slug": "small",
"size": "calc(var(--wp--preset--font-size--normal) / var(--wp--custom--typo--scale))"
},
{
"name": "Normal",
"slug": "normal",
"size": "calc(var(--wp--custom--typo--root-size) * 1px + var(--wp--custom--typo--adaptive-ratio) * 1vw)"
},
{
"name": "Medium",
"slug": "medium",
"size": "calc(var(--wp--preset--font-size--normal) * var(--wp--custom--typo--scale))"
},
{
"name": "large",
"slug": "large",
"size": "calc(var(--wp--preset--font-size--medium) * var(--wp--custom--typo--scale))"
},
{
"name": "Huge",
"slug": "huge",
"size": "calc(var(--wp--preset--font-size--large) * var(--wp--custom--typo--scale))"
}
]
}
}
}
} The above example has responsive-typography AND a consistent typography scale, all in one. I understand that most developers & designers have been working with breakpoints for a decade and it's hard to start thinking in a pixel-agnostic way, but it's where we should be going. I wouldn't want us to start advocating for the use of breakpoints when there are better ways out there to handle things... |
Actually, I didn't know we could do this, and I totally agree with you on breakpoints (at least in this particular case). |
That's a good point @aristath! However I do think it's idealistic, because there are use cases where a) you might want to change a variable other than size or b) having a responsive type scale would not work for the design. Take Seedlet's primary navigation for example:
The type size becomes significantly larger on mobile, in addition to the font-family itself changing. |
@aristath Your approach looks great. But not only is what @jffng an important point. @media queries may also be useful and required for certain base elements and not just the min/max-width features. It's clear that breakpoints are not required for an adaptive typography scale, which we certainly need to be able to set as theme designers/developers. Yet, I personally still see a need to make adjustments to our base styles, based on the current viewing environment or, user choice for example https://www.w3.org/TR/mediaqueries-5/#mf-user-preferences. (Maybe not the best example for font-size, but see it as on the same trail.) I only just got to the party. I'm super late. But am looking at this situation directly at the moment when writing my first block themes and attempting to write as little CSS in the theme, especially overriding block editor styles! |
At least for now not having breakpoint support would be a hard blocker against using theme.json and FSE. This is especially true for reasons mentioned above, where certain user interface elements should behave differently or not be visible at all on certain screens. Media Queries are not going anywhere for the time being and considering all major css frameworks are building heavily on them makes me think that this is more wishful thinking than designing and building for what is already needed. |
I don't think it needs to be all-or-nothing. I've been trying to use techniques like |
Just noting that the fluid typography approach mentioned above as a solution, does not in fact work with the Block Editor preview modes, as it is based on the width of the viewport. It also does not take the editor UI sidebars into consideration. |
Allowing media query in theme.json would also make it easier to integrate Bootstrap 5 values into theme.json. For example, bootstrap-reboot.css (Bootstrap Reboot v5.0.2 (https://getbootstrap.com/docs/5.1/content/reboot/#css-variable)) defines h1 property as this: For now, I'll use CSS Max, Min and Camp to approximate, but browsers prior to 2020 don't all support these newer CSS features. By the way, there is a new excellent free Bootstrap plugin for WordPress called "All Bootstrap Blocks Plugin" by AREOI (https://areoi.io/all-bootstrap-blocks/). Which does an excellent job of integrating Bootstrap with WordPress. (I am not affiliated with the developer.) |
Hi everyone. I'd like to expand on this with another bit of context.
This will generate 6 sizes like so :
Now imagine you apply the preset spacing 80 to an element using the margin slider, it will give you the following result :
My original intention would be to wonder if I could decrease the spacingScale increment value on smaller screens, so that I can keep my variable names, but have lower generated sizes based on a media query. Is there a way to generate dynamic values in this case? How would you address this rather common use case of reducing margins responsively so it stays relevant and doesn't look too gigantic on mobile? Thank you. |
While for some people fluid type works well, nearly anyone who has a high profile website designed by an agency knows that they will often provide two mockups: mobile and desktop. Even if there are more screen sizes, hundreds of thousands of people design to fixed with I'm really surprised there hasn't been more conversation on this topic, as I see many people elsewhere demanding this feature as well. Rather silly that to have, for example, a heading of font size 2rem and 3rem on desktop, one is completely unable to use the gutenberg block and must make a custom component called a heading, register the block, and then invoke the block, and that this is true for literally all type across the entire website. Let's say you get a design that specifies h3 are 2rem on mobile and past 1024px are 3rem. This is literally impossible to implement without making a custom component for every single heading, or making a heading component with a huge drop down, not to mention paragraphs, subheadings, links, etc. It's just absurd. Everyone is forced into this fluid design or completely unable to use the |
It would be awesome if this ends up in production soon |
Breakpoints are current industry standard for responsive website, which very often does block element restacking on different screens (i.e. from 3 columns to 2) and fluid clamping with not work for many such scenarios. |
media hover:noneThese styles will turn iinks in to hoverable objects but not on hover less devices (touch screens): /* default hover effect */
@media (hover: hover) {
a {
color: inherit; /* use color of surrounding element */
text-decoration: none;
}
/* when hover is supported */
a:hover {
color: blue; /* use default color of links, blue */
text-decoration: underline;
}
} Support should be included not only for sizes but also for featrure media queries. |
Let me share my opinion on fluid vs breakpoints, given I do produce fluid themes. As @justingolden21 said, you often receive two (or more) designs built for particular screen. I am currently working with five, at 2560, 1920, 922, 921 and 357. I convert those designs to fluid design, typography is fluid (but fluid in sizes from 2560-922 and 921-357, two different formulas with breakpoint) and resizing elements is fluid, again often with breapoint at 921, where things start to rearrange to fit smaller screens. This is in-line with what @aristath wrote. While I admit that fluid design is good idea, since it provides better UI across various devices, it still needs breakpoints. There is much time I spend writing css instead of theme.json because some other elements change properties when above/below certain dimensions. For example:
So my conclusion is that breakpoints are still needed and will be needed, because mobile devices provide different challenges compared to desktop (because of small screen everything must be comparatively bigger to be readable and due to narrow space things must be re-shaped and rearranged). So I vote for introduction of media queries into theme.json, but authors should be warned to use them sparingly and try to achieve fluid design instead. Regarding fonts proposition, @aristath 's proposition has one missing point: font sizes in theme.json definition should be connected with screen widths, it is not developer-friendly to fix fluid typography to certain screen widths like it is now. To help with managing fluid designs more easily, I even created this small library a while ago: |
Hello!
In the theme.json, it would be useful to specify different preset values depending on the viewport size.
The specific use case that comes to mind is font sizes. Just throwing this out there, but something like:
As it stands, theme authors would have to specify one value in the theme.json and override it in CSS via a media query.
cc @MaggieCabrera @kjellr @nosolosw
The text was updated successfully, but these errors were encountered: