@trackunit/react-drawer 1.0.15 → 1.0.17
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 +13 -16
- package/index.esm.js +13 -16
- package/package.json +3 -3
package/index.cjs.js
CHANGED
|
@@ -416,37 +416,36 @@ const SNAP_POINT_TRANSITION = "transform 500ms cubic-bezier(0.32,0.72,0,1)";
|
|
|
416
416
|
*/
|
|
417
417
|
const SwipeableDrawer = react.forwardRef(({ open, onClose, onOpenGesture, onSnapPointChange, children, keepMountedWhenClosed, className, position = "bottom", snapPoints, activeSnapPoint: activeSnapPointProp = null, container, ...others }, ref) => {
|
|
418
418
|
const drawerRef = react.useRef(null);
|
|
419
|
-
const containerRef = react.useRef(container
|
|
419
|
+
const containerRef = react.useRef(container ?? null);
|
|
420
420
|
const containerGeometry = reactComponents.useGeometry(containerRef);
|
|
421
421
|
react.useImperativeHandle(ref, () => drawerRef.current);
|
|
422
422
|
const [activeSnapPoint, setActiveSnapPoint] = react.useState(activeSnapPointProp);
|
|
423
423
|
const [snapPointOffsets, setSnapPointOffsets] = react.useState([]);
|
|
424
424
|
const isLastSnapPoint = react.useMemo(() => {
|
|
425
|
-
return activeSnapPoint ===
|
|
425
|
+
return activeSnapPoint === snapPoints?.[snapPoints.length - 1];
|
|
426
426
|
}, [activeSnapPoint, snapPoints]);
|
|
427
427
|
const isFirstSnapPoint = react.useMemo(() => {
|
|
428
|
-
return activeSnapPoint ===
|
|
428
|
+
return activeSnapPoint === snapPoints?.[0];
|
|
429
429
|
}, [activeSnapPoint, snapPoints]);
|
|
430
|
-
const activeSnapPointIndex = react.useMemo(() =>
|
|
430
|
+
const activeSnapPointIndex = react.useMemo(() => snapPoints?.findIndex(snapPoint => snapPoint === activeSnapPoint) ?? -1, [snapPoints, activeSnapPoint]);
|
|
431
431
|
const onHandleDragEnd = react.useCallback((draggedBy, velocity) => {
|
|
432
|
-
var _a, _b, _c;
|
|
433
432
|
if (!snapPoints || !snapPoints.length) {
|
|
434
433
|
return;
|
|
435
434
|
}
|
|
436
435
|
const direction = draggedBy > 0 ? -1 : 1;
|
|
437
436
|
// If the velocity is greater than the threshold and the dragged distance is less than 40% of the container height, snap to the next snap point
|
|
438
437
|
if (velocity > VELOCITY_THRESHOLD && Math.abs(draggedBy) < containerGeometry.height * 0.4) {
|
|
439
|
-
const newSnapPoint =
|
|
438
|
+
const newSnapPoint = snapPoints[activeSnapPointIndex + direction] ?? null;
|
|
440
439
|
if (newSnapPoint) {
|
|
441
440
|
setActiveSnapPoint(newSnapPoint);
|
|
442
|
-
onSnapPointChange
|
|
441
|
+
onSnapPointChange?.(newSnapPoint);
|
|
443
442
|
}
|
|
444
443
|
return;
|
|
445
444
|
}
|
|
446
445
|
// Otherwise, find the closest snap point
|
|
447
446
|
// This is when the user is dragging but not swiping.
|
|
448
447
|
// For example, when the user is dragging the drawer to the bottom but not fast enough to trigger a swipe.
|
|
449
|
-
const currentPosition = (
|
|
448
|
+
const currentPosition = (snapPointOffsets[activeSnapPointIndex] ?? 0) + draggedBy;
|
|
450
449
|
const closestSnapPoint = snapPointOffsets.reduce((prev, curr) => {
|
|
451
450
|
if (typeof prev !== "number" || typeof curr !== "number") {
|
|
452
451
|
return prev;
|
|
@@ -454,12 +453,12 @@ const SwipeableDrawer = react.forwardRef(({ open, onClose, onOpenGesture, onSnap
|
|
|
454
453
|
return Math.abs(curr - currentPosition) < Math.abs(prev - currentPosition) ? curr : prev;
|
|
455
454
|
});
|
|
456
455
|
const snapPointIndex = snapPointOffsets.findIndex(offset => offset === closestSnapPoint);
|
|
457
|
-
const nextSnapPoint =
|
|
456
|
+
const nextSnapPoint = snapPoints[snapPointIndex] ?? null;
|
|
458
457
|
if (!nextSnapPoint) {
|
|
459
458
|
return;
|
|
460
459
|
}
|
|
461
460
|
setActiveSnapPoint(nextSnapPoint);
|
|
462
|
-
onSnapPointChange
|
|
461
|
+
onSnapPointChange?.(nextSnapPoint);
|
|
463
462
|
}, [snapPoints, snapPointOffsets, activeSnapPointIndex, containerGeometry, onSnapPointChange]);
|
|
464
463
|
const { handlers, isDragging, dragDistance } = useSwipeHandlers({
|
|
465
464
|
ref: drawerRef,
|
|
@@ -511,17 +510,16 @@ const SwipeableDrawer = react.forwardRef(({ open, onClose, onOpenGesture, onSnap
|
|
|
511
510
|
}
|
|
512
511
|
}, [container]);
|
|
513
512
|
react.useEffect(() => {
|
|
514
|
-
var _a;
|
|
515
513
|
if (!snapPoints || snapPoints.length === 0) {
|
|
516
514
|
return;
|
|
517
515
|
}
|
|
518
516
|
const snapPointIndex = snapPoints.findIndex(offset => offset === activeSnapPointProp);
|
|
519
|
-
const nextSnapPoint =
|
|
517
|
+
const nextSnapPoint = snapPoints[snapPointIndex > -1 ? snapPointIndex : 0] ?? null;
|
|
520
518
|
if (!nextSnapPoint) {
|
|
521
519
|
return;
|
|
522
520
|
}
|
|
523
521
|
setActiveSnapPoint(nextSnapPoint);
|
|
524
|
-
onSnapPointChange
|
|
522
|
+
onSnapPointChange?.(nextSnapPoint);
|
|
525
523
|
}, [snapPoints, activeSnapPointProp, onSnapPointChange]);
|
|
526
524
|
const drawerStyle = react.useMemo(() => {
|
|
527
525
|
if (isDragging) {
|
|
@@ -539,13 +537,12 @@ const SwipeableDrawer = react.forwardRef(({ open, onClose, onOpenGesture, onSnap
|
|
|
539
537
|
return {};
|
|
540
538
|
}, [isDragging, dragDistance, snapPointOffsets, activeSnapPointIndex, snapPoints]);
|
|
541
539
|
react.useEffect(() => {
|
|
542
|
-
var _a, _b;
|
|
543
540
|
if (!drawerRef.current) {
|
|
544
541
|
return;
|
|
545
542
|
}
|
|
546
543
|
if (open) {
|
|
547
|
-
drawerRef.current.style.transform =
|
|
548
|
-
drawerRef.current.style.transition =
|
|
544
|
+
drawerRef.current.style.transform = drawerStyle.transform ?? "";
|
|
545
|
+
drawerRef.current.style.transition = drawerStyle.transition ?? "";
|
|
549
546
|
}
|
|
550
547
|
else {
|
|
551
548
|
drawerRef.current.style.transform = "";
|
package/index.esm.js
CHANGED
|
@@ -414,37 +414,36 @@ const SNAP_POINT_TRANSITION = "transform 500ms cubic-bezier(0.32,0.72,0,1)";
|
|
|
414
414
|
*/
|
|
415
415
|
const SwipeableDrawer = forwardRef(({ open, onClose, onOpenGesture, onSnapPointChange, children, keepMountedWhenClosed, className, position = "bottom", snapPoints, activeSnapPoint: activeSnapPointProp = null, container, ...others }, ref) => {
|
|
416
416
|
const drawerRef = useRef(null);
|
|
417
|
-
const containerRef = useRef(container
|
|
417
|
+
const containerRef = useRef(container ?? null);
|
|
418
418
|
const containerGeometry = useGeometry(containerRef);
|
|
419
419
|
useImperativeHandle(ref, () => drawerRef.current);
|
|
420
420
|
const [activeSnapPoint, setActiveSnapPoint] = useState(activeSnapPointProp);
|
|
421
421
|
const [snapPointOffsets, setSnapPointOffsets] = useState([]);
|
|
422
422
|
const isLastSnapPoint = useMemo(() => {
|
|
423
|
-
return activeSnapPoint ===
|
|
423
|
+
return activeSnapPoint === snapPoints?.[snapPoints.length - 1];
|
|
424
424
|
}, [activeSnapPoint, snapPoints]);
|
|
425
425
|
const isFirstSnapPoint = useMemo(() => {
|
|
426
|
-
return activeSnapPoint ===
|
|
426
|
+
return activeSnapPoint === snapPoints?.[0];
|
|
427
427
|
}, [activeSnapPoint, snapPoints]);
|
|
428
|
-
const activeSnapPointIndex = useMemo(() =>
|
|
428
|
+
const activeSnapPointIndex = useMemo(() => snapPoints?.findIndex(snapPoint => snapPoint === activeSnapPoint) ?? -1, [snapPoints, activeSnapPoint]);
|
|
429
429
|
const onHandleDragEnd = useCallback((draggedBy, velocity) => {
|
|
430
|
-
var _a, _b, _c;
|
|
431
430
|
if (!snapPoints || !snapPoints.length) {
|
|
432
431
|
return;
|
|
433
432
|
}
|
|
434
433
|
const direction = draggedBy > 0 ? -1 : 1;
|
|
435
434
|
// If the velocity is greater than the threshold and the dragged distance is less than 40% of the container height, snap to the next snap point
|
|
436
435
|
if (velocity > VELOCITY_THRESHOLD && Math.abs(draggedBy) < containerGeometry.height * 0.4) {
|
|
437
|
-
const newSnapPoint =
|
|
436
|
+
const newSnapPoint = snapPoints[activeSnapPointIndex + direction] ?? null;
|
|
438
437
|
if (newSnapPoint) {
|
|
439
438
|
setActiveSnapPoint(newSnapPoint);
|
|
440
|
-
onSnapPointChange
|
|
439
|
+
onSnapPointChange?.(newSnapPoint);
|
|
441
440
|
}
|
|
442
441
|
return;
|
|
443
442
|
}
|
|
444
443
|
// Otherwise, find the closest snap point
|
|
445
444
|
// This is when the user is dragging but not swiping.
|
|
446
445
|
// For example, when the user is dragging the drawer to the bottom but not fast enough to trigger a swipe.
|
|
447
|
-
const currentPosition = (
|
|
446
|
+
const currentPosition = (snapPointOffsets[activeSnapPointIndex] ?? 0) + draggedBy;
|
|
448
447
|
const closestSnapPoint = snapPointOffsets.reduce((prev, curr) => {
|
|
449
448
|
if (typeof prev !== "number" || typeof curr !== "number") {
|
|
450
449
|
return prev;
|
|
@@ -452,12 +451,12 @@ const SwipeableDrawer = forwardRef(({ open, onClose, onOpenGesture, onSnapPointC
|
|
|
452
451
|
return Math.abs(curr - currentPosition) < Math.abs(prev - currentPosition) ? curr : prev;
|
|
453
452
|
});
|
|
454
453
|
const snapPointIndex = snapPointOffsets.findIndex(offset => offset === closestSnapPoint);
|
|
455
|
-
const nextSnapPoint =
|
|
454
|
+
const nextSnapPoint = snapPoints[snapPointIndex] ?? null;
|
|
456
455
|
if (!nextSnapPoint) {
|
|
457
456
|
return;
|
|
458
457
|
}
|
|
459
458
|
setActiveSnapPoint(nextSnapPoint);
|
|
460
|
-
onSnapPointChange
|
|
459
|
+
onSnapPointChange?.(nextSnapPoint);
|
|
461
460
|
}, [snapPoints, snapPointOffsets, activeSnapPointIndex, containerGeometry, onSnapPointChange]);
|
|
462
461
|
const { handlers, isDragging, dragDistance } = useSwipeHandlers({
|
|
463
462
|
ref: drawerRef,
|
|
@@ -509,17 +508,16 @@ const SwipeableDrawer = forwardRef(({ open, onClose, onOpenGesture, onSnapPointC
|
|
|
509
508
|
}
|
|
510
509
|
}, [container]);
|
|
511
510
|
useEffect(() => {
|
|
512
|
-
var _a;
|
|
513
511
|
if (!snapPoints || snapPoints.length === 0) {
|
|
514
512
|
return;
|
|
515
513
|
}
|
|
516
514
|
const snapPointIndex = snapPoints.findIndex(offset => offset === activeSnapPointProp);
|
|
517
|
-
const nextSnapPoint =
|
|
515
|
+
const nextSnapPoint = snapPoints[snapPointIndex > -1 ? snapPointIndex : 0] ?? null;
|
|
518
516
|
if (!nextSnapPoint) {
|
|
519
517
|
return;
|
|
520
518
|
}
|
|
521
519
|
setActiveSnapPoint(nextSnapPoint);
|
|
522
|
-
onSnapPointChange
|
|
520
|
+
onSnapPointChange?.(nextSnapPoint);
|
|
523
521
|
}, [snapPoints, activeSnapPointProp, onSnapPointChange]);
|
|
524
522
|
const drawerStyle = useMemo(() => {
|
|
525
523
|
if (isDragging) {
|
|
@@ -537,13 +535,12 @@ const SwipeableDrawer = forwardRef(({ open, onClose, onOpenGesture, onSnapPointC
|
|
|
537
535
|
return {};
|
|
538
536
|
}, [isDragging, dragDistance, snapPointOffsets, activeSnapPointIndex, snapPoints]);
|
|
539
537
|
useEffect(() => {
|
|
540
|
-
var _a, _b;
|
|
541
538
|
if (!drawerRef.current) {
|
|
542
539
|
return;
|
|
543
540
|
}
|
|
544
541
|
if (open) {
|
|
545
|
-
drawerRef.current.style.transform =
|
|
546
|
-
drawerRef.current.style.transition =
|
|
542
|
+
drawerRef.current.style.transform = drawerStyle.transform ?? "";
|
|
543
|
+
drawerRef.current.style.transition = drawerStyle.transition ?? "";
|
|
547
544
|
}
|
|
548
545
|
else {
|
|
549
546
|
drawerRef.current.style.transform = "";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/react-drawer",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.17",
|
|
4
4
|
"repository": "https://github.com/Trackunit/manager",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"engines": {
|
|
@@ -9,10 +9,10 @@
|
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"react": "18.3.1",
|
|
11
11
|
"react-swipeable": "^7.0.1",
|
|
12
|
-
"@trackunit/react-components": "^1.0
|
|
12
|
+
"@trackunit/react-components": "^1.1.0",
|
|
13
13
|
"@trackunit/css-class-variance-utilities": "^1.0.1",
|
|
14
14
|
"@trackunit/ui-icons": "^1.0.3",
|
|
15
|
-
"@trackunit/i18n-library-translation": "^1.0.
|
|
15
|
+
"@trackunit/i18n-library-translation": "^1.0.8"
|
|
16
16
|
},
|
|
17
17
|
"module": "./index.esm.js",
|
|
18
18
|
"main": "./index.cjs.js",
|