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

[material-ui] Return styles directly if the selector is & when using applyStyles #43633

Merged
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
6 changes: 6 additions & 0 deletions packages/mui-material/src/styles/createTheme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,12 @@ describe('createTheme', () => {
});
});

it('should return the styles directly when using applyStyles if the selector is `&`', function test() {
const theme = createTheme({ cssVariables: true, palette: { mode: 'dark' } });

expect(theme.applyStyles('dark', { color: 'red' })).to.deep.equal({ color: 'red' });
});

it('Throw an informative error when the key `vars` is passed as part of `options` passed', () => {
try {
createTheme({
Expand Down
12 changes: 12 additions & 0 deletions packages/mui-system/src/createTheme/applyStyles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,16 @@ describe('applyStyles', () => {
const styles = { background: '#e5e5e5' };
expect(applyStyles.call(theme, 'dark', styles)).to.deep.equal({});
});

it('should return the styles directly if selector is &', () => {
const theme = {
vars: {},
colorSchemes: { light: true },
getColorSchemeSelector: () => {
return '&';
},
};
const styles = { background: '#e5e5e5' };
expect(applyStyles.call(theme, 'light', styles)).to.deep.equal(styles);
});
});
3 changes: 3 additions & 0 deletions packages/mui-system/src/createTheme/applyStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ export default function applyStyles<K extends string>(key: K, styles: CSSObject)
}
// If CssVarsProvider is used as a provider, returns '*:where({selector}) &'
let selector = theme.getColorSchemeSelector(key);
if (selector === '&') {
return styles;
}
if (selector.includes('data-') || selector.includes('.')) {
// '*' is required as a workaround for Emotion issue (https://github.com/emotion-js/emotion/issues/2836)
selector = `*:where(${selector.replace(/\s*&$/, '')}) &`;
Expand Down
Loading