tokens-studio / sd-transforms

Custom transforms for Style-Dictionary, to work with Design Tokens that are exported from Tokens Studio
MIT License
175 stars 27 forks source link

feat: config option for math fraction digits #290

Closed Razinsky closed 3 weeks ago

Razinsky commented 3 weeks ago

Fixes #289

This PR adds a new platform config option called mathFractionDigits to control the rounding of values when resolving math.

The current hardcoded value found here is 3 and one could potentially need this value to be any number.

⚠️ I suggest making the default value 4 as it tends to make more sense when trying to convert px to em and rem, but this is debatable. We might prefer to avoid breaking changes and keep 3 as default.

The variable name mathFractionDigits was borrowed from the .toFixed() function whose first argument is fractionDigits to which I prepended math for better context in relationship with the TS transform resolveMath. This is also to be discussed.

Usage

/* Style Dictionary config */
{
  platforms: {
    css: {
      options: {
        mathFractionDigits: 3
      },
      ...
    }
    ...
  }
}
changeset-bot[bot] commented 3 weeks ago

🦋 Changeset detected

Latest commit: c8e5203477aeffc795e36b514441700906f1c0af

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | ---------------------------- | ----- | | @tokens-studio/sd-transforms | Minor |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

jorenbroekema commented 3 weeks ago

Great! I cleaned things up a bit:

Thanks for contributing!

Razinsky commented 3 weeks ago

Excellent 👌 Thanks @jorenbroekema ! It's great to see your additions, this will be helpful for my next contribution ;)

Quick question: what is your stance regarding the use of the options object? I was under the impression that this was the right way to go when it comes to adding custom "options" to our Style Dictionary config. I see that you pull the new variable out to be at the same level as the rest of the config.

jorenbroekema commented 3 weeks ago

Ah now that you mention it, it should probably be this:

transform: (token, platform) => checkAndEvaluateMath(token, platform.mathFractionDigits),

The current code also happens to work because platform options tend to be put on the config.options object, but it's not exactly its intention to use it like that, if readonly props were a thing in javascript then this would be one.

jorenbroekema commented 3 weeks ago

https://github.com/tokens-studio/sd-transforms/pull/295 fixing it here