Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle SVG sprite #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Handle SVG sprite #32

wants to merge 2 commits into from

Conversation

sgruhier
Copy link

Thanks for this great component. I use it to load a bunch of icons. To minimize HTTP requests, I made an SVG sprite. This PR handle that.
Any feedback are welcome.

Copy link
Owner

@czeckd czeckd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! This pull request looks promising, but it needs a few additional changes.

First, it will need modifications to svg-icon.component.ts to pass the symbolID parameter to the service.

Second, the rollup.config.js will need to have the additional rxjs operations added to it so the module can be built successfully for distribution.

Third, there are some code quirks I would like to see resolved that I will mark in the code at the line they occur.

Third, I am in the process of converting to HttpClient (see the ng5 branch), so you may wish to work from that branch or wait until I merge into master (sometime in December).

import 'rxjs/add/observable/of';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/finally';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/share';
import 'rxjs/add/operator/mergeMap';
import 'rxjs/add/operator/delay';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add additional rxjs/operators to rollup.config.js.


loadSvg(url:string, symbolID:string = ''): Observable<SVGElement> {
const orgUrl = url;
url += `#${symbolID}`;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds # to all Urls whether or not they are using sprite symbols.

I would prefer that the original Url is kept as url and the sprite or symbol Url is changed, so something like:

const symbolUrl = url + `#${symbol}`;

@@ -19,28 +20,45 @@ export class SvgIconRegistryService {
constructor(private http:Http) {
}

loadSvg(url:string): Observable<SVGElement> {

loadSvg(url:string, symbolID:string = ''): Observable<SVGElement> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer symbol rather than symbolID.

const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
const spriteElement: SVGSVGElement = svg.querySelector(`#${symbolID}`);
svgElement.setAttribute('width', spriteElement.viewBox.baseVal.width + 'px');
svgElement.setAttribute('height', spriteElement.viewBox.baseVal.height + 'px');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting width and height circumvents width/height styling with CSS. So, please remove these two lines of code.

If setting width and height is desirable, then maybe an additional parameter in the component to instruct setting the viewbox width and height as the size.

const spriteElement: SVGSVGElement = svg.querySelector(`#${symbolID}`);
svgElement.setAttribute('width', spriteElement.viewBox.baseVal.width + 'px');
svgElement.setAttribute('height', spriteElement.viewBox.baseVal.height + 'px');
svgElement.setAttribute('viewBox', spriteElement.getAttribute('viewBox'));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tab indent please.

@czeckd
Copy link
Owner

czeckd commented Nov 28, 2017

Also, unsure how unloadSvg(url:string) should work with sprites. If I understand your code, it looks as the cache is the svg file with sprites and then by individual sprite. The unload then should remove all cached sprites if the main svg is unloaded.

// Extract sprite
getSprite(svg: SVGElement, symbolID: string) {
const svgElement = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
const spriteElement: SVGSVGElement = svg.querySelector(`#${symbolID}`);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had difficulty getting this to build without explicit casting:

		const spriteElement:SVGSVGElement = <SVGSVGElement>svg.querySelector(`#${symbolID}`);

@katyapavlenko
Copy link

Hello! How's it going?
This PR seems helpful for me

@czeckd
Copy link
Owner

czeckd commented Sep 26, 2019

@katyapavlenko - the PR would need to be updated to resolve the conflicts. I do not have the time to do it myself.

@fynnfeldpausch
Copy link

@czeckd Any change to bring this into a version? Loading SVG sprites would be a great addition.

@czeckd
Copy link
Owner

czeckd commented Feb 12, 2021

@fynnfeldpausch - No change, it would take some work to refactor to merge into master. At this point, my guess is that it would largely be a rewrite and I do not have the time to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants