Skip to content

Custom spacings not working #64

@MUppenkamp

Description

@MUppenkamp

Hello to you 🙂

I am happily using your plugin in my private and work projects. At work, we've been using it for almost 2 years now and it works perfectly. But now with the update to v4 an error occurred.
In our project we have various customized spacings. For example --spacing-10l or --spacing-type-area which each holds a calc value. The error that occurs is the following:
Image

I have cloned your project and tracked down where this happens. When we take a look at the inlineSize.js file (but also any other case doesn't work as well) we'll see this for-loop. When we take --spacing-type-area as our example the theme("width") returns 'type-area': 'calc(var(--spacing) * 35)'. In the if-clauses this will go into the else case creating the error.

for (const property in values) {
    if (keywords.includes(property)) {
      calculatedValues[property] = values[property];
    } else if (property === "px") {
      calculatedValues[property] = "1px";
    } else if (property.includes("/")) {
      calculatedValues[property] = `calc(${property} * 100%)`;
    } else {
      calculatedValues[property] = `calc(var(--spacing) * ${property})`;
    }
  }

I build a fix with a colleague of mine and this is our suggestion:

  for (const property in values) {
    if (keywords.includes(property) || (!/^\d+(?:\.\d+)?$/.test(property) && values[property])) {
      calculatedValues[property] = values[property];
    } else if (property === "px") {
      calculatedValues[property] = "1px";
    } else if (property.includes("/")) {
      calculatedValues[property] = `calc(${property} * 100%)`;
    } else {
      calculatedValues[property] = `calc(var(--spacing) * ${property})`;
    }
  }

The regular expression /^\d+(?:\.\d+)?$/ matches a positive whole number or decimal number. This is the normal case for the spacings coming from tailwind. In our case we need to use the value that's defined for the spacing and not the name itself.

Your tests also keep working and I tested it with the normal tailwind setup without any configurations and everything works perfectly!

This change would be added in these files:
Image

Would you be so kind to have a look at this? I couldn't create a pull request but I'd be very happy to do so if you approve 😊

Best regards,
Mona

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions