Skip to content

Commit

Permalink
[theme] Support string args in theme.spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
m4theushw committed Apr 5, 2020
1 parent 1e8b293 commit b07f16f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
11 changes: 6 additions & 5 deletions docs/src/pages/customization/spacing/spacing.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ theme.spacing(2); // = 8
## Multiple arity

The `theme.spacing()` helper accepts up to 4 arguments.
You can use the arguments to reduce the boilerplate. Instead of doing:
You can use the arguments to reduce the boilerplate.

```js
padding: `${theme.spacing(1)}px ${theme.spacing(2)}px`, // '8px 16px'
```diff
-padding: `${theme.spacing(1)}px ${theme.spacing(2)}px`, // '8px 16px'
+padding: theme.spacing(1, 2), // '8px 16px'
```

you can do:
Mixing string values is also supported:

```js
padding: theme.spacing(1, 2), // '8px 16px'
margin: theme.spacing(1, 'auto'), // '8px auto'
```
7 changes: 5 additions & 2 deletions packages/material-ui/src/styles/createSpacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ export default function createSpacing(spacingInput = 8) {
}

return args
.map((factor) => {
const output = transform(factor);
.map((argument) => {
if (typeof argument === 'string') {
return argument;
}
const output = transform(argument);
return typeof output === 'number' ? `${output}px` : output;
})
.join(' ');
Expand Down
8 changes: 8 additions & 0 deletions packages/material-ui/src/styles/createSpacing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ describe('createSpacing', () => {
expect(spacing(1, 2)).to.equal('0.25rem 0.5rem');
});

it('should support string arguments', () => {
let spacing;
spacing = createSpacing();
expect(spacing(1, 'auto')).to.equal('8px auto');
spacing = createSpacing((factor) => `${0.25 * factor}rem`);
expect(spacing(1, 'auto', 2, 3)).to.equal('0.25rem auto 0.5rem 0.75rem');
});

describe('warnings', () => {
beforeEach(() => {
consoleErrorMock.spy();
Expand Down

0 comments on commit b07f16f

Please sign in to comment.