@trackunit/react-drawer 0.3.13 → 0.3.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs.js +19 -1
- package/index.esm.js +19 -1
- package/package.json +1 -1
- package/src/hooks/useSwipeHandlers.d.ts +2 -1
package/index.cjs.js
CHANGED
|
@@ -342,7 +342,7 @@ const isPassedThreshold = (dragDistance, threshold) => {
|
|
|
342
342
|
/**
|
|
343
343
|
* Hook for handling swipe events on the drawer.
|
|
344
344
|
*/
|
|
345
|
-
const useSwipeHandlers = ({ ref, closingThreshold, onCloseGesture, onOpenGesture, onDragEnd, }) => {
|
|
345
|
+
const useSwipeHandlers = ({ ref, closingThreshold, onCloseGesture, onOpenGesture, onDragEnd, constrainDrag, }) => {
|
|
346
346
|
const [isDragging, setIsDragging] = react.useState(false);
|
|
347
347
|
const [dragDistance, setDragDistance] = react.useState(0);
|
|
348
348
|
const currentY = react.useRef(0);
|
|
@@ -360,6 +360,9 @@ const useSwipeHandlers = ({ ref, closingThreshold, onCloseGesture, onOpenGesture
|
|
|
360
360
|
},
|
|
361
361
|
onSwiping: (e) => {
|
|
362
362
|
const newY = currentY.current + e.deltaY;
|
|
363
|
+
if (constrainDrag && constrainDrag(e.deltaY)) {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
363
366
|
if (newY >= 0) {
|
|
364
367
|
setDragDistance(newY);
|
|
365
368
|
}
|
|
@@ -420,6 +423,12 @@ const SwipeableDrawer = react.forwardRef(({ open, onClose, onOpenGesture, onSnap
|
|
|
420
423
|
react.useImperativeHandle(ref, () => drawerRef.current);
|
|
421
424
|
const [activeSnapPoint, setActiveSnapPoint] = react.useState(activeSnapPointProp);
|
|
422
425
|
const [snapPointOffsets, setSnapPointOffsets] = react.useState([]);
|
|
426
|
+
const isLastSnapPoint = react.useMemo(() => {
|
|
427
|
+
return activeSnapPoint === (snapPoints === null || snapPoints === void 0 ? void 0 : snapPoints[snapPoints.length - 1]);
|
|
428
|
+
}, [activeSnapPoint, snapPoints]);
|
|
429
|
+
const isFirstSnapPoint = react.useMemo(() => {
|
|
430
|
+
return activeSnapPoint === (snapPoints === null || snapPoints === void 0 ? void 0 : snapPoints[0]);
|
|
431
|
+
}, [activeSnapPoint, snapPoints]);
|
|
423
432
|
const activeSnapPointIndex = react.useMemo(() => { var _a; return (_a = snapPoints === null || snapPoints === void 0 ? void 0 : snapPoints.findIndex(snapPoint => snapPoint === activeSnapPoint)) !== null && _a !== void 0 ? _a : -1; }, [snapPoints, activeSnapPoint]);
|
|
424
433
|
const onHandleDragEnd = react.useCallback((draggedBy, velocity) => {
|
|
425
434
|
var _a, _b, _c;
|
|
@@ -468,6 +477,15 @@ const SwipeableDrawer = react.forwardRef(({ open, onClose, onOpenGesture, onSnap
|
|
|
468
477
|
}
|
|
469
478
|
},
|
|
470
479
|
onDragEnd: onHandleDragEnd,
|
|
480
|
+
constrainDrag: (y) => {
|
|
481
|
+
if (isLastSnapPoint) {
|
|
482
|
+
return y <= 0;
|
|
483
|
+
}
|
|
484
|
+
if (isFirstSnapPoint) {
|
|
485
|
+
return y >= 0;
|
|
486
|
+
}
|
|
487
|
+
return false;
|
|
488
|
+
},
|
|
471
489
|
});
|
|
472
490
|
useScrollBlock({ isDisabled: !open || isDragging });
|
|
473
491
|
react.useEffect(() => {
|
package/index.esm.js
CHANGED
|
@@ -340,7 +340,7 @@ const isPassedThreshold = (dragDistance, threshold) => {
|
|
|
340
340
|
/**
|
|
341
341
|
* Hook for handling swipe events on the drawer.
|
|
342
342
|
*/
|
|
343
|
-
const useSwipeHandlers = ({ ref, closingThreshold, onCloseGesture, onOpenGesture, onDragEnd, }) => {
|
|
343
|
+
const useSwipeHandlers = ({ ref, closingThreshold, onCloseGesture, onOpenGesture, onDragEnd, constrainDrag, }) => {
|
|
344
344
|
const [isDragging, setIsDragging] = useState(false);
|
|
345
345
|
const [dragDistance, setDragDistance] = useState(0);
|
|
346
346
|
const currentY = useRef(0);
|
|
@@ -358,6 +358,9 @@ const useSwipeHandlers = ({ ref, closingThreshold, onCloseGesture, onOpenGesture
|
|
|
358
358
|
},
|
|
359
359
|
onSwiping: (e) => {
|
|
360
360
|
const newY = currentY.current + e.deltaY;
|
|
361
|
+
if (constrainDrag && constrainDrag(e.deltaY)) {
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
361
364
|
if (newY >= 0) {
|
|
362
365
|
setDragDistance(newY);
|
|
363
366
|
}
|
|
@@ -418,6 +421,12 @@ const SwipeableDrawer = forwardRef(({ open, onClose, onOpenGesture, onSnapPointC
|
|
|
418
421
|
useImperativeHandle(ref, () => drawerRef.current);
|
|
419
422
|
const [activeSnapPoint, setActiveSnapPoint] = useState(activeSnapPointProp);
|
|
420
423
|
const [snapPointOffsets, setSnapPointOffsets] = useState([]);
|
|
424
|
+
const isLastSnapPoint = useMemo(() => {
|
|
425
|
+
return activeSnapPoint === (snapPoints === null || snapPoints === void 0 ? void 0 : snapPoints[snapPoints.length - 1]);
|
|
426
|
+
}, [activeSnapPoint, snapPoints]);
|
|
427
|
+
const isFirstSnapPoint = useMemo(() => {
|
|
428
|
+
return activeSnapPoint === (snapPoints === null || snapPoints === void 0 ? void 0 : snapPoints[0]);
|
|
429
|
+
}, [activeSnapPoint, snapPoints]);
|
|
421
430
|
const activeSnapPointIndex = useMemo(() => { var _a; return (_a = snapPoints === null || snapPoints === void 0 ? void 0 : snapPoints.findIndex(snapPoint => snapPoint === activeSnapPoint)) !== null && _a !== void 0 ? _a : -1; }, [snapPoints, activeSnapPoint]);
|
|
422
431
|
const onHandleDragEnd = useCallback((draggedBy, velocity) => {
|
|
423
432
|
var _a, _b, _c;
|
|
@@ -466,6 +475,15 @@ const SwipeableDrawer = forwardRef(({ open, onClose, onOpenGesture, onSnapPointC
|
|
|
466
475
|
}
|
|
467
476
|
},
|
|
468
477
|
onDragEnd: onHandleDragEnd,
|
|
478
|
+
constrainDrag: (y) => {
|
|
479
|
+
if (isLastSnapPoint) {
|
|
480
|
+
return y <= 0;
|
|
481
|
+
}
|
|
482
|
+
if (isFirstSnapPoint) {
|
|
483
|
+
return y >= 0;
|
|
484
|
+
}
|
|
485
|
+
return false;
|
|
486
|
+
},
|
|
469
487
|
});
|
|
470
488
|
useScrollBlock({ isDisabled: !open || isDragging });
|
|
471
489
|
useEffect(() => {
|
package/package.json
CHANGED
|
@@ -5,6 +5,7 @@ interface UseSwipeHandlersProps {
|
|
|
5
5
|
onCloseGesture?: () => void;
|
|
6
6
|
onOpenGesture?: () => void;
|
|
7
7
|
onDragEnd?: (dragDistance: number, velocity: number) => void;
|
|
8
|
+
constrainDrag?: (dragDistance: number) => boolean;
|
|
8
9
|
}
|
|
9
10
|
/**
|
|
10
11
|
* Retrieves the Y-axis translation value of a given HTML element.
|
|
@@ -16,7 +17,7 @@ export declare const getElementYTranslation: (element?: HTMLElement | null) => n
|
|
|
16
17
|
/**
|
|
17
18
|
* Hook for handling swipe events on the drawer.
|
|
18
19
|
*/
|
|
19
|
-
export declare const useSwipeHandlers: ({ ref, closingThreshold, onCloseGesture, onOpenGesture, onDragEnd, }: UseSwipeHandlersProps) => {
|
|
20
|
+
export declare const useSwipeHandlers: ({ ref, closingThreshold, onCloseGesture, onOpenGesture, onDragEnd, constrainDrag, }: UseSwipeHandlersProps) => {
|
|
20
21
|
handlers: import("react-swipeable").SwipeableHandlers;
|
|
21
22
|
isDragging: boolean;
|
|
22
23
|
dragDistance: number;
|