Skip to content

[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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

anupriya13
Copy link
Contributor

@anupriya13 anupriya13 commented Jul 8, 2025

Description

Type of Change

  • New feature (non-breaking change which adds functionality)

Why

Implement snapToInterval property for ScrollView in Fabric

Resolves #13150

What

https://reactnative.dev/docs/scrollview#snaptointerval
image

The snapToInterval property was available in the Paper implementation via ScrollViewManager.cpp but was missing from the Fabric implementation in ScrollViewComponentView.cpp. This meant that developers using the new architecture couldn't use interval-based snapping behavior.

Solution

Added comprehensive support for snapToInterval that:

  1. Detects property changes: Modified updateProps() to check for snapToInterval changes alongside existing snap properties
  2. Generates snap points: Created updateSnapPoints() helper method that converts interval values to discrete snap point offsets
  3. Handles content changes: Modified updateContentVisualSize() to recalculate snap points when content size changes
  4. Respects priority: Follows React Native behavior where snapToOffsets takes priority over snapToInterval

Implementation Details

// Property priority (matches React Native behavior)
if (viewProps.snapToOffsets.size() > 0) {
  // Use explicit snapToOffsets
} else if (viewProps.snapToInterval > 0) {
  // Generate snap points at regular intervals
  float interval = viewProps.snapToInterval * pointScaleFactor;
  for (float offset = 0; offset <= contentLength; offset += interval) {
    snapToOffsets.Append(offset);
  }
}

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

* Initial plan

* Implement snapToInterval property for Fabric ScrollView

Co-authored-by: anupriya13 <54227869+anupriya13@users.noreply.github.com>

* Add safety checks to snapToInterval implementation and remove duplication

Co-authored-by: anupriya13 <54227869+anupriya13@users.noreply.github.com>

* Change files

* Run yarn change prerelease, lint:fix, and format as requested

Co-authored-by: anupriya13 <54227869+anupriya13@users.noreply.github.com>

* Remove changes in vnext/codegen files as requested

Co-authored-by: anupriya13 <54227869+anupriya13@users.noreply.github.com>

* Restore original inline snap points logic while preserving snapToInterval support

Co-authored-by: anupriya13 <54227869+anupriya13@users.noreply.github.com>

* Optimize snapToInterval performance by reducing recalculation frequency

Co-authored-by: anupriya13 <54227869+anupriya13@users.noreply.github.com>

* Revert commit 4bbc0b9 as requested

Co-authored-by: anupriya13 <54227869+anupriya13@users.noreply.github.com>

* Update ScrollViewComponentView.cpp

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: anupriya13 <54227869+anupriya13@users.noreply.github.com>
@anupriya13 anupriya13 marked this pull request as ready for review July 8, 2025 09:49
@anupriya13 anupriya13 requested a review from a team as a code owner July 8, 2025 09:49
@anupriya13 anupriya13 changed the title Implement snapToInterval property for ScrollView in Fabric [Fabric] Implement snapToInterval property for ScrollView Jul 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement snapToInterval property for ScrollView for fabric
3 participants