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

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

burnes
Copy link

@burnes burnes commented Nov 18, 2024

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:

  • using useMemo or UseEffect in conditionals/try-catch is not recommended, so restyled this code structure.

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

  • I have tested this on a device and a simulator
  • I added documentation in USAGE.md
  • I updated the typed files (typescript)
  • I added a test for the API in the __tests__ folder

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:
- using useMemo or UseEffect in conditionals/try-catch is not recommended, so restyled this code structure.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant