Gone are the days when we depended on styling libraries to provide fast access to icons and go through untold suffering due to payload size, performance issues etc.. With the advent of SVG, we can use icons from a variety of sources. We will see how to do just that.
SVG images have the advantage of -
- Being vector based, so it can be scaled to any size
- “Is just text”
- Can be styled
- Small in size!
Using SVG Icons: Why do that?
You see in the old west, we used to have icons like this -
|
|
With libraries like Vuetify, the above statement looks like this -
|
|
The code is easy to use, but you have to depend on the library to provide you the icon. You are also dependent on the library for applying changes to color, size, and probably even for the freaking dark mode.
With SVG, you can use the icon from any source, and apply any styling you want.
Using SVG Icons
Getting started with SVG icons is pretty easy.
- Identify the SVG
- Copy the SVG and paste it directly in a Vue component (OR) Save the SVG in a file and import it in a Vue component
As always the modern web makes using SVG icons trivial. 😜
Method 1: Use direct links to icons
You can use the SVG icon directly in your Vue component.
An example using icongram ie below.
|
|
The parameters are self-explanatory.
You can see the image, change the size / color and see how URLs are constructed for various icons from the icongram website (linked to Material icons, it can be anything really).
Others doing the same fun stuff -
There is an easier way to use the icons directly.
Use something like a NuxtIcon and set it up as a plugin.
And, voila -
|
|
Method 2: Download and use icons
Any of the sites outlined in the previous section allow you to download the SVG icon. Just save the file in your local public / assets folder and use it in your Vue component.
More example sites providing SVG downloads -
Once the images are available in your project, using them should be as simple as -
|
|
Another way to do this (probably more efficient) is to create a pass through Vue component that renders the SVG. This way you can play around with the size/colors etc. in a more uniform way.
Example Vue component to render a given SVG -
|
|
Invoke the component as any other Vue component. This approach has the advantage of consistency in the way you render the SVG icons - e.g., use similar sizes, color, etc.
Method 3: Just paste the SVG
As an alternative to downloading icons, just paste the SVG in your Vue component.
For e.g. -
|
|
As outlined in the previous section, you can also paste the SVG in a separate Vue component and use that component in your project.