Skip to content

Commit 616f3b6

Browse files
authored
Carousel - fix initial page offset (contentOffset) (#1991)
* Carousel - fix initial page offset (contentOffset) * Add old code as comment * Rename to getInitialContentOffset
1 parent 740aa23 commit 616f3b6

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/components/carousel/CarouselPresenter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export function getChildrenLength(props: PropsWithChildren<CarouselProps>): numb
66
return React.Children.count(props.children);
77
}
88

9+
// TODO: This should probably be replaced with carousel.getContentOffset
910
export function calcOffset(props: CarouselProps, state: Omit<CarouselState, 'initialOffset' | 'prevProps' | 'currentStandingPage'>) {
1011
const {currentPage, pageWidth, pageHeight} = state;
1112
const {loop, containerMarginHorizontal = 0} = props;

src/components/carousel/index.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,21 @@ class Carousel extends Component<CarouselProps, CarouselState> {
246246
}
247247
};
248248

249+
getInitialContentOffset = (snapToOffsets: number[] | undefined) => {
250+
const {horizontal, initialPage} = this.props;
251+
const {initialOffset} = this.state;
252+
let contentOffset = initialOffset;
253+
if (snapToOffsets && initialPage !== undefined) {
254+
const offset = snapToOffsets[initialPage];
255+
contentOffset = {
256+
x: horizontal ? offset : 0,
257+
y: horizontal ? 0 : offset
258+
};
259+
}
260+
261+
return contentOffset;
262+
}
263+
249264
shouldUsePageWidth() {
250265
const {loop, pageWidth} = this.props;
251266
return !loop && pageWidth;
@@ -477,13 +492,13 @@ class Carousel extends Component<CarouselProps, CarouselState> {
477492

478493
renderCarousel() {
479494
const {containerStyle, animated, horizontal, animatedScrollOffset, ...others} = this.props;
480-
const {initialOffset} = this.state;
481495
const scrollContainerStyle = this.shouldUsePageWidth()
482496
? {paddingRight: this.getItemSpacings(this.props)}
483497
: undefined;
484498
const snapToOffsets = this.getSnapToOffsets();
485499
const marginBottom = Math.max(0, this.getContainerPaddingVertical() - 16);
486500
const ScrollContainer = animatedScrollOffset ? Animated.ScrollView : ScrollView;
501+
const contentOffset = this.getInitialContentOffset(snapToOffsets);
487502
return (
488503
<View
489504
animated={animated}
@@ -502,8 +517,8 @@ class Carousel extends Component<CarouselProps, CarouselState> {
502517
horizontal={horizontal}
503518
pagingEnabled={this.shouldEnablePagination()}
504519
snapToOffsets={snapToOffsets}
505-
contentOffset={initialOffset} // iOS only
506-
onContentSizeChange={this.onContentSizeChange}
520+
contentOffset={contentOffset}
521+
// onContentSizeChange={this.onContentSizeChange}
507522
onMomentumScrollEnd={this.onMomentumScrollEnd}
508523
>
509524
{this.renderChildren()}

0 commit comments

Comments
 (0)