- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 32.7k
 
          [system] Enable configuring the sx prop in the theme
          #35150
        
          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
Changes from 49 commits
d416262
              a4ac85f
              9d0d4f3
              7e1815a
              4a6bafb
              1e556dc
              bf55df5
              2f2c1a3
              696baf0
              150fab6
              2c1c0e1
              aa8dab7
              5593dc9
              db412b5
              c9b5e14
              3c27f52
              c71e273
              457d61d
              6205b27
              2a282d1
              aab6ec2
              5556acb
              d79f580
              e23405b
              d6756ea
              3d13353
              1bb3f11
              0d71255
              a41f4b0
              1d116d1
              0393080
              bcf632c
              215a70e
              54489dc
              6109c6d
              eb4e6cb
              a719745
              d55b720
              8f4a288
              95fc5e1
              b7e3a5a
              ab6b951
              15c3668
              985012e
              ab13d5b
              2cbf11c
              3b3f5b1
              a387ed7
              e5039b0
              21f55ab
              cf584ee
              4cf4ce2
              9afb562
              0debf67
              d68bbe6
              80a221f
              c0ffaba
              e42d74c
              75b5374
              37c6b59
              99a7021
              743c1c8
              4a5ecc5
              ad8eb6d
              ff505be
              cdd96c9
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| 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 ExtendSxProp() { | ||
| return ( | ||
| <ThemeProvider theme={theme}> | ||
| <Stack direction="row" gap={1}> | ||
| <Box sx={{ borderRadius: 'sm', bgcolor: '#007FFF', color: '#FFF', p: 1 }}> | ||
| sm border radius | ||
| </Box> | ||
| <Box sx={{ borderRadius: 'md', bgcolor: '#007FFF', color: '#FFF', p: 1 }}> | ||
| md border radius | ||
| </Box> | ||
| <Box sx={{ borderRadius: 'lg', bgcolor: '#007FFF', color: '#FFF', p: 1 }}> | ||
| lg border radius | ||
| </Box> | ||
| </Stack> | ||
| </ThemeProvider> | ||
| ); | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| 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 ExtendSxProp() { | ||
| return ( | ||
| <ThemeProvider theme={theme}> | ||
| <Stack direction="row" gap={1}> | ||
                
      
                  mnajdova marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| <Box sx={{ borderRadius: 'sm', bgcolor: '#007FFF', color: '#FFF', p: 1 }}> | ||
| sm border radius | ||
| </Box> | ||
| <Box sx={{ borderRadius: 'md', bgcolor: '#007FFF', color: '#FFF', p: 1 }}> | ||
| md border radius | ||
| </Box> | ||
                
      
                  mnajdova marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| <Box sx={{ borderRadius: 'lg', bgcolor: '#007FFF', color: '#FFF', p: 1 }}> | ||
| lg border radius | ||
| </Box> | ||
| </Stack> | ||
| </ThemeProvider> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <ThemeProvider theme={theme}> | ||
| <Stack direction="row" gap={1}> | ||
| <Box sx={{ borderRadius: 'sm', bgcolor: '#007FFF', color: '#FFF', p: 1 }}> | ||
| sm border radius | ||
| </Box> | ||
| <Box sx={{ borderRadius: 'md', bgcolor: '#007FFF', color: '#FFF', p: 1 }}> | ||
| md border radius | ||
| </Box> | ||
| <Box sx={{ borderRadius: 'lg', bgcolor: '#007FFF', color: '#FFF', p: 1 }}> | ||
| lg border radius | ||
| </Box> | ||
| </Stack> | ||
| </ThemeProvider> | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import * as React from 'react'; | ||
| import { createTheme, ThemeProvider, Box } from '@mui/system'; | ||
| 
     | 
||
| const theme = createTheme({ | ||
| unstable_sxConfig: { | ||
| // You can now use the background key in sx | ||
| // by providing direct values from the palette | ||
| background: { | ||
| themeKey: 'palette', | ||
| }, | ||
| }, | ||
| palette: { | ||
| primary: { | ||
| main: '#007FFF', | ||
| contrastText: '#FFFFFF', | ||
| }, | ||
| }, | ||
| }); | ||
| 
     | 
||
| export default function ExtendSxProp() { | ||
| return ( | ||
| <ThemeProvider theme={theme}> | ||
| <Box | ||
| sx={{ | ||
| background: 'primary.main', | ||
| color: 'primary.contrastText', | ||
| p: 1, | ||
| borderRadius: 2, | ||
| }} | ||
| > | ||
| Primary main background | ||
| </Box> | ||
| </ThemeProvider> | ||
| ); | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import * as React from 'react'; | ||
| import { createTheme, ThemeProvider, Box } from '@mui/system'; | ||
| 
     | 
||
| const theme = createTheme({ | ||
| unstable_sxConfig: { | ||
| // You can now use the background key in sx | ||
| // by providing direct values from the palette | ||
| background: { | ||
| themeKey: 'palette', | ||
| }, | ||
| }, | ||
| palette: { | ||
                
      
                  mnajdova marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| primary: { | ||
| main: '#007FFF', | ||
| contrastText: '#FFFFFF', | ||
| }, | ||
| }, | ||
| }); | ||
| 
     | 
||
| export default function ExtendSxProp() { | ||
| return ( | ||
| <ThemeProvider theme={theme}> | ||
| <Box | ||
| sx={{ | ||
| background: 'primary.main', | ||
| color: 'primary.contrastText', | ||
| p: 1, | ||
| borderRadius: 2, | ||
| }} | ||
| > | ||
| Primary main background | ||
| </Box> | ||
| </ThemeProvider> | ||
| ); | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <ThemeProvider theme={theme}> | ||
| <Box | ||
| sx={{ | ||
| background: 'primary.main', | ||
| color: 'primary.contrastText', | ||
| p: 1, | ||
| borderRadius: 2, | ||
| }} | ||
| > | ||
| Primary main background | ||
| </Box> | ||
| </ThemeProvider> | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Extend the `sx` prop | ||
                
      
                  mnajdova marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| <p class="description">Learn about the experimental API for extending or changing the behavior of the `sx` prop.</p> | ||
                
      
                  mnajdova marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| ## Extend the `sx` prop | ||
                
      
                  mnajdova marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| If you wish to add new keys that would be processed by the `sx` prop, you can do that by extending the `unstable_sxConfig` option inside the theme. | ||
                
      
                  mnajdova marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| {{"demo": "ExtendTheSxProp.js" }} | ||
| 
     | 
||
| ## Override some of the existing behavior | ||
                
      
                  mnajdova marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
| There is also a possibility to change some of the existing behavior. You can simply provide a custom config in the for the specific key. | ||
| 
     | 
||
| {{"demo": "ChangeTheBehaviorSxProp.js" }} | ||
                
      
                  mnajdova marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| 
     | 
||
                
      
                  mnajdova marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| ## 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 | ||
                
      
                  mnajdova marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| - `themeKey` (_string_ [optional]): The path of the theme mapping. | ||
                
      
                  mnajdova marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| - `transform` (_(cssValue: unknown, userValue: unknown) => number | string | React.CSSProperties | CSSObject_ [optional]): Allows users to define a function that can transform the value before it is returned | ||
                
      
                  mnajdova marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| - `style` (_(props: any) => CSSObject_ [optional]): Offers maximum customizability. Note that you need to make sure that the breakpoints values can be processed | ||
                
      
                  mnajdova marked this conversation as resolved.
               
              
                Outdated
          
            Show resolved
            Hide resolved
         | 
||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 
          
            
          
           | 
    @@ -63,6 +63,17 @@ const pages = [ | |||||||||
| icon: 'CodeIcon', | ||||||||||
| children: pagesApi, | ||||||||||
| }, | ||||||||||
| { | ||||||||||
| pathname: '/system/experimental-api', | ||||||||||
| title: 'Experimental APIs', | ||||||||||
| icon: 'ExperimentIcon', | ||||||||||
| children: [ | ||||||||||
| { | ||||||||||
| pathname: '/system/experimental-api/extend-sx-prop', | ||||||||||
| title: 'Extend the `sx` prop', | ||||||||||
                
       | 
||||||||||
| 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.
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.
Yeah, "Configure the sx prop" sounds better, as the page is both about extending and changing. Updated!
Uh oh!
There was an error while loading. Please reload this page.