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

Process SVG on attr changing even if SVG url not changed #134 #156

Merged
merged 1 commit into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions demo/src/demo/demo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { Component, OnInit } from '@angular/core';
<div [inlineSVG]="'img/symbol.svg#fish'"></div>
<div [inlineSVG]="'#fish'"></div>
<div [inlineSVG]="'#fish'" [injectComponent]="true"></div>
<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>
`
})
Expand All @@ -18,6 +20,10 @@ export class DemoComponent implements OnInit {
'width': '50',
'height': '50'
};
private _changeAttrs = {
'width': '50',
'height': '50'
};

ngOnInit() {
setTimeout(() => {
Expand All @@ -30,4 +36,11 @@ export class DemoComponent implements OnInit {
svg.setAttribute('width', '100');
return svg;
}

updateSize(value: number): void {
this._changeAttrs = {
'width': (parseInt(this._changeAttrs['width'], 10) + value).toString(),
'height': (parseInt(this._changeAttrs['height'], 10) + value).toString(),
};
}
}
9 changes: 5 additions & 4 deletions src/inline-svg.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ export class InlineSVGDirective implements OnInit, OnChanges, OnDestroy {
ngOnChanges(changes: SimpleChanges): void {
if (!this._isValidPlatform() || this._isSSRDisabled()) { return; }

if (changes['inlineSVG'] || changes['setSVGAttributes']) {
this._insertSVG();
const setSVGAttributesChanged = Boolean(changes['setSVGAttributes']);
if (changes['inlineSVG'] || setSVGAttributesChanged) {
this._insertSVG(setSVGAttributesChanged);
}
}

Expand All @@ -92,7 +93,7 @@ export class InlineSVGDirective implements OnInit, OnChanges, OnDestroy {
}
}

private _insertSVG(): void {
private _insertSVG(force = false): void {
if (!isPlatformServer(this.platformId) && !this._supportsSVG) { return; }

// Check if a URL was actually passed into the directive
Expand All @@ -102,7 +103,7 @@ export class InlineSVGDirective implements OnInit, OnChanges, OnDestroy {
}

// Short circuit if SVG URL hasn't changed
if (this.inlineSVG === this._prevUrl) {
if (!force && this.inlineSVG === this._prevUrl) {
return;
}
this._prevUrl = this.inlineSVG;
Expand Down