235 Commits

Author SHA1 Message Date
SergeyYurkevich
832522d1c1 feat: implement mask-type property (#2152)
# Summary

"mask-type: alpha" is not supported. 
resolve issue: #1790  

## Explanation

svg example:
```
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100" fill="none">
<g clip-path="url(#clip0_8_3)">
<rect width="100" height="100" fill="white"/>
<mask id="mask0_8_3" mask-type="alpha" maskUnits="userSpaceOnUse" x="0" y="0" width="100" height="100">
<circle cx="50" cy="50" r="50" fill="#000000"/>
</mask>
<g mask="url(#mask0_8_3)">
<rect x="-26" y="-78" width="209" height="263" fill="#252E74"/>
</g>
</g>
<defs>
<clipPath id="clip0_8_3">
<rect width="100" height="100" fill="white"/>
</clipPath>
</defs>
</svg>
```

Current behavior: 

![image](https://github.com/software-mansion/react-native-svg/assets/17138397/2dca6f46-fe8f-48f3-80f9-799563911e8b)

Expected behavior:

![image](https://github.com/software-mansion/react-native-svg/assets/17138397/fb49cf0b-d677-491f-8215-9c9b1d69080f)

## Compatibility

| OS      | Implemented |
| ------- | :---------: |
| iOS     |        |
| Android |        |

## Checklist

<!-- Check completed item, when applicable, via: [X] -->

- [x] I have tested this on a device and a simulator
- [ ] I added documentation in `README.md`
- [x] I updated the typed files (typescript)
- [ ] I added a test for the API in the `__tests__` folder

---------

Co-authored-by: Sergey <s.yurkevich@logiclike.com>
Co-authored-by: Jakub Grzywacz <jakub.grzywacz@swmansion.com>
2024-07-05 15:11:48 +02:00
Jakub Grzywacz
9dc024702b fix: extract opacity use percentage values (#2325)
# Summary

According to the MDN Docs `opacity` as a `<alpha-value>` can be
represented as
> A [<number>](https://developer.mozilla.org/en-US/docs/Web/CSS/number)
in the range 0.0 to 1.0, inclusive, or a
[<percentage>](https://developer.mozilla.org/en-US/docs/Web/CSS/percentage)
in the range 0% to 100%, inclusive, representing the opacity of the
channel (that is, the value of its alpha channel). Any value outside the
interval, though valid, is clamped to the nearest limit in the range.

https://developer.mozilla.org/en-US/docs/Web/CSS/opacity#alpha-value

https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/fill-opacity#usage_notes
2024-07-05 10:48:03 +02:00
Bohdan Artiukhov
9faa387a98 Fix: image onLoad props (#2317)
# Summary
Change data structure returned from the Image onLoad event.
Add fix in new arch for returned sizes.

## Test Plan

Manually test in both architectures and platforms.

### What's required for testing (prerequisites)?

### What are the steps to reproduce (after prerequisites)?

## Compatibility

| OS      | Implemented |
| ------- | :---------: |
| iOS     |           |
| Android |        |

---------

Co-authored-by: Jakub Grzywacz <jakub.grzywacz@swmansion.com>
2024-07-02 09:33:25 +02:00
Bohdan Artiukhov
c0ee3e9ca0 add onLoad prop to Image component (#2293)
# Summary

Closes #1442

We want to add new props to the Image Component.

## Test Plan

Added the Test component. 
Manually test that in Android and IOS platforms on new and old
Architectures.

### What are the steps to reproduce (after prerequisites)?

## Compatibility

| OS      | Implemented |
| ------- | :---------: |
| iOS     |         |
| Android |         |
2024-06-27 16:10:28 +02:00
Bohdan Artiukhov
0596fd9dc5 fix: handle onPress prop on web app (#2302)
# Summary
Closes #1483
Closes #1524

We want a handle `onPress` prop on the web version in SVG components.

## Test Plan

Check on `react-native` web version if prop onPress works.

## Compatibility

| OS      | Implemented |
| ------- | :---------: |
| Web     |         |
2024-06-17 14:07:18 +02:00
Bohdan Artiukhov
8733bea10b Handle error when pass wrong uri to SvgFromUri component (#2289)
# Summary

We want to add the ability to handle the Error when someone passes the
wrong URI to the `SvgFromUri` component.

Closes #2148 

## Test Plan

Manual tests in TestsExample app. (Test2148)
2024-06-06 11:31:12 +02:00
Jakub Grzywacz
c0e5e58f9a fix: parsing < inside comments (#2277)
# Summary

When comment occurred before first tag, there was a possible parsing
error if it contains `<` character.
Here is an example (line 2 and 3 would cause separate errors):

```xml
<!-- sample rectangle -->
<!-- <sample rectangle -->
<!-- <sample> rectangle -->
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
  <rect width="100" height="100" x="50" y="50" fill="red" />
</svg>
```

Fixes #2276

## Test Plan

Manual tests in `TestsExample` app. (Test2276)
2024-05-16 12:46:24 +02:00
Fabrizio Cucci
19b2e42e1b fix: Fix static view config validation for RNSVGSvgViewAndroid (#2274)
# Summary

As per title, when running an app in bridgless mode the following redbox
appears:

> StaticViewConfigValidator: Invalid static view config for
'RNSVGSvgViewAndroid'.
> 
> 'validAttributes.borderBlockColor' is missing.
> 'validAttributes.borderBlockEndColor' is missing.
> 'validAttributes.borderBlockStartColor' is missing.
> 'validAttributes.borderEndEndRadius' is missing.
> 'validAttributes.borderEndStartRadius' is missing.
> 'validAttributes.borderStartEndRadius' is missing.
> 'validAttributes.borderStartStartRadius' is missing.

## Test Plan

Tested that the redbox is gone.

## Compatibility

| OS      | Implemented |
| ------- | :---------: |
| iOS     |    N/A     |
| Android |         |

## Checklist

<!-- Check completed item, when applicable, via: [X] -->

- [X] I have tested this on a device and a simulator
- [ ] I added documentation in `README.md`
- [ ] I updated the typed files (typescript)
- [ ] I added a test for the API in the `__tests__` folder

---------

Co-authored-by: Jakub Grzywacz <jakub.grzywacz@swmansion.com>
2024-05-15 11:13:01 +02:00
Jakub Grzywacz
3f9690d850 fix: imports and remove err export (#2270)
# Summary

* Removed `err` export from root file (ReactNativeSVG)
* replaced imports from `react-native-svg` with relative imports inside
`src` dir

Resolves #2265
2024-05-09 15:38:18 +02:00
Wojciech Lewicki
138c23ba10 feat: add needed changes for RN 0.74 (#2231)
PR bringing support for RN 0.74 and bridgeless mode to the library.
2024-03-05 12:52:09 +01:00
Wojciech Lewicki
50c2289a2c feat: use dynamics instead of strings for props that can be either string or number (#2238)
# Summary

PR bringing proper support for react-native-reanimated in the library by using dynamics instead of strings for props that can be either string or number.
2024-03-05 11:36:17 +01:00
Min
e6b2cc6496 fix: typo on require statement (WithLocalSvg) (#2223)
This PR fixes typo on require statement.
It makes WithLocalSvg function as expected without crash.
2024-02-26 09:18:27 +01:00
Quang
ff18553b71 fix(SvgCssUri): support rendering fallback instead of crashing the app when loading invalid content from remote svg file (#2196)
This crash issue still happening for SvgCssUri component
#1760

Apply similar fix #2071
specifically for SvgCSSUri component to prevent crashes due to invalid svg content

What issues does the pull request solve? Please tag them so that they will get automatically closed once the PR is merged
-> When loading invalid svg uri with SvgCSSUri, the app crashes crashes. For example the content returning html content instead of valid svg content.

How did you implement the solution?
-> Passing the fallback prop to SvgCss where will be render when error being captured

What areas of the library does it impact?
SvgCss and SvgUriCss
2024-01-03 16:48:28 +01:00
Renaud Chaput
d1b02154e3 Fix type exports and enable isolatedModules setting to catch it in tsc (#2197)
This is the same issue as the one I fixed last year in #1874

To prevent this from occurring again, I enabled the `isolatedModules`
Typescript option which will warn when you export non-value identifiers
without using `export type`, which will break transpilation processes
what work on a per-file basis. You can find a [more detailed explanation
in Typescript's
doc](https://www.typescriptlang.org/tsconfig#isolatedModules)
2024-01-03 15:46:49 +01:00
Frank Oppervoll-Larsen
3939e2059e fix: Correct types for transformsArrayToProps (#2193)
# Summary

Type TransformsStyle['transform'] has `string` added as option from
React Native 0.71.9 onward. This breaks the logic of
`transformsArrayToProps`.

This PR fixes this by scoping down the type, and removing a no-op usage
of `transformsArrayToProps`

This has been broken for a while, but not immediately visible to users
compiling using Metro and Babel.
2024-01-03 12:41:03 +01:00
Maxwell You
fc1c9a0347 fix SvgCssUri fallback (#2171)
When using the `SvgCssUri` component, specifying `fallback` does not render the fallback component in the case of an error.

I looked through the source code and believe this is because `SvgCssUri` is rendering a `SvgCss`
22f47333fb/src/css.tsx (L712)
which renders a `SvgAst`
22f47333fb/src/css.tsx (L696)
which does not use the `fallback` prop
22f47333fb/src/xml.tsx (L96)

This fix copies over the error handling code from `SvgUri` so `SvgCssUri` can render a fallback on error. I tested the change in my project and this works like it does for `SvgUri`

Co-authored-by: Maxwell You <maxwell.you@mlb.com>
2023-11-15 16:59:40 +01:00
Wojciech Lewicki
2b030dda12 feat: move css to different package for size reduction (#2168)
PR based on https://github.com/software-mansion/react-native-svg/pull/1452 extracting `css` related components to different package to reduce the size of the package.

***THIS IS A BREAKING CHANGE***. From now on, you should import 
```
  SvgCss,
  SvgCssUri,
  SvgWithCss,
  SvgWithCssUri,
  inlineStyles,
  LocalSvg,
  WithLocalSvg,
  loadLocalRawResource,
  ```
 from `react-native-svg/css` package instead.
2023-11-14 11:50:03 +01:00
Wojciech Lewicki
5f3bd0dd48 fix: error for transform on web (#2139)
Setting transform prop of svg component as undefined in web causes the error, so we set it only if the parsed value is not undefined.
2023-09-20 16:14:35 +02:00
Wojciech Lewicki
416ccc8a86 fix: bump packages, eslint, tsconfig, prettier and resolve all conflicts (#2114)
PR bumping packages, eslint, tsconfig, prettier and resolving all conflicts connected to it.
2023-08-07 17:44:58 +02:00
_nderscore
34243a53af fix: make web platform types compatible with native types (#2091)
When react-native-svg is used on a web project where typescript has been configured to resolve .web.ts file exentions before .ts, it causes type errors.

This is because the types in ReactNativeSVG.web.ts don't match the types in ReactNativeSVG.ts

This PR adds the correct types for all exported components on the web platform inside ReactNativeSVG.web.ts. Those changes are:

Added missing prop types for all components
Made style, gradientTransform, and patternTransform optional in BaseProps.
Made styleProp argument of resolve() undefined-able, to support style being an optional prop (this also seems to follow the logic in the body of the function, which has a check for a truthy styleProp)
These changes only impact types - no functionality has been changed.
2023-07-22 16:53:39 +02:00
Wojciech Lewicki
54bec6d10c chore: run prettier and lint (#2087) 2023-07-06 14:41:36 +02:00
PiotrWszolek
e04a159b46 Prevents SvgUri crashes when uri is invalid and adds fallback prop (#2071)
SvgUri component crashes an app when uri with SVG returns html or some other content instead of valid SVG.
This change prevents those crashes and adds fallback property of JSX type which is rendered instead of corrupted SVG.
2023-07-06 14:21:20 +02:00
Christoph Pader
7983ad2ca6 Add web support for toDataURL() on svg tags (#2072)
Implement web support for Svg.toDataURL()

Continuing the work of @wood1986 from #1361

Co-authored-by: wood1986 <5212215+wood1986@users.noreply.github.com>
2023-07-06 00:19:18 +02:00
Jan
98fdeaa516 fix: remove deprecated import from react-native-web (#2027)
fix: remove deprecated import from react-native-web
2023-05-24 13:03:34 +02:00
fauri13
98836149db Fix setNativeProps type (#2058)
Setting Object to the props type does not allow passing any object to the method. So in TypeScript when using the method for example with Path, you will get an error when passing the d. Instead ob Object, you should use at least P so it allows the original props of the component you are using.
2023-05-23 12:28:08 +02:00
Ganapathy S
11edfc4a71 fix: read svg file on android (#2016)
PR fixing the behavior for fetching local files in `SvgUri` on `Android`.

Co-authored-by: Wojciech Lewicki <wojciech.lewicki@swmansion.com>
2023-03-30 17:00:53 +02:00
Wojciech Lewicki
a917baf2ec fix: remove unnecessary methods provided by superclasses for accessibility (#2013)
PR removing methods that seem unnecessary for the accessibility props since they are provided by superclasses. It was necessary to update fabric updateProps method though, since we don't call super there. Also added proper type definitions and removed unnecessary casts.
2023-03-27 15:18:59 +02:00
Umesh Patidar
baf011fbf1 fix: make image props extend CommonPathProps (#2007)
Since `Image` can use all the props provided by `CommonPathProps` type, we extend it with adding image-specific props.

Co-authored-by: Umesh Dangrecha <umeshdangrecha240@gmail.com>
Co-authored-by: Wojciech Lewicki <wojciech.lewicki@swmansion.com>
2023-03-24 12:23:16 +01:00
Michal
63bf27006b Fix SVG parsing when the closing tags include whitespace (#2012)
Allows whitespace to be used in the closing tag.
2023-03-23 19:59:07 +01:00
toter3
d03604797f add accessibility and testId props and virtual nodes to support e2e tests tools (#2001)
PR adding accessibility and testId props and virtual nodes to support e2e tests tools and provide general accessibility support.

Co-authored-by: yonatan.altaraz <yonatan.altaraz@khealth.ai>
Co-authored-by: galkahana <gal.kahana@hotmail.com>
2023-03-22 13:01:09 +01:00
Tommy Nguyen
f2f533a292 chore: ensure consistent use of import type (#1971)
We hit issues when building react-native-svg because of inconsistent use of import type. This change adds ESLint result to ensure consistency. I also ran yarn lint --fix to fix up the code.
2023-01-31 14:12:21 +01:00
Marlene Cota
a118c879c7 feat(windows): add example app (#1937)
Added example app for Windows
2023-01-09 12:48:20 +01:00
Mantas
76b742c24a fix: Call onLoad prop in SvgCssUri (#1927)
SvgCssUri calls onLoad if it's passed, just like in PR #1817 (with SvgUri).
2023-01-04 16:18:14 +01:00
Wojciech Lewicki
60fcb0f28f fix: restore black as a default color for svgs (#1947)
PR fixing setting of fill color to black when none is provided to the svg.
2023-01-03 21:15:09 +01:00
Wojciech Lewicki
c1808c0978 fix: accept comas in viewbox (#1952)
PR adding support for comas in viewbox property, replacing them with space.
2023-01-03 19:56:54 +01:00
Carl MONNERA
b3dc36ab01 refactor: remove circular dependency (#1929)
When importing the library we have a lot of React Native warning about circular dependency. This PR aims to fix those warnings.

Co-authored-by: Carl-MONNERA <cmonnera@regate.io>
2022-12-08 16:49:30 +01:00
Wojciech Lewicki
05c33d3245 chore: align examples (#1914)
PR aligning example apps and fixing one bug. On web, if you don't pass a color to the elements, they are rendered with `black` fill. We recreate this behavior in examples, but maybe it should be the default behavior if `fill` is `undefined`. It would probably need to be changed on the native side somehow.

Another fix is to parse `fill` prop in `setNativeProps` since Fabric support has been added and `fill`  prop structure has been changed, it cannot be handled on the native side on `Android` due to complying to interfaces.
Another fix is passing `transform` prop in `Svg` to `G`  when it is not `react-native` style `transform` prop so it is always applied. It creates a problem mentioned in the comment in the code and should be addressed in later PRs.
2022-11-15 12:38:40 +01:00
Wojciech Lewicki
1126079425 feat: use codegenNativeComponent to import native views (#1847)
Changed `requireNativeComponent` to `codegenNativeComponent` so that upcoming changes (Static View Configs, Bridgeless Mode and idk what more) in `react-native` are available in the library. Also, types and native components are now taken directly from `fabric` folder to make sure the values passed to the native components are the ones defined in props. It should work on all supported versions since `codegenNativeComponent` function exists from RN v. 0.61.0. Suggested by @RSNara and @cipolleschi

Reason for [`5394bbb` (#1847)](5394bbbced): 
- on `Paper`, `Animated` uses `setNativeProps` method when we set `useNativeDriver`  to `false`, and does not rerender the component. Therefore, new transform lands only in `SvgView` and is parsed in `RCTViewManager.m` .
- on `Fabric`, the same code makes the components rerender. Due to this, information about new transform is passed to the `SvgView` child: `G` , making it apply translations from the transform in its `updateProps` method.
- other than `Animated` use-case, on both archs, if we just passed `transform` prop to `Svg` component, it would end up in double transformations now as well. All of those changes are due to https://github.com/software-mansion/react-native-svg/pull/1895, which added proper parsing of RN style `transform` prop (array of transformations objects) therefore making `G` properly handle `transform` prop passed from `Svg`.

Reason for [`19bcb24` (#1847)](19bcb2464b): Same as https://github.com/software-mansion/react-native-screens/pull/1624
2022-11-03 15:47:29 +01:00
Wojciech Lewicki
6a5242f00b fix: proper transform prop handling (#1895)
PR aligning handling of transform prop between web and native and adding proper handling of transform prop on web.

origin prop is now treated as transform-origin since it seems like the current behavior of this prop on native platforms.
transform prop cannot be an object with transform properties since it does not provide order of transformations which would lead to undefined behavior so this option has been removed.
RN style of transform prop (array of rotation objects) can now be used
font-size on web seems to be 16px by default and it is 12 in library - maybe we should align it somehow
removed maskTransform prop since it does not exist in SVG standard and did nothing on native side
fixed typo in Fabric Pattern updateProps method
2022-10-25 15:55:17 +02:00
Denis Slávik
65f373b084 fix: [types] allow Image "href" & "xlinkHref" to be strings (#1896)
Allow 'string' type for href and xlinkHref props in Image component.
Logic in the component allows passing 'string' values to the Image.resolveAssetSource by checking for its type, so it would be nice to reflect it on the properties types as well.
2022-10-21 14:43:37 +02:00
Robert Sasak
dadf0c975e Web: Support for transform prop (#1835)
Transform prop is ignored on web. This PR stringify transform props so it is compatible with web SVG. It is an extension of #1824
2022-10-18 12:02:17 +02:00
Lenz Weber-Tronic
afaf500db9 make reanimated work in web (#1886)
Up until now, trying to use reanimated with react-native-svg in react-native-web resulted in an error.
This adds a setNativeProps function to the web implementation to directly modify the transform and style props on a SVGElement ref.

Since there is a need to track the "last merged props" and those need to be reset on every render, the render method has been moved into the WebShape class and a tag string property has been added.

As g had some extra handling for x and y, a prepareProps function was added as well.
2022-10-13 14:07:20 +02:00
Wojciech Lewicki
b3d2b76496 feat: remove processColor so the color is parsed on the native side (#1887)
PR fixing the behavior of setting color and tintColor values on SvgView component. Since we added proper annotations and macros on the native side, we don't have to call processColor on the JS side.
2022-10-03 09:48:16 +02:00
Wojciech Lewicki
1beacf1be2 feat: make pointer events work on both platforms (#1879)
PR adding proper handling of pointerEvents in the library. Based on the comment by @RSNara :

> For all Android components, static ViewConfigs are generated from the components types + BaseVIewConfig.android.js. The BaseViewConfig for all android components is facebook/react-native@c5217f1/Libraries/NativeComponent/BaseViewConfig.android.js#L288-L297, which doesn't contain the pointerEvents validAttribute. So, React Native SVG components must re-declare the pointerEvents validAttribute. Otherwise, the pointerEvents validAttribute won't be in the static ViewConfig, which means React won't pass that prop to native with Static ViewConfigs.

We needed a way to trick codegen since we added our own pointerEvents with string type, and such prop is already present in ViewProps from react-native, but as a union of strings, so their types did not match. As codegen parses string literals, we include changed ViewProps type, without pointerEvents prop, so we satisfy both codegen and TS.

As for how pointerEvents are handled now:

iOS:
Paper: prop is passed in RCTViewManager but it looks like it does not matter what you pass there, since even if you set none, resulting in view.userInteractionEnabled = NO; (facebook/react-native@065db68/React/Views/RCTViewManager.m#L256), custom implementation of hitTest will not take it into account anyhow.

Fabric: we don’t use pointerEvents prop but it is not a problem since it is not used in the implementation of neither SvgView and RCTViewComponentView (it is used in hitTest method there, but we override it)

Android:
Paper: the prop was passed through ReactViewManager and was set on the view directly. But the setter for pointerEvents is not visible outside of ReactViewGroup . After adding Fabric integration for both platforms: BaseViewManager does not implement setter for pointerEvents so we have to add the prop by ourselves. Still, we cannot call the setter from ReactViewGroup since it is not visible, so all the logic for pointerEvents should be migrated to SvgView unfortunately or we should use reflection to get that method.

Despite that, it seems that behavior differs on Android and iOS since setting pointerEvents to none on Android makes the view not clickable, whereas on iOS it does not change anything.
2022-09-30 14:24:34 +02:00
Renaud Chaput
f7343013be fix: use export type when exporting types (#1874)
If you built your app using `esbuild` (for example using [`@rnx-kit/metro-serializer-esbuild`](https://microsoft.github.io/rnx-kit/docs/tools/metro-serializer-esbuild)), then it complains that the imports do not exist for type imports.

Marking them explicitely as types fixes this issue, in addition to removing those import/exports from non-TS builds.

I also transformed them from an import + export statements to an `export … from …` statement.
2022-09-27 17:48:26 +02:00
Wojciech Lewicki
3a04189df8 fix: reanimated on old architecture (#1869)
PR adding option for `Double` for all props that were of type `NumberProp` on `Android` so `reanimated` works the same as before on old architecture. It also introduces the change of how `fill` and `stroke` should be constructed for options not going through `render` method, e.g. `react-native-reanimated`. An example of how to handle it can be seen in the provided example: https://github.com/react-native-svg/react-native-svg/pull/1869/files#diff-76a76277daf14518270e8aea8a5e9358a8215d7e4276d2e5f1c4fe95107cdc20
2022-09-15 12:08:57 +02:00
Wojciech Lewicki
ee6e5da195 feat: transfer specs to ts and remove unnecessary props (#1865)
PR transfering Fabric specs to TS, removing unnecessary @ReactProp adnotations, unifying inheritance hierarchy across platforms, adding custom types for color and image source props so they are parsed by react-native.
2022-09-14 12:01:35 +02:00
Wojciech Lewicki
98c14b4f45 chore: add CI for JS, iOS and Android formatting (#1782)
Added CI workflow and local pre-commit hook for formatting and linting the newly added JS, iOS and Android code.
2022-08-16 12:00:32 +02:00
Wojciech Lewicki
8f1bda4856 feat: add Fabric on iOS without ComponentViews (#1821)
Version of #1754 without usage of ComponentViews. It seems like a more proper way, but introduces the necessity of clearing whole state of each component on recycling for it not to be used when view is recycled.

Still known problems:

We stringify props of type NumberProp since codegen only accepts props of a single type. It is the fastest way of dealing with it, but maybe there could be a better way to handle it.
Image resolving should be probably handled the same as in RN core
SvgView needs to set opaque = NO so it is does not have black background (it comes from the fact that RCTViewComponentView overrides backgroundColor setter which in turn somehow messes with the view being opaque). All other svg components do it already so maybe it is not such an issue.
transform prop won't work when set natively since it is not parsed in Fabric
2022-08-11 14:08:11 +02:00
Robert Sasak
4062c2e2b8 Add support for translateX and translateY on web (#1825)
Props translateX and translateY are ignored on web. This PR is addressing it the same way as it is already done for originX and originY.

This PR is not addressing the issue of transform as stated in issue.
2022-08-10 10:27:35 +02:00