Skip to content

Commit

Permalink
fix: saved position of scroll for tab
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0trpietruszewski committed Aug 7, 2020
1 parent 4132ca4 commit 7c2d177
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/StickyParallaxHeader.js
Expand Up @@ -25,6 +25,7 @@ class StickyParallaxHeader extends Component {
const { width } = Dimensions.get('window')
const scrollXIOS = new Value(initialPage * width)
const containerWidthAnimatedValue = new Value(width)
this.tabsScrollPosition = []

// eslint-disable-next-line no-underscore-dangle
containerWidthAnimatedValue.__makeNative()
Expand All @@ -45,16 +46,24 @@ class StickyParallaxHeader extends Component {
}

componentDidUpdate(prevProps, prevState) {
const { headerHeight, parallaxHeight, tabs } = this.props
const { headerHeight, parallaxHeight, tabs, rememberTabScrollPosition } = this.props
const prevPage = prevState.currentPage
const { currentPage, isFolded } = this.state
const isRenderingTabs = tabs && tabs.length > 0

if (isRenderingTabs && prevPage !== currentPage && isFolded) {
const currentScrollPosition = this.scrollY.__getValue().y
const scrollHeight = Math.max(parallaxHeight, headerHeight * 2)

this.tabsScrollPosition[prevPage] = currentScrollPosition

setTimeout(() => {
const scrollTargetPosition =
rememberTabScrollPosition && this.tabsScrollPosition[currentPage]
? this.tabsScrollPosition[currentPage]
: scrollHeight
const scrollNode = getSafelyScrollNode(this.scroll)
scrollNode.scrollTo({ y: scrollHeight, duration: 1000 })
scrollNode.scrollTo({ y: scrollTargetPosition, duration: 1000 })
}, 250)
}
}
Expand Down Expand Up @@ -422,6 +431,7 @@ StickyParallaxHeader.propTypes = {
onChangeTab: func,
onEndReached: func,
parallaxHeight: number,
rememberTabScrollPosition: bool,
scrollEvent: func,
snapToEdge: bool,
tabTextActiveStyle: shape({}),
Expand Down Expand Up @@ -451,6 +461,7 @@ StickyParallaxHeader.defaultProps = {
tabTextContainerStyle: {},
tabTextStyle: {},
tabWrapperStyle: {},
rememberTabScrollPosition: false,
snapStartThreshold: false,
snapStopThreshold: false,
snapValue: false,
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Expand Up @@ -56,6 +56,7 @@ export type TabbedHeaderProps = SharedProps & TabsSharedProps & {
logoContainerStyle?: ViewStyle;
logoResizeMode?: ImageResizeMode;
logoStyle?: ViewStyle;
rememberTabScrollPosition?: boolean;
renderBody?: (title: string) => ReactElement;
scrollEvent?: (event: NativeSyntheticEvent<NativeScrollEvent>) => void;
title?: string;
Expand Down

0 comments on commit 7c2d177

Please sign in to comment.