Тёмный

You're Using Tailwind CSS The Wrong Way! A look at reuse. 

Program With Erik
Подписаться 122 тыс.
Просмотров 5 тыс.
50% 1

Working with Tailwind CSS Utility Classes can be tricky. In this video I discuss a refactor I did, that wasn't right, and what I should have done better.
👉 Check out my last video on this Vue Mistake
• This Vue Mistake Was A...
👉Sign up for my mailing list and get neat stuff!
bit.ly/3Umk7sW
Check out Cloudinary - use my link
cld.media/programwitherik
👉 Need some help with a project, level up your skills, React, Next, Vue, or Nuxt? Check out my 1-on-1 mentoring!
mentors.to/erik
Links:
tailwindcss.com/docs/reusing-...
0:00 What is the Problem
01:03 Patterns first refactor
02:41 Additionall refactors?
04:03 What does Adam Wathan say?
05:20 So 4 tips from Adam
06:05 Split it into components
07:00 It's OK to repeat and HTML can be ugly

Опубликовано:

 

14 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 68   
@ProgramWithErik
@ProgramWithErik 5 месяцев назад
Need some help with a project, level up your skills, React, Next, Vue, or Nuxt? Check out my 1-on-1 mentoring! mentors.to/erik
@faahkoo
@faahkoo 5 месяцев назад
Splitting it into components it's the only viable way for my dev style. I really hate to look at busy HTML, the most compact it can be for me the better.
@ProgramWithErik
@ProgramWithErik 5 месяцев назад
I agree
@rajikkali2381
@rajikkali2381 5 месяцев назад
This. 100% Tailwind is DRY if you use proper component structuring
@ShantanuAryan67
@ShantanuAryan67 5 месяцев назад
I find myself not using tailwind very much when working with vue/svelte. The build in css solutions are nearly perfect, you can manage scoped/global styles from within the component. I personally recommend css modules + open props with vue. gives me clean html and i can also easily write complex selectors (tailwind's arbitrary varinats are not for me) whenever needed
@ProgramWithErik
@ProgramWithErik 5 месяцев назад
Thanks for sharing
@siyedyoussef3202
@siyedyoussef3202 5 месяцев назад
also with the css v-bind it's a magic 🪄 scss > tailwind
@rickyu1978
@rickyu1978 5 месяцев назад
was on the fence all this while, you just made our life easier.. we wont be adding tw to our stack and stick with wonderful vanilla css.
@m4our4
@m4our4 5 месяцев назад
using Mojo CSS you can do something like this and reduce lots of code repetition (without leaving the HTML): ... ... ...
@acesOfGreen
@acesOfGreen 5 месяцев назад
I do agree that when using tailwindcss, your html can become quite busy. Using plugins such as Prettier can help tyding it up to make it more readable but still, your html will still look busy. What I have been doing lately is to configure tailwind so I can make use my tailwindCSS classes such as
@juanmacias5922
@juanmacias5922 5 месяцев назад
Your first solution hurt my soul lol I always try to break up my code into components lol
@CStrolx
@CStrolx 4 месяца назад
So, when I first started looking at Tailwind, I thought. Yeah, that's interesting, that's a new way of doing things. Then I started getting into the thinking, hey, what separates this from inline styles? Classes were intended for us to get away from inline styles and all the repetition involved. I felt that I wanted to do something in my project that represented the semantic classes we are used to, just like you show in the video and saw that this was discouraged and I thought, "why?", it's easier to read and understand, it's not as complicated and it's "DRY", which is important to me. So, as I explored this more carefully, I did come to understand what the author of tailwind was actually saying. In my component framework, I found myself creating a css class that basically matched the name of the parts of my component, almost verbatim. This got me to realize that I wasn't actually writing dry classes at all. I was extracting from my html all the same stuff into a separate file, and in the end, is that actually making things any easier to read and understand, is that actually DRY, or just an over-abstraction? That's when I finally started to get that concept of "hey" if you're repeating yourself inside a component, then one of two things may be happening. You may be applying CSS to individual parts that could be applied at a higher level (like font, for instance), or, ... you could abstract those components into their own sub-components (now that actually is DRY). Now you don't need to write a separate piece of CSS outside of the template, and you can see, at a glance, what that part is supposed to look like. So then I thought about, "What about theming", and Tailwind has ways to handle CSS variables, so theming should be fine. If you need semantic colors and width's and things for your theme, you can abstract those into CSS variables and have those overridden at run time. Also, the way Tailwind has created the atomic classes gets around the performance issues inherent in inline styles. The output is pretty verbose, but with gzip and similar things all that repetition should not be affecting things too negatively. I would not have wanted to use something like Tailwind about 20 years ago before component frameworks and gzip were a thing. For static HTML, it's absolutely horrible. But when you are utilizing a component framework, or simply making custom Web Components, then this approach actually makes a lot of sense.
@nielsbom5558
@nielsbom5558 5 месяцев назад
"WET", Write Everything Twice, is not the opposite of DRY. WET is the practice of *waiting* to introduce an abstraction until there's a good enough usecase for abstraction.
@ProgramWithErik
@ProgramWithErik 5 месяцев назад
That is a good point. That plays into my decision on what I was thinking in this video.
@lbalcerzak
@lbalcerzak Месяц назад
Extract smaller snippets to separate components, for sure. Sometimes, however, you still end up with a component that has very very long "class" attribute. In such situations you can simply try to create multilines (which is possible even with all that prettier config), i.e.: ...
@tmbarral664
@tmbarral664 5 месяцев назад
@Erik: No, I disagree. your html should be beautiful. That means easily readable. And that's the point. And to have those very long lines are not pleasant to read. So, yes ,definitively you should go for the component way. I was surprised you started with CSS until the end when I understood this is where you wished to bring us from the beginning ;)
@DamirSecki
@DamirSecki 5 месяцев назад
completely agree... the way tailwind want to enforce utility classes to tightly cuple it to markup and make it totally unreadable is horrible... it is the "only" gripe I have with it! the Markup should be short, semantic, readable and mantainable
@mrrolandlawrence
@mrrolandlawrence 5 месяцев назад
indeed. shortcuts are great for style layouts. the ones that default for the page - S/M/L etc. then use components. if you are repeating something over and over again, its a great candidate.
@gveresUW
@gveresUW 5 месяцев назад
I will start off by saying that I have never used Tailwind. I have used Vuetify, which has a bunch of similar utility css classes just like tailwind. I have read a number of people who say tailwind and these utility classes are horrible and go against the design of CSS. In fact they would argue that what you started doing, creating specific classes for specific reused elements, is exactly what css was designed for. I also found the article that you quoted to be suspect. One of the benefits you mentioned for not creating the specific classes was that your CSS bundle would grow in size. Yes, that is true, but in the end, every time you reuse a specific class, your html bundle decreases in size. Look at how many characters you save every time you use the .btn class rather than that string of tailwind utilities. For me, working in Vue, I use the Vuetify utility classes sparingly. I usually use the margin and padding ones because it is so easy to use them to add the extra spacing the layout needs. But then for anything else, I use the or section of vue to produce the component level classes. This is perfect because it puts everything in one file right where I need it and the template html is nice and clean and small. When I am dealing with something that is app wide - like the style of a button, I would create a new component that wraps the button and applies the necessary style. Then reuse that button everywhere. When I decide that yellow buttons are no longer in vogue, I can go to the one place in the code, the app button component, and change the look of it very easily. One file changed and the entire app changes. Styling these reused components everywhere they are used is just asking for a maintenance nightmare.
@user-fu8pd4cf6v
@user-fu8pd4cf6v 5 месяцев назад
We are using Vuetify, so i decided to wrap every component into Base one. With this approach you could still pass props from the doc to change them, plus you could override them in one place if you need some default features. Even if you need custom design for your project, you could still go this way, replace UI library with CVA and you are good to go
@holerisen
@holerisen 5 месяцев назад
Or even better solution: use CSS modules and use BEM naming. Can‘t argue on that. Clean file, each class has one name that extends all tailwind classes it needs with @apply And then use purgeCSS to remove all unnecessary css.
@Stoney_Eagle
@Stoney_Eagle 5 месяцев назад
I use the apply directive on a few things but I always stop when I want to make another one and take the time to consider other options. When I know that I need more than 2 of the same elements on the page I just put a v-for in there right away. Yeah tailwind looks messy but it just works so damn well 😊
@ProgramWithErik
@ProgramWithErik 5 месяцев назад
That makes a lot of sense! I would do the same.
@krzysztofs3t532
@krzysztofs3t532 5 месяцев назад
All this code should be split into few separate components - a Button, Slider, Section, Image. If you think about it you can easily create a polymorphic Button component, with three or four different looks, for example main, secondary, back and default. Then this Index file will have 10 - 15 lines of actual code.
@humansaremortal3803
@humansaremortal3803 5 месяцев назад
For the devs out there "DRY" is okay, there are contexts where its okay to repeat yourself. CSS is one of them. Stop spinning in hamster circle.
@ProgramWithErik
@ProgramWithErik 5 месяцев назад
Exactly! That’s the point I was making. DRY is great, but in CSS it’s ok to repeat yourself and have multiple utility classes everywhere
@oleksiipylypchuk6746
@oleksiipylypchuk6746 3 месяца назад
What about PrimeVue inner classes? How would you recommend organize them the best way, to maintain the same look across the app. The best I can imagine is using a common css file.
@hixday93800
@hixday93800 4 месяца назад
Hey we use tailwind in our applcations where I work and we most often use the @apply scoped inside the component style when there is a ton of tailwind classes on a single element. Maybe my collegues did use them if there was too much repetition, but I haven't looked at their html that much.
@chris_ea
@chris_ea 5 месяцев назад
❤ is it any compiler directive that can optimise all code?
@robwatson826
@robwatson826 5 месяцев назад
Hey I'd never heard "Write Everything Twice" from anyone else but me, good use
@aplusphilic
@aplusphilic 5 месяцев назад
Extracting components might be a better option for this case and make use of your tools efficiently might be helpful. BTW your VSCode looks clean do you have you configure shared somewhere where I can get inspiration from
@aqibos
@aqibos 4 месяца назад
Refactor into React components, and reuse them that way Edit - Oh, I see you landed there in the end 👍
@SlavZute
@SlavZute 2 месяца назад
Here's a radical idea - presets Inspired by PrimeVue Tailwind If you think about it - TW classes are just strings, right? So, what if we put the most commonly used combination of these strings into their own variables and share those across components? Obviously, the argument around extra level of abstraction kicks in and keeping the structure of these variables sane comes just like it does when using CSS or SCSS naturally. But at least if you just can't stand the look of your HTML in code editor to the point of it making your eyes bleed and you wake up in cold sweat from the thought of having to update 100s of instances of various components because of new styles... this could be a way to condence those unruly class chains without incurring the costs of @apply 🤔 What do you think, Erik? @ProgramWithErik
@nikolaykolev5143
@nikolaykolev5143 5 месяцев назад
Splitting into components is the way to go if you work with a framework that supports them. But even there, sometimes you just cannot avoid repeating some stuff (for containers, boxes and general layout). Another thing that I find particularly nasty though is, like in your example, if you have an opacity slider, and you think to do a class interpolation say like opacity-{{ settings.opacity }} that, actually, sadly, won’t work! So you have to do probably a computed property with a swich on settings.opacity with all cases and then return the whole class name for each switch case..which get’s some typing! Sure you can extract those in composables and use everywhere, but still feels sad to have to do it!
@mehmetaltinsoy525
@mehmetaltinsoy525 5 месяцев назад
Thanks
@kingshanaman
@kingshanaman 4 месяца назад
There's a nice VSCode plug-in that hides all the class text
@RIT_Edition
@RIT_Edition 5 месяцев назад
Daisyui is perfect solution
@wolfphantom
@wolfphantom 5 месяцев назад
@apply is not the prescribed way within TailwindCSS. However, if you use it to define a "component" style it should be nested in the @layer component for proper scoping
@ProgramWithErik
@ProgramWithErik 5 месяцев назад
I went back and forth if I should show Layer and component style, but either way the point stands that a better way is to not use any of them for most use casws
@wolfphantom
@wolfphantom 5 месяцев назад
​@@ProgramWithErikI've occasionally used @apply within a SFC when I needed things that weren't available within Tailwind at the time.
@wwizer
@wwizer 5 месяцев назад
I still use @apply but I also would create child components. I don’t think using @apply is that big of a deal if the scope is limited and it makes sense.
@ProgramWithErik
@ProgramWithErik 5 месяцев назад
Yeah, maybe just for very small thinks like buttons or certain form elements.
@dukeselwood
@dukeselwood 3 месяца назад
Using the @apply is not recommended by Tailwind. I think a better option would be to split your code into components
@ikbo
@ikbo 5 месяцев назад
This is why stylex is a better solution
@nielsbom5558
@nielsbom5558 5 месяцев назад
1: DRY is OK in principle, but abstractions come at a cost. Which Adam points to in the Tailwind docs. So blindly "trying to make everything DRY" is not good advice imho. 2: Every choice (Tailwind, vanilla CSS, frontend framework or not) has pros and cons (benefits and costs). If you choose Tailwind: your HTML is "ugly" but the chances of splash damage when making style changes goes down a lot. If you choose vanilla CSS: it's simple to make far-reaching changes but it's hard to foresee all the effects of a small change (which is compounded in large apps). Using a frontend JS framework requires all people working on a project to learn and know that framework, but it makes certain things more predictable and testable. Using no frontend framework makes it easier to start and ship less code, at the cost of predictability and long term maintenance. 3: There are no perfect abstractions. If somebody tells you otherwise they're trying to sell you something.
@ProgramWithErik
@ProgramWithErik 5 месяцев назад
Well put.
@DamirSecki
@DamirSecki 5 месяцев назад
NOOOO, not true... if you chose tailwind does not mean your markup is by default ugly. You can use tailwind with @apply and your markup remains clean, your CSS organized in one or few teplmate/theme files. your first point... mothing making blindly is a good advice - doh
@PammyStevensonEquest
@PammyStevensonEquest 5 месяцев назад
Knowing your previous content, this video feels very click bait like. Appreciate for noobs you are making a valid point, but the crux of the video has nothing to do with Tailwind.
@ProgramWithErik
@ProgramWithErik 5 месяцев назад
I disagree. It’s everything to do with Tailwind
@PammyStevensonEquest
@PammyStevensonEquest 5 месяцев назад
@@ProgramWithErik ​I'm probably missing something I guess. Every css framework I've used requires some amount of class concatenation/layering and I would not say that is a tailwind only problem. You have stated in previous videos that we should be using components and your buttons and sliders are the perfect example of where that should be used. Therefore your removal of repetition and desire to be DRY is achieved by using components, regardless of those components still having class layering or custom ".btn" classes. How does using components in this way change the way your using tailwind (or any other css framework)? It doesn't, we're still using css layering which I'd say is by design
@b1-66er.
@b1-66er. 5 месяцев назад
Building reusable components with additional props.
@ProgramWithErik
@ProgramWithErik 5 месяцев назад
That's my go to. Or using a loop
@DamirSecki
@DamirSecki 5 месяцев назад
No way! I hate components with props that dictates styles! Components should dictate functionality... styles should be extrapolates away from the markup
@danko95bgd
@danko95bgd 5 месяцев назад
using tailwind css = wrong from the start
@ProgramWithErik
@ProgramWithErik 5 месяцев назад
You do you!
@misstress1928
@misstress1928 3 месяца назад
Tailwind CSS is overrated. Just use a proper CSS methodology and don't bother with so called "benefits" of bloating your markup. TW utility classes mess is hardly manageable, especially if you want to implement a custom design. For something instantly good-looking use classless CSS libraries.
@yatagun
@yatagun 5 месяцев назад
don't use Tailwind at all! This is a complete anti-pattern.
@ProgramWithErik
@ProgramWithErik 5 месяцев назад
Haha
@holerisen
@holerisen 5 месяцев назад
complete bs
@yatagun
@yatagun 5 месяцев назад
@@holerisen 💯
@kingshanaman
@kingshanaman 4 месяца назад
But when you try to use plain CSS you end up creating your own Tailwind, as the project gets bigger.
@Kingside88
@Kingside88 5 месяцев назад
The clickbait title just sucks. Having 10.000 Components can also be very ugly to be honest. And each component must have a unique name. I think it does not really solve the problem.
@ProgramWithErik
@ProgramWithErik 5 месяцев назад
This was the recommendation from Tailwind, I agree.
@ProgramWithErik
@ProgramWithErik 5 месяцев назад
I changed the title
@monikossa9964
@monikossa9964 5 месяцев назад
Omg... just delete tailwind. Pos
@znkdev
@znkdev 5 месяцев назад
nice video, thanks for it, but you spoke, spoke and said nothing, just what everyone thinks about it. haha anyways 👍
@ProgramWithErik
@ProgramWithErik 5 месяцев назад
If you don't have anything nice to say... maybe don't say it at all. I was going to delete this troll comment, but I'll leave it up
Далее
Is Tailwind CSS No Longer Needed? A Look Into UnoCSS!
12:21
Is Your Vue VSCode Extension Broken?
2:31
Просмотров 4,4 тыс.
StyleX: Meta's Solution To CSS At Scale
13:04
Просмотров 41 тыс.
FREE ONLINE BACHELOR'S & MASTERS DEGREES
7:39
Просмотров 9 тыс.
10 Tailwind Tricks You NEED To Know!
10:45
Просмотров 276 тыс.
Tailwind-Merge Is Incredibly Useful - And Here's Why!
12:58
This Vue Mistake Was A Real Problem
8:02
Просмотров 3,5 тыс.
Dependency Injection | Prime Reacts
28:34
Просмотров 305 тыс.
How the PROS Use Tailwind
9:55
Просмотров 45 тыс.
Avoid This Tailwind Mistake (Dynamic Classes)
6:18
Просмотров 20 тыс.