-
Notifications
You must be signed in to change notification settings - Fork 734
Carousel - fix initial page offset (contentOffset) #1991
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
@@ -502,8 +517,7 @@ class Carousel extends Component<CarouselProps, CarouselState> { | |||
horizontal={horizontal} | |||
pagingEnabled={this.shouldEnablePagination()} | |||
snapToOffsets={snapToOffsets} | |||
contentOffset={initialOffset} // iOS only | |||
onContentSizeChange={this.onContentSizeChange} |
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.
Why did you remove onContentSizeChange
?
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.
I think that contentOffset={initialOffset} // iOS only
was actually only working for iOS and it looks like it's working on Android as well now; and I think that onContentSizeChange
was there to workaround that issue.
Do you know of a case where this is useful?
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.
No, but let's keep it commented for now, so if an issue will rise soon it'll be easier to spot it.
If in the future we'll come across it we'll remove it
if (snapToOffsets && initialPage !== undefined) { | ||
const offset = snapToOffsets[initialPage]; | ||
contentOffset = { | ||
x: horizontal ? offset : 0, | ||
y: horizontal ? 0 : offset | ||
}; | ||
} |
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.
shouldn't this code be part of what we store in the initialOffset? in the onContainerLayout
method?
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.
I do know this is a bit hacky, as I've addressed in my description, there are other places where we should probably change it (such as the constructor
), but TBH if we want to change them then I feel like this conversation is going towards: let's rewrite this component.
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.
Not sure it has to go that way.
We have a method that calcOffset, we use it for setting the initial offset.
Now you the logic for initial offset changed a little, Can't we create a method for calcInitialOffset
that returns the relevant offset and set it in the state?
What are the blockers here?
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 new method (with the new logic) getContentOffset
depends on snapToOffsets
which is calculated using containerWidth
which is obtained in onContainerLayout
.
Technically from testing now, it seems that only the constructor is occurring before this happens, but it feels unsafe to me, WDYT?
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.
Ok, I see your point.
So can we rename the new method to getInitialContentOffset
so it's clear that it's the initial offset
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.
Sure, but I blame RN:
/**
* Used to manually set the starting scroll offset.
* The default value is {x: 0, y: 0}
*/
contentOffset?: PointPropType | undefined; // zeros
Description
Carousel - fix initial page offset (contentOffset)
WOAUILIB-2301
Generally I'd like to replace all
calcOffset
usages to the newgetContentOffset
(sincesnapToOffsets
seem to have the correct data) but I fear of edge cases and bugs.TBH, I think we should rewrite this component as a function component.
Testing snippet:
Changelog
Carousel - fix initial page offset (contentOffset)