ui: CSS Theming hsl variables -> problem with Tailwind Intellisense VSCode extension
I was really looking forward to using the new themes and setting my new color variables
However,
The most important part (for me) is being able to fully take advantage of Tailwind CSS Intelisense plugin to use the colors globally in my project and have the colors being displayed in my inline classNames when I call bg- or text-
I usually preffer setting everything in tailwind.config.cjs and have it all in that file where I can change color hex values as I please
Maybe this isn’t just me, I am curious if other people went through something similar.
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 6
- Comments: 20 (2 by maintainers)
I figured out a neat solution. Just add a debug layer that you can comment in/out. Use the PostCSS VSCode extension.
Notice the layer name is
debug. It’s a copy-paste of the shad variables, but withhslinstead of just the hsl values. This triggers a color tile. Not perfect but made it slightly easier for me to update color variables. It would be amazing to have an online variable builder that translates color values to the HSL values.@rvanbaalen You can define your colors inline like this:
@dBianchii So the reason we do this is to have dark mode handled automatically for you.
<div class="bg-primary" />will use handle both light and dark modes reading values from CSS variables. With one class name.Came across this “old” topic, but I found a solution to get the color preview in VSCode, when using HSL color in
globals.csstogether withtailwind. Using the hsl command in the globals.css filebut removing this hsl command in the
tailwind.config.tsfileno the best solution but it works and save time when searching the relevant color for our website.
Hey, I have created a Pull Request to Color Highlight VSCode extension to include
WSL without functionoption and floats support: https://github.com/enyancc/vscode-ext-color-highlight/pull/200It already supports
RGB without function(disabled by default) so if you are using RGB like--primary: 200 200 200;it will work for you right now.Here is how it looks for me:
Hopefully, @enyancc will accept my PR soon and publish a new version of the extension.
In the meantime, here is a link to download the packaged VSIX file (the extension as a file): https://we.tl/t-VHTnVChsMR
To install it you can just go to “Extensions” sidebar, click the three dots menu icon (up-right corner of extensions), and select Install from VSIX… or with CLI:
code --install-extension myextension.vsixAfter installation, you have to enable
hslWithoutFunction/rgbWithoutFunctionoptions in settings. You can also set a list of languages / files this setting will work for (or exclude languages). And I prefer the “dot after” option instead of the full background color.I may also create a forked new Extension dedicated to Tailwind (with only the minimum options we need) for better performance (the current plugin runs on every VSCode window and parses every text for any type of color definition, but I didn’t notice any issues, all works fine with it enabled).
Please let me know if you would like that dedicated extension, and what are the most important/minimal features?
I’ve also tried to add this feature to the Color Info plugin, my RegEx worked fine but I couldn’t debug it. But a minimalistic square with color is enough for me. If somebody would like to help with Color Info I can create a WIP PR (I was probably very close to make it work, but didn’t have time to solve it).
BTW. It doesn’t support highlighting custom class names in your code, sorry… It would be much more complicated 😉 But it will add color anywhere you have
000.00 000% 000%or000, 000.00%, 000%in your code, including floats (or you can change extension settings to make it work only in CSS files, which will be better for performance).So… No more unnecessary comments in your CSS files 😄
AH… nvm sry, just realized that my tailwind config looks like this:
I changed this to remove the
hsl(...)and moved the hsl to theglobals.cssfile. However I’m curious why this was done this way in the first place? Why place thehsl(...)in tailwind config file instead of directly in the css var? Having it directly in the css var means vscode can actually show color previews which is so helpful during dev.Seeing the color directly in my html is very important. Most classnames are text. When you see a color in there, it makes it easier to read your code and understand where the colors are being applied. Your eyes don’t differenciate easily between different classes just as easily as it differentiates between text classes and colors together. So I guess that’s why I think this is important
It’s how tailwind recommends using CSS variables. If you use color function in CSS you won’t be able to do opacity modifiers like
bg-background/50https://tailwindcss.com/docs/customizing-colors#using-css-variables
is there anything similar for intellij?
FWIW VSCode extension
meouwu.css-var-color-decoratorcan decorate space-separated HSL color variables.Seems to work sufficiently well for me.
You can use the vscode extension colorInfo and just write in comment the hsl color in comment:
I found a way to get color previews, it works great (by cloning and modifying the globals.css file) but it’s a bit tricky to setup. So, here’s how I managed to do it.
Setup
CSS_ROOTconst if your globals.css file is placed somewhere else):./build-globals-edit-css.js
./fix-tailwind-vars.js
scripts:./package.json
./.vscode/settings.json
Usage
This command should have created a file called globals-edit.css in your
CSS_ROOT.hsl()function for every color var defined in globals.css. Don’t edit globals.css directly. It is now an autogenerated file, thanks to the Run on Save extension that executes fix-tailwind-vars.js script after every change to the globals-edit.css file.Enjoy, hopefully!
Weighing in here, personally I prefer the approach shadcn has used in custom variables without blowing out the amount of classes used for dark mode. Kind of feels like what tailwind has been missing to be honest.