-
Notifications
You must be signed in to change notification settings - Fork 1
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
Disable viewBox removal in SVGO #33
Disable viewBox removal in SVGO #33
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ampersarnie thanks for looking into this! There were a couple of minor changes required to get it working.
The proper syntax for the svgoConfig
was a bit hard to track down!
The order of the loaders matters because url-loader
transforms files into Base64 string, so we're currently importing SVGs like so:
import { ReactComponent as MailIcon } from '../static/images/mail-icon.svg';
Without url-loader
being last, the above code will produce an error and the correct way to import an SVG would become:
import MailIcon from '../static/images/mail-icon.svg';
We're doing a bit of a dance by converting SVG to a string and back into a React component, but I assume there's a reason for it. Using the Base64 string as an img src
maybe? SVGR take a different approach to this which might be worth bearing in mind for future and enables the simpler import syntax.
Anyway this works perfectly.
Tested in dev and prod modes
- SVG imports continue to work as expected
- ViewBox attribute remains present on an SVG if width and height values match it
@g-elwell That's actually really interesting to know. I wasn't expecting the position change to have that kind of impact. How would you prefer that to work, because I can move it around if it's going to be too much of an inconvenience? My option from what you've described is to leave this as it is as this reads a lot better; import MailIcon from '../static/images/mail-icon.svg'; As for the backward and forwards conversion, that actually wasn't something that had come up. We needed the option to implement SVGs on a component level and this is how it was implemented without any insight on the side effect. It's possibly something that needs investigating further to avoid that conversion. So thanks for the resources on that. |
The reason the order matters (and apologies as I skipped over this) is because if
I also prefer this syntax, but in order to enable it we'd have to introduce a breaking change since existing SVG imports using the other syntax will stop working and there are a few projects that I know of which are already using them. That said it would be a very simple fix to get them working again. As it stands this PR will fix the |
Thanks for the clarification @g-elwell!
As we're not out of RC we should expect a degree of breaking change, so not overly concerned about that at the moment. Just a risk we have.
That would be much appreciated! |
Description
Fixes #32 - SVG's can have
viewBox
stripped out of them which will cause the appearance of cropping when the SVGs are displayed. This was caused by SVGO which is used by SVGR for optimisation. By default SVGO will strip out any instances ofviewBox
causing the cropping effect. To prevent this from happening we can disable the feature in the SVGR config.Change Log
viewBox
removal in SVGO.