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

Add rgb and hsl color helpers for CSS variables #7665

Merged
merged 5 commits into from Feb 25, 2022

Conversation

thecrypticace
Copy link
Contributor

@thecrypticace thecrypticace commented Feb 25, 2022

This adds two new helpers that can be used to simplify using colors that are CSS variables. The colors MUST use the space separated syntax, and MUST NOT use an alpha channel in their value as Tailwind CSS adds that itself. The user can customize the alpha by using the opacity modifier syntax for colors (e.g. the /50 in text-primary/50).

The rgb and hsl functions only expect the name of a CSS variable / custom property at present. We may revisit this in the future but this will give those who are using CSS variables a nicer, terse syntax for defining their config in a way that works with placeholders, borders, rings, etc…

// tailwind.config.js
module.exports = {
  theme: {
    colors: ({ rgb, hsl }) => ({
      primary: rgb('--color-primary'),
      secondary: hsl('--color-secondary'),
      // ...
    })
  }
}

Closes #4659
Fixes #3953

@bakura10
Copy link
Contributor

This is an awesome addition. Would it be possible to add a second parameter to the RGB specifying the default opacity? This way it would allow us to define variables like this:

'border': rgb('--text-color', 0.15),

Thanks!

@tylerrrkd
Copy link

tylerrrkd commented Apr 4, 2023

Hi, friend, I wanna use rgb function but it throw an error Error: rgb is not a function and my config is below. Is there any wrong?

...
      colors: ({ rgb }) => ({
        primary: rgb("var(--color-primary)"),
        secondary: rgb("var(--color-secondary)"),
      }),
...

ooops, my tailwindcss version is ^3.3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggestion: improve support for color css variables
3 participants