Home

Awesome

Angular SVG Sprite

The aim

The project aims to generate an SVG sprite that enables us to achieve the following objectives:

Installation

Run the commands in your project:

Steps

1. Creating the svg-sprite generation script:

Where:

2. Creating the services that work with sprite:

3. Creating the svg-icon component:

4. Adding the commands to package.json:

5. Adding svg-icons to the assets folder.

6. Adding the svg-sprite to the .gitignore file for not saving it:

# Auto-generated sprites folder
svg-sprite.svg

7. Using the svg-icon component:

Adding icons to template:

<svg-icon [src]="<your-icon-name-here>"></svg-icon>

Changing icon styles (optional):

The usage of fill or stroke depends on the <path> type in the svg file.

Handling Gradient IDs

During sprite generation, we <ins>ensure that all linear and radial gradient IDs in SVGs are unique</ins> to prevent conflicts when multiple icons use gradients. This prevents issues with color references when multiple SVGs are displayed, ensuring that each SVG maintains its intended gradient coloring even if others are added or removed from the DOM.

✨ New Features: Handling Unique IDs in SVGs

We have enhanced the way SVGs are processed during sprite generation: