Material-ui: [system] Support sx on all primitive elements by introducing a jsx pragma

Created on 23 Oct 2020  Â·  4Comments  Â·  Source: mui-org/material-ui

Summary 💡

It would be great if we can support the sx props on the primitives with jsx pragma. That would basically help us to replace

<Box
  component="img"
  sx={{
    display: "block",
    mr: { sm: 3 },
    mx: { xs: "auto", sm: 0 },
    mb: { xs: 1, sm: 0 },
    width: { xs: "4rem", sm: "6rem" },
    height: { xs: "4rem", sm: "6rem" },
    borderRadius: "50%"
  }}
  src="https://material-ui.com/static/logo_raw.svg"
/>

with

<img
  sx={{
    display: "block",
    mr: { sm: 3 },
    mx: { xs: "auto", sm: 0 },
    mb: { xs: 1, sm: 0 },
    width: { xs: "4rem", sm: "6rem" },
    height: { xs: "4rem", sm: "6rem" },
    borderRadius: "50%"
  }}
  src="https://material-ui.com/static/logo_raw.svg"
/>

For instance https://xstyled.dev/docs/emotion/#use-css-prop or https://theme-ui.com/sx-prop. It seems that we could only support it with emotion, which is OK-ish as it's the default engine 🙃.

Motivation 🔦

This would help us to move away from templates that would use the Box component everywhere. On the other hand, we plan to support the sx prop on all core components, so it may not be that bad, but still, I think this is something that will bring us in the right direction.

Raised in #23053

discussion system

Most helpful comment

There’s no need for a pragma, you can release a Babel macro in a similar fashion to Styled Components’.

All 4 comments

There’s no need for a pragma, you can release a Babel macro in a similar fashion to Styled Components’.

How would Pragma work with the new automatic runtime? Has that been considered?

Looking forward to this feature!
As @yuchi mentioned, the Babel macro also seems to be a possible alternative.
Some reference I found for the implementation of the css prop in styled-components:
https://medium.com/styled-components/css-prop-support-for-create-react-app-37e8c5d96861
https://styled-components.com/docs/tooling#babel-macro

Please note that Styled's Babel Macro is a "bad macro", what I mean is that it's actually a Babel Plugin which is triggered file-wide at macro import (they release both the plug-in and the macro).
In this regard it is not an hygienic macro, it doesn't have a trigger reference: every occurrence of a css prop (by name! not by hygienic reference!) gets transpired.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TimoRuetten picture TimoRuetten  Â·  3Comments

pola88 picture pola88  Â·  3Comments

rbozan picture rbozan  Â·  3Comments

revskill10 picture revskill10  Â·  3Comments

FranBran picture FranBran  Â·  3Comments