software-mansion / react-native-svg

SVG library for React Native, React Native Web, and plain React web projects.
MIT License
7.52k stars 1.14k forks source link

**feat**: provide svg props in onload #2540

Open burnes opened 1 week ago

burnes commented 1 week ago

Summary

The onLoad handler is called when the SVG is loaded, but does not provide any information about the SVG nor reference it. In this change, the onLoad handler is not called with the SvgProps of the loaded SVG (e.g. height, width, viewBox etc) - so basically the attributes of the root <svg>tag.

The SvgProps are now provided to the onLoad handler of the SvgUri and SvgCssUri components. Then we can react on different aspects of the SVG when loaded.

Background: Had an issue with SVGs not scaling correctly when changing the height of the image. With the handler I could fix my problem on my app side. I can imagine other use cases for this feature and decided to contribute this change here.

This PR does not fix the issue I had, but adds the feature for the onLoad handler. Any ideas to fix the issue itself are warmly welcome. The issue is described in the PR's test case.

Other considerations in this PR:

Test Plan

The added test showcase uses the onLoad handler to calculate the required with to keep the aspect ratio. It is tested on the SvgUri and the SvgCssUri component.

Screenshot of the test example (Android):

SvgUri on Android

What's required for testing (prerequisites)?

XCode / Android studio

Open the UI test

What are the steps to reproduce (after prerequisites)?

Just open the test example

Compatibility

OS Implemented
iOS ✅❌
MacOS ✅❌
Android ✅❌
Web ✅❌

Checklist