Skip to content

Commit

Permalink
fix getSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
siriwatknp committed Aug 9, 2024
1 parent c18e53d commit cd5410b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
10 changes: 8 additions & 2 deletions packages/mui-material/src/styles/createGetSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export default <
if (rule === 'media') {
return {
':root': css,
'@media (prefers-color-scheme: dark) { :root': excludedVariables,
[`@media (prefers-color-scheme: dark)`]: {
':root': excludedVariables,
},
};
}
if (rule) {
Expand All @@ -49,7 +51,11 @@ export default <
}
} else if (colorScheme) {
if (rule === 'media') {
return `@media (prefers-color-scheme: ${String(colorScheme)}) { :root`;
return {
[`@media (prefers-color-scheme: ${String(colorScheme)})`]: {
':root': css,
},
};
}
if (rule) {
return rule.replace('%s', String(colorScheme));
Expand Down
39 changes: 30 additions & 9 deletions packages/mui-material/src/styles/extendTheme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -721,10 +721,19 @@ describe('extendTheme', () => {
describe('light and dark color schemes', () => {
it('should use prefers-color-scheme (`media`) by default', () => {
const theme = extendTheme({ colorSchemes: { light: true, dark: true } });
expect(theme.generateStyleSheets().flatMap((sheet) => Object.keys(sheet))).to.deep.equal([
':root',
':root',
'@media (prefers-color-scheme: dark) { :root',
const sheets = theme.generateStyleSheets();
sinon.assert.match(sheets, [
{
':root': sheets[0][':root'], // non-colors related variables
},
{
':root': sheets[1][':root'], // light palette
},
{
'@media (prefers-color-scheme: dark)': {
':root': sheets[2]['@media (prefers-color-scheme: dark)'][':root'], // dark palette
},
},
]);
});

Expand All @@ -742,11 +751,23 @@ describe('extendTheme', () => {
colorSchemes: { light: true, dark: true },
defaultColorScheme: 'dark',
});
expect(theme.generateStyleSheets().flatMap((sheet) => Object.keys(sheet))).to.deep.equal([
':root',
':root',
'@media (prefers-color-scheme: dark) { :root', // this key targets excluded variables for dark
'@media (prefers-color-scheme: light) { :root',
const sheets = theme.generateStyleSheets();
sinon.assert.match(sheets, [
{
':root': sheets[0][':root'], // non-colors related variables
},
{
':root': sheets[1][':root'], // dark palette
'@media (prefers-color-scheme: dark)': {
// dark specific variables
':root': sheets[1]['@media (prefers-color-scheme: dark)'][':root'],
},
},
{
'@media (prefers-color-scheme: light)': {
':root': sheets[2]['@media (prefers-color-scheme: light)'][':root'], // light palette
},
},
]);
});

Expand Down
11 changes: 9 additions & 2 deletions packages/mui-system/src/cssVars/prepareCssVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ function prepareCssVars<T extends DefaultCssVarsTheme, ThemeVars extends Record<
colorSchemesMap[defaultColorScheme] = { css, vars };
}

function defaultGetSelector(colorScheme: keyof T['colorSchemes'] | undefined) {
function defaultGetSelector(
colorScheme: keyof T['colorSchemes'] | undefined,
cssObject: Record<string, any>,
) {
let rule = selector;
if (selector === 'class') {
rule = '.%s';
Expand All @@ -66,7 +69,11 @@ function prepareCssVars<T extends DefaultCssVarsTheme, ThemeVars extends Record<
return ':root';
}
const mode = colorSchemes[colorScheme as string]?.palette?.mode || colorScheme;
return `@media (prefers-color-scheme: ${mode}) { :root`;
return {
[`@media (prefers-color-scheme: ${mode})`]: {
':root': cssObject,
},
};
}
if (rule) {
if (theme.defaultColorScheme === colorScheme) {
Expand Down

0 comments on commit cd5410b

Please sign in to comment.