-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Consider using SVGR to simplify SVG handling #14628
Comments
Extracting SVG icons in #14743 might help to explore how this could be achieved as all SVG based icons would be located in their own files making it easier to reason how to convert them to the current form from the commonly known file format. |
@gziolo Any updates or new idea/approaches for this one? How can I help it along? We're looking at improving how social icons uses and manages SVG, right now it requires duplication on PHP and JS sides, which we would be better if we don't need to. |
I didn't perform any further exploration. I documented all my findings above. Feel free to check other options, as well. I'm happy to discuss all the technical options.
I saw those efforts. I think the biggest challenge there is how to store SVG with the Originally, I was mostly concerned about all SVG icons we use with UI components rendered in the block editor and I didn't consider putting them in the post content. However, it looks like we should cover both cases to make it work as intended. |
For social links the Also, using The downside for using render_callback was all the SVGs had to be loadable via PHP, and at that time, not included as their own svg files, thus the "weird" include and definition within PHP. Now, @jasmussen is exploring a solution to use CSS background which would allow using individual SVG files. This would actually solve the server side rendering part too, since SVG markup would not be included in the |
I took a new approach that would allow us to use completely external SVG files. You can see it here: https://codepen.io/joen/pen/XWWXvBL All of that works and is functional, and we can update Social Links to use this method instead, and it would solve the problems Marcus is outlining. The only missing pieces is icons in the block library: It would be so nice if those icons could use the same SVG files as we load using the above codepen-outlined method. |
Can you elaborate what are the benefits of inlining SVG files into JavaScript code rather than offer a way to use CSS-based icons fir blocks as well? The benefit is that you wouldn’t have to download the same icon twice. The drawback is that you probably can’t use HTML based queries for SVG tags. |
There is a related discussion on how to make it possible to inline SVGs and other assets in create-react-app: facebook/create-react-app#3722. They are seeking for the solution which doesn’t require to rewrite Webpack config each time you want to register a new asset type and rather provide ways which scale well with Babel. There is still no decision made as it’s a complex issue to solve. They are talking Babel macros which I’m exploring in #16088 for the block.json files. The biggest challenge is that it breaks development workflows because all tooling assumes that you operate on JS files so in both cases JSON and SVG, the inlined files get cached and Babel don’t clear the cache when those files change. This need to be solved on Babel side first, the only known workaround so far is to disable caching for the files which import those assets... The existing integration for SVG in create-react-app is done with webpack: facebook/create-react-app#3718. |
To be clear I'm not personally suggesting any specific technical implementation. To me, the goal here isn't SVGR. The specific challenge I'm pondering for Social Links is: how do we reduce duplicate code? If you look at https://codepen.io/joen/pen/XWWXvBL, you'll see that each logo is referenced like so:
https://cldup.com/w4j97g2E1Y.svg is an actual URL, and as you see it's just a black SVG. The CSS in the codepen re-colors that SVG to be the color of the specific brand. This already works in the editor and the frontend. The only missing piece is showing those same icons in the block library. And I can't think of a simple way to use an external URL such as https://cldup.com/w4j97g2E1Y.svg to change icons in the block library, and re-color them, without using something like SVGR. But that's the goal and if there are other options, that's cool. I would personally LOVE if we could do this:
That would solve it 🤘 |
I’m sharing a related article about cross-platform SVG icons: |
Can you clarify if this is satisfied by #18243 ? |
It depends on how you look at it. It is implemented only for 3rd party projects that use Regardless, it would be nice to add an additional layer that transforms SVG files into cross-platform components from |
This idea comes from the chat with @mor10 in #14590 where he shared how one can extend default webpack config with custom loaders. It sparked my interest as we still can improve our workflow around using SVG files. To bridge ReactDOM and React Native we introduce wrapper component for all used SVG tag types in #9685. However, it would be great to be able to use regular SVG files without any manual modification and let Babel (to work with npm packages out of the box in contrast to webpack) do the trick.
My initial reaction from #14590 (comment):
This is what @mor10 shared in #14590 (comment):
I did a quick check on the SVGR monorepo and found some useful packages which could work as a good example of how we could customize things:
The text was updated successfully, but these errors were encountered: