Skip to content

armgit / vaul

Public Imported from GitHub
0

A drawer component for React.

Polish snap points (#118)

Emil Kowalski8a79e5f92fdb
diff --git a/src/index.tsx b/src/index.tsx
index 3360850..f4bd723 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -1,7 +1,7 @@
'use client';
import * as DialogPrimitive from '@radix-ui/react-dialog';
-import React from 'react';
+import React, { useCallback } from 'react';
import { DrawerContext, useDrawerContext } from './context';
import './style.css';
import { usePreventScroll, isInput, isIOS } from './use-prevent-scroll';
@@ -95,6 +95,12 @@ function Root({
isOpen,
experimentalSafariThemeAnimation,
);
+
+ const onSnapPointChange = useCallback((activeSnapPointIndex: number) => {
+ // Change openTime ref when we reach the last snap point to prevent dragging for 500ms incase it's scrollable.
+ if (snapPoints && activeSnapPointIndex === snapPointsOffset.length - 1) openTime.current = new Date();
+ }, []);
+
const {
activeSnapPoint,
activeSnapPointIndex,
@@ -111,6 +117,7 @@ function Root({
drawerRef,
fadeFromIndex,
overlayRef,
+ onSnapPointChange,
});
usePreventScroll({
diff --git a/src/use-snap-points.ts b/src/use-snap-points.ts
index f2389b5..7fd1cf3 100644
--- a/src/use-snap-points.ts
+++ b/src/use-snap-points.ts
@@ -10,6 +10,7 @@ export function useSnapPoints({
drawerRef,
overlayRef,
fadeFromIndex,
+ onSnapPointChange,
}: {
activeSnapPointProp?: number | string | null;
setActiveSnapPointProp?(snapPoint: number | null | string): void;
@@ -17,6 +18,7 @@ export function useSnapPoints({
fadeFromIndex?: number;
drawerRef: React.RefObject<HTMLDivElement>;
overlayRef: React.RefObject<HTMLDivElement>;
+ onSnapPointChange(activeSnapPointIndex: number): void;
}) {
const [activeSnapPoint, setActiveSnapPoint] = useControllableState<string | number | null>({
prop: activeSnapPointProp,
@@ -50,6 +52,7 @@ export function useSnapPoints({
}
const height = isPx ? snapPointAsNumber : hasWindow ? snapPoint * window.innerHeight : 0;
+
if (hasWindow) {
return window.innerHeight - height;
}
@@ -67,7 +70,7 @@ export function useSnapPoints({
const snapToPoint = React.useCallback(
(height: number) => {
const newSnapPointIndex = snapPointsOffset?.findIndex((snapPointHeight) => snapPointHeight === height) ?? null;
-
+ onSnapPointChange(newSnapPointIndex);
set(drawerRef.current, {
transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
transform: `translate3d(0, ${height}px, 0)`,
@@ -112,7 +115,7 @@ export function useSnapPoints({
closeDrawer: () => void;
velocity: number;
}) {
- if (typeof activeSnapPointOffset !== 'number' || fadeFromIndex === undefined) return;
+ if (fadeFromIndex === undefined) return;
const currentPosition = activeSnapPointOffset - draggedDistance;
const isOverlaySnapPoint = activeSnapPointIndex === fadeFromIndex - 1;
@@ -144,8 +147,12 @@ 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;
+
// Don't do anything if we swipe upwards while being on the last snap point
- if (dragDirection > 0 && isLastSnapPoint) return;
+ if (dragDirection > 0 && isLastSnapPoint) {
+ snapToPoint(snapPointsOffset[snapPoints.length - 1]);
+ return;
+ }
if (isFirst && dragDirection < 0) {
closeDrawer();
@@ -164,21 +171,14 @@ export function useSnapPoints({
if (activeSnapPointOffset === null) return;
const newYValue = activeSnapPointOffset - draggedDistance;
- if (newYValue < snapPointsOffset[snapPointsOffset.length - 1]) {
- setActiveSnapPoint(snapPoints?.[snapPoints.length - 1] ?? null);
- set(drawerRef.current, {
- transform: `translate3d(0, ${0}px, 0)`,
- });
- return;
- }
-
set(drawerRef.current, {
transform: `translate3d(0, ${newYValue}px, 0)`,
});
}
function getPercentageDragged(absDraggedDistance: number, isDraggingDown: boolean) {
- if (!snapPoints || !activeSnapPointIndex || !snapPointsOffset || fadeFromIndex === undefined) return null;
+ if (!snapPoints || typeof activeSnapPointIndex !== 'number' || !snapPointsOffset || fadeFromIndex === undefined)
+ return null;
// If this is true we are dragging to a snap point that is supposed to have an overlay
const isOverlaySnapPoint = activeSnapPointIndex === fadeFromIndex - 1;
@@ -191,6 +191,7 @@ export function useSnapPoints({
// Don't animate, but still use this one if we are dragging away from the overlaySnapPoint
if (isOverlaySnapPoint && !isDraggingDown) return 1;
if (!shouldFade && !isOverlaySnapPoint) return null;
+
// Either fadeFrom index or the one before
const targetSnapPointIndex = isOverlaySnapPoint ? activeSnapPointIndex + 1 : activeSnapPointIndex - 1;