-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
CSS Theming hsl variables -> problem with Tailwind Intellisense VSCode extension #199
Comments
Is the problem that VSCode does not display color for HSL values? But the name of this color seems to speak for itself, why is it needed? |
@dBianchii I see. I believe this is a limitation of the intellisense plugin. I found tailwindlabs/tailwindcss-intellisense#409 To confirm I tried on play.tailwindcss.com and yeah this does not work either: https://play.tailwindcss.com/PGqCk0dlwn The way we implemented theming with CSS variables is the recommended way to do it in Tailwind. |
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. |
Slightly tagging along on this subject; is there a way, other than hardcoding all HSL colors manually in the config, to create themes? |
Oh, I get your point. Indeed, it would be easier to recognize the code at a glance if they were highlighted. |
Having this bug fixed in tailwind css extension intellisense would be a much more ideal solution. |
@rvanbaalen You can define your colors inline like this: module.exports = {
theme: {
extend: {
colors: {
primary: {
DEFAULT: '#05B6D4',
foreground: '#ffffff',
},
},
},
},
plugins: [],
}
@dBianchii So the reason we do this is to have dark mode handled automatically for you.
|
@shadcn I see, that makes a lot of sense. Thank you |
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. |
@MJtH I agree. I've been using the custom themes and it works so well... If anyone knows a workaround for this Tailwind intellisense, please let me know. I will be continuing to use the custom themes with hsl like this, but I really would also like having the colors show up in the code |
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 @layer debug {
:root {
--background: hsl(0 0% 100%);
--foreground: hsl(222.2 47.4% 11.2%);
--muted: hsl(227 14% 38%);
--muted-foreground: hsa(0 0% 91%);
--popover: hsl(0 0% 100%);
--popover-foreground: hsl(222.2 47.4% 11.2%);
--card: hsl(0 0% 100%);
--card-foreground: hsl(222.2 47.4% 11.2%);
--border: hsl(214.3 31.8% 91.4%);
--input: hsl(214.3 31.8% 91.4%);
--primary: hsl(241 100% 10%);
--primary-foreground: hsl(210 40% 98%);
--secondary: hsl(210 40% 96.1%);
--secondary-foreground: hsl(222.2 47.4% 11.2%);
--accent: hsl(210 40% 96.1%);
--accent-foreground: hsl(222.2 47.4% 11.2%);
--destructive: hsl(0 100% 50%);
--destructive-foreground: hsl(210 40% 98%);
--ring: hsl(215 20.2% 65.1%);
}
} |
I'm running into this too. What's even more frustrating is that shadcn (or maybe it's tailwind) is not respecting hsl(...) or hex syntax. The colors only work if I'm using the @jokull how are you getting the |
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 https://tailwindcss.com/docs/customizing-colors#using-css-variables |
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. Setup
./build-globals-edit-css.js const { readFileSync, writeFileSync } = require("fs");
const path = require("path");
const CSS_ROOT = "src/app";
const file = readFileSync(path.join(__dirname, CSS_ROOT, "globals.css"), {
encoding: "utf-8",
});
const fixed = file.replaceAll(
/(.*--.+:)[ ]*(.+[ ]+.+%[ ]+.+%);/g,
"$1 hsl($2);"
);
writeFileSync(path.join(__dirname, CSS_ROOT, "globals-edit.css"), fixed); ./fix-tailwind-vars.js const { readFileSync, writeFileSync } = require("fs");
const path = require("path");
const CSS_ROOT = "src/app";
const file = readFileSync(path.join(__dirname, CSS_ROOT, "globals-edit.css"), {
encoding: "utf-8",
});
const fixed = file.replaceAll(/(.*--.+:).*hsl\((.+)\);/g, "$1 $2;");
writeFileSync(path.join(__dirname, CSS_ROOT, "globals.css"), fixed);
./package.json "tw:fix": "node fix-tailwind-vars"
./.vscode/settings.json {
"emeraldwalk.runonsave": {
"commands": [
{
// If you have your CSS file somewhere else, you have to specify the
// correct path here.
"match": "./src/app/globals-edit.css",
"cmd": "npm run tw:fix"
}
]
}
} Usage
node build-globals-edit-css This command should have created a file called globals-edit.css in your
Enjoy, hopefully! |
Came across this "old" topic, but I found a solution to get the color preview in VSCode, when using HSL color in
but removing this hsl command in the
no 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 It already supports 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): 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: After installation, you have to enable 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 ;) So... No more unnecessary comments in your CSS files :D |
FWIW VSCode extension Seems to work sufficiently well for me. |
is there anything similar for intellij? |
This is what I was looking for, thank you very much! |
This works perfectly fine and seems like the solution to the original issue. Thanks for sharing! 🚀 |
That's it! Thanks for sharing |
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. |
how does this solve the original issue? I tried it out and it still does not highlight the color when putting in the class names instead I have to go to the index.css file to see the highlight |
Yes of course it doesn't @rashmod , just because this issue can only be resolved by tailwindcss intellisence if they can handle css variables colors in html's css classes. All above workarounds are for at least highlighting the in css. file |
Yes, I understand. Since we're using CSS variables for theming, it's unlikely that Tailwind CSS IntelliSense can detect which theme color to show as highlight. |
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-
ortext-
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 pleaseButton.tsx.-.kodix-t3-latest.-.Visual.Studio.Code.2023-04-18.14-38-06.mp4
Maybe this isn't just me, I am curious if other people went through something similar.
The text was updated successfully, but these errors were encountered: