-
-
Notifications
You must be signed in to change notification settings - Fork 37
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
Question on custom classes and CLI #58
Comments
Hello @sarodspirit, |
@sarodspirit you want to do something like this {
"scripts": {
"generate-css-types": "tailwindcss-classnames --classesFile css-types --typeName ClassNames --config path/to/tailwind.config.js -o src/@types/tailwind.ts"
}
} Your export type ClassNames = 'text-pink-600' | 'text-pink-700' | 'text-pink-800' | 'text-pink-900' | 'text-primary' | 'text-nermin' | 'text-ribbon-200'; |
Closing due to inactivity. Please feel free to reopen if you have more questions! |
I am new to typescript in general so please excuse, if that's an superficial question. So the basic workflow would be to generate types based on my tailwind.config.js and then:
What am I misunderstanding here? Or would I simply import the generated file additionally, giving access to the types? |
Hi, @Hahlh! import { classnames } from "path/to/generated/file"
const buttonStyling = (isAddedToCart: boolean, colorScheme: string) =>
classnames(
// These are always added
"text-white",
"focus:outline-none",
// One is selected based on the condition
isAddedToCart && colorScheme === "blue"
? "bg-gb-600"
: "bg-gb-400",
// Only added for certain condition
{ ["hover:bg-gb-600"]: isAddedToCart && colorScheme === "blue" }
) or even better: classnames(
// These are always added
"text-white",
"focus:outline-none",
// One is selected based on the condition
isAddedToCart && colorScheme === "blue"
? "bg-gb-600"
-- : "bg-gb-400",
++ : classnames("bg-gb-400", "hover:bg-gb-600"),
-- // Only added for certain condition
-- { ["hover:bg-gb-600"]: isAddedToCart && colorScheme === "blue" }
)
|
Hey @muhammadsammy ! Thank you for your answer. I would really like to avoid ternaries with single classNames as this would lead to the same conditional mess I have been in before. Why is passing multiple classNames considered problematic? This seems to work fine:
And I feel that it is easy to understand, easy to change and easy to write. Would you disagree? Using ternaries and single classNames would simply move what I am trying to avoid into a different place while not allowing me to use Headwind and Tailwind Intellisense (but adding of course the awesome typescript support). Am I missing something here or am I approaching this the wrong way? |
It would work but it won't give you the typescript support or validation, for me I tried your example and It gave me errors |
I see. That's a shame. I will have to try to find a hybrid solution or to drop the typescript support / validation for this for the moment. My main goal atm is finding a clean solution for all the complex conditional styling. After that I will work to put validation on top. But it should be possible to pass the in wrapped in an additional classnames(...) and gets validation etc back, correct? |
If I understood you correctly, yes. Also I think doing something like should be fine: classnames(
[buttonBaseStyle],
{
// Layout
[classnames('w-full', 'lg:max-w-max', 'py-3', 'lg:py-1')]: isMobileWidth,
....
},
....
) |
That's exactly what I settled on for the moment :) This should be a great middle ground for the moment. Tooltips that show in what css the classes result are not to be expected in general, correct? |
I am still somewhat confused about how to get my custom classes in. I will need to learn more about Typescript and come back to this. Thank you for the help @muhammadsammy, much appreciated! |
That is correct. the issue for tracking this is here: #10. But it's not supported yet in TS (microsoft/TypeScript#38106).
You are welcome. Thank you! |
I've been trying to get to include custom classes generated with cli but still can't get around it.
My setup:
tsconfig.json
{ ..., "typeRoots": [ "./src/@types", "./node_modules/@types" ] }
src/@types/css-types.d.ts
... | 'text-pink-600' | 'text-pink-700' | 'text-pink-800' | 'text-pink-900' | 'text-primary' | 'text-nermin' | 'text-ribbon-200';
button.tsx
The text was updated successfully, but these errors were encountered: