Skip to content

Add color tokens to tailwind config file #42186

Closed

Description

@huyenltnguyen

We are in the progress of building a component library, and TailwindCSS is what we use for component theming.

The config file has been created, and further configurations will be added in the future. For now, we need to add color tokens to the file, in the colors object.

Link to the config file: https://github.com/freeCodeCamp/freeCodeCamp/blob/main/tools/ui-components/tailwind.config.js#L5

Colors to be added:

Convention:

  • Token names are in camelCase
  • Tokens for dark theme colors are suffixed with the word dark. Tokens for light theme can stay as is. For example: primaryColor and primaryColorDark, secondaryColor and secondaryColorDark, etc.
  • Tokens are sorted alphabetically

Activity

  1. added
    status: discussing
    scope: tools/scripts
    on May 19, 2021
  2. huyenltnguyen commented on May 19, 2021

    @huyenltnguyen
    MemberAuthor

    @ahmadabdolsaheb I'm using the color palette in the style guide as the starting point, but do you think we need other colors in /client's variables.css as well?

  3. ahmaxed commented on May 20, 2021

    @ahmaxed
    Member

    Yes we should add variables.css in addition to the design style guide colors. The whole dark mode, layout, and accent colors might need another review, but that will come later.

  4. huyenltnguyen commented on May 20, 2021

    @huyenltnguyen
    MemberAuthor

    Thanks @ahmadabdolsaheb!

    I have updated the issue description to lay out the requirements. I might need to discuss with you on the token naming (I'm considering using foreground and background), but that is just a minor thing that came into my mind. We can leave it for later since the color palette will be reviewed.

  5. huyenltnguyen commented on May 20, 2021

    @huyenltnguyen
    MemberAuthor

    I'm going to open this issue up for first-time contributor. If you are interested in helping us with this issue, please add a comment here to let us know.

    Please also make sure you read our guidelines for contributing, we prioritize contributors following the instructions in our guides. Join us in our chat room or the forum if you need help contributing, our moderators will guide you through this.

    Sometimes we may get more than one pull requests. We typically accept the most quality contribution followed by the one that is made first.

    Happy contributing.

  6. added
    first timers only
    and removed
    status: discussing
    on May 20, 2021
  7. ChrisPrzR commented on May 20, 2021

    @ChrisPrzR
    Contributor

    I'd like to work on this.

  8. ahmaxed commented on May 21, 2021

    @ahmaxed
    Member

    @huyenltnguyen I know there are different ways to setup the night mode.

    How about adjusting global.css (or adding another .css file) in our tailwind setup to include all of our variables.
    Then we could set tailwind config colors to CSS variables similar to this, but while having our root colors intact.

    Do you see any pros or cons for this setup?
    We could build on @ChrisPrzR's pr if necessary.

  9. huyenltnguyen commented on May 21, 2021

    @huyenltnguyen
    MemberAuthor

    Yeah I was looking for a way to keep the colors in a variable CSS file instead of porting them to the config file as well. The article I read suggested the same approach as the one you shared.

    With that approach, we would need to maintain a file with CSS variables and map the variables to a corresponding color property in the tailwind config. That extra mapping step makes me hesitant to keep a CSS file, and I wasn't sure how complex the palette would eventually be, so I opted for a simpler approach.

    However, I guess we could consider having two layers for our colors (this is what I said I would discuss with you more):

    • One is for color names, which describes what the colors are. For example: lightGreen, darkGreen, gray05, gray90, etc.
    • One is for semantic names, which describes what the colors are used for. For example: primaryText, primaryTextDark, secondaryText, secondaryTextDark, etc.

    And to translate to code:

    The first layer is in a CSS file

    /* colors.css */
    :root {
      --gray00: #ffffff;
      --gray90: #0a0a23;
    }

    The second layer is in the config file, whose tokens will then be used in the styling of our components

    // tailwind.config.js
    theme: {
      colors: {      
        primaryText: 'var(--gray90)',
        primaryTextDark: 'var(--gray00)',
      }
    }
  10. ahmaxed commented on May 21, 2021

    @ahmaxed
    Member

    I think that is a simple approach. How would the component color change from primaryText to primaryTextDark?

    Do we need to add that logic per component based on the theme props it receives?

    Also, I think foreground and background would work since we use color fore more than just text, borders, br, ...

  11. huyenltnguyen commented on May 21, 2021

    @huyenltnguyen
    MemberAuthor

    To be honest, I haven't put much thoughts into the dark theme support. I have added an item for this in the main issue, but the traditional .dark {} way is what I have at the moment. However, I'm afraid that it might not work well with the CSS purging that Tailwind offers.

    Happy to hear you thoughts on this.

  12. huyenltnguyen commented on May 21, 2021

    @huyenltnguyen
    MemberAuthor

    I skimmed the Tailwind doc yesterday and found that it provides a darkMode option in the config. I think I would try out and see if I can use the tokens I proposed with it.

    Here is the doc: https://tailwindcss.com/docs/dark-mode#toggling-dark-mode-manually

  13. removed
    first timers only
    on May 22, 2021
  14. 5 remaining items

  15. nicorr-05 commented on Jun 11, 2021

    @nicorr-05
    Contributor

    I already finished the repo, here is the link:

    https://github.com/Nrestrepo05/darkmode

    I think we can still use colors vars and a class on the body tag as we have already been doing. As you can see in my repo I tried to keep doing what is already implemented and it works perfectly.

    Hope to know what you think!

  16. huyenltnguyen commented on Jun 12, 2021

    @huyenltnguyen
    MemberAuthor

    @Nirajn2311 Thanks for the link! I'll take a look. It's just that I have not written code with atomic CSS for a long time, and I never knew I would need to relearn to get back to this (as in I don't know what the right coding pattern should be now) 😅


    @Nrestrepo05 First of all, welcome to freeCodeCamp and big thank for the demo repo! I am also in favor of following the current implementation. I personally find it easier to maintain that way (though that could be because I'm so used to this implementation approach); but it will help reduce a lot of configurations and changes we need to make.

    The issue's status is "discussing" currently as we have not finalized path to proceed. I'm more than happy to pass it to you once we have things ironed out, so please be patient with us 😄

    In the meantime, please check out other issues with "first timers Only" or "help wanted" label, and come say hi in our contributors chat room.

  17. huyenltnguyen commented on Jun 12, 2021

    @huyenltnguyen
    MemberAuthor

    @ahmadabdolsaheb hope you could weigh in on this. To sum up the entire discussion, there are two approaches we can take regarding dark theme support:

    • The currently approach in /learn in which we use light theme styles as the default, then override them with the .night selector for dark mode.
      • Pros:
        • Consistent with the implementation of /learn, thus easier to get familiar
        • Less configurations/changes needed
      • Cons:
        • This can be considered anti-pattern as Tailwind is a utility-first library
    • The Tailwind approach in which we split the styles smaller into atomic CSS and assign their class names to our components (also see Add color tokens to tailwind config file #42186 (comment))
      • Screen Shot 2021-06-13 at 01 24 13
      • Pros:
        • Get the benefits of the library by following its pattern (I'm not sure 100% what the benefits are except for the CSS purging)
      • Cons:
        • The introduction of the dark class as Tailwind uses it as the theme identifier
        • The HTML may be bloated with all sort of class names. For example: bg-colorA, focus:bg-colorB, hover:bg-colorC, dark:bg-colorD, dark:focus:bg-colorE,... )

    I think approach (1) is more viable. This is not saying that we shouldn't use Tailwind classes at all, as I reckon it will be very useful and convenient when it comes to handling layout. But for dark theme styling specifically, I think we are better off with our current way of writing CSS.

  18. ahmaxed commented on Jun 13, 2021

    @ahmaxed
    Member

    @huyenltnguyen, thank you for the extensive research.

    A little bit of history: we use to set the "night" css class in the html and style elements by adding .night to the night mode selectors. We ended up with a lot of styles to maintain. Then, we moved on to contrast based theming and simplified our sheets a bit by using css variables.

    Although approach 1 is not tailwind's current official method to implement dark mode, it is well documented in the community ex. https://jeffjadulco.com/blog/dark-mode-react-tailwinds

    I think we could use the current convention and keep things dry and straightforward. We could add class based styles later on if we wanted more control.

  19. nicorr-05 commented on Jun 14, 2021

    @nicorr-05
    Contributor

    sorry @ahmadabdolsaheb I didn't understand so well. Do you want to keep doing things like they are already (css variables) or you want to use the tailwind convention?

  20. ahmaxed commented on Jun 14, 2021

    @ahmaxed
    Member

    CSS variables.

  21. nicorr-05 commented on Jun 14, 2021

    @nicorr-05
    Contributor

    Thank you @ahmadabdolsaheb, do you think there something that I can do on this issue?

  22. ahmaxed commented on Jun 15, 2021

    @ahmaxed
    Member

    @Nrestrepo05, thanks for your interest, but I think @huyenltnguyen has got this one.

  23. ahmaxed commented on Jun 15, 2021

    @ahmaxed
    Member

    Since our colors have evolved since we published the design style guide, there are a couple of notes to take into account when approaching the colors config, naming, etc. in developing the component library:

    • there are to main types of colors: the colors that switch during the theme change (background, foreground) and colors that do not (cta yellow).
    • colors that change have less saturation, and are more appropriate for background and foreground.
    • all background and foreground colors could be produced using scss mix or css color-mix functions

    // colors that change with theme change
    Here is a sample of how we could name our colors moving forward (just a suggestion):
    gray-foreground-primary
    gray-background-primary
    red
    red-foreground-primary
    red-background-primary
    blue
    capri
    gold

    Note: The colors that don't change should maintain a good contrast regardless of the theme.
    Note: We should be able to programmatically derive all colors using a couple of scsss functions, but I have not looked into it yet. ( might be an overkill )

  24. huyenltnguyen commented on Jun 19, 2021

    @huyenltnguyen
    MemberAuthor

    Do you see gray-foreground-primary, gray-background-primary,... as names for color variables (those in colors.css), or for the semantic colors (those in the config file)?

    My take on this is that, semantic colors won't have anything that refers to the actual color they use. An example can be the Alert component. Bootstrap and Material UI call the variants success, info, warning, ... instead of green, blue, yellow.

    So I guess gray-foreground-primary would be a color in the variable file, and it is mapped to, say, default-foreground-primary in the config file. Then red-foreground-primary would be something like danger-foreground-primary, etc.

    With the colors are already added to colors.css, our next step is to map them to semantic tokens in the config file. Since the naming of colors in both layers will need to be adjusted again once the design style guide gets new updates, are you okay with me coming up with some temporary tokens for now?

    Though I think I should do it incrementally as I build the components, rather than assuming what each color means right now and add their tokens in bulk.

  25. ahmaxed commented on Jun 23, 2021

    @ahmaxed
    Member

    Yes, your plan makes more sense to me. Please feel free to proceed with mapping the color names to the config file.

  26. self-assigned this
    on Sep 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

scope: tools/scriptsstatus: discussing

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions