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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
d416262
[release] v5.10.12
mnajdova Oct 31, 2022
a4ac85f
Remove autogenerated text
mnajdova Oct 31, 2022
9d0d4f3
Merge branch 'master' into release/5.10.12
mnajdova Oct 31, 2022
7e1815a
Updated changelog
mnajdova Oct 31, 2022
4a6bafb
Merge branch 'master' into release/5.10.12
mnajdova Oct 31, 2022
1e556dc
Updated changelog
mnajdova Oct 31, 2022
bf55df5
Merge branch 'release/5.10.12' of https://github.com/mnajdova/materia…
mnajdova Oct 31, 2022
2f2c1a3
Merge branch 'master' of https://github.com/mui-org/material-ui into …
mnajdova Nov 4, 2022
696baf0
Merge branch 'master' of https://github.com/mui-org/material-ui into …
mnajdova Nov 9, 2022
150fab6
Merge branch 'master' of https://github.com/mui-org/material-ui into …
mnajdova Nov 14, 2022
2c1c0e1
[system] Explore different sx config
mnajdova Nov 14, 2022
aa8dab7
lint issues
mnajdova Nov 15, 2022
5593dc9
Refactor Joy's usage
mnajdova Nov 15, 2022
db412b5
Fix experimental sx
mnajdova Nov 15, 2022
c9b5e14
Fixes
mnajdova Nov 15, 2022
3c27f52
More fixes
mnajdova Nov 15, 2022
c71e273
Moved and fixed some tests
mnajdova Nov 15, 2022
457d61d
fixes & lint issues
mnajdova Nov 15, 2022
6205b27
Merge branch 'master' of https://github.com/mui-org/material-ui into …
mnajdova Nov 15, 2022
2a282d1
Merge branch 'master' into chore/sx-improvements
mnajdova Nov 15, 2022
aab6ec2
Update packages/mui-joy/src/Box/Box.test.js
mnajdova Nov 16, 2022
5556acb
Update packages/mui-joy/src/styles/defaultTheme.ts
mnajdova Nov 16, 2022
d79f580
Resolve comments from review
mnajdova Nov 17, 2022
e23405b
Resolve comments from review
mnajdova Nov 17, 2022
d6756ea
Resolve review comments
mnajdova Nov 18, 2022
3d13353
prettier
mnajdova Nov 18, 2022
1bb3f11
Merge branch 'master' of https://github.com/mui-org/material-ui into …
mnajdova Nov 18, 2022
0d71255
Update docs' system performance section
mnajdova Nov 21, 2022
a41f4b0
Convert to TS
mnajdova Nov 21, 2022
1d116d1
Use declaration file
mnajdova Nov 21, 2022
0393080
lint
mnajdova Nov 21, 2022
bcf632c
Merge branch 'master' of https://github.com/mui-org/material-ui into …
mnajdova Nov 21, 2022
215a70e
Merge branch 'master' of https://github.com/mui-org/material-ui into …
mnajdova Nov 21, 2022
54489dc
Merge branch 'master' of https://github.com/mui-org/material-ui into …
mnajdova Nov 22, 2022
6109c6d
Merge branch 'master' into chore/sx-improvements
mnajdova Nov 22, 2022
eb4e6cb
Update @mui/material-next witht he latest sx config
mnajdova Nov 22, 2022
a719745
TS fixes
mnajdova Nov 22, 2022
d55b720
lint fixes
mnajdova Nov 22, 2022
8f4a288
lint
mnajdova Nov 22, 2022
95fc5e1
Merge branch 'master' of https://github.com/mui-org/material-ui into …
mnajdova Nov 23, 2022
b7e3a5a
Make sx work for both MD2 and MD3
mnajdova Nov 25, 2022
ab6b951
Merge branch 'master' of https://github.com/mui-org/material-ui into …
mnajdova Nov 28, 2022
15c3668
Merge branch 'master' of https://github.com/mui-org/material-ui into …
mnajdova Nov 28, 2022
985012e
Update packages/mui-material-next/src/styles/extendTheme.ts
mnajdova Nov 29, 2022
ab13d5b
Add more tests
mnajdova Nov 29, 2022
2cbf11c
Merge branch 'chore/sx-improvements' of https://github.com/mnajdova/m…
mnajdova Nov 29, 2022
3b3f5b1
More generic solution to the `materialYouComponent` flag
mnajdova Nov 29, 2022
a387ed7
Resolve review comments
mnajdova Nov 30, 2022
e5039b0
lint
mnajdova Nov 30, 2022
21f55ab
Remove `experimental_sx`
mnajdova Nov 30, 2022
cf584ee
Small fixes & docs updates
mnajdova Nov 30, 2022
4cf4ce2
Fix test & translations
mnajdova Nov 30, 2022
9afb562
Apply suggestions from code review
mnajdova Dec 1, 2022
0debf67
Update packages/mui-joy/src/styles/CssVarsProvider.tsx
mnajdova Dec 1, 2022
d68bbe6
Update packages/mui-system/src/styleFunctionSx/styleFunctionSx.js
mnajdova Dec 1, 2022
80a221f
Update docs/data/system/experimental-api/extend-sx-prop/ExtendTheSxPr…
mnajdova Dec 1, 2022
c0ffaba
Update docs/data/system/experimental-api/extend-sx-prop/ChangeTheBeha…
mnajdova Dec 1, 2022
e42d74c
Update docs/data/system/experimental-api/extend-sx-prop/ChangeTheBeha…
mnajdova Dec 1, 2022
75b5374
Address review comments
mnajdova Dec 1, 2022
37c6b59
lint issues
mnajdova Dec 1, 2022
99a7021
types
mnajdova Dec 1, 2022
743c1c8
Merge branch 'master' of https://github.com/mui-org/material-ui into …
mnajdova Dec 6, 2022
4a5ecc5
Update docs/data/system/experimental-api/configure-the-sx-prop/config…
mnajdova Dec 7, 2022
ad8eb6d
Merge branch 'master' of https://github.com/mui-org/material-ui into …
mnajdova Dec 7, 2022
ff505be
Merge branch 'master' into chore/sx-improvements
mnajdova Dec 7, 2022
cdd96c9
prettier
mnajdova Dec 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,30 +1,28 @@
import * as React from 'react';
import {
ThemeProvider,
createTheme,
experimental_sx as sx,
} from '@mui/material/styles';
import { ThemeProvider, createTheme } from '@mui/material/styles';
import Chip from '@mui/material/Chip';
import Check from '@mui/icons-material/Check';

