Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

angular-svg-icon

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

angular-svg-icon

Angular 5 component for inlining SVGs allowing them to be easily styled with CSS.

  • 5.0.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

npm version

Angular SVG Icon

The angular-svg-icon is an Angular 4.3+ service and component that provides a means to inline SVG files to allow for them to be easily styled by CSS and code.

The service provides an icon registery that loads and caches a SVG indexed by its url. The component is responsible for displaying the SVG. After getting the svg from the registry it clones the SVGElement and the SVG to the component's inner HTML.

A working demo shows solution in action.

How to use?

$ npm i angular-svg-icon --save

(Note: For use Angular 2.4 through Angular 4.2, please install angular-svg-icon@4.2.6 and see the module's accompanying README.md for instructions.)

Integration

The angular-svg-icon should work as-is with webpack/angular-cli. Just import the AngularSvgIconModule and the HttpClientModule.

import { HttpClientModule } from '@angular/common/http';
import { AngularSvgIconModule } from 'angular-svg-icon';

@NgModule({
    imports: [ HttpClientModule, AngularSvgIconModule ],
    ...
})
export class AppModule {}

Usage

Basic usage is:

<svg-icon src="path/to/icon.svg"></svg-icon>

An example of styling the svg:

<svg-icon src="images/eye.svg" style="fill:green;width:90px;"></svg-icon>

Programatic interaction with the registry is also possible. Include the private iconReg:SvgIconRegistryService in the constructor:

constructor(private iconReg:SvgIconRegistryService) { }

The registry has two public functions: loadSvg(string) and unloadSvg(string).

To preload a svg file into the registry:

{
   ...
   this.iconReg.loadSvg('foo.svg');
}

To unload a svg from the registry.

{
    ...
    this.iconReg.unloadSvg('foo.svg');
}

SVG Preparation

The SVG should be modified to remove the height and width attributes from the file per Sara Soueidan's advice in "Making SVGs Responsive With CSS" if size is to be modified through CSS. Removing the height and width has two immedate impacts: (1) CSS can be used to size the SVG, and (2) CSS will be required to size the SVG.

Background

The svg-icon is an Angular 2 component that allows for the continuation of the AngularJS method for easily inlining SVGs explained by Ben Markowitz and others. Including the SVG source inline allows for the graphic to be easily styled by CSS.

The technique made use of ng-include to inline the svg source into the document. Angular 2, however, drops the support of ng-include, so this is my work-around method.

Note: The icon component from angular/material2 used to have a direct means to load svg similar to this, but this functionality was removed because of security concerns.

License

MIT

Author

  • David Czeck @czeckd

Keywords

FAQs

Package last updated on 03 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc