Skip to content
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

Predefined Transform for Gradients in color/css #995

Closed
dbaitech opened this issue Jul 12, 2023 · 7 comments
Closed

Predefined Transform for Gradients in color/css #995

dbaitech opened this issue Jul 12, 2023 · 7 comments

Comments

@dbaitech
Copy link

I would like to see an added feature to the color/css predefined transform that allows the "value" key to have a string containing a css gradient.
For example, I am currently trying to add a linear gradient to a list of backgroundColors in the "color" section which looks like this:

"promo gradient": {
                "backgroundColor": {
                    "value": "linear-gradient(135deg, #52a1dd 20.0%, #a60054 88.00%)"
                }
 }

However, when Style Dictionary converts it into CSS tokens, it is not being transformed and simply results in the value #000000.

Since gradients are an important part of color styling, it would be great to see this feature added.

@dbaitech
Copy link
Author

A workaround I'm using is to create a registeredTransform that overrides the color/css transform by adding a feature that identifies when the string value contains "gradient" so that it just returns the string itself. Otherwise, it works the same as color/css.

// Add custom transform to override color/css predefined transform
  //  Works the same as color/css but also includes linear-gradient
  StyleDictionary.registerTransform({
    name: "color/css",
    type: "value",
    matcher: (token) => token.attributes.category === "color",
    transformer: (token) => {
      if (token.value.includes("gradient")) {
        return `${token.value}`;
      }

      // if color is not fully opaque, then write as RGBA to include opacity
      if (
        token.value.includes("transparent") ||
        /rgba\(.*[^1]\)/.test(token.value) // checks if opacity is not 1
      ) {
        return Color(token.value).toRgbString();
      }

      return Color(token.value).toHexString();
    },
  });

@Rykus0
Copy link
Contributor

Rykus0 commented May 1, 2024

I think it would be a better global solution to update the matcher instead of changing the transform, since we effectively want to skip transforming the gradient the same as a straight color.
Thoughts?

@jorenbroekema
Copy link
Collaborator

I agree, PRs welcome to update the matcher function: https://github.com/amzn/style-dictionary/blob/v4/lib/common/transforms.js#L40

When this returns false for colors in formats such as linear-gradient or similar, none of the color conversion transforms will be applied and you're free to use your own custom transform to transform it as you need (for CSS, probably no transform needed since it supports this format out of the box).

My suggestion, since all of the color transforms are using tinycolor2 would be to use: token.type === 'color' && Color(token.$value ?? token.value).isValid() check for the isColor matcher. Please add a test for this as well, probably here: https://github.com/amzn/style-dictionary/blob/v4/__tests__/transform/transformToken.test.js

@Rykus0
Copy link
Contributor

Rykus0 commented May 7, 2024

FYI - I'm working on this, just need to finish up tests.

@Rykus0
Copy link
Contributor

Rykus0 commented May 8, 2024

Having a problem with the precommit hook; prettier keeps listing my file as different. If I run the formatter it seems to be applying whitespace changes to a bunch of files. Even after I add the changes for my file, the precommit hook is flagging it as changed and not committing it. Could it be because I'm on Windows?

Figured it out. Had to apply the formatter changes without saving again in my editor.

@jorenbroekema
Copy link
Collaborator

Hey, you'll probably want to set your git global config core.autocrlf setting to either 'auto' or 'input', windows uses different characters for line endings.

@jorenbroekema
Copy link
Collaborator

Fix will be released in prerelease.28, I expect somewhere next week, thanks for raising the issue and contributing a fix folks, much appreciated!

