Skip to content

Commit

Permalink
fix: readjust defaultSnap on resize
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Dec 7, 2020
1 parent eb07741 commit b812786
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,10 @@ export const BottomSheet = React.forwardRef<
defaultSnapRef.current = findSnapRef.current(getDefaultSnap)
}, [getDefaultSnap, ready])

// @TODO can be renamed or deleted
// Wether to interpolate refs or states, useful when needing to transition between changed snapshot bounds
const shouldInterpolateRefs = useRef(false)

// Adjust the height whenever the snap points are changed due to resize events
const springOnResize = useRef(false)
useEffect(() => {
if (shouldInterpolateRefs.current) {
if (springOnResize.current) {
set({
// @ts-expect-error
to: async (next) => {
Expand Down Expand Up @@ -191,8 +188,11 @@ export const BottomSheet = React.forwardRef<
immediate: prefersReducedMotion.current,
})

//maxSnapRef.current = maxSnap
//minSnapRef.current = minSnap
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' })

Expand Down Expand Up @@ -247,7 +247,7 @@ export const BottomSheet = React.forwardRef<
// @ts-expect-error
to: async (next) => {
console.group('OPEN')
shouldInterpolateRefs.current = false
springOnResize.current = false

if (maybeCancel()) return

Expand Down Expand Up @@ -318,7 +318,7 @@ export const BottomSheet = React.forwardRef<
if (maybeCancel()) return

heightRef.current = defaultSnapRef.current
shouldInterpolateRefs.current = true
springOnResize.current = true
await next({
y: defaultSnapRef.current,
backdrop: 1,
Expand Down Expand Up @@ -375,7 +375,7 @@ export const BottomSheet = React.forwardRef<
set({
// @ts-expect-error
to: async (next) => {
shouldInterpolateRefs.current = false
springOnResize.current = false
console.group('CLOSE')
if (maybeCancel()) return

Expand Down Expand Up @@ -414,7 +414,7 @@ export const BottomSheet = React.forwardRef<
await onSpringEndRef.current?.({ type: 'CLOSE' })

if (!cancelled) {
shouldInterpolateRefs.current = true
springOnResize.current = true
console.groupEnd()
}
},
Expand Down Expand Up @@ -502,13 +502,13 @@ export const BottomSheet = React.forwardRef<
console.log({ first, memo })
if (first) {
console.log('first ', { memo })
shouldInterpolateRefs.current = false
springOnResize.current = false
}

// Cancel the drag operation if the canDrag state changed
if (!canDragRef.current) {
console.log('handleDrag cancelled dragging because canDragRef is false')
shouldInterpolateRefs.current = true
springOnResize.current = true
cancel()
return
}
Expand All @@ -518,7 +518,7 @@ export const BottomSheet = React.forwardRef<
newY = findSnap(newY)
heightRef.current = newY
lastSnapRef.current = newY
shouldInterpolateRefs.current = true
springOnResize.current = true
}

set({
Expand Down

0 comments on commit b812786

Please sign in to comment.