Skip to content

[theme] Always return default spacing value with px units#22552

Merged
mbrookes merged 7 commits into
mui:nextfrom
mbrookes:theme-spacing-px
Sep 12, 2020
Merged

[theme] Always return default spacing value with px units#22552
mbrookes merged 7 commits into
mui:nextfrom
mbrookes:theme-spacing-px

Conversation

@mbrookes

@mbrookes mbrookes commented Sep 10, 2020

Copy link
Copy Markdown
Member

Breaking changes

  • theme.spacing now returns single values with px units by default.
    This change improves the integration with styled-components & emotion.

    Before:

    theme.spacing(2) => 16
    

    After:

    theme.spacing(2) => '16px'
    

    You can restore the previous behavior with:

    -const theme = createMuiTheme();
    +const theme = createMuiTheme({
    +  spacing: x => x * 8,
    +});

Closes #16205

@mbrookes mbrookes added breaking change Introduces changes that are not backward compatible. internal Behind-the-scenes enhancement. Formerly called “core”. labels Sep 10, 2020
@mui-pr-bot

mui-pr-bot commented Sep 10, 2020

Copy link
Copy Markdown

Details of bundle changes

Generated by 🚫 dangerJS against 44b8dcf

@github-actions github-actions Bot added the PR: out-of-date The pull request has merge conflicts and can't be merged. label Sep 10, 2020
@github-actions github-actions Bot removed the PR: out-of-date The pull request has merge conflicts and can't be merged. label Sep 11, 2020
@mbrookes

mbrookes commented Sep 11, 2020

Copy link
Copy Markdown
Member Author
width: 300 + parseInt(theme.spacing(6).slice(0, -2));

😄

It's the problem that @joshwooding raised here: #16205 (comment)

@mbrookes

mbrookes commented Sep 11, 2020

Copy link
Copy Markdown
Member Author

How about if theme.spacing() accepted the unit to append, and treated an empty string as a special case?:

> theme.spacing(1)
'8px'
> theme.spacing([1, 'rem'])
'8rem'
> theme.spacing([1, ''])
8

@oliviertassinari

oliviertassinari commented Sep 11, 2020

Copy link
Copy Markdown
Member

I have tried to benchmark what the others are doing here.

How about if theme.spacing() accepted the unit to append, and treated an empty string as a special case?:

I think that it would leak the intent, so not working.


I wonder if the best answer here isn't on CSS land: calc(). If you take the above example, the motivation is to have 300px and 2 spacing unit (in whatever CSS unit) on each side. @mbrookes Would it work, it seems to?

@mbrookes

Copy link
Copy Markdown
Member Author

@mbrookes Would it work, it seems to?

Perfect - it even works in IE.

@mbrookes

Copy link
Copy Markdown
Member Author

Hmm:

padding: theme.spacing(0.5, 0, 0.5, `${Math.max(0, theme.spacing(1) - 3)}px`),

CSS max() isn't supported in IE: https://caniuse.com/?search=max()

@mbrookes

Copy link
Copy Markdown
Member Author

Also:

    [theme.breakpoints.up(600 + theme.spacing(2) * 2)]: {
      width: 600,
      marginLeft: 'auto',
      marginRight: 'auto',
    },

@oliviertassinari

Copy link
Copy Markdown
Member

@mbrookes For the first case we can use px, and the second a breakpoint value, md?

@mbrookes

mbrookes commented Sep 11, 2020

Copy link
Copy Markdown
Member Author

I'm not sure what the second was about (and I wrote it! 😅 ) – it seems to work just the same with 'md'.

I don't understand what you mean for the first - it is in px?

@oliviertassinari

Copy link
Copy Markdown
Member

I don't understand what you mean for the first - it is in px?

Sorry for the confusion, I meant using hardcoded pixels, not. The value from the theme.

Comment thread docs/src/modules/components/TopLayoutBlog.js Outdated
Comment thread docs/src/modules/components/TopLayoutCompany.js Outdated
Comment thread docs/src/pages/components/slider/CustomizedSlider.js Outdated
Comment thread docs/src/pages/components/slider/CustomizedSlider.tsx Outdated
Co-authored-by: Olivier Tassinari <[email protected]>
@mbrookes
mbrookes merged commit 5b95c39 into mui:next Sep 12, 2020
@mbrookes
mbrookes deleted the theme-spacing-px branch September 12, 2020 19:12
@oliviertassinari oliviertassinari added this to the v5 milestone Sep 15, 2020
@oliviertassinari oliviertassinari mentioned this pull request Sep 15, 2020
42 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change Introduces changes that are not backward compatible. internal Behind-the-scenes enhancement. Formerly called “core”.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Always return value with 'px' unit in the spacing function

3 participants