8000 GitHub - pacoita/svg-icon: 🀑 A lightweight library that makes it easier to use SVG icons in your Angular Application
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
forked from ngneat/svg-icon

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

License

Notifications You must be signed in to change notification settings

pacoita/svg-icon

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

25 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Reposi 8000 tory files navigation


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 svg-to-ts to clean and extract the icons content:

    {
      "scripts": {
        "generate-icons": "svg-to-ts"
      },
      "svg-to-ts": {
        "conversionType": "object",
        "srcFiles": [
          "./src/assets/svg/*.svg"
        ],
        "outputDirectory": "./src/assets/svg",
        "fileName": "svg-icons",
        "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 icons from '../assets/svg/svg-icons';
    
    @NgModule({
      imports: [
        SvgIconsModule.forRoot({
          icons
        })
      ],
      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>

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'
      },
      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';
import { mySvg } from 'my-icons.ts';
 
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({ settings: `<svg>...</svg>`});
     // You can pass a Icon structure map or array as well
     registry.register({ mySvg });
     registry.register([mySvg]);
     registry.get(key);
     registry.getAll();
   }
}

Buy Me A Coffee

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Netanel Basal

πŸ’» πŸ“– πŸ€”

Inbal Sinai

πŸ“–

Shahar Kazaz

πŸ’» πŸ€”

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

About

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

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 91.2%
  • JavaScript 7.2%
  • HTML 1.5%
  • SCSS 0.1%
0