Skip to content

Commit 03a6765

Browse files
authored
Paint iOS box shadows below child content
## Summary: On iOS a `boxShadow` draws over the view's children when the view has no border layer, as a view with no border does. Each shadow layer gets `_borderLayer.zPosition`, so without a border layer it gets 0, the same as the children, and it is added after them. An inset ring that an opaque child should cover shows over the child, and an outset shadow covers the part of a child that overflows the view. CSS paints box shadows below the element's content: inset shadows immediately above its background and below its border, outset shadows immediately below its background ([css-backgrounds-3](https://drafts.csswg.org/css-backgrounds-3/#shadow-layers)). This change gives the shadow layers a fixed z-position between the background layers (`BACKGROUND_COLOR_ZPOSITION`) and the border layer. Outset shadows therefore sit above the background, unlike CSS, but they are masked to the outside of the border box, where there is no background, so the difference is not visible. Views with a border layer already drew shadows below the children; their shadows now sit just below the border layer instead of level with it, which is not visible either, since an inset shadow is masked to the inside of the padding box. ## Changelog: [IOS] [FIXED] - Draw `boxShadow` below the view's children when the view has no separate border layer: an inset shadow no longer covers them, and an outset shadow no longer covers a child that overflows the view ## Test Plan: `testInsetBoxShadowIsBelowChildrenWithoutBorderLayer` in `RCTViewComponentViewTests` is new. With `RCTViewComponentView.mm` from `main` it fails (`0` is not less than `0`). Run with Xcode 27.1 on an iOS 27.0 simulator; RNTester builds: ```sh xcodebuild test -workspace packages/rn-tester/RNTesterPods.xcworkspace -scheme React-RCTFabric-Unit-Tests \ -destination 'platform=iOS Simulator,id=<iPhone 18 Pro>' ``` RNTester's Podfile does not install the `React-RCTFabric` `Tests` test spec. To run it (locally, not in this change): add `:testspecs => ['Tests']` to that pod in `scripts/cocoapods/fabric.rb`, narrow the test spec's `source_files` to this test file (`RCTSelectableParagraphComponentViewTests.mm` does not compile in open source), and add an empty Swift file to it so the bundle links the Swift runtime `RCTSwiftUI` needs. Not checked on screen. In RNTester, take a view with no border and an opaque child. With `boxShadow: 'inset 0 0 0 8px black'` and a child that fills the view, a black ring shows over the child before and not after. With `boxShadow: '0 0 0 8px black'` and a child that extends past the view's edge, the shadow shows over the overflow before and not after. Differential Revision: D135059381 Pull Request resolved: #60366
1 parent 0fcf6ec commit 03a6765

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)