Skip to content

Commit a207808

Browse files
mbrookesoliviertassinari
authored andcommitted
[docs] Document the CSS API (#12174)
* [docs] Document the CSS API * Attempt to document the Slider props, add exception for Select
1 parent 001c142 commit a207808

File tree

188 files changed

+1551
-655
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

188 files changed

+1551
-655
lines changed

docs/scripts/buildApi.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@ function buildDocs(options) {
8383

8484
// eslint-disable-next-line global-require, import/no-dynamic-require
8585
const component = require(componentObject.filename);
86+
const name = path.parse(componentObject.filename).name;
8687
const styles = {
8788
classes: [],
8889
name: null,
90+
descriptions: {},
8991
};
9092

9193
if (component.styles && component.default.options) {
@@ -94,6 +96,29 @@ function buildDocs(options) {
9496
className => !className.match(/^(@media|@keyframes)/),
9597
);
9698
styles.name = component.default.options.name;
99+
100+
let styleSrc = src;
101+
// Exception for Select where the classes are imported from NativeSelect
102+
if (name === 'Select') {
103+
styleSrc = readFileSync(
104+
componentObject.filename.replace('Select/Select', 'NativeSelect/NativeSelect'),
105+
'utf8',
106+
);
107+
}
108+
109+
/**
110+
* Collect classes comments from the source
111+
*/
112+
const stylesRegexp = /export const styles.*\n(.*\n)*};\n\n/;
113+
const styleRegexp = /\/\* (.*) \*\/\n\s*(\w*)/g;
114+
// Extract the styles section from the source
115+
const stylesSrc = stylesRegexp.exec(styleSrc);
116+
if (stylesSrc) {
117+
// Extract individual classes and descriptions
118+
stylesSrc[0].replace(styleRegexp, (match, desc, key) => {
119+
styles.descriptions[key] = desc;
120+
});
121+
}
97122
}
98123

99124
let reactAPI;
@@ -104,7 +129,7 @@ function buildDocs(options) {
104129
throw err;
105130
}
106131

107-
reactAPI.name = path.parse(componentObject.filename).name;
132+
reactAPI.name = name;
108133
reactAPI.styles = styles;
109134
reactAPI.pagesMarkdown = pagesMarkdown;
110135
reactAPI.src = src;
@@ -147,7 +172,7 @@ export default withRoot(Page);
147172
`,
148173
);
149174

150-
console.log('Built markdown docs for', componentObject.filename);
175+
console.log('Built markdown docs for', reactAPI.name);
151176
});
152177
}
153178

docs/src/modules/utils/generateMarkdown.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,29 @@ function generateClasses(reactAPI) {
239239
throw new Error(`Missing styles name on ${reactAPI.name} component`);
240240
}
241241

242+
let text = '';
243+
if (Object.keys(reactAPI.styles.descriptions).length) {
244+
text = `
245+
| Name | Description |
246+
|:-----|:------------|\n`;
247+
text += reactAPI.styles.classes
248+
.map(
249+
className =>
250+
`| <span class="prop-name">${className}</span> | ${
251+
reactAPI.styles.descriptions[className] ? reactAPI.styles.descriptions[className] : ''
252+
}`,
253+
)
254+
.join('\n');
255+
} else {
256+
text = reactAPI.styles.classes.map(className => `- \`${className}\``).join('\n');
257+
}
258+
242259
return `## CSS API
243260
244261
You can override all the class names injected by Material-UI thanks to the \`classes\` property.
245262
This property accepts the following keys:
246-
${reactAPI.styles.classes.map(className => `- \`${className}\``).join('\n')}
263+
264+
${text}
247265
248266
Have a look at [overriding with classes](/customization/overrides#overriding-with-classes) section
249267
and the [implementation of the component](${SOURCE_CODE_ROOT_URL}${normalizePath(

packages/material-ui-lab/src/Slider/Slider.js

+21-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import ButtonBase from '@material-ui/core/ButtonBase';
88
import { fade } from '@material-ui/core/styles/colorManipulator';
99
import clamp from '../utils/clamp';
1010

11-
export const style = theme => {
11+
export const styles = theme => {
1212
const commonTransitionsOptions = {
1313
duration: theme.transitions.duration.short,
1414
easing: theme.transitions.easing.easeOut,
@@ -29,7 +29,7 @@ export const style = theme => {
2929
};
3030

3131
return {
32-
// /* Styles for root node */
32+
/* Styles applied to the root element. */
3333
root: {
3434
position: 'relative',
3535
width: '100%',
@@ -50,13 +50,14 @@ export const style = theme => {
5050
transform: 'scaleY(-1)',
5151
},
5252
},
53+
/* Styles applied to the container element. */
5354
container: {
5455
position: 'relative',
5556
'&$vertical': {
5657
height: '100%',
5758
},
5859
},
59-
/* Tracks styles */
60+
/* Styles applied to the track elements. */
6061
track: {
6162
position: 'absolute',
6263
transform: 'translate(0, -50%)',
@@ -79,6 +80,7 @@ export const style = theme => {
7980
backgroundColor: colors.focused,
8081
},
8182
},
83+
/* Styles applied to the track element before the thumb. */
8284
trackBefore: {
8385
zIndex: 1,
8486
left: 0,
@@ -88,6 +90,7 @@ export const style = theme => {
8890
backgroundColor: colors.primary,
8991
},
9092
},
93+
/* Styles applied to the track element after the thumb. */
9194
trackAfter: {
9295
right: 0,
9396
backgroundColor: colors.secondary,
@@ -96,7 +99,7 @@ export const style = theme => {
9699
bottom: 0,
97100
},
98101
},
99-
/* Thumb styles */
102+
/* Styles applied to the thumb element. */
100103
thumb: {
101104
position: 'absolute',
102105
zIndex: 2,
@@ -137,13 +140,20 @@ export const style = theme => {
137140
height: 17,
138141
},
139142
},
143+
/* Class applied to the root element to trigger JSS nested styles if `reverse={true}` . */
144+
reverse: {},
145+
/* Class applied to the track and thumb elements to trigger JSS nested styles if `disabled`. */
146+
disabled: {},
147+
/* Class applied to the track and thumb elements to trigger JSS nested styles if `jumped`. */
148+
jumped: {},
149+
/* Class applied to the track and thumb elements to trigger JSS nested styles if `focused`. */
140150
focused: {},
151+
/* Class applied to the track and thumb elements to trigger JSS nested styles if `activated`. */
141152
activated: {},
142-
disabled: {},
143-
zero: {},
153+
/* Class applied to the root, track and container to trigger JSS nested styles if `vertical`. */
144154
vertical: {},
145-
reverse: {},
146-
jumped: {},
155+
/* Class applied to the thumb to trigger nested styles if `value` = `min` . */
156+
zero: {},
147157
};
148158
};
149159

@@ -417,7 +427,7 @@ class Slider extends React.Component {
417427
[classes.activated]: !disabled && currentState === 'activated',
418428
};
419429

420-
const rootClasses = classNames(
430+
const className = classNames(
421431
classes.root,
422432
{
423433
[classes.vertical]: vertical,
@@ -452,7 +462,7 @@ class Slider extends React.Component {
452462
return (
453463
<Component
454464
role="slider"
455-
className={rootClasses}
465+
className={className}
456466
aria-valuenow={value}
457467
aria-valuemin={min}
458468
aria-valuemax={max}
@@ -555,4 +565,4 @@ Slider.defaultProps = {
555565
component: 'div',
556566
};
557567

558-
export default withStyles(style, { name: 'MuiSlider', withTheme: true })(Slider);
568+
export default withStyles(styles, { name: 'MuiSlider', withTheme: true })(Slider);

packages/material-ui-lab/src/SpeedDial/SpeedDial.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@ import { duration } from '@material-ui/core/styles/transitions';
1010
import Button from '@material-ui/core/Button';
1111
import { isMuiElement } from '@material-ui/core/utils/reactHelpers';
1212

13-
const styles = {
13+
export const styles = {
14+
/* Styles applied to the root element. */
1415
root: {
1516
zIndex: 1050,
1617
display: 'flex',
1718
flexDirection: 'column-reverse', // Place the Actions above the FAB.
1819
},
20+
/* Styles applied to the actions (`children` wrapper) element. */
1921
actions: {
2022
display: 'flex',
2123
flexDirection: 'column-reverse', // Display the first action at the bottom.
2224
marginBottom: 16,
2325
},
26+
/* Styles applied to the actions (`children` wrapper) element if `open={false}`. */
2427
actionsClosed: {
2528
transition: 'top 0s linear 0.2s',
2629
},
@@ -51,8 +54,9 @@ class SpeedDial extends React.Component {
5154
actions.firstChild.firstChild.focus();
5255

5356
// This determines which key focuses the next / previous action.
54-
// For example, if a visually impaired user presses down to select the first action
55-
// (i.e. following DOM ordering), down will select the next action, and up the previous.
57+
// For example, if a user presses down to select the first action
58+
// (i.e. following DOM ordering rather than visual ordering),
59+
// down will select the next action, and up the previous.
5660
if (nextKey == null) {
5761
this.setState({ nextKey: key });
5862
this.setState({ prevKey: key === 'up' ? 'down' : 'up' });
@@ -268,4 +272,4 @@ SpeedDial.defaultProps = {
268272
},
269273
};
270274

271-
export default withStyles(styles)(SpeedDial);
275+
export default withStyles(styles, { name: 'MuiSpeedDial' })(SpeedDial);

packages/material-ui-lab/src/SpeedDialAction/SpeedDialAction.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import { withStyles } from '@material-ui/core/styles';
55
import Button from '@material-ui/core/Button';
66
import Tooltip from '@material-ui/core/Tooltip';
77

8-
const styles = theme => ({
8+
export const styles = theme => ({
9+
/* Styles applied to the root (`Tooltip`) component. */
910
root: {
1011
position: 'relative',
1112
},
13+
/* Styles applied to the `Button` component. */
1214
button: {
1315
margin: 8,
1416
color: theme.palette.text.secondary,
@@ -17,6 +19,7 @@ const styles = theme => ({
1719
})}, opacity 0.8s`,
1820
opacity: 1,
1921
},
22+
/* Styles applied to the `Button` component if `open={false}`. */
2023
buttonClosed: {
2124
opacity: 0,
2225
transform: 'scale(0)',
@@ -127,4 +130,4 @@ SpeedDialAction.defaultProps = {
127130
open: false,
128131
};
129132

130-
export default withStyles(styles)(SpeedDialAction);
133+
export default withStyles(styles, { name: 'MuiSpeedDialAction' })(SpeedDialAction);

packages/material-ui-lab/src/SpeedDialIcon/SpeedDialIcon.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,26 @@ import classNames from 'classnames';
44
import { withStyles } from '@material-ui/core/styles';
55
import AddIcon from '../internal/svg-icons/Add';
66

7-
const styles = theme => ({
7+
export const styles = theme => ({
8+
/* Styles applied to the root element. */
89
root: {
910
height: 24,
1011
},
12+
/* Styles applied to the icon component. */
1113
icon: {
1214
transition: theme.transitions.create(['transform', 'opacity'], {
1315
duration: theme.transitions.duration.short,
1416
}),
1517
},
18+
/* Styles applied to the icon component if `open={true}`. */
1619
iconOpen: {
1720
transform: 'rotate(45deg)',
1821
},
19-
// Style applied to the icon if there is an openIcon, when the SpeedDial is open
22+
/* Styles applied to the icon when and `openIcon` is provided & if `open={true}`. */
2023
iconWithOpenIconOpen: {
2124
opacity: 0,
2225
},
26+
/* Styles applied to the `openIcon` if provided. */
2327
openIcon: {
2428
position: 'absolute',
2529
transition: theme.transitions.create(['transform', 'opacity'], {
@@ -28,6 +32,7 @@ const styles = theme => ({
2832
opacity: 0,
2933
transform: 'rotate(-45deg)',
3034
},
35+
/* Styles applied to the `openIcon` if provided & if `open={true}` */
3136
openIconOpen: {
3237
transform: 'rotate(0deg)',
3338
opacity: 1,
@@ -81,4 +86,4 @@ SpeedDialIcon.propTypes = {
8186

8287
SpeedDialIcon.muiName = 'SpeedDialIcon';
8388

84-
export default withStyles(styles)(SpeedDialIcon);
89+
export default withStyles(styles, { name: 'MuiSpeedDialIcon' })(SpeedDialIcon);

packages/material-ui-lab/src/ToggleButton/ToggleButton.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { fade } from '@material-ui/core/styles/colorManipulator';
88
import ButtonBase from '@material-ui/core/ButtonBase';
99

1010
export const styles = theme => ({
11+
/* Styles applied to the root element. */
1112
root: {
1213
...theme.typography.button,
1314
height: 32,
@@ -28,26 +29,20 @@ export const styles = theme => ({
2829
backgroundColor: 'transparent',
2930
},
3031
},
31-
3232
'&:not(:first-child)': {
3333
borderTopLeftRadius: 0,
3434
borderBottomLeftRadius: 0,
3535
},
36-
3736
'&:not(:last-child)': {
3837
borderTopRightRadius: 0,
3938
borderBottomRightRadius: 0,
4039
},
4140
},
42-
label: {
43-
width: '100%',
44-
display: 'inherit',
45-
alignItems: 'inherit',
46-
justifyContent: 'inherit',
47-
},
41+
/* Styles applied to the root element if `disabled={true}`. */
4842
disabled: {
4943
color: fade(theme.palette.action.disabled, 0.12),
5044
},
45+
/* Styles applied to the root element if `selected={true}`. */
5146
selected: {
5247
color: theme.palette.action.active,
5348
'&:after': {
@@ -65,7 +60,6 @@ export const styles = theme => ({
6560
backgroundColor: 'currentColor',
6661
opacity: 0.38,
6762
},
68-
6963
'& + &:before': {
7064
content: '""',
7165
display: 'block',
@@ -81,6 +75,13 @@ export const styles = theme => ({
8175
opacity: 0.12,
8276
},
8377
},
78+
/* Styles applied to the `label` wrapper element. */
79+
label: {
80+
width: '100%',
81+
display: 'inherit',
82+
alignItems: 'inherit',
83+
justifyContent: 'inherit',
84+
},
8485
});
8586

8687
class ToggleButton extends React.Component {

packages/material-ui-lab/src/ToggleButton/ToggleButtonGroup.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import hasValue from './hasValue';
66
import isValueSelected from './isValueSelected';
77

88
export const styles = theme => ({
9+
/* Styles applied to the root element. */
910
root: {
1011
transition: theme.transitions.create('background,box-shadow'),
1112
background: 'transparent',
1213
borderRadius: 2,
1314
overflow: 'hidden',
1415
},
15-
16+
/* Styles applied to the root element if `selected={true}` or `selected="auto" and `value` set. */
1617
selected: {
1718
background: theme.palette.background.paper,
1819
boxShadow: theme.shadows[2],

0 commit comments

Comments
 (0)