Skip to content

Commit 2bc4ba1

Browse files
Ryan review
1 parent 8811cce commit 2bc4ba1

File tree

4 files changed

+29
-35
lines changed

4 files changed

+29
-35
lines changed

packages/material-ui-styles/src/ThemeProvider/ThemeProvider.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import warning from 'warning';
44
import { exactProp } from '@material-ui/utils';
55
import ThemeContext from '../useTheme/ThemeContext';
66
import useTheme from '../useTheme';
7+
import nested from './nested';
78

89
// To support composition of theme.
910
function mergeOuterLocalTheme(outerTheme, localTheme) {
@@ -24,8 +25,6 @@ function mergeOuterLocalTheme(outerTheme, localTheme) {
2425
return { ...outerTheme, ...localTheme };
2526
}
2627

27-
export const nested = Symbol('nested');
28-
2928
/**
3029
* This component takes a `theme` property.
3130
* It makes the `theme` available down the React tree thanks to React context.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const nested = Symbol('nested');
2+
3+
export default nested;

packages/material-ui-styles/src/createGenerateClassName/createGenerateClassName.js

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import warning from 'warning';
2-
import { nested } from '../ThemeProvider/ThemeProvider';
3-
4-
function safePrefix(classNamePrefix) {
5-
const prefix = String(classNamePrefix);
6-
warning(prefix.length < 256, `Material-UI: the class name prefix is too long: ${prefix}.`);
7-
return prefix;
8-
}
2+
import nested from '../ThemeProvider/nested';
93

104
/**
115
* This is the list of the style rule name we use as drop in replacement for the built-in
@@ -35,30 +29,10 @@ const pseudoClasses = [
3529
// https://github.com/cssinjs/jss/blob/4e6a05dd3f7b6572fdd3ab216861d9e446c20331/src/utils/createGenerateClassName.js
3630
export default function createGenerateClassName(options = {}) {
3731
const { disableGlobal = false, productionPrefix = 'jss', seed = '' } = options;
32+
const classNameSeed = seed === '' ? '' : `-${seed}`;
3833
let ruleCounter = 0;
3934

4035
return (rule, styleSheet) => {
41-
const isGlobal =
42-
!styleSheet.options.link &&
43-
styleSheet.options.name &&
44-
styleSheet.options.name.indexOf('Mui') === 0 &&
45-
!disableGlobal;
46-
47-
if (isGlobal) {
48-
if (pseudoClasses.indexOf(rule.key) !== -1) {
49-
return rule.key;
50-
}
51-
52-
if (!styleSheet.options.theme[nested]) {
53-
const prefix = `${safePrefix(styleSheet.options.name)}${seed}`;
54-
55-
if (rule.key === 'root') {
56-
return prefix;
57-
}
58-
return `${prefix}-${rule.key}`;
59-
}
60-
}
61-
6236
ruleCounter += 1;
6337
warning(
6438
ruleCounter < 1e10,
@@ -68,15 +42,33 @@ export default function createGenerateClassName(options = {}) {
6842
].join(''),
6943
);
7044

45+
const name = styleSheet.options.name;
46+
47+
// Is a global static MUI style?
48+
if (name && name.indexOf('Mui') === 0 && !styleSheet.options.link && !disableGlobal) {
49+
// We can use a shorthand class name, we never use the keys to style the components.
50+
if (pseudoClasses.indexOf(rule.key) !== -1) {
51+
return rule.key;
52+
}
53+
54+
const prefix = `${name}${rule.key === 'root' ? '' : `-${rule.key}`}${classNameSeed}`;
55+
56+
if (!styleSheet.options.theme[nested] || seed !== '') {
57+
return prefix;
58+
}
59+
60+
return `${prefix}-${ruleCounter}`;
61+
}
62+
7163
if (process.env.NODE_ENV === 'production' && productionPrefix !== '') {
72-
return `${productionPrefix}${seed}${ruleCounter}`;
64+
return `${productionPrefix}${classNameSeed}${ruleCounter}`;
7365
}
7466

75-
const suffix = `${rule.key}-${seed}${ruleCounter}`;
67+
const suffix = `${rule.key}${classNameSeed}-${ruleCounter}`;
7668

7769
// Help with debuggability.
7870
if (styleSheet.options.classNamePrefix) {
79-
return `${safePrefix(styleSheet.options.classNamePrefix)}-${suffix}`;
71+
return `${styleSheet.options.classNamePrefix}-${suffix}`;
8072
}
8173

8274
return suffix;

packages/material-ui-styles/src/createGenerateClassName/createGenerateClassName.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { assert } from 'chai';
22
import consoleErrorMock from 'test/utils/consoleErrorMock';
33
import createGenerateClassName from './createGenerateClassName';
4-
import { nested } from '../ThemeProvider/ThemeProvider';
4+
import nested from '../ThemeProvider/nested';
55

66
describe('createGenerateClassName', () => {
77
it('should generate a class name', () => {
@@ -91,7 +91,7 @@ describe('createGenerateClassName', () => {
9191
},
9292
},
9393
),
94-
'root-1',
94+
'MuiButton-2',
9595
);
9696
});
9797

0 commit comments

Comments
 (0)