Coder Social home page Coder Social logo

vltansky / svg-icon Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ngneat/svg-icon

0.0 1.0 0.0 1.7 MB

๐Ÿคก A lightweight library that makes it easier to use SVG icons in your Angular Application

Home Page: https://netbasal.com

License: MIT License

JavaScript 6.91% TypeScript 91.57% HTML 1.44% SCSS 0.08%

svg-icon's Introduction


A lightweight library that makes it easier to use SVG icons in your Angular Application

MIT commitizen PRs styled with prettier All Contributors ngneat spectator

The svg-icon library enables using the <svg-icon> tag to directly display SVG icons in the DOM. This approach offers an advantage over using an <img> tag or via the CSS background-image property, because it allows styling and animating the SVG with CSS.

For example, if the fill or stroke properties of elements in the svg are set to currentColor, they will have the color defined for the containing DOM element,. So the color can easily be changed by changing the color style on the svg-icon element.

Installation

ng add @ngneat/svg-icon

This command will automatically preform the recommended flow (steps 2-4).

Recommended Flow

Icons Preparation

  1. Add the icons to src/assets/svg

  2. Use @ngneat/svg-generator to clean and extract the icons content:

    {
     "scripts": {
       "generate-icons": "svgGenerator"
     },
     "svgGenerator": {
       "outputPath": "./src/app/svg",
       "prefix": "app",
       "srcPath": "./src/assets/svg",
       "outputDirectory": "./src/app",
       "svgoConfig": {
         "plugins": [
           {
             "removeDimensions": true,
             "cleanupAttrs": true
           }
         ]
       }
     }
    }
  3. Run npm run generate-icons

    Icons Rendering

  4. Import the SvgIconsModule in your AppModule, and register the icons:

    import { SvgIconsModule } from '@ngneat/svg-icon';
    
    import { appSettings } from '../svg';
    
    @NgModule({
      imports: [
        SvgIconsModule.forRoot({
          icons: [appSettings]
        })
      ],
      bootstrap: [AppComponent]
    })
    export class AppModule {}
  5. Now you can use the svg-icon component:

    <svg-icon key="settings"></svg-icon> <svg-icon key="settings" color="hotpink" fontSize="40px"></svg-icon>

In lazy load modules, you can use the forChild method:

import { DashboardRoutingModule } from './dashboard-routing.module';
import { DashboardComponent } from './dashboard.component';
import { appSettings } from '../svg/app-settings.icon';
import { SvgIconsModule } from '@ngneat/svg-icon';

@NgModule({
  declarations: [DashboardComponent],
  imports: [CommonModule, DashboardRoutingModule, SvgIconsModule.forChild([appSettings])]
})
export class DashboardModule {}

Icon Sizing

To control the SVG size, we use the font-size property as described in this article. You also have the option to pass fixed sizes and use them across the application:

@NgModule({
  imports: [
    SvgIconsModule.forRoot({
      sizes: {
        xs: '10px',
        sm: '12px',
        md: '16px',
        lg: '20px',
        xl: '25px',
        xxl: '30px'
      },
      defaultSize: 'md'
      icons
    })
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

They are used in the size input:

<svg-icon key="settings" size="lg"></svg-icon>

SvgIconRegistry

You can inject the SvgIconRegistry, and get existing SVG icons or register new ones:

import { SvgIconRegistry } from '@ngneat/svg-icon';

interface Icon {
  name: string;
  data: string;
}

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  constructor(private registry: SvgIconRegistry) {
    registry.register([Icon, Icon, Icon]);
    registry.register(Icon);
    registry.get(name);
    registry.getAll();
  }
}

Buy Me A Coffee

Contributors โœจ

Thanks goes to these wonderful people (emoji key):


Netanel Basal

๐Ÿ’ป ๐Ÿ“– ๐Ÿค”

Inbal Sinai

๐Ÿ“–

Shahar Kazaz

๐Ÿ’ป ๐Ÿค”

Artur Androsovych

๐Ÿ’ป

Vlad Tansky

๐Ÿ’ป

This project follows the all-contributors specification. Contributions of any kind welcome!
Logo icon was made by Freepik from www.flaticon.com

svg-icon's People

Contributors

netanelbasal avatar vltansky avatar shaharkazaz avatar daboudii avatar jimeh87 avatar arturovt avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.