-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -246,6 +246,21 @@ class Carousel extends Component<CarouselProps, CarouselState> { | |
} | ||
}; | ||
|
||
getInitialContentOffset = (snapToOffsets: number[] | undefined) => { | ||
const {horizontal, initialPage} = this.props; | ||
const {initialOffset} = this.state; | ||
let contentOffset = initialOffset; | ||
if (snapToOffsets && initialPage !== undefined) { | ||
const offset = snapToOffsets[initialPage]; | ||
contentOffset = { | ||
x: horizontal ? offset : 0, | ||
y: horizontal ? 0 : offset | ||
}; | ||
} | ||
|
||
return contentOffset; | ||
} | ||
|
||
shouldUsePageWidth() { | ||
const {loop, pageWidth} = this.props; | ||
return !loop && pageWidth; | ||
|
@@ -477,13 +492,13 @@ class Carousel extends Component<CarouselProps, CarouselState> { | |
|
||
renderCarousel() { | ||
const {containerStyle, animated, horizontal, animatedScrollOffset, ...others} = this.props; | ||
const {initialOffset} = this.state; | ||
const scrollContainerStyle = this.shouldUsePageWidth() | ||
? {paddingRight: this.getItemSpacings(this.props)} | ||
: undefined; | ||
const snapToOffsets = this.getSnapToOffsets(); | ||
const marginBottom = Math.max(0, this.getContainerPaddingVertical() - 16); | ||
const ScrollContainer = animatedScrollOffset ? Animated.ScrollView : ScrollView; | ||
const contentOffset = this.getInitialContentOffset(snapToOffsets); | ||
return ( | ||
<View | ||
animated={animated} | ||
|
@@ -502,8 +517,8 @@ 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 commentThe reason will be displayed to describe this comment to others. Learn more. Why did you remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
contentOffset={contentOffset} | ||
// onContentSizeChange={this.onContentSizeChange} | ||
onMomentumScrollEnd={this.onMomentumScrollEnd} | ||
> | ||
{this.renderChildren()} | ||
|
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 onsnapToOffsets
which is calculated usingcontainerWidth
which is obtained inonContainerLayout
.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 offsetThere 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: