Skip to content

Commit de8a763

Browse files
authored
fix: properly handle touch events outside the Svg (#2611)
# Summary Fixes #2585 Improve the `onPress` events by ignoring touches outside the `Svg` view. This is implemented as follows: * on Android by clipping the Android's element dimensions to (0, 0, canvas width, canvas height) * on Apple by improving `hitTest` to exclude touches outside the bounds of (0, 0, bounds width, bounds height) https://github.com/user-attachments/assets/59417493-d849-47df-84e8-d5b0a6045b00
1 parent 3bf4298 commit de8a763

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,14 @@ void setClientRect(RectF rect) {
591591
int bottom = (int) Math.ceil(mClientRect.bottom);
592592
setMeasuredDimension(width, height);
593593
if (!(this instanceof GroupView)) {
594-
setLeft(left);
595-
setTop(top);
596-
setRight(right);
597-
setBottom(bottom);
594+
SvgView root = this.getSvgView();
595+
// Prevent going out of the root view bounds to properly handle touch events
596+
if (root != null) {
597+
setLeft(Math.max(left, 0));
598+
setTop(Math.max(top, 0));
599+
setRight(Math.min(right, root.getWidth()));
600+
setBottom(Math.min(bottom, root.getHeight()));
601+
}
598602
}
599603
EventDispatcher eventDispatcher =
600604
UIManagerHelper.getEventDispatcherForReactTag(mContext, getId());

apple/Elements/RNSVGSvgView.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
342342

343343
- (RNSVGPlatformView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
344344
{
345+
if (point.x < 0 || point.y < 0 || point.x > self.bounds.size.width || point.y > self.bounds.size.height) {
346+
return nil;
347+
}
345348
CGPoint transformed = point;
346349
if (self.align) {
347350
transformed = CGPointApplyAffineTransform(transformed, _invViewBoxTransform);

0 commit comments

Comments
 (0)