Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[system] Enable configuring the sx prop in the theme #35150

Merged
merged 66 commits into from Dec 7, 2022

Conversation

mnajdova
Copy link
Member

@mnajdova mnajdova commented Nov 14, 2022

Breaking changes

Changes in the experimental API:

-import { styled, experimental_sx } from '@mui/material/styles';
+import { styled } from '@mui/material/styles';

-const Component = styled('div)(experimental_sx({ p: 1 }});
+const Component = styled('div)(({ theme }) => theme.unstable_sx({ p: 1 }});

This PR changes the way the sx props works, by defining a config inside the theme for each key supported. This allows us to offer extensibility of the functionality of the sx prop, as well as overrides for some of its behavior. There shouldn't be any changes of the behavior.

Related to #30725 (it is now possible for this to be done in userland).
Fixes #32734, fixes #33525, fixes #33281

Docs preview: https://deploy-preview-35150--material-ui.netlify.app/system/experimental-api/configure-the-sx-prop/

Examples:

const sxConfig = {
  // the color value will be mapped to a value inside theme.palette
  color: {
    themeKey: 'palette',
  },
  // allowing for custom transform functions
  displayPrint: {
    cssProperty: false,
    transform: (value) => ({
      '@media print': {
        display: value,
      },
    }),
  },
  // also allowing for completely different logic
  p: {
    style: () => {}
  }
};

We are going to export the default sx config, so that people can compose it with their custom logic.

@mui-bot
Copy link

mui-bot commented Nov 14, 2022

Messages
📖 Netlify deploy preview: https://deploy-preview-35150--material-ui.netlify.app/

@material-ui/core: parsed: +0.26% , gzip: +0.24%
@material-ui/lab: parsed: +0.53% , gzip: +0.32%
@material-ui/system: parsed: +4.37% , gzip: +2.72%
@mui/joy: parsed: +0.37% , gzip: +0.14%

Details of bundle changes

Generated by 🚫 dangerJS against cdd96c9

@zannager zannager added the package: system Specific to @mui/system label Nov 15, 2022
@mnajdova mnajdova changed the title [WIP][system] Explore alternative sx config [POC][system] Explore alternative sx config Nov 15, 2022
@mnajdova mnajdova added the proof of concept Studying and/or experimenting with a to be validated approach label Nov 15, 2022
@mnajdova mnajdova marked this pull request as ready for review November 16, 2022 09:38
Signed-off-by: Marija Najdova <mnajdova@gmail.com>
Signed-off-by: Marija Najdova <mnajdova@gmail.com>
@@ -0,0 +1,35 @@
import { unstable_defaultSxConfig } from '@mui/system';

const sxConfig = {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

packages/mui-system/src/styleFunctionSx/styleFunctionSx.js Outdated Show resolved Hide resolved
packages/mui-system/src/styleFunctionSx/defaultSxConfig.js Outdated Show resolved Hide resolved
@@ -469,6 +469,8 @@ const Button = React.forwardRef(function Button<
tabIndex,
type,
variant,
// required for making sure the sx prop is handled correctly
materialYouComponent: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think of Material You as a separate library similar to Joy UI. Then add a doc similar to https://mui.com/joy-ui/guides/using-joy-ui-and-material-ui-together/ that guides how to use MD3 in the existing MD2 project.

I have tried the implications of this PR in the Joy UI + Material UI demos, e.g. https://codesandbox.io/s/material-ui-feat-joy-ui-forked-z9bv90?file=/demo.tsx. It seems to mostly work like before, only that the sx behavior is linked to the theme config, so based on the last theme that is deep merged.

It's a bit sad though that we can't have a different emotion context. I had a friend who recently asked me about using Joy UI inside an existing Material UI app (an engineer without a front-end background). He was intimidated by the size of the examples, he didn't try 🙃.

Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A great step froward for MUI System 👍

Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 The implementation looks good to me.

Comment on lines 72 to 73
pathname: '/system/experimental-api/extend-sx-prop',
title: 'Extend the `sx` prop',
Copy link
Member

@siriwatknp siriwatknp Dec 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pathname: '/system/experimental-api/extend-sx-prop',
title: 'Extend the `sx` prop',
pathname: '/system/experimental-api/sx-config',
title: 'sx config',

How about renaming the page to sx config or configure sx prop and grouping these into the page:

  • override the default config (from Override some of the existing behavior )
  • add your own config (from Extend the sx prop)
  • API

cc @samuelsycamore might have a better name.

Copy link
Member Author

@mnajdova mnajdova Dec 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, "Configure the sx prop" sounds better, as the page is both about extending and changing. Updated!

Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mnajdova and others added 7 commits December 1, 2022 10:11
Co-authored-by: Sam Sycamore <71297412+samuelsycamore@users.noreply.github.com>
Co-authored-by: Olivier Tassinari <olivier.tassinari@gmail.com>
Signed-off-by: Marija Najdova <mnajdova@gmail.com>
Co-authored-by: Olivier Tassinari <olivier.tassinari@gmail.com>
Signed-off-by: Marija Najdova <mnajdova@gmail.com>
Co-authored-by: Olivier Tassinari <olivier.tassinari@gmail.com>
Signed-off-by: Marija Najdova <mnajdova@gmail.com>
…op.tsx

Co-authored-by: Olivier Tassinari <olivier.tassinari@gmail.com>
Signed-off-by: Marija Najdova <mnajdova@gmail.com>
…viorSxProp.tsx

Co-authored-by: Olivier Tassinari <olivier.tassinari@gmail.com>
Signed-off-by: Marija Najdova <mnajdova@gmail.com>
…viorSxProp.tsx

Co-authored-by: Olivier Tassinari <olivier.tassinari@gmail.com>
Signed-off-by: Marija Najdova <mnajdova@gmail.com>
@mnajdova
Copy link
Member Author

mnajdova commented Dec 1, 2022

This section should be updated or removed. https://deploy-preview-35150--material-ui.netlify.app/material-ui/customization/theme-components/#the-sx-syntax-experimental

I've updated it to use the theme.unstable_sx syntax. Nice catch Jun! :)

@mnajdova
Copy link
Member Author

mnajdova commented Dec 6, 2022

@samuelsycamore this should be ready for final review :)

Copy link
Member

@samuelsycamore samuelsycamore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Text looks good! Just left one final comment with grammar suggestions.

mnajdova and others added 4 commits December 7, 2022 07:48
…ure-the-sx-prop.md

Co-authored-by: Sam Sycamore <71297412+samuelsycamore@users.noreply.github.com>
Signed-off-by: Marija Najdova <mnajdova@gmail.com>
@mnajdova mnajdova merged commit d23e42b into mui:master Dec 7, 2022
@mnajdova mnajdova changed the title [system] Enable configuring the sx mappings in the theme [system] Enable configuring the sx prop in the theme Dec 7, 2022
@mnajdova mnajdova mentioned this pull request Dec 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This is not a bug, nor a new feature package: system Specific to @mui/system
Projects
None yet
6 participants