svg-inline-ng-plugin

0.0.3 • Public • Published

SVG Inline Angular Plugin

Takes a directory of .svg files and generates an angular 2 service file with the svg code for inline use.

Why?

<object> svg's can not be styled with CSS easily. To get dynamic sizing and CSS (color fill) styling, inline <svg> is the best option. But for ease of use we want just use files. So this takes the files and creates an Angular service with the inlined svg code for use.

https://svgontheweb.com/#implementation

https://css-tricks.com/using-svg/

Usage

Install

Add to package.json & npm install "svg-inline-ng-plugin": "git+ssh://git@github.com/presencelearning/svg-inline-ng-plugin"

Add to webpack config:

And ignore the generated file (write directory) to prevent infinite webpack build loops.

var SvgInlineNgPlugin = require('svg-inline-ng-plugin');
var WatchIgnorePlugin = webpack.WatchIgnorePlugin;
...
new SvgInlineNgPlugin({
    svgDir: helpers.root('public', 'images', 'svg'),
    writeDir: helpers.root('src', 'build')
}),

new WatchIgnorePlugin([
    helpers.root('src', 'build')
])

Import our generated file and use the svg code.

import { SvgInlineNgPluginService } from '../../../../build/svg-inline-ng-plugin.service';
...
providers: [SvgInlineNgPluginService]
...
constructor(private sanitizer: DomSanitizer,
 private svgInlineNgPluginService: SvgInlineNgPluginService ) {}
...
this.svgCode = this.sanitizer.bypassSecurityTrustHtml(this.svgInlineNgPluginService.svgs[this.svg].html);

And then in the html:

<div [innerHtml]="svgCode" class="pl-icon-svg"></div>

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.0.3
    0
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.0.3
    0
  • 0.0.2
    0
  • 0.0.1
    0

Package Sidebar

Install

npm i svg-inline-ng-plugin

Weekly Downloads

0

Version

0.0.3

License

MIT

Last publish

Collaborators

  • lukemadera