Skip to content

[docs] Add warning to array spacing section #46542

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/data/material/customization/spacing/spacing.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,44 @@
theme.spacing(2); // = '8px'
```

:::warning
Note that when spacing is defined as an array, it only works with positive integers that will be used as array indexes.<br />

Check warning on line 48 in docs/data/material/customization/spacing/spacing.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Google.Will] Avoid using 'will'. Raw Output: {"message": "[Google.Will] Avoid using 'will'.", "location": {"path": "docs/data/material/customization/spacing/spacing.md", "range": {"start": {"line": 48, "column": 90}}}, "severity": "WARNING"}
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')`.

If you must use spacing array, consider using a function signature that can handle all possible signatures of the `theme.spacing()` helper:

<details>
<summary>Spacing function example</summary>

```tsx
const spacings = [0, 4, 8, 16, 32, 64];

const theme = createTheme({
spacing: (factor: number | 'auto' = 0) => {
if (factor === 'auto') {
return 'auto';
}
const sign = factor >= 0 ? 1 : -1;
const factorAbs = Math.min(Math.abs(factor), spacings.length - 1);
if (Number.isInteger(factor)) {
return spacings[factorAbs] * sign;
}
return interpolate(factorAbs, spacings) * sign;
},
});

const interpolate = (value: number, array: readonly number[]) => {
const floor = Math.floor(value);
const ceil = Math.ceil(value);
const diff = value - floor;
return array[floor] + (array[ceil] - array[floor]) * diff;
};
```

</details>

:::

## Multiple arity

The `theme.spacing()` helper accepts up to 4 arguments.
Expand Down
Loading