question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[system] transform and themeKey do not work together when using style from material-ui/system

See original GitHub issue

Using the material-ui system function ‘style’ does not work properly when used with theme and styled-components.

const spacing = style({
  prop: "spacing",
  cssProperty: "margin",
  themeKey: "spacing",
  transform: (value) => {
    // This is never run when theme is provided to a styled-component
    console.log(value);
    return `-${value}px`;
  }
});
  • [ x] The issue is present in the latest release.
  • [ x] I have searched the issues of this repository and believe that this is not a duplicate.

This is addressed in a closed PR (stale) that was never merged https://github.com/mui-org/material-ui/pull/16439/commits/c9fdebb8e63f2ddbf45cd9f06458bc3d05947490

with discussion here: https://github.com/mui-org/material-ui/pull/16439/files#diff-4d07c4a6e8c6006d5f726c3374e11607c23ba62024314e819983e3cfda099768R32-R35

I believe that this piece of the commit should be merged if possible

Current Behavior 😯

The transform function is never ran.

Expected Behavior 🤔

The transform function should run on my css value.

Steps to Reproduce 🕹

https://codesandbox.io/s/material-ui-issue-forked-61g88?file=/src/Demo.js

Sidenote: The latest official material-ui codesandbox is broken when using newest alpha version

Steps:

  1. When passing a theme to a styled-component and using the style system like the example

Context 🔦

I want to have the ability to apply a negative margin based on a spacing prop in order to achieve something similar to Mui Grid.

Your Environment 🌎

Tech Version
Material-UI v5.?.?
React
Browser
TypeScript
etc.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
ZovcIfzmcommented, Dec 14, 2020

Can I take this one up?

2reactions
oliviertassinaricommented, Nov 13, 2020

@cbravo Thanks for opening this issue, it’s an interesting one. I’m doing a digression. We have recently revamped the documentation of the system in #23294. See before vs. after. In this move, we have put a lot of emphasis on the sx as it seemed to be the main driver for the usage of the system. However, I think that your issue is a good reminder that developers are also interested in extending it. I think that it’s something we should pay attention to.

This made me think about how stitches as a utility key in the theme. Basically, developers can do:

createMuiTheme({
  sx: {
    spacing: (value) => ({
      margin: -value * 8
    })
  },
});

<Box sx={{ spacing: 2 }} />

@cbravo Is being able to cherry-pick the different style transformations important for you? Meaning, would the above solution be enough or would you rather only add the transformations you need for a specific component?

@mnajdova I wonder if, no matter what, we shouldn’t add a page in the documentation around how to compose the style functions 1. to extend the sx prop with new behaviors, 2. to cherry-pick custom transformations.


@cbravo Regarding your specific issue, it does sound like a bug and https://github.com/mui-org/material-ui/pull/16439/files#r300456428 seems to solve the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[system] transform and themeKey do not work together when ...
Using the material-ui system function 'style' does not work properly when used with theme and styled-components. const spacing = style({ ...
Read more >
Troubleshooting - Material UI - MUI
There are two reasons why component styles may be broken after you've completed all steps in the migration process. First, check if you...
Read more >
Transitions - Material UI - MUI
The theme key enables you to customize the durations and easings of the various transitions used across MUI components, and offers a utility...
Read more >
material-ui/system
Encourage UI consistency. Write responsive style effortlessly. Work with any theme object. Work with the most popular CSS-in-JS solutions. Less ...
Read more >
Style library interoperability - Material UI - MUI
This guide aims to document the most popular alternatives, but you should find that the principles applied here can be adapted to other...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found