npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@doughlass/ng-inline-svg

v1.0.0

Published

Angular directive for inserting an SVG inline within an element.

Downloads

57

Readme

@doughlass/ng-inline-svg

Inline SVG for Angular 15>


NPM

Formerly called ng-inline-svg & ng-inline-svg-2

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 @doughlass/ng-inline-svg

Usage

Add HttpClientModule and InlineSVGModule to your list of module imports:

import { HttpClientModule } from '@angular/common/http';
import { InlineSVGModule } from '@doughlass/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 name | Type | Default | Description | | ------------- | ---- | ------- | ----------- | | cacheSVG | boolean | true | Caches the SVG based on the absolute URL. Cache only persists for the (sessional) lifetime of the page. | | resolveSVGUrl | boolean | true | Bypass logic that tries to determine the absolute URL using the page's or configured base URL. | | prepend | boolean | false | Inserts before the first child instead of appending, overwrites replaceContents | | replaceContents | boolean | true | Replaces the contents of the element with the SVG instead of just appending it to its children. | | injectComponent | boolean | false | Injects 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. | | removeSVGAttributes | string[] | | An array of attribute names to remove from all child elements within the SVG. | | 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

| Property name | Callback arguments | Description | | ------------- | ------------------ | ----------- | | onSVGInserted | e: SVGElement | Emits the SVGElement post-insertion. | | onSVGFailed | e: any | Emits 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 '@doughlass/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 '@doughlass/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;
  }
}