My struggle with implementing icons in Angular
While working on a new Angular project, I've encountered challenges with my current SVG-icon implementation method from a previous project (@ngneat/svg-icon). The process involves organizing SVG files in a folder, registering them in Typescript, and compiling them into (iconName).ts files using a script.
A simpler approach
In my current project, I came across a more straightforward SVG implementation while exploring Bootstrap documentation:
Source: https://getbootstrap.com/docs/5.0/components/alerts/
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<symbol id="check-circle-fill" fill="currentColor" viewBox="0 0 16 16">
<path d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zm-3.97-3.03a.75.75 0 0 0-1.08.022L7.477 9.417 5.384 7.323a.75.75 0 0 0-1.06 1.06L6.97 11.03a.75.75 0 0 0 1.079-.02l3.992-4.99a.75.75 0 0 0-.01-1.05z"/>
</symbol>
<symbol id="info-fill" fill="currentColor" viewBox="0 0 16 16">
...
</svg>
...
</pre>
<p>This method involves adding all SVG icons as symbols in a hidden element, assigning appropriate IDs to the icons, and referencing them using xlink:href anywhere on the page.</p>
<h3>Concern about deprecation</h3>
<p>Although MDN mentions that xlink:href is deprecated (<a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/xlink:href" rel="nofollow noreferrer">Source</a>), many sources suggest that it won't be removed due to widespread usage, like in Bootstrap. So, I'm considering sticking with it.</p>
<h3>An alternative without deprecation</h3>
<p>The MDN page for SVG <use> (<a href="https://developer.mozilla.org/en-US/docs/Web/SVG/Element/use" rel="nofollow noreferrer">Source</a>) offers an example using only href instead of xlink:href. Testing this on my site showed promising results.</p>
<pre><code><svg viewBox="0 0 30 10" xmlns="http://www.w3.org/2000/svg">
<circle id="myCircle" cx="5" cy="5" r="4" stroke="blue"/>
<use href="#myCircle" x="10" fill="blue"/>
...
</svg>
The Dilemma
In essence, the question boils down to "what is the best way to implement icons?" Here are some queries to consider:
- Should I continue using xlink:href based on Bootstrap's usage?
- Is <use href> a viable replacement or designed for different purposes?
- Does hiding paths impact performance or page load, as all icons would be included in the HTML even if not displayed?