GithubHelp home page GithubHelp logo

profitwarning / angular-svg-icon Goto Github PK

View Code? Open in Web Editor NEW

This project forked from czeckd/angular-svg-icon

0.0 2.0 0.0 49 KB

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

License: MIT License

TypeScript 45.78% HTML 27.00% JavaScript 20.70% CSS 6.53%

angular-svg-icon's Introduction

npm version

Angular SVG Icon

The angular-svg-icon is an Angular 2+ service and component that provides a means to inline SVG images 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

Integration

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

import { AngularSvgIconModule } from 'angular-svg-icon';

@NgModule({
    imports: [ 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');
}

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.