npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@spremi/svg-icon

v0.6.1

Published

Add SVG icons

Downloads

12

Readme

A simple, yet customizable, module to add SVG icons in Angular 6+ applications.

The icons are included inline. So, they can be manipulated via CSS.

Demo on StackBlitz.

Features

  • Small footprint.
  • No icon registration required.
  • Provides a blank icon for specified size.
    • Can be used as filler.
  • Provides an error icon - used when specified SVG icon is not found.
    • Developers can spot missing icons easily.
  • [NEW] Dynamically change input params.
  • Customizable
    • Set specific icon size.
    • Scale icon by a factor.
    • Set fill color for error icons.
      • Makes it easy to spot missing icons in any theme.
    • Set own error icon.
      • Use failures as opportunity with branded error icons.

Stability

Beta

  • Features can be demonstrated.
    • Optimization is possible.
  • Documentation almost complete.
  • No test cases.
    • But sample application covers many usage scenarios.
  • Issues/ limitations not known.

Installation

npm

npm install --save @spremi/svg-icon

yarn

yarn add @spremi/svg-icon

Dependencies

import { HttpClientModule } from '@angular/common/http';

...later, add it to imports:

imports: [BrowserModule, HttpClientModule],

How to use

Import the module

In app.module.ts, import SvgIconModule.

  • Also import HttpClientModule, if not already.
import { SvgIconModule } from '@spremi/svg-icon';

@NgModule({
  declarations: [...],
  imports: [BrowserModule, HttpClientModule, SvgIconModule.forRoot()],
  providers: [...],
  bootstrap: [...],
})
export class AppModule {}

Add icons to the project

Add icons to a sub-directory that can be accessed at runtime.

In the projects generated via @angular/cli, the sub-directory src/assets can be accessed as /assets.

Update HTML template

Add icon as-is:

<ang-svg-icon url="/assets/my_icon.svg"></ang-svg-icon>

Add icon with width and height set as 24 pixels:

<ang-svg-icon url="/assets/my_icon.svg" width="24" height="24"></ang-svg-icon>

Add icon and scale it 2x:

<ang-svg-icon url="/assets/my_icon.svg" scale="2"></ang-svg-icon>

Note:

  • For scale to work, original SVG must specify a size (width & height).
  • If scale is specified, it takes precedence over width and height.

How to customize

We need to import SvgIconService as well.

import { SvgIconModule, SvgIconService } from '@spremi/svg-icon';

Set custom fill color for error icons

export class AppModule {
  constructor(private iconSvc: SvgIconService) {
    this.iconSvc.setErrorFill('purple');
  }
}

Set custom error SVG

Add markup for the custom error SVG.

  • Ensure it has viewBox defined, so that it scales well.
  • Change width and height to %WIDTH% and %HEIGHT% as shown below.
const CUST_ERROR = `<svg xmlns="http://www.w3.org/2000/svg"
  width="%WIDTH%" height="%HEIGHT%" viewBox="0 0 24 24">
  ...
  ...
  </svg >`;

export class AppModule {
  constructor(private iconSvc: SvgIconService) {
    this.iconSvc.setErrorTemplate(CUST_ERROR);
  }
}

License

BSD-3-Clause Copyright Sanjeev Premi