Skip to content

chore(tools): split color tokens to separate layers - #42388

Merged
ahmaxed merged 1 commit into
freeCodeCamp:mainfrom
huyenltnguyen:chore/color-tokens
Jun 15, 2021
Merged

ahmaxed merged 1 commit into
freeCodeCamp:mainfrom
huyenltnguyen:chore/color-tokens

Conversation

@huyenltnguyen

Copy link
Copy Markdown
Member

Checklist:

  • I have read freeCodeCamp's contribution guidelines.
  • My pull request has a descriptive title (not a vague title like Update index.md)
  • My pull request targets the main branch of freeCodeCamp.
  • I have tested these changes either locally on my machine, or GitPod.

In #42186 (comment), we decided to split the color variables into 2 layers: one keeps them with their actual color names (red, blue, green,...); and the other refers to the color by their usage (background, foreground,...). Furthermore, @ahmadabdolsaheb and I decided to use numbers in the color names (similar to gray10, gray20) in place of lightGreen, darkGreen, etc. This will keep the naming consistent, as well as allowing us to add more shades to the colors in the future.

This PR partially addresses that by:

  • Creating a colors.css in ui-components to store the color values, which is the first layer I mentioned above
  • Moving all the colors in tailwind.config.js to colors.css. The config file will contain color tokens, which is the second layer
  • Creating a Storybook page to help visualize the color palettes

What this PR does not address are:

  • Mapping color name to colors tokens in tailwind.config.js. I might need some help with this part, as I haven't been working a lot with our design system. Though I think I could try to propose/add the tokens in as we move to the component development phase
  • Support for dark theme. This is a tricky part and I need to discuss a bit more before moving further. I'll comment in the issue thread

Screenshot

Screen Shot 2021-06-07 at 01 40 31

@gitpod-io

gitpod-io Bot commented Jun 6, 2021

Copy link
Copy Markdown

@github-actions github-actions Bot added the scope: tools/scripts Scripts for supporting dev work, generating config and build artifacts, etc. label Jun 6, 2021
@@ -0,0 +1,69 @@
import React from 'react';

@huyenltnguyen huyenltnguyen Jun 6, 2021 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This file is quite crazy. It is to create the color palettes as React components and then display on Storybook.

To render them, we first import the color list from colors.css, which comes in as an object:

{ 
  '--gray00': '#ffffff' 
  '--gray05': '#f5f6f7',
  ... 
}

In order to access the list a bit easier, I map the object to an array of object via the transformedColorList function. The list is now

[
  { label: 'gray00', value: '#ffffff' },
  { label: 'gray05', value: '#f5f6f7' },
  ... 
]

Each object in the list will be rendered as a palette item. The object's value is the background color of the item, while the label is the text, which is handled by the getPaletteItemStyle function. To ensure the text is readable, I do the parseInt trick to check where the color is on the scale, and set the text color to white if the color shade is dark and vice versa.

The Palette component is a single palette for each color, which renders its available shades into each row. The AllPalettes component is the combination of all the palettes we have.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great trick.
I wonder if we should be pulling these colors directly from tailwind once we have the variables setup?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The color names are fine for now but could introduce a naming convention down the road for the colors.css. Since the number next to the color is referred to the mix ratio of the colors in the gray white gradient instance, we might need to separate the colors that are pure from the colors that are mixed to be used for background and foreground.

Additionally a double digit for the alpha of the translucent colors.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I wonder if we should be pulling these colors directly from tailwind once we have the variables setup?

Are you referring to the tokens in the Tailwind config? Yeah I think it would make more sense to display those tokens instead on Storybook, as we kind of want to hide the colors.css layer and try to communicate using semantic names.

Though I guess we can show both ¯\_(ツ)_/¯ . I would think it's convenient for you still, to be able to check how a color would fit when developing the new palette. I initially didn't plan to display these on Storybook yet, but had to eventually, as I needed to give the variables in colors.css a new name and I couldn't tell which should be which without the ramp 😛

// ---------------------------------------------------------- //
const Palette = ({ colors }) => {
return (
<div className='inline-flex flex-col m-4 w-3/12'>

@huyenltnguyen huyenltnguyen Jun 6, 2021 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I took this as an opportunity to learn Tailwind, but now that I got a taste of it, I'm not sure if I like it to be honest 😅 I find it quite annoying to have to look up for a class name for each CSS attribute, and our markup might get messy very quickly if the styling is intense. Though that's just an opinion of someone who has been so used to writing CSS in the abstraction way.

A note here is that Tailwind uses REM as the default unit, so we will need to have some pixel mapping in order to use utility classes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree with you, but the good thing is that you set the classes once and you forget about them.
We might be able to get away it if we keep REM first approach when migrating the components.

Comment on lines +4 to +11
const story = {
title: 'Design System/Color',
component: AllPalettes
};

export const ColorSystem = () => <AllPalettes />;

export default story;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is the way we write our story page.

We need to export default an object that has title, which is the path of the component on the sidebar, and a component, which Storybook will use to extract the props and description and display on the page.

ColorSystem is a component that wraps around the component we want to render (AllPalettes). It needs to be named-exported so that Storybook can display it as a story under the Design System/Color section.

Comment on lines +16 to +17
--yellow10: #ffc300;
--yellow20: #ffbf00;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I keep these two as the values are different but they are almost the same visually, so we might want to revisit and drop one.

Comment on lines +31 to +32
--red20: #f8577c;
--red80: #f82153;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

These are is the love color used in light and dark themes respectively. Since this file is for the actual color names, I renamed them to red20 and red80 (not sure which number to give tbh, I just compared them to the other reds and assigned the number based on where they should be on the scale).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is fine for now. We will revisit colors and their "actual" naming down the line.

// Colors in variables.css
blueMid: '#198eee',
blueTranslucent: 'rgba(0, 46, 173, 0.3)',
blueTranslucentDark: 'rgba(153, 201, 255, 0.3)',

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is the same as #99c9ff (line 18), so I merged it to blue30 in the colors.css file.

darkYellow: '#4d3800',
// Colors in variables.css
blueMid: '#198eee',
blueTranslucent: 'rgba(0, 46, 173, 0.3)',

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is the same as #002ead (line 23), so I merged it to blue90 in the colors.css file.

blueMid: '#198eee',
blueTranslucent: 'rgba(0, 46, 173, 0.3)',
blueTranslucentDark: 'rgba(153, 201, 255, 0.3)',
editorBackgroundDark: '#2a2b40',

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Removed as this is basically gray80, and I don't believe the token belongs here. It is more appropriate as a /learn color token, I think.

editorBackgroundDark: '#2a2b40',
loveDark: '#f82153',
darkRed: '#850000',
editorBackground: '#fffffe',

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Removed as this is basically gray00, and same as above, it is a /learn color token rather than one in the component library.

red: '#ffadad',
yellowGold: '#ffbf00',
yellowLight: '#ffc300'
fccSecondary: 'var(--green90)'

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No idea how the token name should actually be. It was darkGreen before this change, but we don't use that name in this layer anymore.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

see the following regarding naming convention: #42186 (comment)

@huyenltnguyen
huyenltnguyen marked this pull request as ready for review June 6, 2021 20:18
@huyenltnguyen
huyenltnguyen requested a review from a team June 6, 2021 20:18
@ahmaxed
ahmaxed merged commit 4a240fc into freeCodeCamp:main Jun 15, 2021
@huyenltnguyen
huyenltnguyen deleted the chore/color-tokens branch June 17, 2021 05:15
@ahmaxed ahmaxed mentioned this pull request Jan 4, 2022
37 of 49 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: tools/scripts Scripts for supporting dev work, generating config and build artifacts, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants