question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Theme] spacing function

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
valoricDecommented, Feb 19, 2019

How would I write margin: 16px auto? [theme.spacing(2), 'auto']?

0reactions
socketpaircommented, Jun 11, 2019
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found