Best Practices When Using Components with Tailwind
Hi all, just wondering what the best practices are for passing additional class names as props for a component using Tailwind. Is this the recommended way, or is there a better alternative?
12 Replies
Concatenation is fine
checkout clsx, it will help you concatenate conditionally too
+100000 for cslx. It cleans up code so much.
clsx + cva >>>
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Awesome definitely checking these out, thanks guys
+1 for cva
just make sure you set up your linter to work with it, but there's an example in the docs if I remember correctly
There are always tools like clsx and others if you want to create a design system with "variants" etc.
But most of the time, and for your example I would definitely recommend writing it like this instead. with optional className prop
do you get proper linting with this solution?
yes
okay I tried it out, and you do indeed!
I thought maybe the intellisense / linter doesn't pick this up, if it's a prop, but it does
nice pattern, thanks!
(I guess it's regexp... so yeah it makes sense that it picks it up...)
I almost always use cslx inside of className.