ui icon indicating copy to clipboard operation
ui copied to clipboard

Put full colors in CSS variables

Open multiwebinc opened this issue 2 years ago • 10 comments

I'm not sure if there is a reasoning behind the current way, but why do we have:

module.exports = {
  theme: {
    extend: {
      colors: {
        border: "hsl(var(--primary))",
      }
    }
  }
}

And then:

@layer base {
  :root {
    --primary: 222.2 47.4% 11.2%;
  }
}

This way VSCode doesn't give you a color preview in the CSS file (probably other editors as well).

Wouldn't it be better to do:

module.exports = {
  theme: {
    extend: {
      colors: {
        border: "var(--primary)",
      }
    }
  }
}

And then:

@layer base {
  :root {
    --primary: hsl(222.2 47.4% 11.2%);
  }
}

That way we get a preview of the color and a color picker in VSCode like this beside --primary:

image

multiwebinc avatar Apr 19 '23 23:04 multiwebinc

@multiwebinc This is how Tailwind does CSS variables. See https://tailwindcss.com/docs/customizing-colors#using-css-variables

See also my comment here: https://github.com/shadcn/ui/issues/199#issuecomment-1513596747

shadcn avatar Apr 20 '23 10:04 shadcn

Ok, I read the docs more in detail and I understand why they do it like that. It's because of the <alpha-value> that allows tailwindcss opacity modifiers to work. So maybe this library should add / <alpha-value> to the end of all colors in tailwind.config.js in case anyone wants to use opacity modifiers.

Let me know if you would be interested in a PR.

multiwebinc avatar Apr 20 '23 15:04 multiwebinc

I understand the design decision given the options, but currently this makes shadcn unusable for me. I need to see the colors in VsCode more than the time saved with darkmode handling. Is there a good workaround?

John-Dennehy avatar Jun 14 '23 01:06 John-Dennehy

Same issue in Webstorm. Having to deal with hsl values is a real pain 😕.

dperetti avatar Jul 13 '23 13:07 dperetti

Agree with @multiwebinc on adding the / <alpha-value> to all colors in tailwind.config.js. Does anything speak against this, @shadcn? Could also PR it in.

wottpal avatar Sep 09 '23 07:09 wottpal

Just stumbled onto this pain point and wanted to +1 the notion of working towards a more robust solution

coppinger avatar Sep 13 '23 00:09 coppinger

I also agree with building a more robust solution which provides better developer experience. Not being able to visualize colors while applying them is a very big pain point. Maybe build a vs code plugin to rectify this.

shahbazshueb avatar Sep 20 '23 13:09 shahbazshueb

any update on this ?

primemb avatar Oct 23 '23 13:10 primemb

Hi everyone, I faced the same problem today and I created a vscode extension for it. The extension adds color decorators to css variables. You can preview and edit the color with color decorators. It only works for hsl and hsla.

This is the first time I write a vscode extension, so there may be some bugs.

https://marketplace.visualstudio.com/items?itemName=meouwu.css-var-color-decorator

color decorator

meouwu-dev avatar Nov 11 '23 20:11 meouwu-dev

This is the first time I write a vscode extension, so there may be some bugs.

https://marketplace.visualstudio.com/items?itemName=meouwu.css-var-color-decorator

@meouwu-dev This plugin looks like exactly what I need! But when I install it, I see no difference in my CSS file. The plugin is enabled in VSCode. Is there some activation or configuration that I need to make? How can I check if I'm hitting a bug?

My index.css file looks like this:

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
  :root {
    --background: 0 0% 100%;
    --foreground: 20 14.3% 4.1%;

    --card: 0 0% 100%;
    --card-foreground: 20 14.3% 4.1%;

    --popover: 0 0% 100%;
    --popover-foreground: 20 14.3% 4.1%;

image

tauren avatar Jan 27 '24 10:01 tauren

Same here, nothing happens in my CSS file

benyamynbrkyc avatar Apr 07 '24 13:04 benyamynbrkyc

Same here, nothing happens in my CSS file

@benyamynbrkyc i add this in my .vscode/settings.json "cssVarColorDecorator.supportedLanguages": ["css", "scss", "tailwindcss", "html"], and it works

swingislee avatar Apr 20 '24 09:04 swingislee

Hi everyone, I faced the same problem today and I created a vscode extension for it. The extension adds color decorators to css variables. You can preview and edit the color with color decorators. It only works for hsl and hsla.

This is the first time I write a vscode extension, so there may be some bugs.

https://marketplace.visualstudio.com/items?itemName=meouwu.css-var-color-decorator

color decorator color decorator

I love you bro

adriangonzy avatar Apr 30 '24 13:04 adriangonzy

Same here, nothing happens in my CSS file

@benyamynbrkyc i add this in my .vscode/settings.json "cssVarColorDecorator.supportedLanguages": ["css", "scss", "tailwindcss", "html"], and it works

Tried it, still nothing :(

benyamynbrkyc avatar May 06 '24 21:05 benyamynbrkyc

Not working for me either, looks like a fantastic solution though.

mazshakibaii avatar Jun 01 '24 13:06 mazshakibaii

@benyamynbrkyc @tauren I found what was causing it not to work for me... The PostCSS Language Support extension (csstools.postcss) was blocking it, if you have that installed try disabling it.

mazshakibaii avatar Jun 01 '24 13:06 mazshakibaii

Any extension like for this Webstorm?

orlandodiaz avatar Jun 09 '24 04:06 orlandodiaz

This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you.

shadcn avatar Jul 09 '24 23:07 shadcn