Skip to content

Commit fa07c10

Browse files
ready for review
1 parent a694362 commit fa07c10

37 files changed

+1019
-1161
lines changed

docs/src/pages/css-in-js/advanced/advanced.md

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const DeepChild = withTheme(DeepChildRaw);
5858
### Theme nesting
5959

6060
You can nest multiple theme providers.
61-
This can be really useful when dealing with different area of your application that have distinct appearance from each other.
61+
This can be really useful when dealing with different areas of your application that have distinct appearance from each other.
6262

6363
```jsx
6464
<ThemeProvider theme={outerTheme}>
@@ -85,10 +85,10 @@ You can extend the outer theme by providing a function:
8585

8686
## JSS plugins
8787

88-
JSS uses plugins to extend its core, allowing you to cherry-pick the features you need,
88+
JSS uses plugins to extend its core, allowing you to cherry-pick the features you need,
8989
and only pay the performance overhead for what you are using.
9090

91-
Not all the plugins are available in Material-UI by default. The following (which is a subset of
91+
Not all the plugins are available in Material-UI by default. The following (which is a subset of
9292
[jss-preset-default](https://cssinjs.org/jss-preset-default/)) are included:
9393

9494
- [jss-plugin-rule-value-function](https://cssinjs.org/jss-plugin-rule-value-function/)
@@ -140,7 +140,7 @@ const useStyles = makeStyles({
140140
});
141141
```
142142

143-
Note that this doesn't support selectors, or nested rules.
143+
Note that this doesn't support selectors or nested rules.
144144

145145
{{"demo": "pages/css-in-js/advanced/StringTemplates.js"}}
146146

