Skip to content

Commit 3c058a2

Browse files
ideclaude
andcommitted
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 inset shadows above the element's background and all box shadows below its border and content (https://drafts.csswg.org/css-backgrounds-3/#shadow-layers). Give the shadow layers a fixed z-position between the background layers and the border layer. Outset shadows then sit above the background, but they are masked to the outside of the border box. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 94aee19 commit 3c058a2

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1480,7 +1480,10 @@ - (void)invalidateLayer
14801480
RCTCornerRadiiFromBorderRadii(borderMetrics.borderRadii),
14811481
RCTUIEdgeInsetsFromEdgeInsets(borderMetrics.borderWidths),
14821482
self.layer.bounds.size);
1483-
shadowLayer.zPosition = _borderLayer.zPosition;
1483+
// Below the border and the children, as CSS paints box shadows
1484+
// (https://drafts.csswg.org/css-backgrounds-3/#shadow-layers). Outset shadows sit above the
1485+
// background, unlike CSS, but are masked to outside the border box.
1486+
shadowLayer.zPosition = BACKGROUND_COLOR_ZPOSITION + 0.5;
14841487
[layer addSublayer:shadowLayer];
14851488
[_boxShadowLayers addObject:shadowLayer];
14861489
}

‎packages/react-native/React/Tests/Mounting/RCTViewComponentViewTests.mm‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,26 @@ - (void)testSquareDottedAndDashedOutlinesAreDrawnWithCoreGraphics
408408
}
409409
}
410410

411+
#pragma mark - box shadow paint order
412+
413+
- (void)testInsetBoxShadowIsBelowChildrenWithoutBorderLayer
414+
{
415+
RCTViewComponentView *view = [RCTViewComponentView new];
416+
view.frame = CGRectMake(0, 0, 100, 100);
417+
418+
// A view without borders draws with Core Animation and has no border layer.
419+
auto props = std::make_shared<ViewProps>();
420+
props->boxShadow = {BoxShadow{.spreadDistance = 8, .color = colorFromRGBA(0, 0, 0, 255), .inset = true}};
421+
[view updateProps:props oldProps:ViewShadowNode::defaultSharedProps()];
422+
423+
UIView *child = [UIView new];
424+
[view addSubview:child];
425+
[view finalizeUpdates:RNComponentViewUpdateMaskProps];
426+
427+
NSArray<CALayer *> *shadowLayers = [view valueForKey:@"_boxShadowLayers"];
428+
XCTAssertEqual(shadowLayers.count, 1u);
429+
XCTAssertNil([view valueForKey:@"_borderLayer"]);
430+
XCTAssertLessThan(shadowLayers.firstObject.zPosition, child.layer.zPosition);
431+
}
432+
411433
@end

0 commit comments

Comments
 (0)