Skip to content

Commit

Permalink
feat: add support for !default in SCSS variables format
Browse files Browse the repository at this point in the history
For any token definition that includes the property
`themeable: true`, a `!default` will now be added in
the scss/variables format.

GitHub issue amzn#307
  • Loading branch information
jmmcduffie committed Nov 23, 2019
1 parent 42fedce commit f0e837e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
12 changes: 12 additions & 0 deletions __tests__/__output/json-nested.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"color": {
"base": {
"red": {
"primary": "#611D1C",
"secondary": {
"inverse": "#000000"
}
}
}
}
}
14 changes: 14 additions & 0 deletions __tests__/formats/scssVariables.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

var formats = require('../../lib/common/formats');
var scss = require('node-sass');
var _ = require('lodash');

var file = {
"destination": "__output/",
Expand Down Expand Up @@ -62,5 +63,18 @@ describe('formats', () => {
});
});

it('should optionally use !default', done => {
var themeableDictionary = _.cloneDeep(dictionary),
formattedScss = formatter(dictionary),
themeableScss = "";

expect(formattedScss).not.toMatch("!default");

themeableDictionary.allProperties[0].themeable = true;
themeableScss = formatter(themeableDictionary);

expect(themeableScss).toMatch("#EF5350 !default;");
return done();
});
});
});
16 changes: 13 additions & 3 deletions lib/common/formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ function fileHeader(options, commentStyle) {

function variablesWithPrefix(prefix, properties, commentStyle) {
return properties.map(function(prop) {
var to_ret_prop = prefix + prop.name + ': ' + (prop.attributes.category==='asset' ? '"'+prop.value+'"' : prop.value) + ';';
var to_ret_prop = prefix + prop.name + ': ';
to_ret_prop += (prop.attributes.category==='asset' ? '"'+prop.value+'"' : prop.value);

if (prop.themeable === true) {
to_ret_prop += ' !default';
}

to_ret_prop += ';';

if (prop.comment) {
if (commentStyle === 'short') {
Expand Down Expand Up @@ -175,14 +182,16 @@ module.exports = {
},

/**
* Creates a SCSS file with variable definitions based on the style dictionary
* Creates a SCSS file with variable definitions based on the style dictionary.
*
* Add `!default` to any variable by setting a `themeable: true` property in the token's definition.
*
* @memberof Formats
* @kind member
* @example
* ```scss
* $color-background-base: #f0f0f0;
* $color-background-alt: #eeeeee;
* $color-background-alt: #eeeeee !default;
* ```
*/
'scss/variables': function(dictionary) {
Expand Down Expand Up @@ -371,6 +380,7 @@ module.exports = {
dictionary.allProperties.map(function(prop) {
var to_ret_prop = 'export const ' + prop.name + ' = ' + JSON.stringify(prop.value) + ';';
if (prop.comment)

to_ret_prop = to_ret_prop.concat(' // ' + prop.comment);
return to_ret_prop;
}).join('\n');
Expand Down

0 comments on commit f0e837e

Please sign in to comment.