From 69fdbe30cb1448c84d0ed5166cfafe37e9517e7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Kwa=C5=9Bniewski?= Date: Mon, 5 Feb 2024 13:31:47 +0100 Subject: [PATCH] fix: providing unsupported value to hoverStyle crashed the app (#88) --- .../View/RCTViewComponentView.mm | 37 +++++------ packages/react-native/React/Views/RCTView.m | 61 ++++++++++--------- 2 files changed, 52 insertions(+), 46 deletions(-) diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm index 4ac4da5b07ae42..0b63086d6498ee 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm @@ -523,23 +523,26 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event #if TARGET_OS_VISION - (void) updateHoverEffect:(NSString*)hoverEffect withCornerRadius:(CGFloat)cornerRadius { - if (hoverEffect == nil || [hoverEffect isEqualToString:@""]) { - self.hoverStyle = nil; - return; - } - - UIShape *shape = [UIShape rectShapeWithCornerRadius:cornerRadius]; - id effect; - - if ([hoverEffect isEqualToString:@"lift"]) { - effect = [UIHoverLiftEffect effect]; - } else if ([hoverEffect isEqualToString:@"highlight"]) { - effect = [UIHoverHighlightEffect effect]; - } - - if (hoverEffect != nil) { - self.hoverStyle = [UIHoverStyle styleWithEffect:effect shape:shape]; - } + if (hoverEffect == nil) { + self.hoverStyle = nil; + return; + } + + UIShape *shape = [UIShape rectShapeWithCornerRadius:cornerRadius]; + id effect; + + if ([hoverEffect isEqualToString:@"lift"]) { + effect = [UIHoverLiftEffect effect]; + } else if ([hoverEffect isEqualToString:@"highlight"]) { + effect = [UIHoverHighlightEffect effect]; + } + + if (effect == nil) { + self.hoverStyle = nil; + return; + } + + self.hoverStyle = [UIHoverStyle styleWithEffect:effect shape:shape]; } #endif diff --git a/packages/react-native/React/Views/RCTView.m b/packages/react-native/React/Views/RCTView.m index b1e77639678325..bfb7952537ee2a 100644 --- a/packages/react-native/React/Views/RCTView.m +++ b/packages/react-native/React/Views/RCTView.m @@ -669,36 +669,39 @@ - (UIEdgeInsets)bordersAsInsets #if TARGET_OS_VISION - (void)setHoverEffect:(NSString *)hoverEffect { - _hoverEffect = hoverEffect; - - if (hoverEffect == nil) { - self.hoverStyle = nil; - return; - } - - CGFloat cornerRadius = 0.0; - RCTCornerRadii cornerRadii = [self cornerRadii]; - - if (RCTCornerRadiiAreEqual(cornerRadii)) { - cornerRadius = cornerRadii.topLeft; - - } else { - // TODO: Handle a case when corner radius is different for each corner. - cornerRadius = cornerRadii.topLeft; - } - - UIShape *shape = [UIShape rectShapeWithCornerRadius:cornerRadius]; - id effect; - - if ([hoverEffect isEqualToString:@"lift"]) { - effect = [UIHoverLiftEffect effect]; - } else if ([hoverEffect isEqualToString:@"highlight"]) { - effect = [UIHoverHighlightEffect effect]; - } + _hoverEffect = hoverEffect; + + if (hoverEffect == nil) { + self.hoverStyle = nil; + return; + } + + CGFloat cornerRadius = 0.0; + RCTCornerRadii cornerRadii = [self cornerRadii]; + + if (RCTCornerRadiiAreEqual(cornerRadii)) { + cornerRadius = cornerRadii.topLeft; - if (hoverEffect != nil) { - self.hoverStyle = [UIHoverStyle styleWithEffect:effect shape:shape]; - } + } else { + // TODO: Handle a case when corner radius is different for each corner. + cornerRadius = cornerRadii.topLeft; + } + + UIShape *shape = [UIShape rectShapeWithCornerRadius:cornerRadius]; + id effect; + + if ([hoverEffect isEqualToString:@"lift"]) { + effect = [UIHoverLiftEffect effect]; + } else if ([hoverEffect isEqualToString:@"highlight"]) { + effect = [UIHoverHighlightEffect effect]; + } + + if (effect == nil) { + self.hoverStyle = nil; + return; + } + + self.hoverStyle = [UIHoverStyle styleWithEffect:effect shape:shape]; } #endif