Skip to content

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

Merged
merged 4 commits into from
Apr 28, 2022

Conversation

M-i-k-e-l
Copy link
Collaborator

@M-i-k-e-l M-i-k-e-l commented Apr 18, 2022

Description

Carousel - fix initial page offset (contentOffset)
WOAUILIB-2301
Generally I'd like to replace all calcOffset usages to the new getContentOffset (since snapToOffsets 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:

<Carousel
  onChangePage={this.onChangePage}
  pageWidth={249 + (Spacings.s5 * 2)}
  itemSpacings={Spacings.s5}
  initialPage={1}
  containerStyle={{height: 160}}
>
  {_.map([...Array(numberOfPagesShown)], (item, index) => (
    <Page style={{backgroundColor: BACKGROUND_COLORS[index]}} key={index}>
      <Text margin-15>CARD {index}</Text>
    </Page>
  ))}
</Carousel>
...
page: {
  flex: 1,
  width: 249,
  borderWidth: 1,
  borderRadius: 8
}

Changelog

Carousel - fix initial page offset (contentOffset)

@@ -502,8 +517,7 @@ class Carousel extends Component<CarouselProps, CarouselState> {
horizontal={horizontal}
pagingEnabled={this.shouldEnablePagination()}
snapToOffsets={snapToOffsets}
contentOffset={initialOffset} // iOS only
onContentSizeChange={this.onContentSizeChange}
Copy link
Collaborator

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?

Copy link
Collaborator Author

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?

Copy link
Collaborator

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

Comment on lines +253 to +259
if (snapToOffsets && initialPage !== undefined) {
const offset = snapToOffsets[initialPage];
contentOffset = {
x: horizontal ? offset : 0,
y: horizontal ? 0 : offset
};
}
Copy link
Collaborator

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?

Copy link
Collaborator Author

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.

Copy link
Collaborator

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?

Copy link
Collaborator Author

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?

Copy link
Collaborator

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

Copy link
Collaborator Author

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

@M-i-k-e-l M-i-k-e-l requested a review from ethanshar April 19, 2022 17:49
@ethanshar ethanshar merged commit 616f3b6 into master Apr 28, 2022
@M-i-k-e-l M-i-k-e-l deleted the fix/carousel-initial-page-offset branch April 28, 2022 08:17
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.

2 participants