Skip to content
This repository has been archived by the owner on Nov 16, 2021. It is now read-only.

Commit

Permalink
Adding fallbackSVG Input (#164)
Browse files Browse the repository at this point in the history
* Adding fallbackSVG Input

* Adding fallbackSVG Input in README

* Minor formatting

Co-authored-by: emanuele.fricano <emanuele.fricano@rgigroup.com>
Co-authored-by: arkon <arkon@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 22, 2021
1 parent 69b148b commit 38029bd
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ The SVG file (if found) will be inserted *inside* the element with the `[inlineS
| forceEvalStyles | boolean | `false` | Forces embeded style tags' contents to be evaluated (for IE 11). |
| evalScripts | `'always'`, `'once'`, `'none'` | `'always'` | Whether to evaluate embedded scripts in the loaded SVG files. The `SVGScriptEvalMode` enum is also provided. |
| fallbackImgUrl | string | | URL for a regular image to be displayed as a fallback if the SVG fails to load. |
| fallbackSVG | string | | SVG filename to be displayed as a fallback if the SVG fails to load. |
| onSVGLoaded | `(svg: SVGElement, parent: Element \| null) => SVGElement` | | Lifecycle hook that allows the loaded SVG to be manipulated prior to insertion. |

#### Outputs
Expand Down
1 change: 1 addition & 0 deletions demo/src/demo/demo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Component, OnInit } from '@angular/core';
<div><button (click)="updateSize(10)">Increase</button><button (click)="updateSize(-10)">Decrease</button></div>
<div [inlineSVG]="'#fish'" [setSVGAttributes]="_changeAttrs"></div>
<div [inlineSVG]="'img/nope.svg'" [fallbackImgUrl]="'https://nodei.co/npm/ng-inline-svg.png?compact=true'"></div>
<div [inlineSVG]="'img/nope.svg'" [fallbackSVG]="'#fish'"></div>
`
})
export class DemoComponent implements OnInit {
Expand Down
4 changes: 4 additions & 0 deletions src/inline-svg.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class InlineSVGDirective implements OnInit, OnChanges, OnDestroy {
@Input() forceEvalStyles: boolean = false;
@Input() evalScripts: SVGScriptEvalMode = SVGScriptEvalMode.ALWAYS;
@Input() fallbackImgUrl: string;
@Input() fallbackSVG: string;
@Input() onSVGLoaded: (svg: SVGElement, parent: Element | null) => SVGElement;

@Output() onSVGInserted: EventEmitter<SVGElement> = new EventEmitter<SVGElement>();
Expand Down Expand Up @@ -198,6 +199,9 @@ export class InlineSVGDirective implements OnInit, OnChanges, OnDestroy {
this._renderer.setAttribute(elImg, 'src', this.fallbackImgUrl);

this._insertEl(elImg);
} else if (this.fallbackSVG && this.fallbackSVG !== this.inlineSVG) {
this.inlineSVG = this.fallbackSVG;
this._insertSVG();
}
}

Expand Down

0 comments on commit 38029bd

Please sign in to comment.