Skip to content

Commit 1fbc535

Browse files
authored
[Fabric] ScrollViewComponentView should report ContentOffset to its shadownode state (#12588)
* [Fabric] ScrollViewComponentView should report ContentOffset to its shadownode state * Change files
1 parent a948daf commit 1fbc535

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "[Fabric] ScrollViewComponentView should report ContentOffset to its shadownode state",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

vnext/Microsoft.ReactNative/Fabric/Composition/ScrollViewComponentView.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,24 @@ void ScrollViewComponentView::updateProps(
181181
void ScrollViewComponentView::updateState(
182182
facebook::react::State::Shared const &state,
183183
facebook::react::State::Shared const &oldState) noexcept {
184-
const auto &newState = *std::static_pointer_cast<facebook::react::ScrollViewShadowNode::ConcreteState const>(state);
185-
186-
m_contentSize = newState.getData().getContentSize();
184+
m_state = std::static_pointer_cast<facebook::react::ScrollViewShadowNode::ConcreteState const>(state);
185+
m_contentSize = m_state->getData().getContentSize();
187186
updateContentVisualSize();
188187
}
189188

189+
void ScrollViewComponentView::updateStateWithContentOffset() noexcept {
190+
if (!m_state) {
191+
return;
192+
}
193+
194+
m_state->updateState([contentOffset = m_scrollVisual.ScrollPosition()](
195+
const facebook::react::ScrollViewShadowNode::ConcreteState::Data &data) {
196+
auto newData = data;
197+
newData.contentOffset = {contentOffset.x, contentOffset.y};
198+
return std::make_shared<facebook::react::ScrollViewShadowNode::ConcreteState::Data const>(newData);
199+
});
200+
}
201+
190202
void ScrollViewComponentView::updateLayoutMetrics(
191203
facebook::react::LayoutMetrics const &layoutMetrics,
192204
facebook::react::LayoutMetrics const &oldLayoutMetrics) noexcept {
@@ -529,6 +541,7 @@ void ScrollViewComponentView::ensureVisual() noexcept {
529541
[this](
530542
winrt::IInspectable const & /*sender*/,
531543
winrt::Microsoft::ReactNative::Composition::IScrollPositionChangedArgs const &args) {
544+
updateStateWithContentOffset();
532545
auto eventEmitter = GetEventEmitter();
533546
if (eventEmitter) {
534547
facebook::react::ScrollViewMetrics scrollMetrics;

vnext/Microsoft.ReactNative/Fabric/Composition/ScrollViewComponentView.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#pragma warning(push)
1212
#pragma warning(disable : 4305)
1313
#include <react/renderer/components/scrollview/ScrollViewProps.h>
14+
#include <react/renderer/components/scrollview/ScrollViewShadowNode.h>
1415
#pragma warning(pop)
1516
#include "Composition.ScrollViewComponentView.g.h"
1617
#include <winrt/Windows.UI.Composition.interactions.h>
@@ -108,6 +109,7 @@ struct ScrollInteractionTrackerOwner : public winrt::implements<
108109
bool scrollLeft(float delta, bool aniamte) noexcept;
109110
bool scrollRight(float delta, bool animate) noexcept;
110111
void updateBackgroundColor(const facebook::react::SharedColor &color) noexcept;
112+
void updateStateWithContentOffset() noexcept;
111113

112114
facebook::react::Size m_contentSize;
113115
winrt::Microsoft::ReactNative::Composition::ISpriteVisual m_visual{nullptr};
@@ -122,6 +124,7 @@ struct ScrollInteractionTrackerOwner : public winrt::implements<
122124
bool m_isHorizontal = false;
123125
bool m_changeViewAfterLoaded = false;
124126
bool m_dismissKeyboardOnDrag = false;
127+
std::shared_ptr<facebook::react::ScrollViewShadowNode::ConcreteState const> m_state;
125128

126129
private:
127130
bool shouldBeControl() const noexcept;

0 commit comments

Comments
 (0)