-
Notifications
You must be signed in to change notification settings - Fork 562
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
Feature/css var deep #428
Feature/css var deep #428
Conversation
I just wanted to check in on this one. I know it now shows a conflict on the test snap file, but I can fix that easily. Mainly, I want to see if this formatter is going in the direction you'd like. This was approved to have me start via issue #398. |
@tonyjwalt thanks for starting this. I'm brand new here, but tried building your
{
"vds": {
"line-height": {
"md": {
"value": "1.50"
},
"body": {
"value": "{vds.line-height.md.value}"
}
}
}
} produces: :root {
--vds-line-height-md: 1.50;
--vds-line-height-body: 1.50;
}
/**
* Do not edit directly
* Generated on Thu, 06 Aug 2020 18:40:29 GMT
*/ |
@tonyjwalt I've hacked together my local version that fixes some syntax (soft tab It even works with nested variables! Meaning this JSON: {
"vds": {
"font": {
"scale": {
"base": {
"value": "1.6rem",
"comment": "assumes font-size 62.5% on html"
},
"multiplier": {
"value": "1.25",
"comment": "Major Third (4:5) modular scale https://www.modularscale.com/?16&px&1.25"
}
},
"size": {
"md": {
"value": "{vds.font.scale.base.value}",
"comment": "16/24px !!! consider --vds-font-* to set all font properties, not just font-size"
},
"sm": {
"value": "calc({vds.font.size.md.value} / {vds.font.scale.multiplier.value})",
"comment": "13/18px !!! consider --vds-font-* to set all font properties, not just font-size"
}
}
}
}
} Produces this CSS: :root {
--vds-font-scale-base: 1.6rem;
--vds-font-scale-multiplier: 1.25;
--vds-font-size-md: var(--vds-font-scale-base);
--vds-font-size-sm: calc(var(--vds-font-size-md) / var(--vds-font-scale-multiplier));
} I'm really missing the file header and comment support though… @dbanksdesign any thoughts on how to reuse these two helpers in a custom format? |
I'm thinking that perhaps what we really need here isn't a custom format (e.g. css/variables-deep) but a custom transform to be piggy-backed on an existing format (e.g. css/variables)… This PR loses some of the nice-to-haves of the CSS format and transform group, while duplicating other logic like kebab-case. |
This code was really just posted to start the process of comment. If the idea and direction felt right I intended to finish building it out here (which would include the header and comments). That said, I do have a much more robust version fleshed out in my local project. I chose a custom format because i pipe SCSS-flat, CSS-flat, CSS-deep, SCSS-deep, and versions that split out dark tokens through this. I like having all of those formats able to run through the same logic and checks (like applying quotes to strings when necessary). I'm happy to throw a few more hours into this over the next week to get it updated and cleaned up further if that makes sense to people. |
@tonyjwalt I haven't had enough time to fully understand or articulate my needs. But for posterity, here are my tweaks to your I'm currently playing with a custom transform. |
Ok, I've read through your comments and looked at the code as it is on my machine right now. I'm style-dictionary as an npm link against one of my projects to verify everything is working as expected. A few thoughts. I chose a formatter because I wanted a CSS variables format that maintains variable hierarchy. That means my desire is specific to a given format and determines I use a custom format. If you just want to maintain aliases, I'd recommend a transform at the end of your transform group that just applies the original value of a prop back to the value for the prop. I think the CSS formatter (and SCSS one) should be looked at and possibly combined. They should go from a template to something a little smarter. A perfect example of why is that some strings need quotes, and others don't. This formatter file is a stripped down version of the formatter I have in my project. I also like being able to use the formater to generate 2 tokens like Here are a few thoughts/edits based on the file you linked me to. Line number on the left refers to the line in code at the link you sent: |
@tonyjwalt thanks for taking a look! 67-69. Totally open to stricter regex for tightly scoped changes. I simplified it at some point to get it working and forgot to try your stricter version at the end. I think for lines 93, 96, and 99, I wasn't asking "why this code" as much as explaining our code via comments :) I'm hoping to contribute a PR with a small CSS variable transform, because that's all I need to solve my immediate problem. Maybe that will help you with your custom format? |
Got it. Thanks for going through the formatter and for your comments and suggestions! |
My pleasure! You helped me learn about formatters and transformers with a
practical, thorough example.
|
I think it would be great as well if we could choose the selector ( |
Circling back to this PR... I believe we have solved this issue with the
Here is an example style dictionary setup that uses outputReferences too: https://github.com/amzn/style-dictionary/tree/3.0/examples/advanced/variables-in-outputs @tonyjwalt if this solves your issue we can close this PR. If there are other things you would like to see let me know and we can try to build those in. Thanks! |
@dbanksdesign really nice! One thing I found though: It looks like it can't reference "nested" or "concatened" values (however you want to call it). E.g.: global:
static:
breakpoint:
xs:
value: "304px"
sm:
value: "768px"
md:
value: "calc({global.dimension.static.breakpoint.xs} / {global.dimension.static.breakpoint.sm})" with :root {
--global-dimension-static-breakpoint-sm: 768px;
--global-dimension-static-breakpoint-xs: 304px;
--global-static-breakpoint-md: var(--global-dimension-static-breakpoint-sm);
} but should be: :root {
--global-dimension-static-breakpoint-sm: 768px;
--global-dimension-static-breakpoint-xs: 304px;
--global-static-breakpoint-md: calc(var(--global-dimension-static-breakpoint-xs) / var(--global-dimension-static-breakpoint-sm));
} |
@mrtnbroder ah that makes sense. Let me see if I can try to build that in |
The |
@mrtnbroder this now works (#590) in the latest release candidate version of style dictionary! |
Issue #, if available: #398
Description of changes:
I've added the basics of a CSS variables deep formatter for conversation.
This one also currently supports CSS variables flat, split out dark tokens, and string data-types (CSS has some stings that need quotes, and others that can't have them). I use the split dark tokens in tandem with a custom transform in my current project.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.