13.1.2 • Published 2 years ago

ng-inline-svg v13.1.2

Weekly downloads
35,619
License
MIT
Repository
github
Last release
2 years ago

ng-inline-svg

⚠️ This package is deprecated and not maintained. ⚠️


NPM

Demo

Formerly called ng2-inline-svg

Angular directive for inserting an SVG inline within an element, allowing for easily styling with CSS like fill: currentColor;.

This is based on md-icon, except this is meant purely for inserting SVG files within an element, without the extra things like font icons.

Installation

npm install --save ng-inline-svg

Usage

Add HttpClientModule and InlineSVGModule to your list of module imports:

import { HttpClientModule } from '@angular/common/http';
import { InlineSVGModule } from 'ng-inline-svg';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, HttpClientModule, InlineSVGModule.forRoot()],
  bootstrap: [AppComponent]
})
class AppModule {}

The base URL will be automatically fetched based on your <base> tag. If you want to configure the base URL manually, you can use the forRoot function:

InlineSVGModule.forRoot({ baseUrl: 'https://mysite.com/myapp/' })

You can then use the directive in your templates:

@Component({
  selector: 'app',
  template: `
    <div class="my-icon" aria-label="My icon" [inlineSVG]="'/img/image.svg'"></div>
  `
})
export class AppComponent {}

The SVG file (if found) will be inserted inside the element with the [inlineSVG] directive. Support for icons using the symbol method is also supported (e.g. [inlineSVG]="'#shape-id'").

Options

Inputs

Property nameTypeDefaultDescription
cacheSVGbooleantrueCaches the SVG based on the absolute URL. Cache only persists for the (sessional) lifetime of the page.
resolveSVGUrlbooleantrueBypass logic that tries to determine the absolute URL using the page's or configured base URL.
prependbooleanfalseInserts before the first child instead of appending, overwrites replaceContents
replaceContentsbooleantrueReplaces the contents of the element with the SVG instead of just appending it to its children.
injectComponentbooleanfalseInjects an <inline-svg> component containing the SVG inside the element with the directive.
setSVGAttributes{ [key: string]: any }Sets attributes on the SVG element. Runs after removeSVGAttributes.
removeSVGAttributesstring[]An array of attribute names to remove from all child elements within the SVG.
forceEvalStylesbooleanfalseForces 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.
fallbackImgUrlstringURL for a regular image to be displayed as a fallback if the SVG fails to load.
fallbackSVGstringSVG filename to be displayed as a fallback if the SVG fails to load.
onSVGLoaded(svg: SVGElement, parent: Element \| null) => SVGElementLifecycle hook that allows the loaded SVG to be manipulated prior to insertion.

Outputs

Property nameCallback argumentsDescription
onSVGInsertede: SVGElementEmits the SVGElement post-insertion.
onSVGFailede: anyEmits when there is some error (e.g. embed SVG not supported, fetch failed, etc.)

Server-side rendering with Angular Universal

The SVG files can also be rendered server-side. For this to work, you have to set the baseURL, since Angular needs to have an absolute URL to retrieve the files server-side and we're not able to get your baseURL automatically in a server-side environment. See manual for setting baseURL above.

Here is one way to achieve this dynamically by adding an app initalizing service which sets the base URL based on the environment it runs.

app-server.module.ts:

import { NgModule, APP_INITIALIZER } from '@angular/core';
import { InlineSVGConfig } from 'ng-inline-svg';
import { SVGConfig } from './svg-config';

@NgModule({
  providers: [
    { provide: InlineSVGConfig, useClass: SVGConfig }
  ]
})
export class AppServerModule {}

svg-config.ts:

import { Injectable, Inject } from '@angular/core';
import { InlineSVGConfig } from 'ng-inline-svg';

@Injectable()
export class SVGConfig extends InlineSVGConfig {
  // Do what ever conditions you need to set this, e.g. checking for server-side rendering
  // and only set baseURL when server-side rendered if you want.

  constructor(...) {
    super();

    // When the server-side rendered app runs on localhost:3000, make sure baseURL is
    // http://localhost:3000 and make sure the Express server is configured properly to
    // allow the URL of the asset folders storing the SVG files.
    this.baseUrl = 'http://localhost:3000';

    // If you don't want the directive to run on the server side.
    this.clientOnly = true;

    // If you want to bypass your HttpClient interceptor chain when fetching SVGs.
    this.bypassHttpClientInterceptorChain = true;
  }
}
13.1.2

2 years ago

13.1.1

2 years ago

13.1.0

3 years ago

13.0.0

3 years ago

12.1.0

3 years ago

12.0.0

3 years ago

11.0.2

3 years ago

11.0.1

4 years ago

11.0.0

4 years ago

10.1.0

4 years ago

10.0.0

4 years ago

8.6.3

4 years ago

9.2.2

4 years ago

9.2.1

4 years ago

8.6.2

4 years ago

9.2.0

4 years ago

8.6.1

4 years ago

9.1.0

5 years ago

9.0.1

5 years ago

9.0.0

5 years ago

8.6.0

5 years ago

8.5.1

5 years ago

8.5.0

5 years ago

8.4.1

5 years ago

8.4.0

5 years ago

8.3.0

5 years ago

8.2.2

5 years ago

8.2.1

5 years ago

8.2.0

5 years ago

8.1.0

6 years ago

8.0.2

6 years ago

8.0.1

6 years ago

8.0.0

6 years ago

7.0.1

6 years ago

7.0.0

6 years ago

6.2.1

6 years ago

6.2.0

6 years ago

6.1.0

6 years ago

6.0.0

6 years ago

5.1.4

6 years ago

5.1.3

6 years ago

5.1.2

6 years ago

5.1.1

6 years ago

5.1.0

6 years ago

5.0.0

6 years ago

4.0.1

7 years ago

4.0.0

7 years ago

3.4.0

7 years ago

3.3.0

7 years ago

3.2.3

7 years ago

3.2.2

7 years ago

3.2.1

7 years ago

3.2.0

7 years ago

3.1.2

7 years ago

3.1.1

7 years ago

3.1.0

7 years ago

3.0.0

7 years ago

2.2.0

7 years ago

2.1.0

7 years ago

2.0.0

7 years ago