Re:Goodbye CSS Modules, Hello TailwindCSS
by noisy_boy on Thursday November 04, 2021 @09:50PM
(#29114606)
So in this example:
What happens if I have 20 of these divs and need to modify py-3 to py-4? Can I create a "local" .css file to "group" these tailwind classes ala pseudocode below?
If so, then the div becomes <div class='divxyz-default'>. A thought came to mind that in that case I have gone back to CSS modules but then we still have some benefits of standardized classes e.g. instead of specifying how much roundness, I am using standard amount of roundness via "rounded-lg" and if I want to change that globally, I can edit in one place. I am probably missing something.
[ Parent
| on HN ]
Re:Goodbye CSS Modules, Hello TailwindCSS
by verisimilidude on Friday November 05, 2021 @02:12AM
(#29115995)
Yes, you can use Tailwind utility classes within your own CSS files. The Tailwind syntax for creating custom classes is @apply. Here's how they recommend approaching the problem.
https://tailwindcss.com/docs/extracting-components
I find @apply useful for styling HTML that I don't directly control, such as rendered Markdown. Otherwise, I do prefer Tailwind's suggestion to use templates/components instead of custom classes, wherever it makes sense.
Re:Goodbye CSS Modules, Hello TailwindCSS
by 01brett on Thursday November 04, 2021 @10:06PM
(#29114684)
Author here, I am using React. What I've done for "it shares some styles but not others" is create a
const sharedStyles = "..."
and in my component combine the shared with the unique attributes <CoolComponent className={`${sharedStyles} py-3`} />
or using a string combiner utility like classNames/clsx <CoolComponent className={clsx(sharedStyles, "py-3")} />
If you have a props to change on, you can do this <CoolComponent className={clsx(sharedStyles, someProp ? "py-3" : "py-4")} />
Re:Goodbye CSS Modules, Hello TailwindCSS
by tcfunk on Thursday November 04, 2021 @10:13PM
(#29114726)
I've been using tailwind in combination with styled components, so that I can easily capture repeated sets of classes. Something like:
const PostImage = styled(Img).attrs({
className: `
duration-100
transform
transition-transform
ease-in
hover:scale-110
`
})``