Skip to content

Instantly share code, notes, and snippets.

@bryandh
Last active August 23, 2022 11:57
Show Gist options
  • Save bryandh/313e4bc1ca4b86825c24d4e24dbd73f8 to your computer and use it in GitHub Desktop.
Save bryandh/313e4bc1ca4b86825c24d4e24dbd73f8 to your computer and use it in GitHub Desktop.
Aurelia InlineSvg component, renders an SVG from file path

Aurelia InlineSvg component

Renders an SVG from file path.

How to use

<inline-svg src="/assets/icons/icon.svg"></inline-svg>

import { bindable, inject, inlineView } from 'aurelia-framework';
import { HttpClient } from 'aurelia-fetch-client';
@inlineView('<template></template>')
@inject(Element, HttpClient)
export class InlineSvg {
@bindable private src: string;
constructor(
private el: HTMLElement,
private http: HttpClient
) { }
private bind(): void {
this.updateSvg(this.src);
}
private srcChanged(src: string): void {
this.updateSvg(src);
}
private async updateSvg(svgSrc): Promise<void> {
if (svgSrc) {
const currentSvg = this.el.innerHTML;
const response = await this.http.fetch(svgSrc);
const text = await response.text();
this.el.innerHTML = text;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment