Skip to content

Commit c7b0311

Browse files
Kacper ŻółkiewskiKacper Żółkiewski
authored andcommitted
feat: add onSvgLayout event to native components
1 parent 613170f commit c7b0311

19 files changed

+166
-12
lines changed

android/src/main/java/com/horcrux/svg/RenderableView.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import android.graphics.RectF;
2323
import android.graphics.Region;
2424
import android.view.ViewParent;
25+
import com.facebook.react.bridge.Arguments;
2526
import com.facebook.react.bridge.ColorPropConverter;
2627
import com.facebook.react.bridge.Dynamic;
2728
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
@@ -30,7 +31,9 @@
3031
import com.facebook.react.bridge.ReadableArray;
3132
import com.facebook.react.bridge.ReadableMap;
3233
import com.facebook.react.bridge.ReadableType;
34+
import com.facebook.react.bridge.WritableMap;
3335
import com.facebook.react.touch.ReactHitSlopView;
36+
import com.facebook.react.uimanager.events.RCTEventEmitter;
3437
import com.facebook.react.uimanager.PointerEvents;
3538
import java.lang.reflect.Field;
3639
import java.util.ArrayList;
@@ -98,6 +101,14 @@ public abstract class RenderableView extends VirtualView implements ReactHitSlop
98101
private @Nullable ArrayList<String> mAttributeList;
99102
private @Nullable RenderableView mCaller;
100103

104+
public void onReceiveNativeEvent() {
105+
WritableMap event = Arguments.createMap();
106+
ReactContext reactContext = (ReactContext)getContext();
107+
reactContext
108+
.getJSModule(RCTEventEmitter.class)
109+
.receiveEvent(getId(), "topSvgLayout", event);
110+
}
111+
101112
@Nullable String mFilter;
102113

103114
private static final Pattern regex = Pattern.compile("[0-9.-]+");

android/src/main/java/com/horcrux/svg/RenderableViewManager.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,16 @@ class RenderableViewManager<T extends RenderableView> extends VirtualViewManager
589589
super(svgclass);
590590
}
591591

