-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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] Support callback value in styleOverrides
slot
#30524
Changes from all commits
14c0324
9eba2cf
b02f6cb
13838ea
27e330f
1fab8ca
d83b76f
6d626e8
943e3cb
08bee1b
43956fe
1925f48
19cb837
de46f74
052dcf2
925abcf
296327b
14502fa
dafe8d7
8d7fc2e
d8bf232
03db4af
b018e67
b85903e
90701bb
373e566
5b1721a
cf9227a
4791add
2122360
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,54 @@ | ||
import * as React from 'react'; | ||
import { ThemeProvider, createTheme } from '@mui/material/styles'; | ||
import Box from '@mui/material/Box'; | ||
import Slider, { sliderClasses } from '@mui/material/Slider'; | ||
|
||
const finalTheme = createTheme({ | ||
components: { | ||
MuiSlider: { | ||
styleOverrides: { | ||
valueLabel: ({ ownerState, theme }) => ({ | ||
...(ownerState.orientation === 'vertical' && { | ||
backgroundColor: 'transparent', | ||
color: theme.palette.grey[500], | ||
fontWeight: 700, | ||
padding: 0, | ||
left: '2rem', | ||
}), | ||
[`&.${sliderClasses.valueLabelOpen}`]: { | ||
transform: 'none', | ||
top: 'initial', | ||
}, | ||
}), | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
function valuetext(value) { | ||
return `${value}°C`; | ||
} | ||
|
||
export default function GlobalThemeOverride() { | ||
return ( | ||
<ThemeProvider theme={finalTheme}> | ||
<Box sx={{ height: 180, display: 'inline-block' }}> | ||
<Slider | ||
getAriaLabel={() => 'Temperature'} | ||
orientation="vertical" | ||
getAriaValueText={valuetext} | ||
defaultValue={[25, 50]} | ||
marks={[ | ||
{ value: 0 }, | ||
{ value: 25 }, | ||
{ value: 50 }, | ||
{ value: 75 }, | ||
{ value: 100 }, | ||
]} | ||
valueLabelFormat={valuetext} | ||
valueLabelDisplay="on" | ||
/> | ||
</Box> | ||
</ThemeProvider> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import * as React from 'react'; | ||
import { ThemeProvider, createTheme } from '@mui/material/styles'; | ||
import Box from '@mui/material/Box'; | ||
import Slider, { sliderClasses } from '@mui/material/Slider'; | ||
|
||
const finalTheme = createTheme({ | ||
components: { | ||
MuiSlider: { | ||
styleOverrides: { | ||
valueLabel: ({ ownerState, theme }) => ({ | ||
...(ownerState.orientation === 'vertical' && { | ||
backgroundColor: 'transparent', | ||
color: theme.palette.grey[500], | ||
fontWeight: 700, | ||
padding: 0, | ||
left: '2rem', | ||
}), | ||
[`&.${sliderClasses.valueLabelOpen}`]: { | ||
transform: 'none', | ||
top: 'initial', | ||
}, | ||
}), | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
function valuetext(value: number) { | ||
return `${value}°C`; | ||
} | ||
|
||
export default function GlobalThemeOverride() { | ||
return ( | ||
<ThemeProvider theme={finalTheme}> | ||
<Box sx={{ height: 180, display: 'inline-block' }}> | ||
<Slider | ||
getAriaLabel={() => 'Temperature'} | ||
orientation="vertical" | ||
getAriaValueText={valuetext} | ||
defaultValue={[25, 50]} | ||
marks={[ | ||
{ value: 0 }, | ||
{ value: 25 }, | ||
{ value: 50 }, | ||
{ value: 75 }, | ||
{ value: 100 }, | ||
]} | ||
valueLabelFormat={valuetext} | ||
valueLabelDisplay="on" | ||
/> | ||
</Box> | ||
</ThemeProvider> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as React from 'react'; | ||
import { | ||
ThemeProvider, | ||
createTheme, | ||
experimental_sx as sx, | ||
} 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/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', | ||
}), | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
export default function GlobalThemeOverride() { | ||
return ( | ||
<ThemeProvider theme={finalTheme}> | ||
<Chip | ||
color="success" | ||
label={ | ||
<span> | ||
<b>Status:</b> Completed | ||
</span> | ||
} | ||
icon={<Check fontSize="small" />} | ||
/> | ||
</ThemeProvider> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as React from 'react'; | ||
import { | ||
ThemeProvider, | ||
createTheme, | ||
experimental_sx as sx, | ||
} 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/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', | ||
}), | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
export default function GlobalThemeOverride() { | ||
return ( | ||
<ThemeProvider theme={finalTheme}> | ||
<Chip | ||
color="success" | ||
label={ | ||
<span> | ||
<b>Status:</b> Completed | ||
</span> | ||
} | ||
icon={<Check fontSize="small" />} | ||
/> | ||
</ThemeProvider> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<ThemeProvider theme={finalTheme}> | ||
<Chip | ||
color="success" | ||
label={ | ||
<span> | ||
<b>Status:</b> Completed | ||
</span> | ||
} | ||
icon={<Check fontSize="small" />} | ||
/> | ||
</ThemeProvider> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,29 @@ | |
|
||
<p class="description">The theme's components key allows you to customize a component without wrapping it in another component. You can change the styles, the default props, and more.</p> | ||
|
||
## Default props | ||
|
||
You can change the default of every prop of a MUI component. | ||
A `defaultProps` key is exposed in the theme's `components` key for this use case. | ||
|
||
```js | ||
const theme = createTheme({ | ||
components: { | ||
// Name of the component | ||
MuiButtonBase: { | ||
defaultProps: { | ||
// The props to change the default for. | ||
disableRipple: true, // No more ripple! | ||
}, | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
{{"demo": "pages/customization/theme-components/DefaultProps.js"}} | ||
|
||
To override lab component styles with TypeScript, check [this page](/components/about-the-lab/#typescript). | ||
|
||
## Global style overrides | ||
|
||
You can use the theme's `styleOverrides` key to potentially change every single style injected by MUI into the DOM. | ||
|
@@ -29,31 +52,45 @@ The list of each component's classes is documented under the **CSS** section of | |
|
||
To override a lab component's styles with TypeScript, check [this section of the documentation](/components/about-the-lab/#typescript). | ||
|
||
## Default props | ||
### Overrides based on props | ||
|
||
You can change the default of every prop of a MUI component. | ||
A `defaultProps` key is exposed in the theme's `components` key for this use case. | ||
You can pass a callback as a value in each slot of the component's `styleOverrides` to apply styles based on props. | ||
|
||
The `ownerState` prop is a combination of public props that you pass to the component + internal state of the component. | ||
|
||
```js | ||
const theme = createTheme({ | ||
const finalTheme = createTheme({ | ||
components: { | ||
// Name of the component | ||
MuiButtonBase: { | ||
defaultProps: { | ||
// The props to change the default for. | ||
disableRipple: true, // No more ripple! | ||
MuiSlider: { | ||
styleOverrides: { | ||
valueLabel: ({ ownerState, theme }) => ({ | ||
...(ownerState.orientation === 'vertical' && { | ||
backgroundColor: 'transparent', | ||
color: theme.palette.grey[500], | ||
}), | ||
}), | ||
}, | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
{{"demo": "pages/customization/theme-components/DefaultProps.js"}} | ||
{{"demo": "pages/customization/theme-components/GlobalThemeOverrideCallback.js"}} | ||
|
||
To override lab component styles with TypeScript, check [this page](/components/about-the-lab/#typescript). | ||
### Using `sx` (experimental) syntax | ||
|
||
If you are not familiar `sx`, first check out [the concept](/system/the-sx-prop) and [the difference with the `styled`](/system/styled/#difference-with-the-sx-prop). | ||
|
||
`sx` is also compatible with theme style overrides if you prefer the shorthand notation. | ||
|
||
{{"demo": "pages/customization/theme-components/GlobalThemeOverrideSx.js"}} | ||
|
||
## Adding new component variants | ||
|
||
> ⚠️ This API has been **deprecated** and will likely be removed in the next major release. If you want to apply styles based on props, take a look at [Overrides based on props](#overrides-based-on-props) instead. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I landed here after seeing the notion of deprecation in #30668. We might be jumping one step head too quickly on this one. I will add a new comment on why we added the "variant" API in the first place, it's true purpose doesn't seem covered in #30412 or in #28107 (from my perspective on why we added it in the first place, it wasn't because of JSS, when we added it we were already adding emotion from what I recall, we knew this PR was going to be possible). Edit: done #30412 (comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I will remove the deprecation section for now and update the #30412. |
||
> | ||
> If you are interested to see the reasoning behind this change, check out [issue #30412](https://github.com/mui-org/material-ui/issues/30412) | ||
You can use the `variants` key in the theme's `components` section to add new variants to MUI components. These new variants can specify what styles the component should have when specific props are applied. | ||
|
||
The definitions are specified in an array, under the component's name. For each of them a CSS class is added to the HTML `<head>`. The order is important, so make sure that the styles that should win are specified last. | ||
|
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.
moved
Default props
up