Repository navigation
[ScrollView][iOS] Refactor ScrollView inset handling to lay out natively #59555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+720
−649
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 0 additions & 140 deletions
140
...ative/Libraries/Components/ScrollView/__tests__/ScrollView-safeAreaContentPadding-test.js
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...ative/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollContentViewComponentView.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * 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 <UIKit/UIKit.h> | ||
|
|
||
| #import <React/RCTViewComponentView.h> | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| /* | ||
| * UIView class for the <ScrollContentView> component, the content container of a <ScrollView>. | ||
| */ | ||
| @interface RCTScrollContentViewComponentView : RCTViewComponentView | ||
|
|
||
| /* | ||
| * The safe area the content is laid out clear of, as padding on top of the content container's own. The enclosing | ||
| * scroll view sets the pair of insets across its scrollable axis, and zero while its `contentInsetAdjustmentBehavior` | ||
| * is `never`. | ||
| */ | ||
| @property (nonatomic, assign) UIEdgeInsets safeAreaPadding; | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END |
106 changes: 106 additions & 0 deletions
106
...tive/React/Fabric/Mounting/ComponentViews/ScrollView/RCTScrollContentViewComponentView.mm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * 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 "RCTScrollContentViewComponentView.h" | ||
|
|
||
| #import <React/RCTI18nUtil.h> | ||
| #import <React/RCTUtils.h> | ||
| #import <react/renderer/components/scrollview/ScrollContentViewComponentDescriptor.h> | ||
| #import <react/renderer/components/scrollview/ScrollContentViewState.h> | ||
| #import "RCTConversions.h" | ||
| #import "RCTFabricComponentsPlugins.h" | ||
|
|
||
| using namespace facebook::react; | ||
|
|
||
| @implementation RCTScrollContentViewComponentView { | ||
| ScrollContentViewShadowNode::ConcreteState::Shared _state; | ||
| } | ||
|
|
||
| - (instancetype)initWithFrame:(CGRect)frame | ||
| { | ||
| if (self = [super initWithFrame:frame]) { | ||
| _props = ScrollContentViewShadowNode::defaultSharedProps(); | ||
| } | ||
|
|
||
| return self; | ||
| } | ||
|
|
||
| - (void)setSafeAreaPadding:(UIEdgeInsets)safeAreaPadding | ||
| { | ||
| _safeAreaPadding = safeAreaPadding; | ||
| [self _updateStateIfNecessary]; | ||
| } | ||
|
|
||
| - (void)_updateStateIfNecessary | ||
| { | ||
| if (!_state) { | ||
| return; | ||
| } | ||
|
|
||
| UIEdgeInsets insets = _safeAreaPadding; | ||
| insets.left = RCTRoundPixelValue(insets.left); | ||
| insets.top = RCTRoundPixelValue(insets.top); | ||
| insets.right = RCTRoundPixelValue(insets.right); | ||
| insets.bottom = RCTRoundPixelValue(insets.bottom); | ||
|
|
||
| auto newPadding = RCTEdgeInsetsFromUIEdgeInsets(insets); | ||
| RCTI18nUtil *i18nUtil = [RCTI18nUtil sharedInstance]; | ||
| auto layoutDirection = i18nUtil.isRTL ? LayoutDirection::RightToLeft : LayoutDirection::LeftToRight; | ||
| bool swapLeftAndRightInRTL = i18nUtil.isRTL && i18nUtil.doLeftAndRightSwapInRTL; | ||
| auto threshold = 1.0 / RCTScreenScale() + 0.01; // Size of a pixel plus some small threshold. | ||
|
|
||
| _state->updateState( | ||
| [=](const ScrollContentViewShadowNode::ConcreteState::Data &oldData) | ||
| -> ScrollContentViewShadowNode::ConcreteState::SharedData { | ||
| auto deltaPadding = newPadding - oldData.safeAreaPadding; | ||
|
|
||
| if (std::abs(deltaPadding.left) < threshold && std::abs(deltaPadding.top) < threshold && | ||
| std::abs(deltaPadding.right) < threshold && std::abs(deltaPadding.bottom) < threshold && | ||
| oldData.layoutDirection == layoutDirection && oldData.swapLeftAndRightInRTL == swapLeftAndRightInRTL) { | ||
| return nullptr; | ||
| } | ||
|
|
||
| auto newData = oldData; | ||
| newData.safeAreaPadding = newPadding; | ||
| newData.layoutDirection = layoutDirection; | ||
| newData.swapLeftAndRightInRTL = swapLeftAndRightInRTL; | ||
| return std::make_shared<const ScrollContentViewShadowNode::ConcreteState::Data>(newData); | ||
| }); | ||
| } | ||
|
|
||
| #pragma mark - RCTComponentViewProtocol | ||
|
|
||
| + (ComponentDescriptorProvider)componentDescriptorProvider | ||
| { | ||
| return concreteComponentDescriptorProvider<ScrollContentViewComponentDescriptor>(); | ||
| } | ||
|
|
||
| - (void)updateState:(const facebook::react::State::Shared &)state | ||
| oldState:(const facebook::react::State::Shared &)oldState | ||
| { | ||
| _state = std::static_pointer_cast<const ScrollContentViewShadowNode::ConcreteState>(state); | ||
| } | ||
|
|
||
| - (void)finalizeUpdates:(RNComponentViewUpdateMask)updateMask | ||
| { | ||
| [super finalizeUpdates:updateMask]; | ||
| [self _updateStateIfNecessary]; | ||
| } | ||
|
|
||
| - (void)prepareForRecycle | ||
| { | ||
| [super prepareForRecycle]; | ||
| _state.reset(); | ||
| _safeAreaPadding = UIEdgeInsetsZero; | ||
| } | ||
|
|
||
| @end | ||
|
|
||
| Class<RCTComponentViewProtocol> RCTScrollContentViewCls(void) | ||
| { | ||
| return RCTScrollContentViewComponentView.class; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The padding now arrives through an asynchronous
updateStateafter mount and after rotation or unfolding, where the JS version re-rendered synchronously on the native event. Is there a one-frame jump when the modal opens or the device rotates?EventQueue::UpdateMode::unstable_Immediateis available if so.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, saw this and decided to keep consistent with other components (in particular, our private
<SafeAreaView>itself). Can observe no one-frame jump in real use.Claude flagged it for me too, but then recommended against
unstable_Immediateuntil it's more widely adopted in the codebase.➡️ Potentially you/I follow up though and migrate to
unstable_Immediate, if ready, across the codebase?