Material Design Icons: Font vs SVG. And how to use SVG Sprites in Angular?

Material Design Icons: Font vs SVG. And how to use SVG Sprites in Angular?

ยท

13 min read

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

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:

  1. Add @angular/material by running ng add @angular/material
  2. Import MatIconModule
  3. Create svg file for the icon under assets
  4. 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

SizeCan be reduced?
Font42KBโŒ
SVG62KBโœ…

source

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.

Font vs SVG

As you can see, they both look almost same for 24px, 36px and 48px. Let's try by reducing the size to 18px:

Font vs SVG 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:

Font vs SVG 18px Part 2

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.

Let's get started

๐Ÿ› ๏ธ Setup

Go to your favorite ๐Ÿ“ directory and open-up the ๐Ÿ‘จโ€๐Ÿ’ป terminal.

  1. npm i -g @angular-cli
  2. ng new material-svg-sprites --minimal --interactive=false
  3. Go to ๐Ÿ˜ด sleep for sometime and wake-up once above command is done ๐Ÿƒ running
  4. cd material-svg-sprites
  5. 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 @Inputs 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...

  1. [ngStyle]- we are going to use @Input() size here to give fixed height and width to our svg
  2. <use>- this element takes nodes from within the SVG document, and duplicates them somewhere else. As you can see in its href, 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:

  1. Download SVG files for the icons which we need from material icons library. We will keep them at ๐Ÿ“ src/icons/svg.
  2. Read them and convert them into <symbol>
  3. Combine all <symbol> to one single file, i.e. ๐Ÿ“„ sprites.svg under ๐Ÿ“ assets directory
  4. 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:

  1. Read all .svg files from ๐Ÿ“ src/icons/svg. This is the folder where we will download all of our icon svg files.
  2. Minify them by keeping viewBox and removing dimensions. Thanks to gulp-svgmin
  3. Compile all of it in <symbol> and merge them into single svg file. With this, it should also keep id of <symbol> same as filename, which will be useful in our <app-icon> component template's <use> tag's href attribute. Credit goes to gulp-svgstore
  4. Rename the result file to ๐Ÿ“„ sprites.svg using gulp-rename
  5. And put that file at ๐Ÿ“ src/assets/icons
  6. Start Angular app by running command ng serve -o
  7. 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.

Clapping

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>&lt;p&gt;</code> tag with
  <app-icon size="18"></app-icon> icon.
</p>
<h1>
  Hi, I am a <code>&lt;h1&gt;</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:

Icon with inline text

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:

  1. Instead of directly setting size, we introduced a property _size and used setter. So based on @Input() size, _size will be calculated.
  2. _top - this will help us to align icon properly.
  3. _size setter - now, we are also accepting inherit as value. If user has provides inherit as value for @Input() size, we are getting icon's parentElement's font-size using window.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.
  4. 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 style position: absolute;top: 50%;height: <HEIGHT>;margin-top: -<HEIGHT/2> and wrap it inside an element with position: relative. You can read more about vertical-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:

Icon with inline text updated

Alright, now it looks good.

๐Ÿ’ป Code for Angular application is present at github/material-svg-sprites


โ˜‘๏ธ Conclusion

  1. We learned what Google Material Design Icons are
  2. We also saw 2 ways of adding icons: Font and SVG
  3. We saw how we can create single svg sprite file of symbols using gulp in an Angular application
  4. 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.

breathe neon on green leaves

Credits

Footer Photo by Tim Goedhart on Unsplash

Did you find this article valuable?

Support Dharmen Shah by becoming a sponsor. Any amount is appreciated!