Skip to content

Commit 985f804

Browse files
authored
[ENHANCEMENT] theme: add support for color-scheme (#78)
* [ENHANCEMENT] theme: add support for color-scheme Signed-off-by: Guillaume LADORME <Gladorme@users.noreply.github.com> * Add prop for enabling/disabling Signed-off-by: Guillaume LADORME <Gladorme@users.noreply.github.com> --------- Signed-off-by: Guillaume LADORME <Gladorme@users.noreply.github.com>
1 parent e2ffac0 commit 985f804

1 file changed

Lines changed: 49 additions & 26 deletions

File tree

components/src/theme/theme.ts

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,44 +43,67 @@ const getModalBackgroundStyle = ({
4343
*
4444
* Need to reinstantiate the theme everytime to support switching between light and dark themes
4545
* https://github.com/mui-org/material-ui/issues/18831
46+
*
47+
* Enable disableBodyOverride to prevent the theme from applying color scheme to the body element.
4648
*/
47-
export function getTheme(mode: PaletteMode, options: Parameters<typeof createTheme>[0] = {}): Theme {
49+
export function getTheme(
50+
mode: PaletteMode,
51+
options: Parameters<typeof createTheme>[0] = {},
52+
disableBodyOverride: boolean = false
53+
): Theme {
4854
return createTheme({
4955
palette: getPaletteOptions(mode),
5056
typography,
5157
mixins: {},
52-
components,
58+
components: getComponents(mode, disableBodyOverride),
5359
...options,
5460
});
5561
}
5662

5763
// Overrides for component default prop values and styles go here
58-
const components: ThemeOptions['components'] = {
59-
MuiAlert,
60-
MuiFormControl: {
61-
defaultProps: {
62-
size: 'small',
64+
function getComponents(mode: PaletteMode, disableBodyOverride: boolean): ThemeOptions['components'] {
65+
const components: ThemeOptions['components'] = {
66+
MuiAlert,
67+
MuiFormControl: {
68+
defaultProps: {
69+
size: 'small',
70+
},
6371
},
64-
},
65-
MuiPaper,
66-
MuiTextField: {
67-
defaultProps: {
68-
size: 'small',
72+
MuiPaper,
73+
MuiTextField: {
74+
defaultProps: {
75+
size: 'small',
76+
},
6977
},
70-
},
71-
MuiDrawer: {
72-
styleOverrides: {
73-
paper: getModalBackgroundStyle,
78+
MuiDrawer: {
79+
styleOverrides: {
80+
paper: getModalBackgroundStyle,
81+
},
7482
},
75-
},
76-
MuiDialog: {
77-
styleOverrides: {
78-
paper: getModalBackgroundStyle,
83+
MuiDialog: {
84+
styleOverrides: {
85+
paper: getModalBackgroundStyle,
86+
},
7987
},
80-
},
81-
MuiPopover: {
82-
styleOverrides: {
83-
paper: getModalBackgroundStyle,
88+
MuiPopover: {
89+
styleOverrides: {
90+
paper: getModalBackgroundStyle,
91+
},
8492
},
85-
},
86-
};
93+
};
94+
95+
if (disableBodyOverride) {
96+
return components;
97+
}
98+
99+
return {
100+
MuiCssBaseline: {
101+
styleOverrides: {
102+
body: {
103+
colorScheme: mode,
104+
},
105+
},
106+
},
107+
...components,
108+
};
109+
}

0 commit comments

Comments
 (0)