DEV Community

Cover image for 🎊 You don't need SVG! Creating animated loaders for content with Tailwind CSS

🎊 You don't need SVG! Creating animated loaders for content with Tailwind CSS

Vic Shóstak on November 08, 2021

Introduction Hi, DEV friends! 😁 Today we'll talk about how to improve the UX of your project by several times by implementing just a co...
Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

...so what's the advantage over using SVG instead?

While this isn't really the best use-case for SVG, as the loader still ultimately adheres to the box-model, there's still nothing wrong with using SVG to build it. It certainly won't be slower than HTML+CSS, and might even be faster if your CSS loads from a remote file.


EDIT: Actually, SVG is the better option here: Not only will you need less of it, you will also need even less if you intend to repeat your loader several times thanks to <use> elements; so you can reduce your file-size by quite a bit.


For comparison: same thing with SVG, sans the animation because that's trivial anyway.

Collapse
 
koddr profile image
Vic Shóstak • Edited

Hi,

Yes, if you use inline SVG, it will be just as fast as my version.

But in this article I specifically mentioned a variant of SVG loaders, which I met in a huge number of projects on the Internet. Namely, a file like ./src/assets/images/loader.svg, which is not even on the CDN, and not optimized via SVGO... That's why such a provocative headline was born. :)

Compared to this approach, a bunch of Tailwind CSS + HTML will be much faster, since it's the markup and CSS have priority over loading, and only then everything else (as far as I remember the spec).

The main point I wanted to make is that you don't have to use SVG at all, if wonderful Tailwind provides all tools to implement loaders (of any complexity and appearance) through plain CSS. That's all.

EDIT: I'm not even talking about different solutions like this. Where they suggest installing additional dependencies in the project, for the sake of creating what Tailwind can do out of the box with just a few CSS classes. Or your inline SVG option.

Collapse
 
hoangvu12 profile image
Vu Nguyen

Uh I've been doing in this in most of my projects..

I just create a component that take children and add pulse animation and background color to it and it works.

I've tried SVG version, I need to adjust height, width also x and y (or maybe i did something wrong). It was really inconvenience.

With this, all I need to do is use width and height.

Nice article by the way!

Collapse
 
koddr profile image
Vic Shóstak

Hi,

Great example of a solution to the problem I bring up in this article! Thanks.

Collapse
 
divyeshkamalanaban profile image
Divyesh Kamalanaban • Edited

You can even create these graphics using normal CSS. Why Tailwind though? A little bit of JS can help you avoid Tailwind, maybe that will reduce load times?

Collapse
 
koddr profile image
Vic Shóstak

Hi,

Of course it is. But if you have a design system already using this CSS framework in the frontend team, why not use the built-in tools in Tailwind so you don't have to write the code yourself? ;)

Speeding up development is also a win.

Collapse
 
divyeshkamalanaban profile image
Divyesh Kamalanaban

Seems justified from your aspect. But making your own code gives you more flexibility. Either way, we win.

Thread Thread
 
koddr profile image
Vic Shóstak

As I answered in this comment, the purpose of the article was to show that you don't have to use loaders everywhere in SVG files or when plugging in JS dependencies to do the same via Tailwind (especially if it is ALREADY used in the project).

It's not like I'm saying anywhere in the article, "hey folks, install Tailwind to make the loaders animation". No! There was no such thing in the article. Even in the title and description I immediately pointed out that it was Tailwind's built-in features I was going to talk about.

Further, in the article I wanted to share my experience. I do a lot of reviews of different web projects (mostly on UX) and I often see a picture like this:

  • Files like loader-for-X.svg can be very numerous in the project for each type of content loading (I counted over 25 in one project, for example);
  • They are not optimized through SVGO;
  • The size of such files is many times greater than it could be;

Hence this article was born. I in no way meant to hurt the feelings of the true SVG developers (can I call them that?) who move this wonderful technology forward.

Yes, I understand that the flexibility to write your own solutions would be higher, but not many project managers are willing to wait for a frontend developer to implement such a thing on pure SVG or especially JavaScript (which you will then have to maintain). Time-to-market in 2k21 is a king.

Thread Thread
 
divyeshkamalanaban profile image
Divyesh Kamalanaban

I never knew there were these many hurdles in using SVGs. But, I might always use CSS (Normal CSS or Tailwind) than SVGs from now on. Anyways, thanks for the info!

Collapse
 
eshimischi profile image
eshimischi

Over-complexity. I have nothing against using CSS Frameworks, but we have SVG, period.

Collapse
 
koddr profile image
Vic Shóstak

Hi,

As I answered above in the comments (twice) here and here, the article was written from the perspective that Tailwind CSS is ALREADY in your project.

So why write something by hand from scratch if you ALREADY use a framework where this functionality is ALREADY implemented out of the box with just two classes?

That's all, there was no further purpose to this article.

OK. I will add a warning about this at the beginning of the article, so that readers immediately understand that no one is going to CANCEL their favorite technology (SVG) on a global scale. Only on a project scale, where Tailwind is ALREADY used.