Skip to content

Commit bc28b64

Browse files
committed
add callout for array spacing
1 parent bdec8c2 commit bc28b64

File tree

1 file changed

+38
-0
lines changed
  • docs/data/material/customization/spacing

1 file changed

+38
-0
lines changed

docs/data/material/customization/spacing/spacing.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,44 @@ const theme = createTheme({
4444
theme.spacing(2); // = '8px'
4545
```
4646

47+
:::warning
48+
Note that when spacing is defined as an array, it only works with positive integers that will be used as array indexes.<br />
49+
It doesn't support all possible signatures of the `theme.spacing()` helper, for example `theme.spacing(0.5)`, `theme.spacing(-1)`, or `theme.spacing(1, 'auto')`.
50+
51+
If you must use spacing array, consider using a function signature that can handle all possible signatures of the `theme.spacing()` helper:
52+
53+
<details>
54+
<summary>Spacing function example</summary>
55+
56+
```tsx
57+
const spacings = [0, 4, 8, 16, 32, 64];
58+
59+
const theme = createTheme({
60+
spacing: (factor: number | 'auto' = 0) => {
61+
if (factor === 'auto') {
62+
return 'auto';
63+
}
64+
const sign = factor >= 0 ? 1 : -1;
65+
const factorAbs = Math.min(Math.abs(factor), spacings.length - 1);
66+
if (Number.isInteger(factor)) {
67+
return spacings[factorAbs] * sign;
68+
}
69+
return interpolate(factorAbs, spacings) * sign;
70+
},
71+
});
72+
73+
const interpolate = (value: number, array: readonly number[]) => {
74+
const floor = Math.floor(value);
75+
const ceil = Math.ceil(value);
76+
const diff = value - floor;
77+
return array[floor] + (array[ceil] - array[floor]) * diff;
78+
};
79+
```
80+
81+
</details>
82+
83+
:::
84+
4785
## Multiple arity
4886

4987
The `theme.spacing()` helper accepts up to 4 arguments.

0 commit comments

Comments
 (0)