mui / mui/material-ui

[material-ui] Inconsistent spacing units between components under default styling

Open
#40,994 4 comments 2 reactions 1 assignee View on GitHub

@siriwatknp is already working on this.

Since Feb 8, 2024.

scope: all components type: enhancement
Dominant language
JavaScript
Stars
99.1k
Forks
32.5k
Avg merge
2d 17h
Merged PRs (30d)
106

Description

Steps to reproduce

Link to live example: https://codesandbox.io/p/sandbox/sad-forest-g88786?file=%2Fsrc%2Findex.tsx%3A35%2C3
You'll see there is red inner content surrounded by green outer content - the hope is that this illustrates where padding is used.

Current behavior

Spacing units is inconsistent between different components' default styles. This issue becomes very apparent when adding a custom spacing function to the theme.

When specifying space in the sx of any component the behavior is consistent and predictable

  • padding: 16 will get converted to padding: 1.6rem
  • padding: "16px" stays padding: "16px"
  • padding: theme.spacing(20, 40) becomes padding: 2rem 4rem
    etc

Unfortunately with the way default styles are defined on out of the box components, this custom spacing function completely breaks consistency on some base styles.

For example:

Button and CardContent's padding ends up matching what the original intent was and renders appropriately.
AccordionSummary's padding ends up being completely wrong.

Expected behavior

My hope is that in the future there is consistency between component's base styles' use of units on spacing. It seems like unit-less spacing in these default styles would be preferred, so that in the end the spacing function is used and devs have more control over what that actually means. This seems like something that should be enforced on the project as a whole.

Context

Above is an example (and mirrors my project's production environment) where we convert all spacing units to rem, and base it on the fact the base website font size is 10px.

Your environment

No response

Search keywords: space spacing units theme

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.