Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[theme] Support string args in theme.spacing #20408

Merged
merged 1 commit into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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