Skip to content

Commit

Permalink
fix: more stable defaultSnap RESIZE sync
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Dec 7, 2020
1 parent 07910bd commit 230dbe2
Showing 1 changed file with 3 additions and 32 deletions.
35 changes: 3 additions & 32 deletions src/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,15 @@ export const BottomSheet = React.forwardRef<
const minSnapRef = useRef(minSnap)
const maxSnapRef = useRef(maxSnap)
const findSnapRef = useRef(findSnap)
const defaultSnapRef = useRef(0)
// Sync the refs with current state, giving the spring full control over when to respond to changes
useEffect(() => {
maxHeightRef.current = maxHeight
maxSnapRef.current = maxSnap
minSnapRef.current = minSnap
findSnapRef.current = findSnap
}, [findSnap, maxHeight, maxSnap, minSnap])

const defaultSnapRef = useRef(0)
useEffect(() => {
// Wait with selectin default snap until element dimensions are measured
if (!ready) return
console.count('selecting default snap')

defaultSnapRef.current = findSnapRef.current(getDefaultSnap)
}, [getDefaultSnap, ready])
defaultSnapRef.current = findSnap(getDefaultSnap)
}, [findSnap, getDefaultSnap, maxHeight, maxSnap, minSnap])

// Adjust the height whenever the snap points are changed due to resize events
const springOnResize = useRef(false)
Expand All @@ -160,22 +153,6 @@ export const BottomSheet = React.forwardRef<
onSpringStartRef.current?.({ type: 'RESIZE' })

const snap = findSnapRef.current(heightRef.current)

console.log('animate resize')

// adjust bounds so that the rubberband effects don't show while resizing
console.log(
'Resizing due to',
'maxSnap:',
maxSnapRef.current !== maxSnap,
'minSnap:',
minSnapRef.current !== minSnap
)
// Adjust bounds to have enough room for the transition
// maxSnapRef.current = Math.max(snap, heightRef.current)
//minSnapRef.current = Math.min(snap, heightRef.current)
console.log('new maxSnapRef', maxSnapRef.current)

heightRef.current = snap
lastSnapRef.current = snap

Expand All @@ -188,12 +165,6 @@ export const BottomSheet = React.forwardRef<
immediate: prefersReducedMotion.current,
})

maxSnapRef.current = maxSnap
minSnapRef.current = minSnap
// Ensure the default snap is using a correct value
// @TODO consider calling the prop to allow userland to decide a new default snap based on the new values
defaultSnapRef.current = findSnapRef.current(defaultSnapRef.current)

onSpringEndRef.current?.({ type: 'RESIZE' })

console.groupEnd()
Expand Down

0 comments on commit 230dbe2

Please sign in to comment.