From 69da5b82dc11e746c2b11fbf6dc4b4c64171c960 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren?= Date: Thu, 31 Aug 2023 12:56:29 +0200 Subject: [PATCH] Improve SheetModal for mobile --- src/components/atoms/SheetModal.story.vue | 2 +- src/components/atoms/SheetModal.vue | 32 ++++++++++++++--------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/components/atoms/SheetModal.story.vue b/src/components/atoms/SheetModal.story.vue index 83459f6d..85498434 100644 --- a/src/components/atoms/SheetModal.story.vue +++ b/src/components/atoms/SheetModal.story.vue @@ -9,7 +9,7 @@ const progress = ref(0)
diff --git a/src/components/atoms/SheetModal.vue b/src/components/atoms/SheetModal.vue index 46573a7e..3ff774cb 100644 --- a/src/components/atoms/SheetModal.vue +++ b/src/components/atoms/SheetModal.vue @@ -28,27 +28,36 @@ const emit = defineEmits({ 'update:progress': (_: number) => true, }) -const dif = props.maxHeight - props.initialHeight +const heightDifference = props.maxHeight - props.initialHeight let initialY = 0 -let initialTime = 0 +let initialX = 0 +let initialOpen = false const dragging = ref(false) const container = ref(null) const isOpen = computed(() => props.progress === 1) function onStart(event: PointerEvent) { dragging.value = true - initialTime = event.timeStamp initialY = event.clientY + initialX = event.clientX + initialOpen = isOpen.value container.value!.setPointerCapture(event.pointerId) } function onMove(event: PointerEvent) { if (!dragging.value) return - const yDelta = (initialY - event.clientY) - const startingPoint = isOpen.value ? yDelta + dif : yDelta - const newProgress = Math.max(0, Math.min(startingPoint / dif, 1)) + const yDelta = initialY - event.clientY + let newProgress: number + if (initialOpen) { + // yDelta is negative for dragging down + newProgress = Math.min(Math.max(heightDifference + yDelta, 0), heightDifference) / heightDifference + } + else { + // yDelta is positive for dragging up + newProgress = Math.min(Math.max(yDelta, 0), heightDifference) / heightDifference + } emit('update:progress', newProgress) } @@ -57,15 +66,14 @@ function onEnd(event: PointerEvent) { container.value!.releasePointerCapture(event.pointerId) animateShortly() - const timeDelta = event.timeStamp - initialTime - const isClick = timeDelta < 500 + const isClick = Math.abs(initialY - event.clientY) < 10 && Math.abs(initialX - event.clientX) < 10 - if (isClick && !isOpen.value) { + if (isClick && !initialOpen) { open() } else { - if (isOpen.value) + if (initialOpen) props.progress < 0.85 ? close() : open() else @@ -91,7 +99,7 @@ function onCardDrag(progress: number) { const radius = (1 - progress) * props.initialBorderRadius style.value = { - height: `${props.initialHeight + dif * progress}px`, + height: `${props.initialHeight + heightDifference * progress}px`, marginBottom: `${(1 - progress) * props.initialGapToScreen}px`, borderBottomRightRadius: `${radius}px`, borderBottomLeftRadius: `${radius}px`, @@ -115,7 +123,7 @@ onBeforeUnmount(() => {