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

Extract true vs false inconsistencies #123

Open
albertogasparin opened this issue May 16, 2017 · 14 comments
Open

Extract true vs false inconsistencies #123

albertogasparin opened this issue May 16, 2017 · 14 comments
Assignees
Milestone

Comments

@albertogasparin
Copy link
Contributor

Currently, I'm using v2.1.0 and my code looks like:

import icon from './icon.svg';
// ...
<svg viewBox={icon.viewBox}>
    <use xlinkHref={`#${icon.id}`} />
</svg>

This works perfectly when extract: false.
However, whenever I set extract: true, icon is now a string. I could check the type of icon but the problem is that viewBox is no longer available.
May I ask you why? Or am I using the loader in the wrong way?

PS: I read that providing a custom runtimeGenerator could solve my issue, however sounds a bit overkill, especially because I might need to copy-paste all default generator code, just without an if statement.

@kisenka
Copy link
Contributor

kisenka commented May 16, 2017

The main goal for extract mode is import images from CSS/HTML:

.logo {background-image: url(image.svg)}

/* compiled to */
.logo {background-image: url(sprite.svg#image-usage)}

In this case you don't need any data from image expect it's final URL. But seems like users want to use external sprites from JS too. It's a feature request.

@kisenka kisenka self-assigned this May 16, 2017
@kisenka
Copy link
Contributor

kisenka commented May 21, 2017

@albertogasparin please check svg-sprite-loader@3.0.0.

@albertogasparin
Copy link
Contributor Author

I'm sorry to say that it is not yet working. Looks like generateExport expects a string, and as you are passing an object with a custom toString implementation, it returns the exact same output as v2.

@albertogasparin
Copy link
Contributor Author

On top of that, the id returned from the extract: true mode is different (looks like it appends -usage to the original id), making it harder to use it dynamically.

@kisenka
Copy link
Contributor

kisenka commented May 22, 2017

@albertogasparin oh shi~, thanks for reporting, fix it asap

@kisenka kisenka reopened this May 22, 2017
@kisenka
Copy link
Contributor

kisenka commented May 22, 2017

Fixed in svg-sprite-loader@3.0.1, please check

@kisenka
Copy link
Contributor

kisenka commented May 22, 2017

@albertogasparin
Copy link
Contributor Author

Great, version 3.0.1 correctly set the viewbox attribute, however the id is still problematic.
The issue with the current output is that icons do not work if the svg is inlined in the page (technique used to support all browsers).

What happens is that the svg generated has symbol with the original id and use tags with the -usage id. However, the style inside the svg hides all use#xyz-usage elements so the icons do not appear as the imported id refers to the hidden use tag, not the symbol.

  • svg <symbol id="icon" /> is correct
  • svg <use id="icon-usage" /> has display none
  • import icon returns id=icon-usage when extract mode is true, which is hidden

I could replace the -usage in the id string in my own components (or add a css that forces the sprite-symbol-usage display) , but I still think that id should be consistent regardless of the extract flag, and the -usage suffix should only be added on the url property (where it makes sense).

@kisenka
Copy link
Contributor

kisenka commented May 24, 2017

@albertogasparin I can suggest to add useId property with right value, I will be ok for you?

@kisenka
Copy link
Contributor

kisenka commented May 24, 2017

Ah, I see what you talking about. External sprite which will be injected in the page should not contain any styles at all.

@kisenka kisenka reopened this May 24, 2017
@albertogasparin
Copy link
Contributor Author

I have nothing against a useId property, but I would personally try to limit the API surface in order to reduce the confusion for the consumers.
Why don't you use id and url for the two scenarios:

  1. People who want to use svg externally should use url property on the use tag. That will be ./path/sprite.svg#icon-usage when extract is true, otherwise just icon

  2. People who want to use svg injected in the page should use id property on the use tag. That will always be icon, regardless of extract flag.

What do you think? Are there any other scenarios?

PS: I'm just trying to help you out. At the end of the day this is your work, so feel free to ignore my suggestions, as you already did a great job with this loader 😉

@kisenka
Copy link
Contributor

kisenka commented May 27, 2017

Why don't you use id and url for the two scenarios...

I am almost agreed, here is my suggestion with a few updates:

  1. id should be always an symbol id in the sprite. It could be used to generate reference in runtime mode, but it's not necessary.
  2. url is always presented, and it's mode dependent:
    • #icon in runtime mode.
    • sprite.svg#icon-usage in extract mode.
  3. viewBox, width and height are always presented.
  4. toString() is also always presented.

In this case runtime & extract mode differs only in used JS wrapped code.

PS: I'm just trying to help you out. At the end of the day this is your work, so feel free to ignore my suggestions, as you already did a great job with this loader 😉

Wrong, your thoughts is important for me, as other useful thoughts from my users :)

@albertogasparin
Copy link
Contributor Author

☝️Sounds great!

@jburghardt
Copy link

why does the id of the icons differ in the mode ?

should it not generate the same id ?

#icon in runtime mode.
#sprite.svg#icon in extract mode.

having the sprite-loader add the -usage string to every id is very confusing

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

No branches or pull requests

3 participants