-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[Fabric] Implement snapToInterval property for ScrollView #14847
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
Conversation
* Initial plan * Implement snapToInterval property for Fabric ScrollView Co-authored-by: anupriya13 <[email protected]> * Add safety checks to snapToInterval implementation and remove duplication Co-authored-by: anupriya13 <[email protected]> * Change files * Run yarn change prerelease, lint:fix, and format as requested Co-authored-by: anupriya13 <[email protected]> * Remove changes in vnext/codegen files as requested Co-authored-by: anupriya13 <[email protected]> * Restore original inline snap points logic while preserving snapToInterval support Co-authored-by: anupriya13 <[email protected]> * Optimize snapToInterval performance by reducing recalculation frequency Co-authored-by: anupriya13 <[email protected]> * Revert commit 4bbc0b9 as requested Co-authored-by: anupriya13 <[email protected]> * Update ScrollViewComponentView.cpp --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: anupriya13 <[email protected]>
Waiting on review since long time > assigned to @sharath2727 for iteration 2507-02 |
@@ -863,6 +878,9 @@ void ScrollViewComponentView::updateContentVisualSize() noexcept { | |||
m_verticalScrollbarComponent->ContentSize(contentSize); | |||
m_horizontalScrollbarComponent->ContentSize(contentSize); | |||
m_scrollVisual.ContentSize(contentSize); | |||
|
|||
// Update snap points if snapToInterval is being used, as content size affects the number of snap points | |||
updateSnapPoints(); |
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.
what is the scenario for this? In the sense when would this be called? is it during initialization?
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.
It's whenever the content size is changed the offsets / intervals would be updated as per new dimensions
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.
Ah I see. so basically if layout size changes during runtime and then this gets triggered. Got it. Thank you for helping me understand this code better. I hope all the tests are validated to the best of your knowledge.
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.
Approved with one pending question.
Merging as discussed in the meeting |
Description
Type of Change
Why
Implement snapToInterval property for ScrollView in Fabric
Resolves #13150
What
https://reactnative.dev/docs/scrollview#snaptointerval

The
snapToInterval
property was available in the Paper implementation viaScrollViewManager.cpp
but was missing from the Fabric implementation inScrollViewComponentView.cpp
. This meant that developers using the new architecture couldn't use interval-based snapping behavior.Refer IOS https://github.com/facebook/react-native/blob/618279508159191f2b11c0b20446f91e82a27abf/packages/react-native/React/Views/ScrollView/RCTScrollView.m#L810C5-L813C8
Solution
Added comprehensive support for
snapToInterval
that:updateProps()
to check forsnapToInterval
changes alongside existing snap propertiesupdateSnapPoints()
helper method that converts interval values to discrete snap point offsetsupdateContentVisualSize()
to recalculate snap points when content size changessnapToOffsets
takes priority oversnapToInterval
Implementation Details
updateSnapPoints() function sets up where the scroll view should snap when scrolling stops. It first checks if explicit snap positions (snapToOffsets) are provided and uses them. If not, and a snap interval is set (snapToInterval), it generates evenly spaced snap points across the content size. Finally, it updates the scroll visual to use these snap points.
Step by Step:
✅ 1. Get the snap configuration from props
snapToOffsets: a list of exact positions to snap to
snapToInterval: a distance between each snap point
horizontal: whether the scroll is sideways
✅ 2. Decide which to use:
If snapToOffsets is provided and has items, it takes priority.
If not, but snapToInterval > 0, it generates snap points by repeating the interval.
✅ 3. Calculate how big the scrollable content is:
If horizontal, use the content width.
If vertical, use the content height.
Multiply by pointScaleFactor (to handle screen scaling).
✅ 4. Create snap points every interval distance:
For example, every 100 pixels, 200 pixels, etc.
Stop when you reach the end of content or hit a safety limit (max 1000 snap points).
✅ 5. Save the snap points in m_scrollVisual:
These will be used when scrolling stops to snap to the nearest position.
Testing
Playground tested
Changelog
Should this change be included in the release notes: yes
Add a brief summary of the change to use in the release notes for the next release.
Implement snapToInterval property for ScrollView in Fabric
Microsoft Reviewers: Open in CodeFlow