Skip to content

Commit 549c153

Browse files
author
Oscar Franco
authored
Modify logic to calculate snapping after animation (#163)
* Modify logic to calculate snapping after animation * fix: missmatched typescript types * fix: bottom sheet not staying on initial snap point * fix: modify lower point to avoid errors on android
1 parent 51bf135 commit 549c153

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

src/index.tsx

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,19 @@ export default class BottomSheetBehavior extends React.Component<Props, State> {
345345
this.state = BottomSheetBehavior.getDerivedStateFromProps(props, undefined)
346346

347347
const { snapPoints, init } = this.state
348-
const middlesOfSnapPoints: Animated.Node<number>[] = []
348+
const middlesOfSnapPoints: [
349+
Animated.Node<number>,
350+
Animated.Node<number>
351+
][] = []
352+
349353
for (let i = 1; i < snapPoints.length; i++) {
350-
middlesOfSnapPoints.push(divide(add(snapPoints[i - 1], snapPoints[i]), 2))
354+
const tuple: [Animated.Node<number>, Animated.Node<number>] = [
355+
add(snapPoints[i - 1], 10),
356+
sub(snapPoints[i], 25),
357+
]
358+
middlesOfSnapPoints.push(tuple)
351359
}
360+
352361
const masterOffseted = new Value(init)
353362
// destination point is a approximation of movement if finger released
354363
const tossForMaster =
@@ -360,14 +369,35 @@ export default class BottomSheetBehavior extends React.Component<Props, State> {
360369
masterOffseted,
361370
multiply(tossForMaster, this.masterVelocity)
362371
)
372+
373+
const positive = greaterOrEq(
374+
multiply(tossForMaster, this.masterVelocity),
375+
0
376+
)
363377
// method for generating condition for finding the nearest snap point
364378
const currentSnapPoint = (i = 0): Animated.Node<number> =>
365379
i + 1 === snapPoints.length
366380
? snapPoints[i]
367381
: cond(
368-
lessThan(destinationPoint, middlesOfSnapPoints[i]),
369-
snapPoints[i],
370-
currentSnapPoint(i + 1)
382+
positive,
383+
cond(
384+
greaterThan(destinationPoint, middlesOfSnapPoints[i][0]),
385+
cond(
386+
lessThan(destinationPoint, middlesOfSnapPoints[i][1]),
387+
snapPoints[i + 1],
388+
currentSnapPoint(i + 1)
389+
),
390+
snapPoints[i]
391+
),
392+
cond(
393+
greaterThan(destinationPoint, middlesOfSnapPoints[i][1]),
394+
cond(
395+
lessThan(destinationPoint, middlesOfSnapPoints[i][0]),
396+
snapPoints[i + 1],
397+
currentSnapPoint(i + 1)
398+
),
399+
snapPoints[i]
400+
)
371401
)
372402
// current snap point desired
373403
this.snapPoint = currentSnapPoint()

0 commit comments

Comments
 (0)