-
Notifications
You must be signed in to change notification settings - Fork 566
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
output multi references in one value e.g. --c-hsl: hsl(var(--c-h), var(--c-s), 11%);
?
#565
Comments
🙃 It took me the whole day to solve this problem 🙃 a possible custom formatter is here (works but not elegant) : StyleDictionary.registerFormat({
name: 'css/variables',
formatter: function ({ dictionary, options }) {
return `${this.selectorName} {
${dictionary.allProperties
.map((prop) => {
let value = prop.value;
const ov = prop.original.value;
const propIsColorHsl =
prop.attributes.category === 'color' &&
ov.hasOwnProperty('h') &&
ov.hasOwnProperty('s') &&
ov.hasOwnProperty('l');
function hslPropUsesReference(p) {
return (
dictionary.getReference(p.original.value.h.toString()) ||
dictionary.getReference(p.original.value.s) ||
dictionary.getReference(p.original.value.l)
);
}
function outputHslProp(valStr, fallback) {
const fbOut = fallback !== undefined ? `, ${fallback}` : '';
// if (valStr.startsWith('{')) {
if (dictionary.getReference(valStr.toString())) {
return `var(--${dictionary.getReference(valStr).name}${fbOut})`;
}
return valStr;
}
if (propIsColorHsl && hslPropUsesReference(prop)) {
const hslStrBeforeHex = `hsl(${value.h},${value.s},${value.l})`;
value = hslToHex(hslStrBeforeHex);
}
if (options.outputReferences) {
if (propIsColorHsl) {
if (!hslPropUsesReference(prop)) {
return ` --${prop.name}: ${value};`;
}
const hOut = outputHslProp(prop.original.value.h, prop.value.h);
const sOut = outputHslProp(prop.original.value.s, prop.value.s);
const lOut = outputHslProp(prop.original.value.l, prop.value.l);
// e.g. --color-hsl: hsl(var(--color-btn-h), var(--color-btn-s), 11%);
return ` --${prop.name}: hsl(${hOut}, ${sOut}, ${lOut});`;
}
// if (prop.original.value is not hsl && prop.value is hsl)
if (dictionary.usesReference(prop.original.value)) {
const reference = dictionary.getReference(prop.original.value);
value = reference.name;
return ` --${prop.name}: var(--${value}, ${prop.value});`;
}
}
return ` --${prop.name}: ${prop.value};`;
})
.join('\n')}
}`;
},
}); any better idea? |
uptonking
changed the title
How to output multi references in one value, e.g. output
output multi references in one value e.g. Mar 10, 2021
-color-btn-hsl: hsl(var(--color-btn-h), var(--color-btn-s), 11%);
--c-hsl: hsl(var(--c-h), var(--c-s), 11%);
?
I think what you have makes sense. I started writing out an example, but it would be fairly similar to what you have. A few minor notes:
|
I have used this solution in my project, but a few more problems occurred.
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
outputReferences
and transitive transform to output a value referencing multiple css variables--color-btn-hsl: hsl(var(--color-btn-h), var(--color-btn-s), 11%);
tokens source:
outputAsItIs
option,color/css
transform will skip the h, s, l prop value.expected output:
The text was updated successfully, but these errors were encountered: