In this article we will compare usage of Google's Material Design Icons, as font and SVG. And we will also see how we can use SVG sprites in an Angular application.
๐ Table of Contents
- Material Icons
- Font vs SVG
- โณ Using material icon SVG symbol sprites in Angular
Material Icons
Material Icons are Google's official icon pack. They are carefully designed following Material Design Guidelines. It's one of the most widely used icon sets for both, web and mobile applications. It has got around 40k+ โญ๏ธ on github.
Icon ๐ font for the web
The easiest way to include the material icons is to use material icon font. All the material icons are packaged into a single font.
You just need to add single line of HTML:
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
And then you can use them easily, See below example:
<i class="material-icons">face</i>
To further style them, check this official guide.
Icon ๐ผ๏ธ SVGs for the web
The material icons are also provided as SVGs that are suitable for web projects. Individual icons are downloadable from the material icons library.
For Angular, we can also use @angular/material
's icon component, with some additional setup. To summarize, below are the steps which will be needed:
- Add
@angular/material
by runningng add @angular/material
- Import
MatIconModule
- Create svg file for the icon under assets
- Register the same svg file for it's corresponding icon. For example, you can do below in a component:
...
constructor(iconRegistry: MatIconRegistry, sanitizer: DomSanitizer) {
iconRegistry.addSvgIcon(
'thumbs-up',
sanitizer.bypassSecurityTrustResourceUrl('assets/img/examples/thumbup-icon.svg'));
}
...
and then use it in HTML:
<mat-icon svgIcon="thumbs-up" aria-hidden="false" aria-label="Example thumbs up SVG icon"></mat-icon>
Now, ๐ repeat steps 3 and 4 for all your icons.
Font vs SVG
There are multiple aspects when it comes to selection. We are going to look at the most important ones only:
- Bundle size
- Performance
๐ฆ Bundle size
Size | Can be reduced? | |
Font | 42KB | โ |
SVG | 62KB | โ |
Clearly, font file is winner when it comes to bundle size. But, SVG file size can be reduced considerably by compiling only the icons you need into a single SVG file with symbol sprites. We will look into it later on.
๐ญ Performance
Now let's see which one looks better in browser, we will use standard sizes suggested by Google Material Design in sizing, i.e. 18px
, 24px
, 36px
and 48px
.
As you can see, they both look almost same for 24px
, 36px
and 48px
. Let's try by reducing the size to 18px
:
Ok, as you can see for 18px
, font loses its consistency around circular edges while svg still looks good. But, it totally depends on which icons you're using. For instance, look at below:
done_all
and swap_horiz
don't have round edges, that's why they look fine in font, too.
๐ป Code for above preview samples is available here.
๐ What to choose
As you saw, except for some cases, font and svg, both perform well in browsers. But we can improve the bundle size of svg, by compiling only the icons we need in a single file. Let's see how we can do it in Angular.
โณ Using material icon SVG symbol sprites in Angular
First of all, compiling single file with svg symbol sprite is not related to Angular or any other web framework. It's an opinionated way of achieving cost and performance effective solution to use material icons in your web application. You can read more about it here.
๐ ๏ธ Setup
Go to your favorite ๐ directory and open-up the ๐จโ๐ป terminal.
npm i -g @angular-cli
ng new material-svg-sprites --minimal --interactive=false
- Go to ๐ด sleep for sometime and wake-up once above command is done ๐ running
cd material-svg-sprites
ng serve
Cool. Let's move on to next part.
โ๏ธ Coding
1. Create <app-icon>
component
Open-up another terminal at the project directory and:
ng g c icon --inlineStyle --inlineTemplate=false
๐ Ideally, you should create such components in a shared module. For more, head out to official docs.
2. Modify icon component class
Let's add 2 @Input
s to our component:
// src\app\icon\icon.component.ts
import { Component, Input, OnInit } from "@angular/core";
@Component({
selector: "app-icon",
templateUrl: "./icon.component.html",
styles: [],
})
export class IconComponent implements OnInit {
@Input() size = 24; // ๐ added
@Input() name = 'face'; // ๐ added
constructor() {}
ngOnInit(): void {}
}
So, size
is going to be responsible for size (height and width), of-course ๐
. And name
represents name of the icon, by default we will show face
, cz it looks nice ๐.
3. Modify icon component template
<!-- src\app\icon\icon.component.html -->
<svg [ngStyle]="{ height: size + 'px', width: size + 'px' }">
<use [attr.href]="'assets/icons/sprites.svg#' + name"></use>
</svg>
Let's see what's going on here...
[ngStyle]
- we are going to use@Input() size
here to give fixed height and width to our svg<use>
- this element takes nodes from within the SVG document, and duplicates them somewhere else. As you can see in itshref
, we are giving location of the icon which are going to use.
From this, you might have understood that sprites.svg is going to be our single file with svg symbol sprites. Let's get into that.
4. Create ๐ sprites.svg
Now, we are not directly going to create sprites file, we are going to automate the flow. To summarize we are going to do below:
- Download SVG files for the icons which we need from material icons library. We will keep them at ๐ src/icons/svg.
- Read them and convert them into
<symbol>
- Combine all
<symbol>
to one single file, i.e. ๐ sprites.svg under ๐ assets directory - Repeat steps 2 and 3 whenever any new icon is added under ๐ src/icons/svg
To automate above flow, we will use gulp. You do not need to have detailed understanding of gulp, just remember that it's a toolkit to automate and enhance our workflow.
4.1 ๐ gulpfile.js
Let's install some devDependencies first:
npm i -D gulp gulp-svgstore gulp-svgmin path gulp-rename gulp-run-command
Now create ๐ gulpfile.js:
// gulpfile.js
const gulp = require("gulp");
const svgstore = require("gulp-svgstore");
const svgmin = require("gulp-svgmin");
const rename = require("gulp-rename");
const run = require("gulp-run-command").default;
gulp.task("svg-sprite", () => {
return gulp
.src("src/icons/svg/*.svg")
.pipe(
svgmin(() => {
return {
plugins: [
{
removeViewBox: false,
},
{
removeDimensions: true,
},
],
};
})
)
.pipe(svgstore())
.pipe(rename({ basename: "sprites" }))
.pipe(gulp.dest("src/assets/icons"));
});
gulp.task("ng-serve", run("ng serve -o"));
gulp.task("ng-build", run("ng build --prod"));
gulp.task("watch", () =>
gulp.watch(["src/icons/svg/*.svg"], gulp.series("svg-sprite"))
);
gulp.task(
"default",
gulp.series("svg-sprite", gulp.parallel("ng-serve", "watch"))
);
gulp.task("build", gulp.series("svg-sprite", "ng-build"));
Without much details, below is the flow of default task gulp:
- Read all
.svg
files from ๐ src/icons/svg. This is the folder where we will download all of our icon svg files. - Minify them by keeping
viewBox
and removing dimensions. Thanks to gulp-svgmin - Compile all of it in
<symbol>
and merge them into single svg file. With this, it should also keepid
of<symbol>
same as filename, which will be useful in our<app-icon>
component template's<use>
tag'shref
attribute. Credit goes to gulp-svgstore - Rename the result file to ๐ sprites.svg using gulp-rename
- And put that file at ๐ src/assets/icons
- Start Angular app by running command
ng serve -o
- Keep watching for svg files under ๐ src/icons/svg, if changed repeat steps 1, 2, 3, 4, 5.
There is also one more task called build
, which does the same thing as above, except it doesn't keep watching svg files and instead of ng serve
, it runs ng build --prod
.
5. npm
scripts
As we have created ๐ gulpfile.js in such a way that it handles running of Angular project, too, we can simply have below script in our ๐ package.json to run it:
// package.json
...
"scripts": {
"ng": "ng",
"start": "gulp", // ๐ modified
"build": "gulp build", // ๐ modified
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
...
Now, we can simply run npm start
and npm run build
to run the project in watch mode and build it respectively.
6. Icon SVGs
Download face
icon's svg file from here or you can goto material icons library, find and download face
icon's svg file.
Once it's downloaded, rename it to ๐ face.svg, so that when it gets compiled, id
of <symbol>
is face
. Don't forget to keep it under ๐ src/icons/svg.
7. <app-root>
component
We're almost done. Let's add reference to <app-icon>
component in <app-root>
. Simply have below in your template:
<app-icon></app-icon>
And ๐ voila!!! You should see face
icon's svg in your browser.
Also try by giving different sizes, like below, it should work fine:
<app-icon size="48"></app-icon>
With above, we done with all the setup and coding. Now, to use any other icon, for example done_all
icon, simply download it from material icons library, rename it to some memorable general keyword, put it under ๐ src/icons/svg. And use it like below:
<app-icon name="done_all" size="36"></app-icon>
Great ๐!!! If you're with me up-to this, consider I am continuously ๐๐๐ clapping for you.
8. Advanced
This is some advanced changes which we will be doing for better alignment and size when icon is placed inline with text, this is totally optional.
8.1 <app-root>
template changes
First, let's see how it looks inline with text. For that, simply change <app-root>
template:
<p>
Hi, I am a <code><p></code> tag with
<app-icon size="18"></app-icon> icon.
</p>
<h1>
Hi, I am a <code><h1></code> tag with
<app-icon size="36"></app-icon> icon.
</h1>
I have kept icon sizes at 1.125 multiplier with font size. So by default, in chrome, <p>
and <h1>
tags have 16px
and 32px
font size, so that will become 18px
and 36px
for icon size. You can choose your multiplier, but 1.125 works for me.
Let's look at output:
As you can see, we need to really work on alignment. Let's do that.
8.2 <app-icon>
component and template changes
Modify <app-icon>
component like below:
// src\app\icon\icon.component.ts
...
export class IconComponent implements OnInit {
private readonly SIZE_MULTIPLIER = 1.125; // ๐ you can change this as per your need
_size = 24;
_top = 0;
@Input() name = "face";
@Input()
set size(size: "inherit" | number) {
if (size === "inherit") {
const parentElement = this.elRef.nativeElement.parentElement;
this._size = Math.ceil(
parseFloat(
window.getComputedStyle(parentElement).getPropertyValue("font-size")
) * this.SIZE_MULTIPLIER
);
this._top = -(this._size / Math.ceil(14.22 * this.SIZE_MULTIPLIER));
} else if (isNaN(size)) {
throw new Error('size must be a number or "inherit"');
} else {
this._size = size;
}
}
constructor(private elRef: ElementRef) {}
...
}
<!-- src\app\icon\icon.component.html -->
<svg
[ngStyle]="{
height: _size + 'px',
width: _size + 'px',
top: _top + 'px'
}"
style="display: inline-block; vertical-align: top; position: relative;"
>
<use [attr.href]="'assets/icons/sprites.svg#' + name"></use>
</svg>
To summarize, below is what we did:
- Instead of directly setting
size
, we introduced a property_size
and usedsetter
. So based on@Input() size
,_size
will be calculated. _top
- this will help us to align icon properly._size
setter - now, we are also acceptinginherit
as value. If user has providesinherit
as value for@Input() size
, we are getting icon'sparentElement
'sfont-size
usingwindow.getComputedStyle
. And we are multiplying it with our multiplier, i.e.SIZE_MULTIPLIER = 1.125
. Lastly,_top
is also calculated. Everything is simple maths, you can play around and make changes as per your need.- template - we have made some style changes so that it looks proper. Now, these style changes are really an opinionated way. You can select your own way, maybe make
<svg>
's styleposition: absolute;top: 50%;height: <HEIGHT>;margin-top: -<HEIGHT/2>
and wrap it inside an element withposition: relative
. You can read more aboutvertical-align
at MDN Docs.
๐ You can try to have
fill-color
set based on parent element and/or input.
Now, let's look at the output in browser again:
Alright, now it looks good.
๐ป Code for Angular application is present at github/material-svg-sprites
โ๏ธ Conclusion
- We learned what Google Material Design Icons are
- We also saw 2 ways of adding icons: Font and SVG
- We saw how we can create single svg sprite file of symbols using gulp in an Angular application
- And at last, we also took a look at how we can align the icon for inline text
๐ Thank you
You guys have been great readers. I really appreciate that you've taken some time to read this article. Let me know your thoughts and feedbacks in the comments section.
You can follow me on ๐ฆ twitter @shhdharmen. Don't be shy even just say hi ๐!
And yes, always believe in yourself.
Credits
Footer Photo by Tim Goedhart on Unsplash