const finalTheme = createTheme({
components: {
MuiChip: {
styleOverrides: {
root: sx({
// https://mui.com/system/getting-started/the-sx-prop/#spacing
px: 1,
py: 0.25,
// https://mui.com/system/borders/#border-radius
borderRadius: 1, // 4px as default.
}),
root: ({ theme }) =>
theme.unstable_sx({
// https://mui.com/system/getting-started/the-sx-prop/#spacing
px: 1,
py: 0.25,
// https://mui.com/system/borders/#border-radius
borderRadius: 1, // 4px as default.
}),
label: {
padding: 'initial',
},
icon: sx({
mr: 0.5,
ml: '-2px',
}),
icon: ({ theme }) =>
theme.unstable_sx({
mr: 0.5,
ml: '-2px',
}),
},
},
},
Expand Down
@@ -1,30 +1,28 @@
import * as React from 'react';
import {
ThemeProvider,
createTheme,
experimental_sx as sx,
} from '@mui/material/styles';
import { ThemeProvider, createTheme } from '@mui/material/styles';
import Chip from '@mui/material/Chip';
import Check from '@mui/icons-material/Check';

const finalTheme = createTheme({
components: {
MuiChip: {
styleOverrides: {
root: sx({
// https://mui.com/system/getting-started/the-sx-prop/#spacing
px: 1,
py: 0.25,
// https://mui.com/system/borders/#border-radius
borderRadius: 1, // 4px as default.
}),
root: ({ theme }) =>
theme.unstable_sx({
// https://mui.com/system/getting-started/the-sx-prop/#spacing
px: 1,
py: 0.25,
// https://mui.com/system/borders/#border-radius
borderRadius: 1, // 4px as default.
}),
label: {
padding: 'initial',
},
icon: sx({
mr: 0.5,
ml: '-2px',
}),
icon: ({ theme }) =>
theme.unstable_sx({
mr: 0.5,
ml: '-2px',
}),
},
},
},
Expand Down
Expand Up @@ -116,18 +116,20 @@ const finalTheme = createTheme({
components: {
MuiChip: {
styleOverrides: {
root: sx({
px: 1,
py: 0.25,
borderRadius: 1,
}),
root: ({ theme }) =>
theme.unstable_sx({
px: 1,
py: 0.25,
borderRadius: 1,
}),
label: {
padding: 'initial',
},
icon: sx({
mr: 0.5,
ml: '-2px',
}),
icon: ({ theme }) =>
theme.unstable_sx({
mr: 0.5,
ml: '-2px',
}),
},
},
},
Expand Down
@@ -0,0 +1,31 @@
import * as React from 'react';
import { createTheme, ThemeProvider, Box, Stack } from '@mui/system';

// Retain type safety.

const theme = createTheme({
unstable_sxConfig: {
// You can now use the background key in sx
// by providing direct values from the palette
borderRadius: {
themeKey: 'shape',
},
},
shape: {
sm: 4,
md: 8,
lg: 12,
},
});

export default function ChangeTheBehaviorSxProp() {
return (
<Stack direction="row" gap={1}>
<ThemeProvider theme={theme}>
<Box sx={{ borderRadius: 'sm', border: '1px solid black', p: 4 }} />
<Box sx={{ borderRadius: 'md', border: '1px solid black', p: 4 }} />
<Box sx={{ borderRadius: 'lg', border: '1px solid black', p: 4 }} />
</ThemeProvider>
</Stack>
);
}
@@ -0,0 +1,38 @@
import * as React from 'react';
import { createTheme, ThemeProvider, Box, Stack } from '@mui/system';

// Retain type safety.
declare module '@mui/system' {
interface Shape {
sm: number;
md: number;
lg: number;
}
}

const theme = createTheme({
unstable_sxConfig: {
// You can now use the background key in sx
// by providing direct values from the palette
borderRadius: {
themeKey: 'shape',
},
},
shape: {
sm: 4,
md: 8,
lg: 12,
},
});

export default function ChangeTheBehaviorSxProp() {
return (
<Stack direction="row" gap={1}>
<ThemeProvider theme={theme}>
<Box sx={{ borderRadius: 'sm', border: '1px solid black', p: 4 }} />
<Box sx={{ borderRadius: 'md', border: '1px solid black', p: 4 }} />
<Box sx={{ borderRadius: 'lg', border: '1px solid black', p: 4 }} />
</ThemeProvider>
</Stack>
);
}
@@ -0,0 +1,5 @@
<ThemeProvider theme={theme}>
<Box sx={{ borderRadius: 'sm', border: '1px solid black', p: 4 }} />
<Box sx={{ borderRadius: 'md', border: '1px solid black', p: 4 }} />
<Box sx={{ borderRadius: 'lg', border: '1px solid black', p: 4 }} />
</ThemeProvider>
@@ -0,0 +1,37 @@
import * as React from 'react';
import { createTheme, ThemeProvider, Box, handleBreakpoints } from '@mui/system';

const customTheme = createTheme({
unstable_sxConfig: {
size: {
style: (props) => {
const { size, theme } = props;

const styleFromPropValue = (propValueFinal) => {
const value = theme.spacing(propValueFinal);

return {
width: value,
height: value,
};
};

// Adding support for the breakpoints syntax
return handleBreakpoints(props, size, styleFromPropValue);
},
},
},
});

export default function ExtendSxProp() {
return (
<ThemeProvider theme={customTheme}>
<Box
sx={{
size: 10,
border: '1px solid black',
}}
/>
</ThemeProvider>
);
}
@@ -0,0 +1,37 @@
import * as React from 'react';
import { createTheme, ThemeProvider, Box, handleBreakpoints } from '@mui/system';

const customTheme = createTheme({
unstable_sxConfig: {
size: {
style: (props) => {
const { size, theme } = props;

const styleFromPropValue = (propValueFinal: number) => {
const value = theme.spacing(propValueFinal);

return {
width: value,
height: value,
};
};

// Adding support for the breakpoints syntax
return handleBreakpoints(props, size, styleFromPropValue);
},
},
},
});

export default function ExtendSxProp() {
return (
<ThemeProvider theme={customTheme}>
<Box
sx={{
size: 10,
border: '1px solid black',
}}
/>
</ThemeProvider>
);
}
@@ -0,0 +1,8 @@
<ThemeProvider theme={customTheme}>
<Box
sx={{
size: 10,
border: '1px solid black',
}}
/>
</ThemeProvider>
@@ -0,0 +1,26 @@
# Configure the sx prop

<p class="description">Learn about the experimental API for extending or changing the behavior of the sx prop.</p>

## Extend the sx prop

You can add new keys to be processed by the `sx` prop by extending the `unstable_sxConfig` option inside the theme, as shown below:

{{"demo": "ExtendTheSxProp.js" }}

## Override existing behavior

It is also possible to change some of the existing behavior of the `sx` prop.
For example, in some design systems, the border radiuses need to be restricted to specific values, instead of allowing any number to be used—as is the default with MUI System.
You can change this behavior by providing a custom config for the `borderRadius` property:

{{"demo": "ChangeTheBehaviorSxProp.js" }}

## API

Each key in the `unstable_sx` config can define the following properties:

- `cssProperty` (_string_ [optional]): Indicates the CSS property, if it is different than the key
- `themeKey` (_string_ [optional]): The path of the theme mapping
- `transform` (_(cssValue: unknown, userValue: unknown) => number | string | React.CSSProperties | CSSObject_ [optional]): Lets users define a function that can transform the value before it's returned
- `style` (_(props: any) => CSSObject_ [optional]): Offers maximum customizability. Note that you need to make sure that the breakpoint values can be processed
6 changes: 3 additions & 3 deletions docs/data/system/getting-started/usage/usage.md
Expand Up @@ -157,9 +157,9 @@ It works with both Emotion and styled-components.
| Benchmark case | Code snippet | Time normalized |
| :-------------------------------- | :-------------------- | --------------- |
| a. Render 1,000 primitives | `<div className="…">` | 100ms |
| b. Render 1,000 components | `<Div>` | 120ms |
| c. Render 1,000 styled components | `<StyledDiv>` | 160ms |
| d. Render 1,000 Box | `<Box sx={…}>` | 370ms |
| b. Render 1,000 components | `<Div>` | 112ms |
| c. Render 1,000 styled components | `<StyledDiv>` | 181ms |
| d. Render 1,000 Box | `<Box sx={…}>` | 296ms |

<!-- #default-branch-switch -->

Expand Down
11 changes: 11 additions & 0 deletions docs/data/system/pages.ts
Expand Up @@ -63,6 +63,17 @@ const pages = [
icon: 'CodeIcon',
children: pagesApi,
},
{
pathname: '/system/experimental-api',
title: 'Experimental APIs',
icon: 'ExperimentIcon',
children: [
{
pathname: '/system/experimental-api/configure-the-sx-prop',
title: 'Configure the sx prop',
},
],
},
{
pathname: '/system/styles',
title: 'Styles',
Expand Down
11 changes: 3 additions & 8 deletions docs/data/system/styled/UsingWithSx.js
@@ -1,10 +1,5 @@
import * as React from 'react';
import {
styled,
createTheme,
ThemeProvider,
experimental_sx as sx,
} from '@mui/system';
import { styled, createTheme, ThemeProvider } from '@mui/system';

const customTheme = createTheme({
palette: {
Expand All @@ -15,8 +10,8 @@ const customTheme = createTheme({
},
});

const MyThemeComponent = styled('div')(
sx({
const MyThemeComponent = styled('div')(({ theme }) =>
theme.unstable_sx({
color: 'primary.contrastText',
backgroundColor: 'primary.main',
padding: 1,
Expand Down
11 changes: 3 additions & 8 deletions docs/data/system/styled/UsingWithSx.tsx
@@ -1,10 +1,5 @@
import * as React from 'react';
import {
styled,
createTheme,
ThemeProvider,
experimental_sx as sx,
} from '@mui/system';
import { styled, createTheme, ThemeProvider } from '@mui/system';

const customTheme = createTheme({
palette: {
Expand All @@ -15,8 +10,8 @@ const customTheme = createTheme({
},
});

const MyThemeComponent = styled('div')(
sx({
const MyThemeComponent = styled('div')(({ theme }) =>
theme.unstable_sx({
color: 'primary.contrastText',
backgroundColor: 'primary.main',
padding: 1,
Expand Down