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] Support callback value in styleOverrides slot #30524

Merged
merged 30 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
14c0324
pass ownerState as props to styleOverrides callback
siriwatknp Jan 6, 2022
9eba2cf
add callback types to styleOverrides
siriwatknp Jan 6, 2022
b02f6cb
add styleOverrides callback type support for lab
siriwatknp Jan 6, 2022
13838ea
add styleOverrides callback docs
siriwatknp Jan 6, 2022
27e330f
add deprecation
siriwatknp Jan 7, 2022
1fab8ca
fix useThemeProps type
siriwatknp Jan 7, 2022
d83b76f
fix browser test
siriwatknp Jan 7, 2022
6d626e8
shut off auto exporting
siriwatknp Jan 7, 2022
943e3cb
add additional unknown props to ease customization
siriwatknp Jan 7, 2022
08bee1b
add more tests
siriwatknp Jan 11, 2022
43956fe
fix typo
siriwatknp Jan 11, 2022
1925f48
Merge branch 'master' of https://github.com/mui-org/material-ui into …
siriwatknp Jan 12, 2022
19cb837
Merge branch 'master' of https://github.com/mui-org/material-ui into …
siriwatknp Jan 13, 2022
de46f74
use ownerState
siriwatknp Jan 13, 2022
052dcf2
remove unnecessary comment
siriwatknp Jan 13, 2022
925abcf
Apply suggestions from code review
siriwatknp Jan 13, 2022
296327b
add module augmentation test
siriwatknp Jan 14, 2022
14502fa
add comments
siriwatknp Jan 14, 2022
dafe8d7
fix lint
siriwatknp Jan 14, 2022
8d7fc2e
Merge branch 'master' of https://github.com/mui-org/material-ui into …
siriwatknp Jan 16, 2022
d8bf232
support styleOverridesCallback for Joy
siriwatknp Jan 16, 2022
03db4af
add sx support test
siriwatknp Jan 16, 2022
b018e67
add using with sx docs
siriwatknp Jan 16, 2022
b85903e
call if styleOverrides is a function in nested class
siriwatknp Jan 16, 2022
90701bb
yarn docs formatted
siriwatknp Jan 16, 2022
373e566
remove non-slot from Joy components
siriwatknp Jan 17, 2022
5b1721a
remove test variant from Joy component tests
siriwatknp Jan 17, 2022
cf9227a
Revert "call if styleOverrides is a function in nested class"
siriwatknp Jan 17, 2022
4791add
make nested class works with callback
siriwatknp Jan 17, 2022
2122360
Update docs/src/pages/customization/theme-components/theme-components.md
siriwatknp Jan 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: ({ orientation, theme }) => ({
...(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: ({ orientation, theme }) => ({
...(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>
);
}
53 changes: 41 additions & 12 deletions docs/src/pages/customization/theme-components/theme-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

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

moved Default props up


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.
Expand Down Expand Up @@ -29,31 +52,37 @@ 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.

```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: {
// The experience is similar to styled(Component)(props => ({ ... }))
// props = Slider's props + theme
valueLabel: (props) => ({
...(props.orientation === 'vertical' && {
backgroundColor: 'transparent',
color: props.theme.palette.grey[500],
}),
}),
},
},
},
});
```

{{"demo": "pages/customization/theme-components/DefaultProps.js"}}

To override lab component styles with TypeScript, check [this page](/components/about-the-lab/#typescript).
{{"demo": "pages/customization/theme-components/GlobalThemeOverrideCallback.js"}}

## Adding new component variants

> ⚠️ This API has been **deprecated** which might 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.
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved
>
> To see more details about the why, check out [issue #30412](https://github.com/mui-org/material-ui/issues/30412)
siriwatknp marked this conversation as resolved.
Show resolved Hide resolved

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.
Expand Down
Loading