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 8d1295d8db3..9d25880346e 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm @@ -891,6 +891,13 @@ - (void)prepareForRecycle _filterLayer = nil; [self clearExistingBackgroundImageLayers]; + // Drop the clipping container of the previous owner. Fabric mounts a view's + // children before it applies that view's props, so a container left in place + // would receive the next owner's children. With the flag cleared, this getter + // moves any remaining subviews out and removes the container. + _useCustomContainerView = NO; + [self currentContainerView]; + [self _setLastSentSafeAreaInsets:nil]; _propKeysManagedByAnimated_DO_NOT_USE_THIS_IS_BROKEN = nil; _eventEmitter.reset(); @@ -1149,8 +1156,13 @@ - (UIView *)currentContainerView effectiveContentView = _containerView; } else { if (_containerView) { - for (UIView *subview in _containerView.subviews) { - [effectiveContentView addSubview:subview]; + // Put the children back below the layers this view draws itself (box + // shadows, outlines), where they sit when no container is used. + // `insertSubview:atIndex:` counts sublayers, so appending them after + // those layers would shift every later mount by one. + NSArray *subviews = _containerView.subviews; + for (NSUInteger index = 0; index < subviews.count; index++) { + [effectiveContentView insertSubview:subviews[index] atIndex:index]; } effectiveContentView.clipsToBounds = _containerView.clipsToBounds; effectiveContentView.layer.mask = _containerView.layer.mask; diff --git a/packages/react-native/React/Tests/Mounting/RCTViewComponentViewContainerTests.mm b/packages/react-native/React/Tests/Mounting/RCTViewComponentViewContainerTests.mm new file mode 100644 index 00000000000..9f57146f108 --- /dev/null +++ b/packages/react-native/React/Tests/Mounting/RCTViewComponentViewContainerTests.mm @@ -0,0 +1,163 @@ +/* + * 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 +#import +#import +#import +#import + +using namespace facebook::react; + +// A view that clips and has a box shadow draws the shadow outside its bounds, +// so RCTViewComponentView moves its children into a separate clipping +// container view. +static Props::Shared makeProps(bool clips, bool hasBoxShadow) +{ + auto props = std::make_shared(); + if (clips) { + props->yogaStyle.setOverflow(facebook::yoga::Overflow::Hidden); + } + if (hasBoxShadow) { + BoxShadow shadow; + shadow.offsetY = 2; + shadow.blurRadius = 6; + shadow.color = colorFromRGBA(0, 0, 0, 102); + props->boxShadow = {shadow}; + } + return props; +} + +static void applyProps(RCTViewComponentView *view, const Props::Shared &props) +{ + // Like the mounting layer does for an Insert, pass no old props: the view + // diffs against its own. + [view updateProps:props oldProps:nullptr]; + LayoutMetrics layoutMetrics; + layoutMetrics.frame.size = {100, 40}; + [view updateLayoutMetrics:layoutMetrics oldLayoutMetrics:EmptyLayoutMetrics]; + [view finalizeUpdates:RNComponentViewUpdateMaskAll]; +} + +// Unmounts `child` and returns the message of any RCTAssert it raised, or nil. +static NSString * +unmountAndReturnAssertMessage(RCTViewComponentView *parent, UIView *child, NSInteger index) +{ + __block NSString *message = nil; + RCTPerformBlockWithAssertFunction( + ^{ + @try { + [parent unmountChildComponentView:child index:index]; + } @catch (NSException *exception) { + message = message ?: exception.reason; + } + }, + ^(NSString *condition, NSString *fileName, NSNumber *lineNumber, NSString *function, NSString *assertMessage) { + message = assertMessage; + }); + return message; +} + +@interface RCTViewComponentViewContainerTests : XCTestCase +@end + +@implementation RCTViewComponentViewContainerTests + +// When a view stops clipping, its children move from the container back into +// the view. The box shadow layer was already re-added by then, and +// `insertSubview:atIndex:` counts sublayers, so children appended after it were +// mounted one index too low from then on. +- (void)testChildrenMovedOutOfContainerStayBelowSelfDrawnLayers +{ + RCTViewComponentView *parent = [RCTViewComponentView new]; + applyProps(parent, makeProps(/*clips=*/true, /*hasBoxShadow=*/true)); + + RCTViewComponentView *first = [RCTViewComponentView new]; + RCTViewComponentView *second = [RCTViewComponentView new]; + [parent mountChildComponentView:first index:0]; + [parent mountChildComponentView:second index:1]; + XCTAssertNotEqual(first.superview, parent, @"children of a clipping view with a box shadow go into a container"); + + applyProps(parent, makeProps(/*clips=*/false, /*hasBoxShadow=*/true)); + XCTAssertEqual(first.superview, parent); + XCTAssertEqual(parent.layer.sublayers.firstObject, first.layer, @"children must precede the box shadow layer"); + + RCTViewComponentView *third = [RCTViewComponentView new]; + [parent mountChildComponentView:third index:2]; + NSArray *expected = @[ first, second, third ]; + XCTAssertEqualObjects(parent.subviews, expected); + + XCTAssertNil(unmountAndReturnAssertMessage(parent, third, 2)); +} + +// A recycled view must not keep the container of its previous owner. Fabric +// mounts a view's children before it applies that view's props, so those +// children used to go into the stale container and were later moved above the +// next owner's box shadow layer. +- (void)testRecycledViewDropsContainerBeforeNextOwnerMountsChildren +{ + RCTViewComponentView *view = [RCTViewComponentView new]; + applyProps(view, makeProps(/*clips=*/true, /*hasBoxShadow=*/true)); + RCTViewComponentView *previousChild = [RCTViewComponentView new]; + [view mountChildComponentView:previousChild index:0]; + UIView *previousContainer = previousChild.superview; + XCTAssertNotEqual(previousContainer, view); + [view unmountChildComponentView:previousChild index:0]; + + [view prepareForRecycle]; + XCTAssertNil(previousContainer.superview, @"the container is removed on recycle"); + XCTAssertEqual(view.subviews.count, 0u); + + // The next owner has a box shadow but doesn't clip. + RCTViewComponentView *first = [RCTViewComponentView new]; + [view mountChildComponentView:first index:0]; + XCTAssertEqual(first.superview, view); + applyProps(view, makeProps(/*clips=*/false, /*hasBoxShadow=*/true)); + XCTAssertFalse(view.clipsToBounds); + + RCTViewComponentView *second = [RCTViewComponentView new]; + [view mountChildComponentView:second index:1]; + NSArray *expected = @[ first, second ]; + XCTAssertEqualObjects(view.subviews, expected); + + XCTAssertNil(unmountAndReturnAssertMessage(view, second, 1)); +} + +// `updateProps` compares against the props kept from the previous owner, so a +// next owner that also clips gets no `overflow` update. The clipping that the +// container held must move back to the view when the container is dropped. +- (void)testRecycledViewKeepsClippingForNextOwnerThatClips +{ + RCTViewComponentView *view = [RCTViewComponentView new]; + applyProps(view, makeProps(/*clips=*/true, /*hasBoxShadow=*/true)); + [view prepareForRecycle]; + + RCTViewComponentView *child = [RCTViewComponentView new]; + [view mountChildComponentView:child index:0]; + applyProps(view, makeProps(/*clips=*/true, /*hasBoxShadow=*/false)); + + XCTAssertEqual(child.superview, view); + XCTAssertTrue(view.clipsToBounds); +} + +// Subclasses such as image and text views put their own content view into the +// container. It has to come back with the children. +- (void)testRecycledViewMovesContentViewOutOfContainer +{ + RCTViewComponentView *view = [RCTViewComponentView new]; + applyProps(view, makeProps(/*clips=*/true, /*hasBoxShadow=*/true)); + UIView *contentView = [UIView new]; + view.contentView = contentView; + XCTAssertNotEqual(contentView.superview, view); + + [view prepareForRecycle]; + + XCTAssertEqual(contentView.superview, view); +} + +@end diff --git a/packages/react-native/React/Tests/Mounting/RCTViewComponentViewUnmountTests.mm b/packages/react-native/React/Tests/Mounting/RCTViewComponentViewUnmountTests.mm index 668a0404694..df6dcb4236b 100644 --- a/packages/react-native/React/Tests/Mounting/RCTViewComponentViewUnmountTests.mm +++ b/packages/react-native/React/Tests/Mounting/RCTViewComponentViewUnmountTests.mm @@ -86,10 +86,10 @@ - (void)testUnmountWithInBoundsMismatchStillReportsTagAtIndex XCTAssertNotEqualObjects(thrownName, NSRangeException); } -// A recycled view can receive children before its own props are updated. If -// the new owner is zero-sized, layer invalidation does not reconcile the old -// clipping container. Unmount must do so before checking the child's parent. -- (void)testUnmountReconcilesRecycledContainerAtZeroSize +// When a zero-sized view stops needing its clipping container, layer +// invalidation returns early and does not move the children out of it. Unmount +// must reconcile the container before checking the child's parent. +- (void)testUnmountReconcilesStaleContainerAtZeroSize { RCTViewComponentView *parent = [[RCTViewComponentView alloc] initWithFrame:CGRectZero]; auto clippingProps = std::make_shared(); @@ -98,17 +98,12 @@ - (void)testUnmountReconcilesRecycledContainerAtZeroSize [parent updateProps:clippingProps oldProps:parent.props]; [parent finalizeUpdates:RNComponentViewUpdateMaskAll]; - RCTViewComponentView *previousChild = [RCTViewComponentView new]; - [parent mountChildComponentView:previousChild index:0]; - UIView *previousContainer = previousChild.superview; + RCTViewComponentView *child = [RCTViewComponentView new]; + [parent mountChildComponentView:child index:0]; + UIView *previousContainer = child.superview; XCTAssertNotEqual(previousContainer, parent); XCTAssertNotNil(previousContainer); - [parent unmountChildComponentView:previousChild index:0]; - [parent prepareForRecycle]; - // Fabric mounts descendants before applying the recycled parent's new props. - RCTViewComponentView *child = [RCTViewComponentView new]; - [parent mountChildComponentView:child index:0]; [parent updateProps:std::make_shared() oldProps:parent.props]; [parent finalizeUpdates:RNComponentViewUpdateMaskAll]; XCTAssertTrue(CGSizeEqualToSize(parent.bounds.size, CGSizeZero));