-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[Discussion] Support some (or maybe all?) non-integer values passed to theme.spacing()
#29677
Comments
I like the simplicity of the approach @oliviertassinari suggested (with regards to half values)
It works as expected, but the results may be a little un-intuitive. Suppose
Integer values passed to
Now let's see what happens if we use the suggested formula for "half-values", namely:
The specific use case mentioned makes a lot of sense.
Suppose some variable In other words, the following...
...could instead simply become this:
That said, it does seem a little strange that...
I'm a little torn on this, but part of me thinks that If we only allow positive integer values and positive "half-values", we will have to perform some kind of check and throw an error / warning when invalid values are passed in (in all cases, regardless of whether
A few observations on
|
I found this workaround that is good enough for me. /**
* Due to a bug in MUI we have to use this function instead of the array like this:
* spacing: [0, 4, 8, 16, 24, 32, 64, 128, 256, 512, 1024],
*
* Source:
* https://github.com/mui/material-ui/issues/29677
*
* Reason:
* Some components will use non-integer values for spacing and this breaks the spacing array.
*
* Solution:
* Use a function instead of an array and calculate the spacing based on the factor
*
* Result:
* theme.spacing(1) // 4px
* theme.spacing(1.5) // 6px
* theme.spacing(2) // 8px
* theme.spacing(2.5) // 12px
*/
spacing: (factor: number) => {
const values = [0, 4, 8, 16, 24, 32, 64, 128, 256, 512, 1024, 2048];
const index = Math.floor(factor);
const currentSpace = values[index];
const nextSpace = values[index + 1] || currentSpace * 2;
const space = currentSpace + (nextSpace - currentSpace) * (factor - index);
return `${space}px`;
}, |
Any official update on this? |
Thank you @madflanderz. Great workaround! |
N.B.: This issue is a continuation of the discussion in #29526, as requested by @siriwatknp and @oliviertassinari
Summary
Should we allow positive, non-integer values to be passed to
theme.spacing()
? And, if so, should we only allow "half-values" (e.g.0.5
,1.5
,2.5
)? Or should we allow all positive non-integer values (e.g.0.05
,1.2
, etc.)?Original issue
In #29526, we addressed and resolved an issue (#29479) with non-integer values (e.g.
0.5
,1.2
, etc.) being passed as parameters totheme.spacing()
. Such values are invalid whenspacing
is an array, and their use in some MUI components was resulting in errors.The fix was to replace all instances where MUI components were trying to pass decimal values to
theme.spacing()
with string interpolation and CSScalc()
.So, something like this...
...was changed to this:
Current behavior
As per the documentation,
spacing
can be defined as:If
spacing
is defined as a number, non-integer decimal values can be safely passed totheme.spacing()
and work as expected. The number in question is a coefficient. If, for instance, this coefficient is K,theme.spacing(1.2)
calculates to 1.2 × K. The same can more or less be said for spacing when it is defined as a custom function, as shown above.If, however,
spacing
is defined as an array, the parameters passed totheme.spacing()
must be valid array indexes. Non-integer numbers are not valid indexes. Whenspacing
is an array, and decimal values are provided totheme.spacing()
, the values are ignored and an error message is displayed:The origin of this error message is another previous issue, #23187.
Suggested change
@siriwatknp suggested that, while the fix resolves the existing issue, it might make more sense to simply allow non-integer values to always be passed to
theme.spacing()
, even ifspacing
is an array. This would involve changing the logic increateSpacing.ts
to do the calculation there, instead of forcing devs to write out the csscalc()
implementation each time.@oliviertassinari responded that non-integer parameters passed to
theme.spacing()
make sense for "half values" (e.g.0.5
,1.5
,2.5
), but not for any other values (e.g.1.2
,2.35
,0.075
)The text was updated successfully, but these errors were encountered: