From 82e10848724ca055196d11512adc77f0137e864a Mon Sep 17 00:00:00 2001 From: harpreetkbadhan Date: Thu, 26 Nov 2026 21:20:05 +0100 Subject: [PATCH] fix: clamp outset box shadow rect for negative spread on iOS A negative spread larger than half the view made CGRectInset return CGRectNull, which reached CGPathAddArc as NaN. Cap each axis's inset at half the layer size so the rect collapses to zero, centred on the layer, as it did before #50636. Fixes #60302. --- .../React/Fabric/Utils/RCTBoxShadow.mm | 8 +- .../React/Tests/Fabric/RCTBoxShadowTests.mm | 97 +++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 packages/react-native/React/Tests/Fabric/RCTBoxShadowTests.mm diff --git a/packages/react-native/React/Fabric/Utils/RCTBoxShadow.mm b/packages/react-native/React/Fabric/Utils/RCTBoxShadow.mm index 54633558d177..a24fe9985a15 100644 --- a/packages/react-native/React/Fabric/Utils/RCTBoxShadow.mm +++ b/packages/react-native/React/Fabric/Utils/RCTBoxShadow.mm @@ -78,7 +78,13 @@ static CGColorRef colorRefFromSharedColor(const SharedColor &color) const RCTCornerInsets shadowRectCornerInsets = RCTGetCornerInsets(cornerRadiiForBoxShadow(cornerRadii, shadow.spreadDistance), UIEdgeInsetsZero); - CGRect shadowRect = CGRectInset(shadowLayer.bounds, -shadow.spreadDistance, -shadow.spreadDistance); + // A negative spread can shrink the shadow rect to zero but not past it: CGRectInset would return CGRectNull, which + // ends up as NaN in CoreGraphics. Capping each inset at half the size keeps the rect centred on the layer. + const CGRect bounds = shadowLayer.bounds; + CGRect shadowRect = CGRectInset( + bounds, + fmin(-shadow.spreadDistance, bounds.size.width / 2), + fmin(-shadow.spreadDistance, bounds.size.height / 2)); shadowRect = CGRectOffset(shadowRect, shadow.offsetX, shadow.offsetY); CGPathRef shadowRectPath = RCTPathCreateWithRoundedRect(shadowRect, shadowRectCornerInsets, nil, NO); shadowLayer.shadowPath = shadowRectPath; diff --git a/packages/react-native/React/Tests/Fabric/RCTBoxShadowTests.mm b/packages/react-native/React/Tests/Fabric/RCTBoxShadowTests.mm new file mode 100644 index 000000000000..060820cd33ce --- /dev/null +++ b/packages/react-native/React/Tests/Fabric/RCTBoxShadowTests.mm @@ -0,0 +1,97 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import + +#import + +using namespace facebook::react; + +static BoxShadow outsetShadow(Float offsetX, Float offsetY, Float blurRadius, Float spreadDistance) +{ + return { + .offsetX = offsetX, + .offsetY = offsetY, + .blurRadius = blurRadius, + .spreadDistance = spreadDistance, + .color = colorFromRGBA(0, 0, 0, 255), + .inset = false, + }; +} + +static RCTCornerRadii uniformCornerRadii(CGFloat radius) +{ + return {radius, radius, radius, radius, radius, radius, radius, radius}; +} + +static CGRect shadowPathBounds(CALayer *shadowLayer) +{ + return CGPathGetBoundingBox(shadowLayer.shadowPath); +} + +static CGRect maskPathBounds(CALayer *shadowLayer) +{ + return CGPathGetBoundingBox(((CAShapeLayer *)shadowLayer.mask).path); +} + +#define RCTAssertRectEqual(actual, expected) \ + XCTAssertTrue( \ + CGRectEqualToRect((actual), (expected)), \ + @"%@ is not equal to %@", \ + NSStringFromCGRect(actual), \ + NSStringFromCGRect(expected)) + +@interface RCTBoxShadowTests : XCTestCase + +@end + +@implementation RCTBoxShadowTests + +- (void)testOutsetShadowWithNegativeSpreadOnZeroSizedLayerIsEmpty +{ + CALayer *shadowLayer = + RCTGetBoxShadowLayer(outsetShadow(0, 1, 2, -1), uniformCornerRadii(8), UIEdgeInsetsZero, CGSizeZero); + + RCTAssertRectEqual(shadowPathBounds(shadowLayer), CGRectMake(0, 1, 0, 0)); + // Mask = layer rect (empty, at the origin) + shadow rect padded by 2 * (blur + 1). + RCTAssertRectEqual(maskPathBounds(shadowLayer), CGRectMake(-6, -5, 12, 12)); +} + +- (void)testOutsetShadowWithNegativeSpreadClampsEachAxisIndependently +{ + CALayer *shadowLayer = + RCTGetBoxShadowLayer(outsetShadow(0, 0, 0, -1), uniformCornerRadii(0), UIEdgeInsetsZero, CGSizeMake(100, 1)); + + // The width shrinks by the spread on each side; the height cannot go below zero, so it collapses onto the centre. + RCTAssertRectEqual(shadowPathBounds(shadowLayer), CGRectMake(1, 0.5, 98, 0)); +} + +- (void)testOutsetShadowWithNegativeSpreadSmallerThanLayer +{ + CALayer *shadowLayer = + RCTGetBoxShadowLayer(outsetShadow(2, 3, 0, -5), uniformCornerRadii(0), UIEdgeInsetsZero, CGSizeMake(100, 50)); + + RCTAssertRectEqual(shadowPathBounds(shadowLayer), CGRectMake(7, 8, 90, 40)); +} + +- (void)testOutsetShadowWithZeroSpread +{ + CALayer *shadowLayer = + RCTGetBoxShadowLayer(outsetShadow(2, 3, 0, 0), uniformCornerRadii(0), UIEdgeInsetsZero, CGSizeMake(100, 50)); + + RCTAssertRectEqual(shadowPathBounds(shadowLayer), CGRectMake(2, 3, 100, 50)); +} + +- (void)testOutsetShadowWithPositiveSpread +{ + CALayer *shadowLayer = + RCTGetBoxShadowLayer(outsetShadow(2, 3, 0, 5), uniformCornerRadii(0), UIEdgeInsetsZero, CGSizeMake(100, 50)); + + RCTAssertRectEqual(shadowPathBounds(shadowLayer), CGRectMake(-3, -2, 110, 60)); +} + +@end