Replies: 4 comments 2 replies
|
Ok, new idea: i could have a list of prefixed regexps and for each one,if it matches the override list, it gets gsubbed out of the default list. Probably too computationally expensive to use at render time 😕 |
|
Talked with my friend about this and he informs me I'm thinking of this all wrong. I'm trying to be too clever, and instead of merging lists of classes, I should be including fewer default classes... basically anything I may want to vary should be specified in each case. There's still something that bothers me about this... it's not DRY. If I end up repeating the same lists of default classes in a bunch of places, then if I want to make sweeping changes, i'll have to hunt them all down and change them in each place. I still think there must be a way to strike a balance here, but I'm not sure what it is. |
|
You can avoid repeating class lists by using Partials. And a more advances approach, to configure some properties and even have unit tests, is to use ViewComponents . One article I liked: https://evilmartians.com/chronicles/viewcomponent-in-the-wild-building-modern-rails-frontends. |
|
I think @sdhull's most recent comment is right: use https://github.com/gjtorikian/tailwind_merge for merging tailwind classes. @sdhull Are you happy with how TailwindMerge works? |
Uh oh!
There was an error while loading. Please reload this page.
Hello friends! Thanks for this lovely utility—it really helped me get up and running with tailwind css in my new Rails project very quickly & painlessly. I'm new to tailwind, so I apologize in advance for my blindspots. Anyway...
I'm frequently setting a string of default classes (button defaults, form input defaults, label defaults, h1 defaults, etc etc). But I want to be able to override various attributes of them on a case-by-case basis. Frankly this challenge first came to my attention when I was investigating how to get my forms looking good with tailwind + simple_form. I stumbled on this post, here's the relevant bit:
The article recommends supporting a
remove_classes:argument, which I'll admit works but IMO it makes the abstraction leaky. Conceptually it's certainly possible to write a utility that would take a list of default classes and override classes and produce a list that automatically removes classes from the default list that target the same css property.I took a stab at this with an icon helper:
It works fine for this use-case, however as I looked at doing this for my button classes, I started to realize that sometimes prefixes actually include the first dash 😭 so this naive approach won't suffice. I'm sure the code could be optimized too.
I'm wondering if such a thing already exists anywhere (surely I can't be the first person who's taken a stab at this), or if there's some other approach folks are taking?
All reactions