mirror of
https://github.com/zoriya/react-native-svg.git
synced 2026-06-06 00:12:21 +00:00
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)](https://github.com/software-mansion/react-native-svg/pull/1847/commits/5394bbbced6c1838e67240997cf4621d7f783197): - 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)](https://github.com/software-mansion/react-native-svg/pull/1847/commits/19bcb2464b3a40a47e4396ea66f49d6a95bfc9bc): Same as https://github.com/software-mansion/react-native-screens/pull/1624
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Horcrux.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the MIT-style license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
#import "RNSVGSvgViewModule.h"
|
||||
#import <React/RCTBridge.h>
|
||||
#import <React/RCTUIManager.h>
|
||||
#import <React/RCTUIManagerUtils.h>
|
||||
#import "RNSVGSvgView.h"
|
||||
|
||||
@implementation RNSVGSvgViewModule
|
||||
|
||||
RCT_EXPORT_MODULE()
|
||||
|
||||
@synthesize viewRegistry_DEPRECATED = _viewRegistry_DEPRECATED;
|
||||
@synthesize bridge = _bridge;
|
||||
|
||||
- (void)toDataURL:(nonnull NSNumber *)reactTag
|
||||
options:(NSDictionary *)options
|
||||
callback:(RCTResponseSenderBlock)callback
|
||||
attempt:(int)attempt
|
||||
{
|
||||
void (^block)(void) = ^{
|
||||
[self.viewRegistry_DEPRECATED addUIBlock:^(RCTViewRegistry *viewRegistry) {
|
||||
__kindof RNSVGPlatformView *view = [viewRegistry viewForReactTag:reactTag];
|
||||
NSString *b64;
|
||||
if ([view isKindOfClass:[RNSVGSvgView class]]) {
|
||||
RNSVGSvgView *svg = view;
|
||||
if (options == nil) {
|
||||
b64 = [svg getDataURL];
|
||||
} else {
|
||||
id width = [options objectForKey:@"width"];
|
||||
id height = [options objectForKey:@"height"];
|
||||
if (![width isKindOfClass:NSNumber.class] || ![height isKindOfClass:NSNumber.class]) {
|
||||
RCTLogError(@"Invalid width or height given to toDataURL");
|
||||
return;
|
||||
}
|
||||
NSNumber *w = width;
|
||||
NSInteger wi = (NSInteger)[w intValue];
|
||||
NSNumber *h = height;
|
||||
NSInteger hi = (NSInteger)[h intValue];
|
||||
|
||||
CGRect bounds = CGRectMake(0, 0, wi, hi);
|
||||
b64 = [svg getDataURLwithBounds:bounds];
|
||||
}
|
||||
} else {
|
||||
RCTLogError(@"Invalid svg returned from registry, expecting RNSVGSvgView, got: %@", view);
|
||||
return;
|
||||
}
|
||||
if (b64) {
|
||||
callback(@[ b64 ]);
|
||||
} else if (attempt < 1) {
|
||||
[self toDataURL:reactTag options:options callback:callback attempt:(attempt + 1)];
|
||||
} else {
|
||||
callback(@[]);
|
||||
}
|
||||
}];
|
||||
};
|
||||
if (self.bridge) {
|
||||
dispatch_async(RCTGetUIManagerQueue(), block);
|
||||
} else {
|
||||
dispatch_async(dispatch_get_main_queue(), block);
|
||||
}
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(toDataURL
|
||||
: (nonnull NSNumber *)reactTag options
|
||||
: (NSDictionary *)options callback
|
||||
: (RCTResponseSenderBlock)callback)
|
||||
{
|
||||
[self toDataURL:reactTag options:options callback:callback attempt:0];
|
||||
}
|
||||
|
||||
#ifdef RN_FABRIC_ENABLED
|
||||
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
||||
(const facebook::react::ObjCTurboModule::InitParams &)params
|
||||
{
|
||||
return std::make_shared<facebook::react::NativeSvgViewModuleSpecJSI>(params);
|
||||
}
|
||||
#endif
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user