Patterns to optimize away the verbosity of Tailwind classes? #11765
Unanswered
lancepollard
asked this question in
Help
Replies: 2 comments 2 replies
-
Personally, Tailwind makes it easier for me to see how to see the HTML is themed, since the "CSS" is right there with the HTML. If it gets a bit unwieldly, you could consider abstracting some elements to some components if you are using a templating system, but do try to avoid the pitfall of premature abstraction. |
Beta Was this translation helpful? Give feedback.
2 replies
-
This: <button className="rounded px-2.5 border-solid border-2 border-black bg-blue-500 text-white"> And this: const Button = styled.button({
padding: 10,
border: '2px solid black',
borderRadius: 4,
background: 'blue',
color: 'white'
}) Are basically the same amount of verbosity, but the latter includes newlines and commas. If you abstract the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am new to Tailwind (well, I am still on and using
styled-components
, SC), but as some have pointed out, refactoring from SC to Tailwind made their mobile PageSpeed Insights score go from 75 (SC) to 90 (Tailwind)! That's a pretty solid improvement. I am debating investing the effort in making the switch, but first I wanted to resolve a concern I and several of my colleagues have about Tailwind verbosity.Being a newcomer to Tailwind, a big fear is that the verbose class names will make it harder to glean the general structure of the DOM tree when looking at the code. And if you have dynamically generated class names, things become even less readable. It seems like the 2-10+ class names on every DOM element will make it noisy and hard to follow the code. Hoping you can clarify that for me, or show me the way that Tailwind deals with this issue.
Comparison
Tailwind:
StyledComponents:
Notice here that it is more verbose to define the "styled components", but using them creates a clean and readable semantic DOM.
So SC wins at being more readable in my option. But, like mentioned before, Tailwind has other pros like being more performant on mobile and such. So hard to weigh the differences.
Question
How can you achieve highly readable and sort of "semantic" style code in Tailwind that matches or exceeds the strengths of styled-components? What is the best way to build an abstraction around Tailwind classes so they don't sort of add a lot of extra details in the middle of your DOM, which seems like it would make it harder to get an overall picture of what is happening in the DOM.
Thank you for your help and insight!
Beta Was this translation helpful? Give feedback.
All reactions