592+
public Map getExportedCustomBubblingEventTypeConstants() {
593+
return MapBuilder.builder().put(
594+
"topSvgLayout",
595+
MapBuilder.of(
596+
"phasedRegistrationNames",
597+
MapBuilder.of("bubbled", "onSvgLayout")
598+
)
599+
).build();
600+
}
601+
592602
static class GroupViewManagerAbstract<U extends GroupView> extends RenderableViewManager<U> {
593603
GroupViewManagerAbstract(SVGClass svgClass) {
594604
super(svgClass);

android/src/main/java/com/horcrux/svg/events/SvgOnLayoutEvent.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
import com.facebook.react.bridge.WritableMap;
77
import com.facebook.react.uimanager.PixelUtil;
88
import com.facebook.react.uimanager.events.Event;
9-
import com.facebook.react.uimanager.events.RCTEventEmitter;
109

1110
public class SvgOnLayoutEvent extends Event<SvgOnLayoutEvent> {
1211

13-
public static final String EVENT_NAME = "topLayout";
12+
public static final String EVENT_NAME = "topSvgLayout";
1413
public int x = 0;
1514
public int y = 0;
1615
public int width = 0;
@@ -34,11 +33,6 @@ public short getCoalescingKey() {
3433
return 0;
3534
}
3635

37-
@Override
38-
public void dispatch(RCTEventEmitter rctEventEmitter) {
39-
rctEventEmitter.receiveEvent(getViewTag(), getEventName(), getEventData());
40-
}
41-
4236
@Override
4337
protected WritableMap getEventData() {
4438
WritableMap layout = Arguments.createMap();

src/fabric/CircleNativeComponent.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
22
import type { ColorValue } from 'react-native';
33
import type {
4+
DirectEventHandler,
45
Float,
56
Int32,
67
WithDefault,
@@ -31,6 +32,13 @@ type ColorStruct = Readonly<{
3132
brushRef?: string;
3233
}>;
3334

35+
type OnSvgLayoutEvent = Readonly<{
36+
x: Int32;
37+
y: Int32;
38+
width: Int32;
39+
height: Int32;
40+
}>;
41+
3442
interface SvgRenderableCommonProps {
3543
color?: ColorValue;
3644
fill?: UnsafeMixed<ColorValue | ColorStruct>;
@@ -56,6 +64,7 @@ interface NativeProps
5664
cx?: UnsafeMixed<NumberProp>;
5765
cy?: UnsafeMixed<NumberProp>;
5866
r?: UnsafeMixed<NumberProp>;
67+
onSvgLayout?: DirectEventHandler<OnSvgLayoutEvent>;
5968
}
6069

6170
export default codegenNativeComponent<NativeProps>('RNSVGCircle', {

src/fabric/ClipPathNativeComponent.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
22
import type { ColorValue } from 'react-native';
33
import type {
4+
DirectEventHandler,
45
Float,
56
Int32,
67
WithDefault,
@@ -31,6 +32,13 @@ type ColorStruct = Readonly<{
3132
brushRef?: string;
3233
}>;
3334

35+
type OnSvgLayoutEvent = Readonly<{
36+
x: Int32;
37+
y: Int32;
38+
width: Int32;
39+
height: Int32;
40+
}>;
41+
3442
interface SvgRenderableCommonProps {
3543
color?: ColorValue;
3644
fill?: UnsafeMixed<ColorValue | ColorStruct>;
@@ -59,7 +67,9 @@ interface NativeProps
5967
extends ViewProps,
6068
SvgNodeCommonProps,
6169
SvgRenderableCommonProps,
62-
SvgGroupCommonProps {}
70+
SvgGroupCommonProps {
71+
onSvgLayout?: DirectEventHandler<OnSvgLayoutEvent>;
72+
}
6373

6474
export default codegenNativeComponent<NativeProps>('RNSVGClipPath', {
6575
interfaceOnly: true,

src/fabric/EllipseNativeComponent.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
22
import type { ColorValue } from 'react-native';
33
import type {
4+
DirectEventHandler,
45
Float,
56
Int32,
67
WithDefault,
@@ -31,6 +32,13 @@ type ColorStruct = Readonly<{
3132
brushRef?: string;
3233
}>;
3334

35+
type OnSvgLayoutEvent = Readonly<{
36+
x: Int32;
37+
y: Int32;
38+
width: Int32;
39+
height: Int32;
40+
}>;
41+
3442
interface SvgRenderableCommonProps {
3543
color?: ColorValue;
3644
fill?: UnsafeMixed<ColorValue | ColorStruct>;
@@ -57,6 +65,7 @@ interface NativeProps
5765
cy?: UnsafeMixed<NumberProp>;
5866
rx?: UnsafeMixed<NumberProp>;
5967
ry?: UnsafeMixed<NumberProp>;
68+
onSvgLayout?: DirectEventHandler<OnSvgLayoutEvent>;
6069
}
6170

6271
export default codegenNativeComponent<NativeProps>('RNSVGEllipse', {

src/fabric/ForeignObjectNativeComponent.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
22
import type { ColorValue } from 'react-native';
33
import type {
4+
DirectEventHandler,
45
Float,
56
Int32,
67
WithDefault,
@@ -31,6 +32,13 @@ type ColorStruct = Readonly<{
3132
brushRef?: string;
3233
}>;
3334

35+
type OnSvgLayoutEvent = Readonly<{
36+
x: Int32;
37+
y: Int32;
38+
width: Int32;
39+
height: Int32;
40+
}>;
41+
3442
interface SvgRenderableCommonProps {
3543
color?: ColorValue;
3644
fill?: UnsafeMixed<ColorValue | ColorStruct>;
@@ -64,6 +72,7 @@ interface NativeProps
6472
y?: UnsafeMixed<NumberProp>;
6573
height?: UnsafeMixed<NumberProp>;
6674
width?: UnsafeMixed<NumberProp>;
75+
onSvgLayout?: DirectEventHandler<OnSvgLayoutEvent>;
6776
}
6877

6978
export default codegenNativeComponent<NativeProps>('RNSVGForeignObject', {

src/fabric/GroupNativeComponent.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
22
import type { ColorValue } from 'react-native';
33
import type {
4+
DirectEventHandler,
45
Float,
56
Int32,
67
WithDefault,
@@ -31,6 +32,13 @@ type ColorStruct = Readonly<{
3132
brushRef?: string;
3233
}>;
3334

35+
type OnSvgLayoutEvent = Readonly<{
36+
x: Int32;
37+
y: Int32;
38+
width: Int32;
39+
height: Int32;
40+
}>;
41+
3442
interface SvgRenderableCommonProps {
3543
color?: ColorValue;
3644
fill?: UnsafeMixed<ColorValue | ColorStruct>;
@@ -59,7 +67,9 @@ interface NativeProps
5967
extends ViewProps,
6068
SvgNodeCommonProps,
6169
SvgRenderableCommonProps,
62-
SvgGroupCommonProps {}
70+
SvgGroupCommonProps {
71+
onSvgLayout?: DirectEventHandler<OnSvgLayoutEvent>;
72+
}
6373

6474
export default codegenNativeComponent<NativeProps>('RNSVGGroup', {
6575
interfaceOnly: true,

src/fabric/ImageNativeComponent.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ type ColorStruct = Readonly<{
4444
brushRef?: string;
4545
}>;
4646

47+
type OnSvgLayoutEvent = Readonly<{
48+
x: Int32;
49+
y: Int32;
50+
width: Int32;
51+
height: Int32;
52+
}>;
53+
4754
interface SvgRenderableCommonProps {
4855
color?: ColorValue;
4956
fill?: UnsafeMixed<ColorValue | ColorStruct>;
@@ -74,6 +81,7 @@ interface NativeProps
7481
align?: string;
7582
meetOrSlice?: Int32;
7683
onLoad?: DirectEventHandler<ImageLoadEventData>;
84+
onLayout?: DirectEventHandler<OnSvgLayoutEvent>;
7785
}
7886

7987
export default codegenNativeComponent<NativeProps>('RNSVGImage', {

src/fabric/LineNativeComponent.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
22
import type { ColorValue } from 'react-native';
33
import type {
4+
DirectEventHandler,
45
Float,
56
Int32,
67
WithDefault,
@@ -31,6 +32,13 @@ type ColorStruct = Readonly<{
3132
brushRef?: string;
3233
}>;
3334

35+
type OnSvgLayoutEvent = Readonly<{
36+
x: Int32;
37+
y: Int32;
38+
width: Int32;
39+
height: Int32;
40+
}>;
41+
3442
interface SvgRenderableCommonProps {
3543
color?: ColorValue;
3644
fill?: UnsafeMixed<ColorValue | ColorStruct>;
@@ -57,6 +65,7 @@ interface NativeProps
5765
y1?: UnsafeMixed<NumberProp>;
5866
x2?: UnsafeMixed<NumberProp>;
5967
y2?: UnsafeMixed<NumberProp>;
68+
onSvgLayout?: DirectEventHandler<OnSvgLayoutEvent>;
6069
}
6170

6271
export default codegenNativeComponent<NativeProps>('RNSVGLine', {

0 commit comments

Comments
 (0)