Skip to content

Commit

Permalink
Fix: non dismissible snap points and onRelease handler (#179)
Browse files Browse the repository at this point in the history
* fix: non dismissible drawer with snap points

* fix: onRelease handler previously not being called
  • Loading branch information
rortan134 authored Dec 4, 2023
1 parent 68038f1 commit 6a1e42a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,9 @@ function Root({
draggedDistance: distMoved,
closeDrawer,
velocity,
dismissible,
});
onReleaseProp?.(event, true);
return;
}

Expand Down
15 changes: 9 additions & 6 deletions src/use-snap-points.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,29 +110,33 @@ export function useSnapPoints({
draggedDistance,
closeDrawer,
velocity,
dismissible,
}: {
draggedDistance: number;
closeDrawer: () => void;
velocity: number;
dismissible: boolean;
}) {
if (fadeFromIndex === undefined) return;

const currentPosition = activeSnapPointOffset - draggedDistance;
const isOverlaySnapPoint = activeSnapPointIndex === fadeFromIndex - 1;
const isFirst = activeSnapPointIndex === 0;
const hasDraggedUp = draggedDistance > 0;

if (isOverlaySnapPoint) {
set(overlayRef.current, {
transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
});
}

if (velocity > 2 && draggedDistance < 0) {
closeDrawer();
if (velocity > 2 && !hasDraggedUp) {
if (dismissible) closeDrawer();
else snapToPoint(snapPointsOffset[0]); // snap to initial point
return;
}

if (velocity > 2 && draggedDistance > 0 && snapPointsOffset && snapPoints) {
if (velocity > 2 && hasDraggedUp && snapPointsOffset && snapPoints) {
snapToPoint(snapPointsOffset[snapPoints.length - 1] as number);
return;
}
Expand All @@ -145,16 +149,15 @@ export function useSnapPoints({
});

if (velocity > VELOCITY_THRESHOLD && Math.abs(draggedDistance) < window.innerHeight * 0.4) {
// -1 = down, 1 = up, might need a better name
const dragDirection = draggedDistance > 0 ? 1 : -1;
const dragDirection = hasDraggedUp ? 1 : -1; // 1 = up, -1 = down

// Don't do anything if we swipe upwards while being on the last snap point
if (dragDirection > 0 && isLastSnapPoint) {
snapToPoint(snapPointsOffset[snapPoints.length - 1]);
return;
}

if (isFirst && dragDirection < 0) {
if (isFirst && dragDirection < 0 && dismissible) {
closeDrawer();
}

Expand Down

0 comments on commit 6a1e42a

Please sign in to comment.