-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
Add forward ref to React SVG Component #5457
Conversation
Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed. If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks! |
This looks good. I think we just need to get the tests passing. |
Okay. Then, I’ll try to figure out how to perform the tests and fix if there are any problems. |
There was a need to update Jest file transform for SVG to include ref via svgRef prop. Also some additional fixes were necessary. I still was not able to get |
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.
We shouldn't need a separate prop for this. Instead there should be a way to tell svgr to use React.forwardRef
which does the same thing directly for the ref
attribute.
Adding “ref” option to SVGR webpack config does add forwardRef to the SVG component. However, the prop that is being used to pass ref is |
I don't quite understand what it means. The sole purpose of |
I think |
To be completely honest, I dont quiet understand it either. I tried to use I might be missing something here and I’ll investigate to see what the issue might be. |
Where does svgr use |
This is the template directory for SVGR Transform: https://github.com/smooth-code/svgr/tree/master/packages/core/src/templates util.js is where forwardRef is implemented + the reactDOMTemplate.js for the main template. |
Apparently it was added specifically for a library called @neoziro Can we make the solution generic so it works with ReactDOM? Ideally it's |
@gaearon the latest version of SVGR supports You can try it here in https://svgr.now.sh/. |
@gaearon if you want to upgrade SVGR, you should probably wait for the next version. I will change a lot of things:
So it will result by a huge performance improvement and a smaller package size. I plan to release it in one or two weeks. |
I completely forgot about checking what version the ref was added to SVGR and got confused why ref was not working :/ Upgrading locally to v3.1.0 worked fine with. I modified the test files to use refs instead of svgRef and added forward ref to the SVG transform for Jest. However, the packages are not upgraded; so, the tests are failing (locally everything works as expected). |
@neoziro This is good to know. I was considering updating CRA to use SVGR 3 but now I'll wait for the new version to be released. Thanks! |
This pull request has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs. |
@iansu any update? |
There's a PR to upgrade to SVGR 4: #5816. I think it makes sense to merge that first and then update and merge this branch. |
@iansu Any update on this? |
@GasimGasimzada Now that SVGR has been upgraded you should be able to update this branch. A few things have changed, for example our webpack config is now in a single file instead of separate dev and prod files. Once your branch is updated and everything is working we should be able to merge this. |
@iansu Great! I will make my changes by tomorrow then :) |
I have updated SVGR configuration in the new webpack config. |
Thanks! |
This pull request adds "ref" option to SVGR loader; so that, the root
svg
component ref can be accessed. The way it works is that SVGR creates a forward ref and passed the ref to svg component usingsvgRef
prop.Example:
This example will print the svg element.
Testing
I have added unit tests to webpack/SVGComponent.js; however, I was not able to perform the test.
yarn e2e:docker -- --test-suite kitchensin
kept giving me permission denied error inside the container.