dbanksdesign pushed a commit that referenced this issue Jun 28, 2024
For a full list of changes see the CHANGELOG.md or the [migration guide](https://styledictionary.com/version-4/migration/) from version 3.x.x to version 4.0.0

* BREAKING: make style-dictionary ESM-only and browser-compatible (#1014)
* Update docs (#1051)
* chore: upgrade glob to latest, set posix:true, verify & fix Windows (#1070)
* chore: fix glob usage in script (#1074)
* fix: glob dependency (#1075)
* chore: release (prerelease) (#1076)
* feat: improve and test ref utils error handling (#1077)
* feat: expose flattenTokens utility (#1079)
* fix: resolved filepath, async parser await (#1083)
* feat: allow deferring transitive transforms from the transformer (#1069)
* feat: support w3c special properties (#1081)
* feat: expose typeW3CDelegate util function (#1087)
* fix: small bug with w3c type delegate util (#1088)
* fix: only run postinstall scripts in non- prod-mode (#1091)
* fix: publish missing script file to npm (#1093)
* fix: dev scripts only run in sd cwd (#1095)
* fix: escape double quotes for ts outputStringLiterals (#1097)
* W3C Syntax refactor to allow value & type token names/groups (#1100)
* feat: support async for all hooks (#1106)
* fix: dtcg option not passed in some places (#1116)
* fix: outputReferenceFallbacks for css/variables formatter (#1113)
* fix: missing type imports (#1118)
* feat: allow transforms to be added on top of transformGroup (#1120)
* chore: fix ref docs mistakes, add DTCG option (#1122)
* feat: remove reliance on CTI, add token type (#1123)
* feat: redesign logging (#1125)
* fix: outputReferences for DTCG spec tokens (#1127)
* feat: allow passing Filesystem volumes to SD instance / extend method (#1128)
* chore: update docs volumes (#1130)
* fix: absolute paths in Node env (#1134)
* feat: add warnings disabled option, fix logging issues (#1136)
* fix(types): add missing type keyword for type export from index.d.ts (#1139)
* Revamp docs (#1141)
* feat: allow outputReferences function, publish filter util for it (#1145)
* feat: move formatHelpers to utils entrypoint (breaking) (#1147)
* chore: add migration docs (#1148)
* Some small docs fixes, and removing docsify (#1149)
* fix: change merging behavior for tokens to override (#1151)
* fix: asset url handling in CSS format (#1153)
* feat: add outputReferencesTransformed utility function (#1154)
* fix: outputReferencesTransformed util handling object-value tokens, unfiltered (#1155)
* chore: fix some docs links (#1159)
* fix: logging to be in sequence per platform (#1161)
* fix: get rid of lodash, refactor templates to ES6 (#1160)
* Format options improvements (#1165)
* feat: turn off generated timestamp by default in file headers (#1174)
* Expand composites (#1169)
* Align Hooks APIs (#1185)
* feat: move fileHeader global prop to hooks.fileHeaders (#1177)
* feat: move filter global prop to hooks.filters, rename matcher to filter (#1179)
* feat: move action global prop to hooks.actions (#1180)
* feat: move parsers global prop to hooks.parsers, apply explicitly (#1181)
* feat: move transformGroup global prop to hooks.transformGroups (#1182)
* feat: move transforms global prop to hooks.transforms, align api (#1183)
* feat: move formats global prop to hooks.formats, align api (#1184)
* fix: color filter now skips invalid colors (#995) (#1188)
* chore: docs mentions extend twice, duplicate (#1192)
* feat: add object-value CSS shorthand transforms (#1194)
* feat: pass options to fileheaders and filters, add unfilteredAllTokens (#1205)
* fix: pass options to transforms filters, live demo main page (#1208)
* chore: allow preselecting playground file (#1209)
* fix: typeDtcgDelegate  at any position, size/rem apply to unitless values (#1210)
* feat: add convert to DTCG spec utilities, file converter docs site (#1200)
* chore: version inject for sd-playground on build (#1216)
* docs(DTCG): Update DTCG docs with latest link and fix full name (#1215)
* chore: add mermaid integration to docs, create architecture (#1221)
* feat: add formatPlatform/formatAllPlatforms methods (#1222)
* fix: allow register hooks override existing (#1223)
* feat: pass dictionary options to preprocessors (#1224)
* feat: pass ref path in getReferences util output (#1226)
* chore: add missing changeset for SD format platform methods (#1227)
* timingFunction and fontFamily props, optional border props (#1229)
* fix: expand on platform level to adjust path prop (#1231)
* fix: fix CLI, update basic example, fix android templates DTCG (#1235)
* feat: allow not throwing on broken refs (#1240)
* feat: recursively expand nested composite tokens (#1244)


---------

Co-authored-by: Andreas Nordahl <andnorda@gmail.com>
Co-authored-by: tkgroot <tkuppensgroo@uos.de>
Co-authored-by: Lukas Oppermann <lukasoppermann@github.com>
Co-authored-by: Lukas Oppermann <oppermann.lukas@gmail.com>
Co-authored-by: Joren Broekema <joren@Jorens-MacBook-Pro.local>
Co-authored-by: Tom Pietrosanti <Rykus0@users.noreply.github.com>
Co-authored-by: Kaelig Deloumeau-Prigent <kaelig@users.noreply.github.com>
Co-authored-by: Daan Vos de Wael <daanvosdewael@users.noreply.github.com>
Co-authored-by: Abel van Beek <abel.van.beek@tromsfylke.no>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants