From 526db5e6511c59da2f7a3e7ce37b718e9f6b853e Mon Sep 17 00:00:00 2001 From: James Ide Date: Sat, 28 Nov 2026 02:35:41 -0800 Subject: [PATCH] Paint iOS box shadows below child content A `boxShadow` drew over the view's children when the view had no border layer. Each shadow layer copied `_borderLayer.zPosition`, and a view whose border Core Animation draws (no border, or a uniform border on a clipping view) has no border layer, so the shadow got z-position 0. Children also have z-position 0 and the shadow layers are added after them, so the shadow painted on top: an inset shadow over every child, and an outset shadow over a child that overflows the view. CSS paints box shadows above the element's background and below its border and content (https://drafts.csswg.org/css-backgrounds-3/#box-shadow). Give the shadow layers a fixed z-position between the background layers and the border layer. Co-Authored-By: Claude Fable 5.2 --- .../View/RCTViewComponentView.mm | 4 +++- .../Mounting/RCTViewComponentViewTests.mm | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) 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 02574182c30..50a82adaef5 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm @@ -1480,7 +1480,9 @@ - (void)invalidateLayer RCTCornerRadiiFromBorderRadii(borderMetrics.borderRadii), RCTUIEdgeInsetsFromEdgeInsets(borderMetrics.borderWidths), self.layer.bounds.size); - shadowLayer.zPosition = _borderLayer.zPosition; + // CSS paints box shadows above the background and below the border and the children + // (https://drafts.csswg.org/css-backgrounds-3/#box-shadow). + shadowLayer.zPosition = BACKGROUND_COLOR_ZPOSITION + 0.5; [layer addSublayer:shadowLayer]; [_boxShadowLayers addObject:shadowLayer]; } diff --git a/packages/react-native/React/Tests/Mounting/RCTViewComponentViewTests.mm b/packages/react-native/React/Tests/Mounting/RCTViewComponentViewTests.mm index 5c00390702c..1698ab1cf3d 100644 --- a/packages/react-native/React/Tests/Mounting/RCTViewComponentViewTests.mm +++ b/packages/react-native/React/Tests/Mounting/RCTViewComponentViewTests.mm @@ -408,4 +408,26 @@ - (void)testSquareDottedAndDashedOutlinesAreDrawnWithCoreGraphics } } +#pragma mark - box shadow paint order + +- (void)testInsetBoxShadowIsBelowChildrenWithoutBorderLayer +{ + RCTViewComponentView *view = [RCTViewComponentView new]; + view.frame = CGRectMake(0, 0, 100, 100); + + // A view without borders draws with Core Animation and has no border layer. + auto props = std::make_shared(); + props->boxShadow = {BoxShadow{.spreadDistance = 8, .color = colorFromRGBA(0, 0, 0, 255), .inset = true}}; + [view updateProps:props oldProps:ViewShadowNode::defaultSharedProps()]; + + UIView *child = [UIView new]; + [view addSubview:child]; + [view finalizeUpdates:RNComponentViewUpdateMaskProps]; + + NSArray *shadowLayers = [view valueForKey:@"_boxShadowLayers"]; + XCTAssertEqual(shadowLayers.count, 1u); + XCTAssertNil([view valueForKey:@"_borderLayer"]); + XCTAssertLessThan(shadowLayers.firstObject.zPosition, child.layer.zPosition); +} + @end