[Theme] spacing function
See original GitHub issueI’d like to propose the following additive change to theme.spacing
:
import warning from 'warning';
const unit = 8;
const spacing = (...args) => {
if (process.env.NODE_ENV !== 'production') {
warning(
args.length <= 4,
`Too many arguments, exepected 1..4, got ${args.length}`,
);
args.forEach((arg, index) => {
warning(
typeof arg === 'number',
`Expected argument ${index + 1} to be a number, got ${arg}`,
);
});
}
return args
.map((multiplier = 0) => multiplier && `${unit * multiplier}px`)
.join(' ');
};
spacing.unit = unit;
export default spacing;
This is a backward compatible change that allows you to then:
Single value:
Example
padding: theme.spacing(1)
Result
padding: 8px;
Multiple values:
Example
padding: theme.spacing(1, 2)
Result
padding: 8px 16px;
Example
padding: theme.spacing(.5, 1, 2, 3)
Result
padding: 4px 8px 16px 24px;
I’d like to open a pull request for this if we’re onboard.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:6
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Spacing - Material UI - MUI
Use the theme.spacing() helper to create consistent spacing between the elements of your UI. MUI uses a recommended 8px scaling factor by default....
Read more >how theme.spacing() is functioning? - Stack Overflow
When your theme is define, you have a spacing value. By default this value is 8px . So when you call theme.spacing(20) ,...
Read more >material-ui/core.Theme.spacing JavaScript and Node.js code ...
Best JavaScript code snippets using @material-ui/core.Theme.spacing(Showing top 15 results out of 1,395) · Most used @material-ui/core functions · Popular in ...
Read more >React MUI Spacing - GeeksforGeeks
spacing() helper. This method helps the user to maintain a uniform spacing all over the UI by specifying a spacing factor and then...
Read more >makeStyles theme.spacing does not work on mui/material: 5.2 ...
... behavior I upgrade from @mui/material 5.2.2 to @mui/material 5.2.7 and suddenly I get TypeError: theme.spacing is not a function 35 .
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
How would I write
margin: 16px auto
?[theme.spacing(2), 'auto']
?#16163