Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Changed some naming conventions
  • Loading branch information
AdityaPahilwani committed Jul 5, 2021
commit 57a818950c78948b41a83151e5eaa5dc0c90206c
19 changes: 11 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ export default class extends Component {
initState.height = height - verticalDiff
}

// We duplicate 2 elements on both ends of list, so we need to consider this value
// while calculating offset because 1 element is actually now positioned at 3 element in list
// that's why 1+2=3
let loopVal = this.props.loop ? 2 : 0
initState.offset[initState.dir] =
initState.dir === 'y'
Expand All @@ -320,7 +323,7 @@ export default class extends Component {
this.internals = {
...this.internals,
isScrolling: false,
adjacentViewDiffWidth: adjacentViewDiffWidth
adjacentViewDiffWidth
}
return initState
}
Expand All @@ -334,23 +337,23 @@ export default class extends Component {
const { width, height } = event.nativeEvent.layout
const offset = (this.internals.offset = { x: 0, y: 0 })
const isHorizontal = this.props.horizontal
const diffOffset = this.internals.adjacentViewDiffWidth
const adjacentViewDiffWidth = 2 * diffOffset
const { adjacentViewDiffWidth } = this.internals
const totalAdjacentWidth = 2 * adjacentViewDiffWidth
const state = {
width: width - (isHorizontal ? adjacentViewDiffWidth : 0),
height: height - (isHorizontal ? 0 : adjacentViewDiffWidth)
width: width - (isHorizontal ? totalAdjacentWidth : 0),
height: height - (isHorizontal ? 0 : totalAdjacentWidth)
}

if (this.state.total > 1) {
let setup = this.state.index
if (this.props.loop) {
setup += 2
}
/// ScrollView renders from 0 pixels but we want an custom offset to scrollTo so our adjacent views can be displayed
/// ScrollView renders from 0 pixels but we want a custom offset to scrollTo so our adjacent views can be displayed
offset[this.state.dir] =
this.state.dir === 'y'
? state.height * setup - diffOffset
: state.width * setup - diffOffset
? state.height * setup - adjacentViewDiffWidth
: state.width * setup - adjacentViewDiffWidth
}

// only update the offset in state if needed, updating offset while swiping
Expand Down