-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
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] Add experimental_sx
utility
#29833
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3a4784b
[system] Add `unstable_sx` utility
mnajdova 9724b75
lint & prettier
mnajdova 6b5c504
Rename unstable_ to experimental_
mnajdova 68fcb7d
Rename unstable_ to experimental_
mnajdova 1f16e9c
prettier
mnajdova cc5f2a8
Changed signature
mnajdova 6d488db
lint
mnajdova 51d59ca
Update docs/src/pages/system/styled/styled.md
mnajdova 138122c
Update docs/src/pages/system/styled/styled.md
mnajdova 938076d
Add test to ensure it does not throw without ThemeProvider
mnajdova 2d63668
Merge branch 'feat/sx-util-function' of https://github.com/mnajdova/m…
mnajdova File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import * as React from 'react'; | ||
import { | ||
styled, | ||
createTheme, | ||
ThemeProvider, | ||
experimental_sx as sx, | ||
} from '@mui/system'; | ||
|
||
const customTheme = createTheme({ | ||
palette: { | ||
primary: { | ||
main: '#1976d2', | ||
contrastText: 'white', | ||
}, | ||
}, | ||
}); | ||
|
||
const MyThemeComponent = styled('div')( | ||
sx({ | ||
color: 'primary.contrastText', | ||
backgroundColor: 'primary.main', | ||
padding: 1, | ||
borderRadius: 1, | ||
}), | ||
); | ||
|
||
export default function ThemeUsage() { | ||
return ( | ||
<ThemeProvider theme={customTheme}> | ||
<MyThemeComponent>Styled div with theme</MyThemeComponent> | ||
</ThemeProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import * as React from 'react'; | ||
import { | ||
styled, | ||
createTheme, | ||
ThemeProvider, | ||
experimental_sx as sx, | ||
} from '@mui/system'; | ||
|
||
const customTheme = createTheme({ | ||
palette: { | ||
primary: { | ||
main: '#1976d2', | ||
contrastText: 'white', | ||
}, | ||
}, | ||
}); | ||
|
||
const MyThemeComponent = styled('div')( | ||
sx({ | ||
color: 'primary.contrastText', | ||
backgroundColor: 'primary.main', | ||
padding: 1, | ||
borderRadius: 1, | ||
}), | ||
); | ||
|
||
export default function ThemeUsage() { | ||
return ( | ||
<ThemeProvider theme={customTheme}> | ||
<MyThemeComponent>Styled div with theme</MyThemeComponent> | ||
</ThemeProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<ThemeProvider theme={customTheme}> | ||
<MyThemeComponent>Styled div with theme</MyThemeComponent> | ||
</ThemeProvider> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { experimental_sx as sx, styled, createTheme } from '@mui/material/styles'; | ||
|
||
// Can use the experimental_sx in the styled() utility | ||
const Test = styled('div')( | ||
sx({ | ||
color: 'primary.main', | ||
bgcolor: 'primary.light', | ||
m: 2, | ||
}), | ||
); | ||
|
||
// Can use the experimental_sx in the theme's variants | ||
const customTheme = createTheme({ | ||
components: { | ||
MuiButton: { | ||
variants: [ | ||
{ | ||
props: {}, | ||
style: sx({ | ||
m: 2, | ||
p: 1, | ||
}), | ||
}, | ||
], | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './sx'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './sx'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { CSSObject } from '@mui/styled-engine'; | ||
import { SxProps } from '../styleFunctionSx'; | ||
|
||
export default function sx<T extends object = {}>(styles: SxProps<T>): CSSObject; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import styleFunctionSx from '../styleFunctionSx'; | ||
|
||
function sx(styles) { | ||
return ({ theme }) => styleFunctionSx({ sx: styles, theme }); | ||
} | ||
|
||
export default sx; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { experimental_sx as sx, styled } from '@mui/system'; | ||
|
||
// Can be used in the styled() utility | ||
const Test = styled('div')( | ||
sx({ | ||
color: 'primary.main', | ||
bgcolor: 'primary.light', | ||
m: 2, | ||
}), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import * as React from 'react'; | ||
import { expect } from 'chai'; | ||
import { createRenderer } from 'test/utils'; | ||
import { experimental_sx as sx, styled, ThemeProvider } from '@mui/system'; | ||
|
||
describe('sx', () => { | ||
const { render } = createRenderer(); | ||
const breakpointsValues = { | ||
xs: 0, | ||
sm: 600, | ||
md: 960, | ||
lg: 1280, | ||
xl: 1920, | ||
}; | ||
|
||
const round = (value) => Math.round(value * 1e5) / 1e5; | ||
|
||
const theme = { | ||
spacing: (val) => `${val * 10}px`, | ||
breakpoints: { | ||
keys: ['xs', 'sm', 'md', 'lg', 'xl'], | ||
values: breakpointsValues, | ||
up: (key) => { | ||
return `@media (min-width:${breakpointsValues[key]}px)`; | ||
}, | ||
}, | ||
unit: 'px', | ||
palette: { | ||
primary: { | ||
main: 'rgb(0, 0, 255)', | ||
}, | ||
secondary: { | ||
main: 'rgb(0, 255, 0)', | ||
}, | ||
}, | ||
typography: { | ||
fontFamily: '"Roboto", "Helvetica", "Arial", sans-serif', | ||
fontWeightLight: 300, | ||
fontSize: 14, | ||
body1: { | ||
fontFamily: '"Roboto", "Helvetica", "Arial", sans-serif', | ||
fontSize: '1rem', | ||
letterSpacing: `${round(0.15 / 16)}em`, | ||
fontWeight: 400, | ||
lineHeight: 1.5, | ||
}, | ||
body2: { | ||
fontFamily: '"Roboto", "Helvetica", "Arial", sans-serif', | ||
fontSize: `${14 / 16}rem`, | ||
letterSpacing: `${round(0.15 / 14)}em`, | ||
fontWeight: 400, | ||
lineHeight: 1.43, | ||
}, | ||
}, | ||
}; | ||
|
||
describe('system', () => { | ||
it('resolves system when used inside styled()', function test() { | ||
if (/jsdom/.test(window.navigator.userAgent)) { | ||
this.skip(); | ||
} | ||
|
||
const Test = styled('div')( | ||
sx({ | ||
color: 'primary.main', | ||
bgcolor: 'secondary.main', | ||
m: 2, | ||
p: 1, | ||
fontSize: 'fontSize', | ||
maxWidth: 'sm', | ||
}), | ||
); | ||
|
||
const { container } = render( | ||
<ThemeProvider theme={theme}> | ||
<Test /> | ||
</ThemeProvider>, | ||
); | ||
|
||
expect(container.firstChild).toHaveComputedStyle({ | ||
color: 'rgb(0, 0, 255)', | ||
backgroundColor: 'rgb(0, 255, 0)', | ||
margin: '20px', | ||
padding: '10px', | ||
fontSize: '14px', | ||
maxWidth: '600px', | ||
}); | ||
}); | ||
|
||
it('resolves system when used inside variants', function test() { | ||
if (/jsdom/.test(window.navigator.userAgent)) { | ||
this.skip(); | ||
} | ||
|
||
const themeWithVariants = { | ||
...theme, | ||
components: { | ||
MuiTest: { | ||
variants: [ | ||
{ | ||
props: {}, // all props | ||
style: sx({ | ||
color: 'primary.main', | ||
bgcolor: 'secondary.main', | ||
m: 2, | ||
p: 1, | ||
fontSize: 'fontSize', | ||
maxWidth: 'sm', | ||
}), | ||
}, | ||
], | ||
}, | ||
}, | ||
}; | ||
|
||
const Test = styled('div', { name: 'MuiTest', slot: 'Root' })( | ||
sx({ | ||
color: 'primary.main', | ||
bgcolor: 'secondary.main', | ||
m: 2, | ||
p: 1, | ||
fontSize: 'fontSize', | ||
maxWidth: 'sm', | ||
}), | ||
); | ||
|
||
const { container } = render( | ||
<ThemeProvider theme={themeWithVariants}> | ||
<Test /> | ||
</ThemeProvider>, | ||
); | ||
|
||
expect(container.firstChild).toHaveComputedStyle({ | ||
color: 'rgb(0, 0, 255)', | ||
backgroundColor: 'rgb(0, 255, 0)', | ||
margin: '20px', | ||
padding: '10px', | ||
fontSize: '14px', | ||
maxWidth: '600px', | ||
}); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One question, does
sx
work without ThemeProvider?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's just that I could not really test that the values are applied :) I will add test for it too, just to ensure it does not throw