Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Non-Dismissable Swiping #276

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,28 @@ function Root({
(pointerStart.current - (isVertical(direction) ? event.screenY : event.screenX)) * directionMultiplier;
const isDraggingInDirection = draggedDistance > 0;

// Pre condition for disallowing dragging in the close direction.
const noCloseSnapPointsPreCondition = snapPoints && !dismissible && !isDraggingInDirection;

// Disallow dragging down to close when first snap point is the active one and dismissible prop is set to false.
if (snapPoints && activeSnapPointIndex === 0 && !dismissible) return;
if (noCloseSnapPointsPreCondition && activeSnapPointIndex === 0) return;

// We need to capture last time when drag with scroll was triggered and have a timeout between
const absDraggedDistance = Math.abs(draggedDistance);
const wrapper = document.querySelector('[vaul-drawer-wrapper]');

// Calculate the percentage dragged, where 1 is the closed position
let percentageDragged = absDraggedDistance / drawerHeightRef.current;
const snapPointPercentageDragged = getSnapPointsPercentageDragged(absDraggedDistance, isDraggingInDirection);

if (snapPointPercentageDragged !== null) {
percentageDragged = snapPointPercentageDragged;
}

// Disallow close dragging beyond the smallest snap point.
if (noCloseSnapPointsPreCondition && percentageDragged >= 1) {
return;
}

if (!isAllowedToDrag.current && !shouldDrag(event.target, isDraggingInDirection)) return;
drawerRef.current.classList.add(DRAG_CLASS);
Expand Down Expand Up @@ -267,17 +287,6 @@ function Root({
return;
}

// We need to capture last time when drag with scroll was triggered and have a timeout between
const absDraggedDistance = Math.abs(draggedDistance);
const wrapper = document.querySelector('[vaul-drawer-wrapper]');

let percentageDragged = absDraggedDistance / drawerHeightRef.current;
const snapPointPercentageDragged = getSnapPointsPercentageDragged(absDraggedDistance, isDraggingInDirection);

if (snapPointPercentageDragged !== null) {
percentageDragged = snapPointPercentageDragged;
}

const opacityValue = 1 - percentageDragged;

if (shouldFade || (fadeFromIndex && activeSnapPointIndex === fadeFromIndex - 1)) {
Expand Down