Skip to content

Commit

Permalink
fix: better velocity physics
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Jan 21, 2021
1 parent 1afe79f commit 65390c7
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import React, {
useImperativeHandle,
useRef,
} from 'react'
import { animated } from 'react-spring'
import { animated, config } from 'react-spring'
import { rubberbandIfOutOfBounds, useDrag } from 'react-use-gesture'
import {
useAriaHider,
Expand All @@ -33,6 +33,8 @@ import type {
} from './types'
import { debugging } from './utils'

const { tension, friction } = config.default

// @TODO implement AbortController to deal with race conditions

// @TODO rename to SpringBottomSheet and allow userland to import it directly, for those who want maximum control and minimal bundlesize
Expand Down Expand Up @@ -148,10 +150,24 @@ export const BottomSheet = React.forwardRef<

// New utility for using events safely
const asyncSet = useCallback<typeof set>(
({ onRest, ...opts }) =>
// @ts-expect-error
({ onRest, config: { velocity = 1, ...config } = {}, ...opts }) =>
new Promise((resolve) =>
set({
...opts,
config: {
velocity,
...config,
// @see https://springs.pomb.us
mass: 1,
// "stiffness"
tension,
// "damping"
friction: Math.max(
friction,
friction + (friction - friction * velocity)
),
},
onRest: (...args) => {
resolve(...args)
onRest?.(...args)
Expand Down Expand Up @@ -483,7 +499,13 @@ export const BottomSheet = React.forwardRef<
}

if (last) {
send('SNAP', { payload: { y: newY, velocity, source: 'dragging' } })
send('SNAP', {
payload: {
y: newY,
velocity: velocity > 0.05 ? velocity : 1,
source: 'dragging',
},
})

return memo
}
Expand Down

0 comments on commit 65390c7

Please sign in to comment.