Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
33 changes: 15 additions & 18 deletions docs/src/pages/customization/components/DynamicThemeNesting.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,27 @@ import FormControlLabel from '@material-ui/core/FormControlLabel';
import { blue } from '@material-ui/core/colors';
import Switch from '@material-ui/core/Switch';

const defaultTheme = createMuiTheme();

export default function DynamicThemeNesting() {
const [color, setColor] = React.useState('default');

const handleChange = event => {
setColor(event.target.checked ? 'blue' : 'default');
};

const theme = React.useMemo(() => {
if (color === 'blue') {
return createMuiTheme({
palette: {
secondary: {
main: blue[500],
contrastText: '#fff',
},
},
});
}
return createMuiTheme();
}, [color]);

return (
<React.Fragment>
<FormControlLabel
Expand All @@ -27,22 +39,7 @@ export default function DynamicThemeNesting() {
}
label="Blue"
/>
<ThemeProvider
theme={
color === 'blue'
? {
...defaultTheme,
palette: {
...defaultTheme.palette,
secondary: {
main: blue[500],
contrastText: '#fff',
},
},
}
: defaultTheme
}
>
<ThemeProvider theme={theme}>
<Button variant="contained" color="secondary">
{'Theme nesting'}
</Button>
Expand Down
35 changes: 16 additions & 19 deletions docs/src/pages/customization/components/DynamicThemeNesting.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import React from 'react';
import Button from '@material-ui/core/Button';
import { createMuiTheme, ThemeProvider } from '@material-ui/core/styles';
import { createMuiTheme, ThemeProvider, Theme } from '@material-ui/core/styles';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import { blue } from '@material-ui/core/colors';
import Switch from '@material-ui/core/Switch';

const defaultTheme = createMuiTheme();

export default function DynamicThemeNesting() {
const [color, setColor] = React.useState('default');

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setColor(event.target.checked ? 'blue' : 'default');
};

const theme = React.useMemo(() => {
if (color === 'blue') {
return createMuiTheme({
palette: {
secondary: {
main: blue[500],
contrastText: '#fff',
},
},
});
}
return createMuiTheme();
}, [color]);

return (
<React.Fragment>
<FormControlLabel
Expand All @@ -27,22 +39,7 @@ export default function DynamicThemeNesting() {
}
label="Blue"
/>
<ThemeProvider
theme={
color === 'blue'
? {
...defaultTheme,
palette: {
...defaultTheme.palette,
secondary: {
Copy link
Member Author

Choose a reason for hiding this comment

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

The merge is not deep which is now explicit with types. Fixes
Screenshot from 2019-11-22 13-21-55

Hovered:
Old
Screenshot from 2019-11-22 13-23-46
New
Screenshot from 2019-11-22 13-24-09

main: blue[500],
contrastText: '#fff',
},
},
}
: defaultTheme
}
>
<ThemeProvider<Theme> theme={theme}>
<Button variant="contained" color="secondary">
{'Theme nesting'}
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DefaultTheme } from '../defaultTheme';

export interface ThemeProviderProps<Theme = DefaultTheme> {
children: React.ReactNode;
theme: Theme | ((outerTheme: Theme) => Theme);
theme: Partial<Theme> | ((outerTheme: Theme) => Theme);
}
export default function ThemeProvider<T = DefaultTheme>(
props: ThemeProviderProps<T>,
Expand Down
9 changes: 9 additions & 0 deletions packages/material-ui/test/typescript/styles.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -640,3 +640,12 @@ withStyles(theme =>
};
});
}

function themeProviderTest() {
<ThemeProvider theme={{ foo: 1 }}>{null}</ThemeProvider>;
// $ExpectError
<ThemeProvider<Theme> theme={{ foo: 1 }}>{null}</ThemeProvider>;
<ThemeProvider<Theme> theme={{ props: { MuiAppBar: { 'aria-atomic': 'true' } } }}>
{null}
</ThemeProvider>;
}