heydon / design-tokens-cli Goto Github PK
View Code? Open in Web Editor NEWSet of design tokens and a custom properties parser for prototyping
Set of design tokens and a custom properties parser for prototyping
My input is a design.tokens.json file:
{ "tiny": { "$value": "8px" },
"small": { "$value": "16px" },
"medium": { "$value": "24px" },
"large": { "$value": "32px" },
"xlarge": { "$value": "40px" },
"error": { "$value": "red" },
"success": { "$value": "green" },
"warning": { "$value": "yellow" },
"radius": { "$value": "3px" }
}
The CSS output is:
:root {
--error: red;
--large: 32px;
--medium: 24px;
--radius: 3px;
--small: 16px;
--success: green;
--tiny: 8px;
--warning: yellow;
--xlarge: 40px;
}
The CSS variables are no longer in a logical order: colors are not shown together and the spacings are no longer sequential. This makes it harder to read through and make sense of the variables in the output file.
It would be nice if it was possible to control the prefixes that get added to the output variables. I am grouping my tokens primarily because I don't want to add a $type to every individual property so instead do it at the group level:
{ "colors": {
"$description": "Color palette",
"$type": "color",
"error": { "$value": "red" },
"success": { "$value": "green" },
"warning": { "$value": "yellow" }
},
"sizes": {
"$type": "dimension",
"radius": { "$value": "3px" },
"tiny": { "$value": "8px" },
"small": { "$value": "16px" },
"medium": { "$value": "24px" },
"large": { "$value": "32px" },
"xlarge": { "$value": "40px" }
}
}
However, I do not want the output variables to have any prefixes. I prefer to use a terse variable name in my code e.g. var(--small)
over var(--sizes-small)
. Most of the names are descriptive enough by themselves e.g. var(--red)
is obviously a color. I don't see much point in the extra verbiage.
This needs a parser that at least handles converting literal, possibly nested/grouped tokens into custom properties placed on :root
.
See https://www.techighness.com/post/javascript-find-key-path-in-deeply-nested-object-or-array/ for getting the key path.
Hi! I came across this repository today and I'm tinkering around with it and it's pretty awesome!
I've found one small issue so far:
If no globalPrefix is added to the config, tokens are generated with undefined-
ahead of them.
$value
stokens[converted reference]
b.c
whereas the token name is a.b.c
(more specific)Currently, references are resolved by finding and applying the true value.
{
"token-1": "#fff",
"token-2": "{ token.1 }"
}
...results in this Sass:
$token-1: #fff;
$token-2: #fff;
But it should be possible to just convert the reference:
$token-1: #fff;
$token-2: $token-1;
The difficulty comes in working out when the order needs changing and how. For CSS custom properties, this isn't an issue because of hoisting. But the following...
{
"token-1": "{ token.2 }"
"token-2": "#fff",
}
would break the Sass output (Error: undefined variable):
$token-1: $token-2;
$token-2: #fff;
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.