@@ -333,45 +333,36 @@ Refer to [this example](https://github.com/mui-org/material-ui/blob/next/example
333333

334334
## Class names
335335

336-
You may have noticed that the class names generated by `@material-ui/styles` are **non-deterministic**,
337-
so you can't rely on them to stay the same.
338336
The class names are generated by [the class name generator](/css-in-js/api/#creategenerateclassname-options-class-name-generator).
339337

340-
Let's take the following style as an example:
338+
### Without a name
341339

342-
```jsx
340+
By default, the class names generated by `@material-ui/styles` are **non-deterministic**, you can't rely on them to stay the same. Let's take the following style as an example:
341+
342+
```js
343343
const useStyles = makeStyles({
344344
root: {
345345
opacity: 1,
346346
},
347-
}, {
348-
name: 'AppBar',
349347
});
350348
```
351349

352-
This will generate a class name such as `AppBar-root-123`. However, the following CSS won't work:
353-
354-
```css
355-
.AppBar-root-123 {
356-
opacity: 0.6;
357-
}
358-
```
350+
This will generate a class name such as `makeStyles-root-123`.
359351

360-
You have to use the `classes` property of a component to override them.
361-
The non-deterministic nature of the class names enables optimization for development and production –
362-
they are easy to debug in development, and as short as possible in production:
352+
You have to use the `classes` property of a component to override the class names.
353+
The non-deterministic nature of the class names enables style isolation.
363354

364-
- In **development**, the class name will be: `.AppBar-root-123`, following this logic:
355+
- In **development**, the class name is: `.makeStyles-root-123`, following this logic:
365356

366357
```js
367-
const sheetName = 'AppBar';
358+
const sheetName = 'makeStyles';
368359
const ruleName = 'root';
369360
const identifier = 123;
370361

371362
const className = `${sheetName}-${ruleName}-${identifier}`;
372363
```
373364

374-
- In **production**, the class name will be: `.jss123`, following this logic:
365+
- In **production**, the class name is: `.jss123`, following this logic:
375366

376367
```js
377368
const productionPrefix = 'jss';
@@ -380,8 +371,18 @@ const identifier = 123;
380371
const className = `${productionPrefix}-${identifier}`;
381372
```
382373

383-
If you don't like this default behavior, you can change it.
384-
JSS allows you to supply a [custom class name generator](https://cssinjs.org/jss-api/#generate-your-class-names).
374+
### With `@material-ui/core`
375+
376+
The generated class names of the `@material-ui/core` components behave differently.
377+
The logic simplifies the style overrides.
378+
As soon as these conditions are met, the class names are **deterministic**:
379+
380+
- Only one theme provider is used (no theme nesting).
381+
- The style sheet name starts with `Mui`.
382+
- The style rule is fully static (no style function).
383+
- The `disableGlobal` option of the class name generator is false (default).
384+
385+
These conditions are with the most common use cases of `@material-ui/core`.
385386

386387
## Global CSS
387388

@@ -397,32 +398,6 @@ You can also combine JSS generated class names with global ones.
397398

398399
{{"demo": "pages/css-in-js/advanced/HybridGlobalCss.js"}}
399400

400-
### Deterministic class names
401-
402-
We provide an option to make the class names **deterministic** with the [`dangerouslyUseGlobalCSS`](/css-in-js/api/#creategenerateclassname-options-class-name-generator) option. When turned on, the class names will look like this:
403-
404-
- development: `.AppBar-root`
405-
- production: `.AppBar-root`
406-
407-
⚠️ **Be cautious when using `dangerouslyUseGlobalCSS`.**
408-
Relying on it for code running in production has the following implications:
409-
410-
- It's harder to keep track of `classes` API changes between major releases.
411-
- Global CSS is inherently fragile.
412-
413-
⚠️ When using `dangerouslyUseGlobalCSS` standalone (without Material-UI), you should name your style sheets using the `options` parameter:
414-
415-
```jsx
416-
// Hook
417-
const useStyles = makeStyles(styles, { name: 'button' });
418-
419-
// Styled-components
420-
const Button = styled(styles, { name: 'button' })(ButtonBase);
421-
422-
// Higher-order component
423-
const Button = withStyles(styles, { name: 'button' })(ButtonBase);
424-
```
425-
426401
## CSS prefixes
427402

428403
JSS uses feature detection to apply the correct prefixes.

docs/src/pages/css-in-js/api/api.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ A function which returns [a class name generator function](http://cssinjs.org/js
99
#### Arguments
1010

1111
1. `options` (*Object* [optional]):
12-
- `options.dangerouslyUseGlobalCSS` (*Boolean* [optional]): Defaults to `false`. Makes the Material-UI class names deterministic.
13-
- `options.productionPrefix` (*String* [optional]): Defaults to `'jss'`. The string used to prefix the class names in production.
14-
- `options.seed` (*String* [optional]): Defaults to `''`. The string used to uniquely identify the generator. It can be used to avoid class name collisions when using multiple generators.
12+
- `options.disableGlobal` (*Boolan* [optional]): Default to `false`. Disable the generation of deterministic class names.
13+
- `options.productionPrefix` (*String* [optional]): Defaults to `'jss'`. The string used to prefix the class names in production. You can disable the minification in production by providing an empty string.
14+
- `options.seed` (*String* [optional]): Defaults to `''`. The string used to uniquely identify the generator. It can be used to avoid class name collisions when using multiple generators in the same document.
1515

1616
#### Returns
1717

@@ -24,7 +24,6 @@ import React from 'react';
2424
import { StylesProvider, createGenerateClassName } from '@material-ui/styles';
2525

2626
const generateClassName = createGenerateClassName({
27-
dangerouslyUseGlobalCSS: true,
2827
productionPrefix: 'c',
2928
});
3029

@@ -212,11 +211,11 @@ It should preferably be used at **the root of your component tree**.
212211

213212
| Name | Type | Default | Description |
214213
|:-----|:-----|:--------|:------------|
215-
| <span class="prop-name required">children&nbsp;*</span> | <span class="prop-type">node</span> | | Your component tree. |
216-
| <span class="prop-name">disableGeneration</span> | <span class="prop-type">bool</span> | false | You can disable the generation of the styles with this option. It can be useful when traversing the React tree outside of the HTML rendering step on the server. Let's say you are using react-apollo to extract all the queries made by the interface server-side. You can significantly speed up the traversal with this property. |
217-
| <span class="prop-name">generateClassName</span> | <span class="prop-type">func</span> | | JSS's class name generator. |
218-
| <span class="prop-name">injectFirst</span> | <span class="prop-type">bool</span> | false | By default, the styles are injected last in the <head> element of your page. They gain more specificity than any other style sheet on your page e.g. CSS modules, styled components. If you want to override the Material-UI's styles, set this prop. |
219-
| <span class="prop-name">jss</span> | <span class="prop-type">object</span> | | JSS's instance. |
214+
| children&nbsp;* | node | | Your component tree. |
215+
| disableGeneration | bool | false | You can disable the generation of the styles with this option. It can be useful when traversing the React tree outside of the HTML rendering step on the server. Let's say you are using react-apollo to extract all the queries made by the interface server-side. You can significantly speed up the traversal with this property. |
216+
| generateClassName | func | | JSS's class name generator. |
217+
| injectFirst | bool | false | By default, the styles are injected last in the <head> element of your page. They gain more specificity than any other style sheet on your page e.g. CSS modules, styled components. If you want to override the Material-UI's styles, set this prop. |
218+
| jss | object | | JSS's instance. |
220219

221220
#### Examples
222221

@@ -243,8 +242,8 @@ It should preferably be used at **the root of your component tree**.
243242

244243
| Name | Type | Default | Description |
245244
|:-----|:-----|:--------|:------------|
246-
| <span class="prop-name required">children&nbsp;*</span> | <span class="prop-type">node</span> | | Your component tree. |
247-
| <span class="prop-name required">theme&nbsp;*</span> | <span class="prop-type">union:&nbsp;object&nbsp;&#124;&nbsp;func</span> | | A theme object. You can provide a function to extend the outer theme. |
245+
| children&nbsp;* | node | | Your component tree. |
246+
| theme&nbsp;* | union:&nbsp;object&nbsp;&#124;&nbsp;func | | A theme object. You can provide a function to extend the outer theme. |
248247

249248
#### Examples
250249

docs/src/pages/demos/badges/CustomizedBadge.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React from 'react';
22
import IconButton from '@material-ui/core/IconButton';
33
import Badge from '@material-ui/core/Badge';
4-
import { makeStyles } from '@material-ui/core/styles';
4+
import { withStyles } from '@material-ui/core/styles';
55
import ShoppingCartIcon from '@material-ui/icons/ShoppingCart';
66

7-
const useStyles = makeStyles(theme => ({
7+
const StyledBadge = withStyles(theme => ({
88
badge: {
99
top: '50%',
1010
right: -3,
@@ -13,16 +13,14 @@ const useStyles = makeStyles(theme => ({
1313
theme.palette.type === 'light' ? theme.palette.grey[200] : theme.palette.grey[900]
1414
}`,
1515
},
16-
}));
16+
}))(Badge);
1717

