|
11 | 11 |
|
12 | 12 | import React from 'react';
|
13 | 13 | import PropTypes from 'prop-types';
|
| 14 | +import warning from 'warning'; |
14 | 15 | import classNames from 'classnames';
|
15 | 16 | import { componentPropType } from '@material-ui/utils';
|
16 | 17 | import withStyles from '../styles/withStyles';
|
17 | 18 | import { keys as breakpointKeys } from '../styles/createBreakpoints';
|
18 | 19 | import requirePropFactory from '../utils/requirePropFactory';
|
19 | 20 |
|
20 |
| -const GUTTERS = [0, 1, 2, 3, 4, 5, 6, 7, 8]; |
| 21 | +const SPACINGS = [0, 1, 2, 3, 4, 5, 6, 7, 8]; |
21 | 22 | const GRID_SIZES = ['auto', true, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
|
22 | 23 |
|
23 | 24 | function generateGrid(globalStyles, theme, breakpoint) {
|
@@ -65,16 +66,57 @@ function generateGrid(globalStyles, theme, breakpoint) {
|
65 | 66 | }
|
66 | 67 | }
|
67 | 68 |
|
| 69 | +// Same logic as /packages/material-ui-system/src/spacing.js |
| 70 | +function getTransformer(theme) { |
| 71 | + const themeTransformer = |
| 72 | + theme.spacing && theme.spacing.unit != null ? theme.spacing.unit : theme.spacing || 8; |
| 73 | + |
| 74 | + if (typeof themeTransformer === 'number') { |
| 75 | + return abs => themeTransformer * abs; |
| 76 | + } |
| 77 | + |
| 78 | + if (Array.isArray(themeTransformer)) { |
| 79 | + return abs => { |
| 80 | + warning( |
| 81 | + abs <= themeTransformer.length - 1, |
| 82 | + [ |
| 83 | + `@material-ui/core: the value provided (${abs}) overflows.`, |
| 84 | + `The supported values are: ${JSON.stringify(themeTransformer)}.`, |
| 85 | + `${abs} > ${themeTransformer.length - 1}, you need to add the missing values.`, |
| 86 | + ].join('\n'), |
| 87 | + ); |
| 88 | + |
| 89 | + return themeTransformer[abs]; |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + if (typeof themeTransformer === 'function') { |
| 94 | + return themeTransformer; |
| 95 | + } |
| 96 | + |
| 97 | + warning( |
| 98 | + false, |
| 99 | + [ |
| 100 | + `@material-ui/core: the \`theme.spacing\` value (${themeTransformer}) is invalid.`, |
| 101 | + 'It should be a number, an array or a function.', |
| 102 | + ].join('\n'), |
| 103 | + ); |
| 104 | + |
| 105 | + return () => undefined; |
| 106 | +} |
| 107 | + |
68 | 108 | function generateGutter(theme, breakpoint) {
|
69 | 109 | const styles = {};
|
70 | 110 |
|
71 |
| - GUTTERS.forEach((spacing, index) => { |
| 111 | + const transformer = getTransformer(theme); |
| 112 | + |
| 113 | + SPACINGS.forEach((spacing, index) => { |
72 | 114 | if (index === 0) {
|
73 | 115 | // Skip the default style.
|
74 | 116 | return;
|
75 | 117 | }
|
76 | 118 |
|
77 |
| - const themeSpacing = spacing * theme.spacing.unit; |
| 119 | + const themeSpacing = transformer(spacing); |
78 | 120 |
|
79 | 121 | styles[`spacing-${breakpoint}-${spacing}`] = {
|
80 | 122 | margin: -themeSpacing / 2,
|
@@ -324,7 +366,7 @@ Grid.propTypes = {
|
324 | 366 | * Defines the space between the type `item` component.
|
325 | 367 | * It can only be used on a type `container` component.
|
326 | 368 | */
|
327 |
| - spacing: PropTypes.oneOf(GUTTERS), |
| 369 | + spacing: PropTypes.oneOf(SPACINGS), |
328 | 370 | /**
|
329 | 371 | * Defines the `flex-wrap` style property.
|
330 | 372 | * It's applied for all screen sizes.
|
|
0 commit comments