Skip to content
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

[BD-46] feat: refactoring design tokens folders structure #2069

Merged

Conversation

PKulkoRaccoonGang
Copy link
Contributor

@PKulkoRaccoonGang PKulkoRaccoonGang commented Feb 21, 2023

Description

  • Ensure the global, alias, and component tokens defined in ./tokens/src/core/ are not specific to a particular theme variant (i.e., light or dark).
  • Ensure the global, alias, and component tokens defined in ./tokens/src/themes/light/ are only related to style properties relevant to a light theme variant (most likely color tokens).

Issue: #2017

Merge Checklist

  • If your update includes visual changes, have they been reviewed by a designer? Send them a link to the Netlify deploy preview, if applicable.
  • Does your change adhere to the documented style conventions?
  • Do any prop types have missing descriptions in the Props API tables in the documentation site (check deploy preview)?
  • Were your changes tested using all available themes (see theme switcher in the header of the deploy preview, under the "Settings" icon)?
  • Were your changes tested in the example app?
  • Is there adequate test coverage for your changes?
  • Consider whether this change needs to reviewed/QA'ed for accessibility (a11y). If so, please add wittjeff and adamstankiewicz as reviewers on this PR.

Post-merge Checklist

  • Verify your changes were released to NPM at the expected version.
  • If you'd like, share your contribution in #show-and-tell.
  • 🎉 🙌 Celebrate! Thanks for your contribution.

@openedx-webhooks openedx-webhooks added the blended PR is managed through 2U's blended developmnt program label Feb 21, 2023
@openedx-webhooks
Copy link

Thanks for the pull request, @PKulkoRaccoonGang!

When this pull request is ready, tag your edX technical lead.

@codecov
Copy link

codecov bot commented Feb 21, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.77%. Comparing base (5ea28ea) to head (6054ca0).
Report is 1122 commits behind head on alpha.

Additional details and impacted files
@@           Coverage Diff           @@
##            alpha    #2069   +/-   ##
=======================================
  Coverage   90.77%   90.77%           
=======================================
  Files         213      213           
  Lines        3501     3501           
  Branches      843      843           
=======================================
  Hits         3178     3178           
  Misses        315      315           
  Partials        8        8           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@adamstankiewicz adamstankiewicz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! I left a few comments; biggest feedback is that it looks like the new token files may be missing type attributes (and possibly other things?) that were in the original tokens.

@@ -14,7 +14,7 @@ const chroma = require('chroma-js');
* @return chroma-js color instance (one of dark or light variants)
*/
function colorYiq(color, light, dark, threshold) {
const defaultsFile = fs.readFileSync(path.resolve(__dirname, 'src', 'global', 'other.json'), 'utf8');
const defaultsFile = fs.readFileSync(path.resolve(__dirname, 'src', 'themes/light/global', 'other.json'), 'utf8');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[curious] Is everything in the other.json file specific to the light theme variant? Or are some of the properties, possibly like theme-interval, and yiq-contrasted-threshold.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, theme-interval and yiq-contrasted-threshold are not theme-specific, moved the to the core tokens

@@ -14,7 +14,7 @@ const chroma = require('chroma-js');
* @return chroma-js color instance (one of dark or light variants)
*/
function colorYiq(color, light, dark, threshold) {
const defaultsFile = fs.readFileSync(path.resolve(__dirname, 'src', 'global', 'other.json'), 'utf8');
const defaultsFile = fs.readFileSync(path.resolve(__dirname, 'src', 'themes/light/global', 'other.json'), 'utf8');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[question] If/when we have a dark theme variant, I wonder if there is a way we might be able to future proof this colorYiq helper function to support multiple theme variants later on (e.g., pulling from themes/dark/global/other.json to generate a dark.css output)?

The approach we're opting to take here is to have a light.css and dark.css output file, which would be generated by running the StyleDictionary.buildAllPlatforms() twice: once with the light theme variant tokens, and again with the dark theme variant tokens. If there's a way to pass in the current theme variant being used into the colorYiq that might be one approach to take here. Open to other ideas of course!

Copy link
Contributor

@viktorrusakov viktorrusakov Feb 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, I've modified our functions to check whether token belongs to any theme and take theme's colors instead (defaults to light)

{
"elevation": {
"annotation": {
"box-shadow": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the type attribute in these tokens might have gotten lost somehow (in the original, now-deleted Annotation.json, the type attributes are there. I wonder if it may be related to the issues we ran into trying to get alpha brought in sync with master that caused us to essentially rewrite the git history for the alpha branch... It may be worth a check to see if there might be anything else missing in addition to the type? It does look like this may be an issue in many of the files.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, great catch! we were missing a lot of them... I've added all the missed types, I don't think we're missing anything else

Comment on lines +29 to +34
// find whether token belongs to any theme based on its location
// split full path by '/', check if 'themes' directory is a part of the path, if it is - the next nested
// directory is the theme name, otherwise use 'light' theme
const pathParts = token.filePath.split('/');
const themePartIndex = pathParts.findIndex(item => item === 'themes');
const themeVariant = themePartIndex === -1 ? 'light' : pathParts[themePartIndex + 1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach seems reasonable. I'm not sure of an alternative way to pass a theme variant into a custom style-dictionary transform to avoid needing to parse file paths.

For example, if build-tokens.js did something like...

const getStyleDictionaryConfig = (themeVariant) => {
  const { buildDir, source: tokensSource } = program.opts();
  const source = tokensSource ? [tokensSource] : [];

  // Could we pass `themeVariant` arg into the config/transform somehow?
  const config = {
    include: [path.resolve(__dirname, 'src/**/*.json')],
    // ...
  };

  return config;
};

const THEME_VARIANTS = ['light'];
THEME_VARIANTS.forEach((themeVariant) => {
  const config = getStyleDictionaryConfig(themeVariant);
  StyleDictionary.extend(config).buildAllPlatforms();
});

But, I think what you have works just as well for now :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think we can do something like this, but it will be more complicated. Currently we register our color transform outside of build-tokens.js file and there is no way to let it know about the theme (style dictionary only passes a token to the transform func, so it does not know anything about style dictionary config).

We could try to dynamically change the transform function in build-tokens.js file, something like this

const THEME_VARIANTS = ['light'];
THEME_VARIANTS.forEach((themeVariant) => {
  const config = getStyleDictionaryConfig(themeVariant);

  StyleDictionary.registerTransform({
    name: 'color/sass-color-functions',
    transitive: true,
    type: 'value',
    matcher(token) {
      return token.attributes.category === 'color' || token.value?.toString().startsWith('#');
    },
    transformer: (token) => colorTransform(token, themeVariant) // pass themeVariant to transform function,
  });

  StyleDictionary.extend(config).buildAllPlatforms();
});

Hopefully re-registering transform doesn't break anything 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, nevermind... I just saw that you could define transforms directly in the config object without using registerTransform method, so we could just use that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yeah, that should theoretically also work! I defer to you if we want to roll with what you have now in this PR to get this PR merged and treat that refactor as a fast-follow or if we want to try to get that change into this PR before merge.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will tackle this as a fast follow in #2014

Comment on lines 10 to 14
* @param {String} [themeVariant] - theme variant name that will be used to find default contrast colors
* @param {String} [light] - light color variant, defaults to 'yiq-text-light'
* from ./src/themes/{themeVariant}/other.json
* @param {String} [dark] - dark color variant, defaults to 'yiq-text-dark'
* from ./src/themes/{themeVariant}/other.json
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: ./src/themes/{themeVariant}/global/other.json

Comment on lines 11 to 14
* @param {String} [light] - light color variant, defaults to 'yiq-text-light'
* from ./src/themes/{themeVariant}/other.json
* @param {String} [dark] - dark color variant, defaults to 'yiq-text-dark'
* from ./src/themes/{themeVariant}/other.json
Copy link
Member

@adamstankiewicz adamstankiewicz Feb 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[clarification] I don't think the theme variants are necessarily defaulting to yiq-text-light or yiq-text-dark depending on the theme variant itself. That choice depends on the color being modified (yiq-ified? 😅).

Should the JSDoc comment here just reference that the light theme pulls both yiq light/dark values from the other.json file in light or dark folder, respectively, without an explicit mention of yiq-text-light or yiq-text-dark here?

@adamstankiewicz adamstankiewicz merged commit a42ea75 into openedx:alpha Feb 24, 2023
@openedx-webhooks
Copy link

@PKulkoRaccoonGang 🎉 Your pull request was merged! Please take a moment to answer a two question survey so we can improve your experience in the future.

@edx-semantic-release
Copy link
Contributor

🎉 This PR is included in version 21.0.0-alpha.15 🎉

The release is available on:

Your semantic-release bot 📦🚀

monteri pushed a commit to raccoongang/paragon that referenced this pull request Aug 17, 2023
* feat: refactoring design tokens folders structure

* fix: add missing "type" attribute to tokens and consider token's theme in color-yiq function

* docs: update docstring for colorYiq function

---------

Co-authored-by: Viktor Rusakov <[email protected]>
PKulkoRaccoonGang added a commit to raccoongang/paragon that referenced this pull request Aug 18, 2023
* feat: refactoring design tokens folders structure

* fix: add missing "type" attribute to tokens and consider token's theme in color-yiq function

* docs: update docstring for colorYiq function

---------

Co-authored-by: Viktor Rusakov <[email protected]>
@edx-semantic-release
Copy link
Contributor

🎉 This PR is included in version 22.0.0-alpha.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@openedx-semantic-release-bot

🎉 This PR is included in version 23.0.0-alpha.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

PKulkoRaccoonGang added a commit that referenced this pull request Aug 1, 2024
* feat: refactoring design tokens folders structure

* fix: add missing "type" attribute to tokens and consider token's theme in color-yiq function

* docs: update docstring for colorYiq function

---------

Co-authored-by: Viktor Rusakov <[email protected]>
PKulkoRaccoonGang added a commit that referenced this pull request Aug 4, 2024
* feat: refactoring design tokens folders structure

* fix: add missing "type" attribute to tokens and consider token's theme in color-yiq function

* docs: update docstring for colorYiq function

---------

Co-authored-by: Viktor Rusakov <[email protected]>
PKulkoRaccoonGang added a commit that referenced this pull request Aug 4, 2024
* feat: refactoring design tokens folders structure

* fix: add missing "type" attribute to tokens and consider token's theme in color-yiq function

* docs: update docstring for colorYiq function

---------

Co-authored-by: Viktor Rusakov <[email protected]>
PKulkoRaccoonGang added a commit that referenced this pull request Aug 5, 2024
* feat: refactoring design tokens folders structure

* fix: add missing "type" attribute to tokens and consider token's theme in color-yiq function

* docs: update docstring for colorYiq function

---------

Co-authored-by: Viktor Rusakov <[email protected]>
@openedx-semantic-release-bot

🎉 This PR is included in version 23.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blended PR is managed through 2U's blended developmnt program released on @alpha released
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Refactor tokens file/folder structure to support additional theme variants
6 participants