1818
function CustomizedBadge() {
19-
const classes = useStyles();
20-
2119
return (
2220
<IconButton aria-label="Cart">
23-
<Badge badgeContent={4} color="primary" classes={{ badge: classes.badge }}>
21+
<StyledBadge badgeContent={4} color="primary">
2422
<ShoppingCartIcon />
25-
</Badge>
23+
</StyledBadge>
2624
</IconButton>
2725
);
2826
}

docs/src/pages/demos/badges/CustomizedBadge.tsx

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
import React from 'react';
22
import IconButton from '@material-ui/core/IconButton';
33
import Badge from '@material-ui/core/Badge';
4-
import { makeStyles, Theme, createStyles } from '@material-ui/core/styles';
4+
import { withStyles, Theme } from '@material-ui/core/styles';
55
import ShoppingCartIcon from '@material-ui/icons/ShoppingCart';
66

7-
const useStyles = makeStyles((theme: Theme) =>
8-
createStyles({
9-
badge: {
10-
top: '50%',
11-
right: -3,
12-
// The border color match the background color.
13-
border: `2px solid ${
14-
theme.palette.type === 'light' ? theme.palette.grey[200] : theme.palette.grey[900]
15-
}`,
16-
},
17-
}),
18-
);
7+
const StyledBadge = withStyles((theme: Theme) => ({
8+
badge: {
9+
top: '50%',
10+
right: -3,
11+
// The border color match the background color.
12+
border: `2px solid ${
13+
theme.palette.type === 'light' ? theme.palette.grey[200] : theme.palette.grey[900]
14+
}`,
15+
},
16+
}))(Badge);
1917

2018
function CustomizedBadge() {
21-
const classes = useStyles();
22-
2319
return (
2420
<IconButton aria-label="Cart">
25-
<Badge badgeContent={4} color="primary" classes={{ badge: classes.badge }}>
21+
<StyledBadge badgeContent={4} color="primary">
2622
<ShoppingCartIcon />
27-
</Badge>
23+
</StyledBadge>
2824
</IconButton>
2925
);
3026
}
Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import React from 'react';
2-
import PropTypes from 'prop-types';
3-
import { withStyles } from '@material-ui/core/styles';
2+
import { withStyles, makeStyles } from '@material-ui/core/styles';
43
import { emphasize } from '@material-ui/core/styles/colorManipulator';
54
import Paper from '@material-ui/core/Paper';
65
import Breadcrumbs from '@material-ui/core/Breadcrumbs';
@@ -9,11 +8,8 @@ import Avatar from '@material-ui/core/Avatar';
98
import HomeIcon from '@material-ui/icons/Home';
109
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
1110

12-
const styles = theme => ({
11+
const StyledBreadcrumb = withStyles(theme => ({
1312
root: {
14-
padding: theme.spacing(1),
15-
},
16-
chip: {
1713
backgroundColor: theme.palette.grey[100],
1814
height: 24,
1915
color: theme.palette.grey[800],
@@ -26,30 +22,25 @@ const styles = theme => ({
2622
backgroundColor: emphasize(theme.palette.grey[300], 0.12),
2723
},
2824
},
29-
avatar: {
30-
background: 'none',
31-
marginRight: -theme.spacing(1.5),
32-
},
33-
});
25+
}))(props => <Chip {...props} />);
3426

3527
function handleClick(event) {
3628
event.preventDefault();
3729
alert('You clicked a breadcrumb.'); // eslint-disable-line no-alert
3830
}
3931

40-
function CustomBreadcrumb(props) {
41-
const { classes, ...rest } = props;
42-
return <Chip className={classes.chip} {...rest} />;
43-
}
44-
45-
CustomBreadcrumb.propTypes = {
46-
classes: PropTypes.object.isRequired,
47-
};
48-
49-
const StyledBreadcrumb = withStyles(styles)(CustomBreadcrumb);
32+
const useStyles = makeStyles(theme => ({
33+
root: {
34+
padding: theme.spacing(1),
35+
},
36+
avatar: {
37+
background: 'none',
38+
marginRight: -theme.spacing(1.5),
39+
},
40+
}));
5041

51-
function CustomizedBreadcrumbs(props) {
52-
const { classes } = props;
42+
function CustomizedBreadcrumbs() {
43+
const classes = useStyles();
5344

5445
return (
5546
<Paper elevation={0} className={classes.root}>
@@ -77,8 +68,4 @@ function CustomizedBreadcrumbs(props) {
7768
);
7869
}
7970

80-
CustomizedBreadcrumbs.propTypes = {
81-
classes: PropTypes.object.isRequired,
82-
};
83-
84-
export default withStyles(styles)(CustomizedBreadcrumbs);
71+
export default CustomizedBreadcrumbs;

0 commit comments

Comments
 (0)