Skip to content

Commit fff54f0

Browse files
committed
fix: refactor onSnapPointChange function
1 parent 2baad96 commit fff54f0

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

lib/Drawer/components/Root.vue

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,11 @@ const drawerRef = ref<ComponentPublicInstance | null>(null);
213213
const drawerHeightRef = ref(drawerRef.value?.$el.getBoundingClientRect().height || 0);
214214
const drawerWidthRef = ref(drawerRef.value?.$el.getBoundingClientRect().width || 0);
215215
216-
let snapPointsOffset: ComputedRef<number[]>
217-
218-
const onSnapPointChange = (activeSnapPointIndex: number) => {
219-
// Change openTime ref when we reach the last snap point to prevent dragging for 500ms incase it's scrollable.
220-
if (snapPoints && activeSnapPointIndex === snapPointsOffset?.value?.length - 1) openTime.value = new Date();
221-
}
222-
223216
const {
224217
activeSnapPoint,
225218
activeSnapPointIndex,
226219
onRelease: onReleaseSnapPoints,
227-
snapPointsOffset: _snapPointsOffset,
220+
snapPointsOffset,
228221
onDrag: onDragSnapPoints,
229222
shouldFade,
230223
getPercentageDragged: getSnapPointsPercentageDragged,
@@ -240,7 +233,11 @@ const {
240233
snapToSequentialPoint,
241234
});
242235
243-
snapPointsOffset = _snapPointsOffset
236+
function onSnapPointChange(activeSnapPointIndex: number, snapPointsOffset: number[]) {
237+
if (snapPoints && activeSnapPointIndex === snapPointsOffset.length - 1) {
238+
openTime.value = new Date()
239+
}
240+
}
244241
245242
provide('drawerContext', {
246243
activeSnapPoint,
@@ -432,7 +429,7 @@ function onDrag(event: PointerEvent) {
432429
433430
const opacityValue = 1 - percentageDragged;
434431
435-
if (shouldFade.value || (fadeFromIndex && activeSnapPointIndex.value === fadeFromIndex - 1)) {
432+
if (shouldFade || (fadeFromIndex && activeSnapPointIndex.value === fadeFromIndex - 1)) {
436433
onDragProp?.(event, percentageDragged);
437434
438435
set(

lib/Drawer/useSnapPoints.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function useSnapPoints({
2222
fadeFromIndex?: number;
2323
drawerRef: Ref<ComponentPublicInstance | null>;
2424
overlayRef: Ref<ComponentPublicInstance | null>;
25-
onSnapPointChange(activeSnapPointIndex: number): void;
25+
onSnapPointChange(activeSnapPointIndex: number, snapPointsOffset: number[]): void;
2626
direction?: DrawerDirection;
2727
snapToSequentialPoint?: boolean;
2828
}) {
@@ -67,14 +67,13 @@ export function useSnapPoints({
6767

6868
const activeSnapPointIndex = computed(() => snapPoints?.findIndex((snapPoint) => snapPoint === activeSnapPoint.value) ?? null)
6969

70-
const shouldFade = computed(() => (
70+
const shouldFade =
7171
(snapPoints &&
7272
snapPoints.length > 0 &&
7373
(fadeFromIndex || fadeFromIndex === 0) &&
7474
!Number.isNaN(fadeFromIndex) &&
7575
snapPoints[fadeFromIndex] === activeSnapPoint.value
76-
) || !snapPoints)
77-
)
76+
) || !snapPoints
7877

7978
const snapPointsOffset = computed(() => {
8079
const containerSize = typeof window !== 'undefined'
@@ -116,7 +115,7 @@ export function useSnapPoints({
116115

117116
const snapToPoint = (dimension: number) => {
118117
const newSnapPointIndex = snapPointsOffset.value?.findIndex((snapPointDim) => snapPointDim === dimension) ?? null;
119-
onSnapPointChange(newSnapPointIndex);
118+
onSnapPointChange(newSnapPointIndex, snapPointsOffset.value);
120119

121120
set(drawerRef.value?.$el, {
122121
transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
@@ -257,7 +256,7 @@ export function useSnapPoints({
257256

258257
// Don't animate, but still use this one if we are dragging away from the overlaySnapPoint
259258
if (isOverlaySnapPoint && !isDraggingDown) return 1;
260-
if (!shouldFade.value && !isOverlaySnapPoint) return null;
259+
if (!shouldFade && !isOverlaySnapPoint) return null;
261260

262261
// Either fadeFrom index or the one before
263262
const targetSnapPointIndex = isOverlaySnapPoint ? activeSnapPointIndex.value + 1 : activeSnapPointIndex.value - 1;
@@ -286,4 +285,4 @@ export function useSnapPoints({
286285
onDrag,
287286
snapPointsOffset,
288287
};
289-
}
288+
}

src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ import NestedDrawer from './components/NestedDrawer.vue'
66
</script>
77

88
<template>
9-
<DefaultDrawer/>
9+
<SnapDrawer/>
1010
</template>

src/components/SnapDrawer.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ const snap = ref<string | number | null>(snapPoints[0])
88
</script>
99

1010
<template>
11-
<Drawer.Root :snapPoints :activeSnapPoint="snap" :setActiveSnapPoint="snapPoint => (snap = snapPoint)">
11+
<Drawer.Root
12+
:snapPoints
13+
:activeSnapPoint="snap"
14+
:setActiveSnapPoint="snapPoint => (snap = snapPoint)"
15+
:fadeFromIndex="1"
16+
>
1217
<Drawer.Trigger class="relative flex h-10 flex-shrink-0 items-center justify-center gap-2 overflow-hidden rounded-full bg-white px-4 text-sm font-medium shadow-sm transition-all hover:bg-[#FAFAFA] dark:bg-[#161615] dark:hover:bg-[#1A1A19] dark:text-white">
1318
Open Drawer
1419
</Drawer.Trigger>

0 commit comments

Comments
 (0)