Skip to content

Commit

Permalink
[theme] Return default value for spacing when no args provided (mui#1…
Browse files Browse the repository at this point in the history
…5891)

* [theme] Add default value for spacing

* Something I still see in people code

It should be more explicit
  • Loading branch information
mbrookes authored and oliviertassinari committed May 28, 2019
1 parent 5499874 commit 09d5f31
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
6 changes: 4 additions & 2 deletions docs/src/pages/guides/migration-v3/migration-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ yarn add @material-ui/styles@next
```
- You can safely remove the next variant from the theme creation:

```js
```diff
typography: {
useNextVariants: true,
- useNextVariants: true,
},
```

Expand All @@ -143,6 +143,8 @@ yarn add @material-ui/styles@next
}
```

You can use the `https://github.com/mui-org/material-ui/tree/master/packages/material-ui-codemod/README.md#theme-spacing-api` migration helper to make the migration process smoother.

*Tip: you can provide more than 1 argument: theme.spacing(1, 2) // = '8px 16px'*

You can use [the migration helper](https://github.com/mui-org/material-ui/tree/master/packages/material-ui-codemod/README.md#theme-spacing-api) on your project to make this smoother.
Expand Down
6 changes: 5 additions & 1 deletion packages/material-ui/src/styles/createSpacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export default function createSpacing(spacingInput = 8) {
}

const spacing = (...args) => {
warning(args.length <= 4, `Too many arguments, expected [1, 4], got ${args.length}`);
warning(args.length <= 4, `Too many arguments, expected between 1 and 4, got ${args.length}`);

if (args.length === 0) {
return transform(1);
}

if (args.length === 1) {
return transform(args[0]);
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 @@ -22,6 +22,14 @@ describe('createSpacing', () => {
createSpacing(spacing);
});

it('should support a default value when no arguments are provided', () => {
let spacing;
spacing = createSpacing();
assert.strictEqual(spacing(), 8);
spacing = createSpacing(factor => `${0.25 * factor}rem`);
assert.strictEqual(spacing(), '0.25rem');
});

it('should support multiple arguments', () => {
let spacing;
spacing = createSpacing();
Expand Down

0 comments on commit 09d5f31

Please sign in to comment.