-
Notifications
You must be signed in to change notification settings - Fork 570
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
Themeable in formats #474
Comments
Do we have to set the |
Right now it is each property. Although it wouldn't be too much work to be able to set it per file or per platform, we would just need to update the formats that support it. Another option that would work right now would be to use a custom parser o.0 module.exports = {
parsers: [{
pattern: /\.json$/,
parse: ({ contents, filePath }) => {
const tokens = JSON.parse(contents);
traverseObj(tokens, (obj) => {
// add themeable to all tokens
if (obj.hasOwnProperty('value')) {
obj.themeable = true;
}
});
return tokens;
}
}],
//...
} |
I could indeed use a custom parser, thanks for providing the code! 👍 Obviously, like anyone else I guess, I try to keep my code as small and "default" as possible, so if it becomes a default option, it would be awesome. 😃 |
I think adding a |
I created a small transform to add a attribute to the platforms I needed. import { Named, AttributeTransform } from 'style-dictionary'
export const themeable: Named<AttributeTransform> = {
name: 'attribute/themeable',
type: 'attribute',
matcher: () => true,
transformer: () => {
return { themeable: true }
},
} |
Oh that is a good idea @jacoblapworth! One potential improvement, you can leave off the matcher function, if there is no matcher function on a transform then it will match all tokens. |
@jacoblapworth for me that didn't quite work. I think the attribute transform adds Am I missing something here? |
It looks like you're right @lennartbuit
I ended up writing a custom format using string literals: import StyleDictionary, { Format } from 'style-dictionary';
import { Formatter } from 'style-dictionary/types/Format';
import { Named } from 'style-dictionary/types/_helpers';
const { fileHeader } = StyleDictionary.formatHelpers;
const template: Formatter = ({ dictionary, file }) => {
const tokens = dictionary.allTokens
.map(token => {
const name = token.name;
const comment = token.comment ? `// ${token.comment}\n` : '';
return `${comment}$${name}: ${token.value} !default;`;
})
.join('\n');
return `${fileHeader({ file, commentStyle: 'long' })}
${tokens}
`;
}; |
Yeah I ended with a similar workaround, I did add const template = ({ dictionary, options, file }) => {
const { outputReferences } = options;
// Little cheat
const dictionaryWithGlobalThemeableKeys = {
...dictionary,
allTokens: dictionary.allTokens.map(token => ({
...token,
themeable: !!token.attributes.themeable,
})),
};
return `
${fileHeader({ file, commentStyle: 'short' })}
${formattedVariables({ format: 'sass', dictionary: dictionaryWithGlobalThemeableKeys, outputReferences })}
`.trim();
} |
Use the `formattedVariables` within the `scss/map-deep` formatter to add support for the `outputReferences` option. Closes: amzn#712 A side effect of this change is that `scss/map-deep` also supports the `themeable` token property. For backward compatibility changes have been made so tokens default to being themeable with `scss/map-deep`, unlike for `scss/variables` where tokens are not themeable by default. See amzn#474
Plus one 👍 I'd find a Aside: this PR adds support for the |
We recently merged in #359 that adds a
themeable
flag on tokens that will have the!default
flag in SCSS. We want to make this broadly applicable to other built-in formats for other platforms. If you have any ideas/thoughts please post them here.The text was updated successfully, but these errors were encountered: