Skip to content

Use rem instead of px units #21030

@mifrej

Description

@mifrej
  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

In order to get fully fluent responsive experiences in the MUI components I setup all "sizing properties" to apply em unit if naked number is used as a value for them.

Current Behavior 😯

  1. I passed my custom settings to defaultUnit plugin in JSS with accordance to documentation
    1
  2. Then I feed StylesProvider with such customized preset:
    2
  3. Theme option for htmlFontSize I set to 10.
  4. And my reset CSS has
    3

As a result, some of the properties in the default theme (which are originally implemented with unitless values) are becoming oversized.
Exemplary original styles implementation chunk of code:
4

This is happening because the implicit output unit (while using numbers only) for "sizes like properties" e.g. padding, margin is preassumed as px.

So when I have margin: 16 it becomes 16em in my scenario. Which produces 160px overblown margins, paddings, and so on in the default theme.
Like for example:
This enforces a lot of tedious override procedures in the theme, like:
5

Expected Behavior 🤔

For the originally set unitless values in the default theme I would expect one of scenario (starting from most appreciated):

  1. The values are recalculated to the respective defaultUnit settings in the JSS. So when I set defaultUnit to em for some CSS properties, the original implementation should recalculate all the unitless values based on htmlFontSize property in theme. Something like:
if (customDefaultUnit === 'em') unitlessProps.map(prop => (prop / theme.htmlFontSize);

It's a pseudo code to get the basic concept.
This scenario would create full responsive (proportional sizing) across all the styling in the default theme.
2. Default unitless props are becoming hardcoded to pixel-units in case if defaultUnit plugin is changed.
This would make some theme props output as pixel-sized, which would create an inconsistent sizing pattern. But at least it would not break the theme while customizing the defaultUnit JSS preset.
3. Make the unitless concept of theming out of the theming, and make everything pixel-based. Very grim scenario but at least consistent and not brittle.

Steps to Reproduce 🕹

I hope the above explanation is enough. I will provide a working example if necessary.

Context 🔦

CSS theme that uses only proportional sizing (em, rem %) in order to get the best responsive UI experiences and simplify the process of customizing styles.

Currently, part of the API support this task like font sizing but some of the props (as described above) produce fixed pixel values which output as an inconsistent theme (some values are em and some px). This enforces many tedious overrides in theme in order to get proper responsive "fluid" results.

I don't know if this type of issue "the Bug" (I guess it is something between the Bug and request to Change functionality) applies to the described scenario. So please amend accordingly. I might, of course, miss some documentation and do things just wrong. So please direct in the better implementation suggestions.

Your Environment 🌎

Tech Version
Material-UI v4.9.14
React v16.13.1
Browser ["last 1 version", "> 0.5%", "not dead"]
TypeScript v3.8.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking changeIntroduces changes that are not backward compatible.discussion

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions