Skip to content

Commit 0033bb3

Browse files
author
Weslen do Nascimento
authored
[system] Array reject on spacing transformation fixed (#19900)
1 parent 41a08d2 commit 0033bb3

File tree

5 files changed

+41
-47
lines changed

5 files changed

+41
-47
lines changed

docs/src/pages/customization/spacing/spacing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ theme.spacing(2); // = 0.25 * 2rem = 0.5rem = 8px
3838

3939
```js
4040
const theme = createMuiTheme({
41-
spacing: factor => [0, 4, 8, 16, 32, 64][factor],
41+
spacing: [0, 4, 8, 16, 32, 64],
4242
});
4343

4444
theme.spacing(2); // = 8

packages/material-ui-system/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export { default as shadows } from './shadows';
1616
export { default as sizing } from './sizing';
1717
export * from './sizing';
1818
export { default as spacing } from './spacing';
19+
export * from './spacing';
1920
export { default as style } from './style';
2021
export { default as typography } from './typography';
2122
export * from './typography';

packages/material-ui-system/src/spacing.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,20 @@ const spacingKeys = [
7474
'paddingY',
7575
];
7676

77-
function getTransformer(theme) {
77+
export function createUnarySpacing(theme) {
7878
const themeSpacing = theme.spacing || 8;
7979

8080
if (typeof themeSpacing === 'number') {
81-
return abs => themeSpacing * abs;
81+
return abs => {
82+
if (process.env.NODE_ENV !== 'production') {
83+
if (typeof abs !== 'number') {
84+
console.error(
85+
`@material-ui/system: expected spacing argument to be a number, got ${abs}.`,
86+
);
87+
}
88+
}
89+
return themeSpacing * abs;
90+
};
8291
}
8392

8493
if (Array.isArray(themeSpacing)) {
@@ -144,7 +153,7 @@ function getStyleFromPropValue(cssProperties, transformer) {
144153

145154
function spacing(props) {
146155
const theme = props.theme;
147-
const transformer = getTransformer(theme);
156+
const transformer = createUnarySpacing(theme);
148157

149158
return Object.keys(props)
150159
.map(prop => {

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

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { createUnarySpacing } from '@material-ui/system';
2+
13
let warnOnce;
24

35
export default function createSpacing(spacingInput = 8) {
@@ -6,32 +8,12 @@ export default function createSpacing(spacingInput = 8) {
68
return spacingInput;
79
}
810

9-
// All components align to an 8dp square baseline grid for mobile, tablet, and desktop.
10-
// https://material.io/design/layout/understanding-layout.html#pixel-density
11-
let transform;
12-
13-
if (typeof spacingInput === 'function') {
14-
transform = spacingInput;
15-
} else {
16-
if (process.env.NODE_ENV !== 'production') {
17-
if (typeof spacingInput !== 'number') {
18-
console.error(
19-
[
20-
`Material-UI: the \`theme.spacing\` value (${spacingInput}) is invalid.`,
21-
'It should be a number or a function.',
22-
].join('\n'),
23-
);
24-
}
25-
}
26-
transform = factor => {
27-
if (process.env.NODE_ENV !== 'production') {
28-
if (typeof factor !== 'number') {
29-
console.error(`Expected spacing argument to be a number, got ${factor}`);
30-
}
31-
}
32-
return spacingInput * factor;
33-
};
34-
}
11+
// Material Design layouts are visually balanced. Most measurements align to an 8dp grid applied, which aligns both spacing and the overall layout.
12+
// Smaller components, such as icons and type, can align to a 4dp grid.
13+
// https://material.io/design/layout/understanding-layout.html#usage
14+
const transform = createUnarySpacing({
15+
spacing: spacingInput,
16+
});
3517

3618
const spacing = (...args) => {
3719
if (process.env.NODE_ENV !== 'production') {

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import { assert } from 'chai';
1+
import { expect } from 'chai';
22
import createSpacing from './createSpacing';
33
import consoleErrorMock from 'test/utils/consoleErrorMock';
44

55
describe('createSpacing', () => {
66
it('should work as expected', () => {
77
let spacing;
88
spacing = createSpacing();
9-
assert.strictEqual(spacing(1), 8);
9+
expect(spacing(1)).to.equal(8);
1010
spacing = createSpacing(10);
11-
assert.strictEqual(spacing(1), 10);
12-
spacing = createSpacing(factor => [0, 8, 16][factor]);
13-
assert.strictEqual(spacing(2), 16);
11+
expect(spacing(1)).to.equal(10);
12+
spacing = createSpacing([0, 8, 16]);
13+
expect(spacing(2)).to.equal(16);
1414
spacing = createSpacing(factor => factor ** 2);
15-
assert.strictEqual(spacing(2), 4);
15+
expect(spacing(2)).to.equal(4);
1616
spacing = createSpacing(factor => `${0.25 * factor}rem`);
17-
assert.strictEqual(spacing(2), '0.5rem');
17+
expect(spacing(2)).to.equal('0.5rem');
1818
});
1919

2020
it('should support recursion', () => {
@@ -25,17 +25,17 @@ describe('createSpacing', () => {
2525
it('should support a default value when no arguments are provided', () => {
2626
let spacing;
2727
spacing = createSpacing();
28-
assert.strictEqual(spacing(), 8);
28+
expect(spacing()).to.equal(8);
2929
spacing = createSpacing(factor => `${0.25 * factor}rem`);
30-
assert.strictEqual(spacing(), '0.25rem');
30+
expect(spacing()).to.equal('0.25rem');
3131
});
3232

3333
it('should support multiple arguments', () => {
3434
let spacing;
3535
spacing = createSpacing();
36-
assert.strictEqual(spacing(1, 2), '8px 16px');
36+
expect(spacing(1, 2)).to.equal('8px 16px');
3737
spacing = createSpacing(factor => `${0.25 * factor}rem`);
38-
assert.strictEqual(spacing(1, 2), '0.25rem 0.5rem');
38+
expect(spacing(1, 2)).to.equal('0.25rem 0.5rem');
3939
});
4040

4141
describe('warnings', () => {
@@ -47,20 +47,22 @@ describe('createSpacing', () => {
4747
consoleErrorMock.reset();
4848
});
4949

50+
// TODO v5: remove
5051
it('should warn for the deprecated API', () => {
5152
const spacing = createSpacing(11);
52-
assert.strictEqual(spacing.unit, 11);
53-
assert.strictEqual(consoleErrorMock.callCount(), 1);
54-
assert.include(consoleErrorMock.args()[0][0], 'theme.spacing.unit usage has been deprecated');
53+
expect(spacing.unit).to.equal(11);
54+
expect(consoleErrorMock.callCount()).to.equal(1);
55+
expect(consoleErrorMock.args()[0][0]).to.include(
56+
'theme.spacing.unit usage has been deprecated',
57+
);
5558
});
5659

5760
it('should warn for wrong input', () => {
5861
createSpacing({
5962
unit: 4,
6063
});
61-
assert.strictEqual(consoleErrorMock.callCount(), 1);
62-
assert.include(
63-
consoleErrorMock.args()[0][0],
64+
expect(consoleErrorMock.callCount()).to.equal(1);
65+
expect(consoleErrorMock.args()[0][0]).to.include(
6466
'the `theme.spacing` value ([object Object]) is invalid',
6567
);
6668
});

0 commit comments

Comments
 (0)