|
| 1 | +<script setup lang="ts"> |
| 2 | +import { ref } from 'vue' |
| 3 | +import { injectDrawerRootContext } from './context' |
| 4 | +import type { DrawerHandleProps } from './controls' |
| 5 | +
|
| 6 | +const props = withDefaults(defineProps<DrawerHandleProps>(), { |
| 7 | + preventCycle: false, |
| 8 | +}) |
| 9 | +
|
| 10 | +const LONG_HANDLE_PRESS_TIMEOUT = 250 |
| 11 | +const DOUBLE_TAP_TIMEOUT = 120 |
| 12 | +
|
| 13 | +const { onPress, onDrag, handleRef, handleOnly, isVisible, snapPoints, activeSnapPoint, isDragging, dismissible, closeDrawer } |
| 14 | + = injectDrawerRootContext() |
| 15 | +
|
| 16 | +const closeTimeoutId = ref<number | null>(null) |
| 17 | +const shouldCancelInteraction = ref(false) |
| 18 | +
|
| 19 | +function handleStartCycle() { |
| 20 | + // Stop if this is the second click of a double click |
| 21 | + if (shouldCancelInteraction.value) { |
| 22 | + handleCancelInteraction() |
| 23 | + return |
| 24 | + } |
| 25 | +
|
| 26 | + window.setTimeout(() => { |
| 27 | + handleCycleSnapPoints() |
| 28 | + }, DOUBLE_TAP_TIMEOUT) |
| 29 | +} |
| 30 | +
|
| 31 | +function handleCycleSnapPoints() { |
| 32 | + // Prevent accidental taps while resizing drawer |
| 33 | + if (isDragging.value || props.preventCycle || shouldCancelInteraction.value) { |
| 34 | + handleCancelInteraction() |
| 35 | + return |
| 36 | + } |
| 37 | +
|
| 38 | + // Make sure to clear the timeout id if the user releases the handle before the cancel timeout |
| 39 | + handleCancelInteraction() |
| 40 | +
|
| 41 | + if (!snapPoints.value || snapPoints.value.length === 0) { |
| 42 | + if (!dismissible.value) |
| 43 | + closeDrawer() |
| 44 | +
|
| 45 | + return |
| 46 | + } |
| 47 | +
|
| 48 | + const isLastSnapPoint = activeSnapPoint.value === snapPoints.value[snapPoints.value.length - 1] |
| 49 | +
|
| 50 | + if (isLastSnapPoint && dismissible.value) { |
| 51 | + closeDrawer() |
| 52 | + return |
| 53 | + } |
| 54 | +
|
| 55 | + const currentSnapIndex = snapPoints.value.findIndex(point => point === activeSnapPoint.value) |
| 56 | +
|
| 57 | + if (currentSnapIndex === -1) |
| 58 | + return // activeSnapPoint not found in snapPoints |
| 59 | +
|
| 60 | + const nextSnapPointIndex = isLastSnapPoint ? 0 : currentSnapIndex + 1 |
| 61 | +
|
| 62 | + activeSnapPoint.value = snapPoints.value[nextSnapPointIndex] |
| 63 | +} |
| 64 | +
|
| 65 | +function handleStartInteraction() { |
| 66 | + closeTimeoutId.value = window.setTimeout(() => { |
| 67 | + // Cancel click interaction on a long press |
| 68 | + shouldCancelInteraction.value = true |
| 69 | + }, LONG_HANDLE_PRESS_TIMEOUT) |
| 70 | +} |
| 71 | +
|
| 72 | +function handleCancelInteraction() { |
| 73 | + if (closeTimeoutId.value) |
| 74 | + window.clearTimeout(closeTimeoutId.value) |
| 75 | +
|
| 76 | + shouldCancelInteraction.value = false |
| 77 | +} |
| 78 | +
|
| 79 | +function handlePointerDown(event: PointerEvent) { |
| 80 | + if (handleOnly.value) |
| 81 | + onPress(event) |
| 82 | + handleStartInteraction() |
| 83 | +} |
| 84 | +
|
| 85 | +function handleOnDrag(event: PointerEvent) { |
| 86 | + if (handleOnly.value) |
| 87 | + onDrag(event) |
| 88 | +} |
| 89 | +</script> |
| 90 | + |
| 91 | +<template> |
| 92 | + <div |
| 93 | + ref="handleRef" |
| 94 | + :data-vaul-drawer-visible="isVisible ? 'true' : 'false'" |
| 95 | + data-vaul-handle="" |
| 96 | + aria-hidden="true" |
| 97 | + @click="handleStartCycle" |
| 98 | + @pointercancel="handleCancelInteraction" |
| 99 | + @pointerdown="handlePointerDown" |
| 100 | + @pointermove="handleOnDrag" |
| 101 | + > |
| 102 | + <span data-vaul-handle-hitarea="" aria-hidden="true"> |
| 103 | + <slot /> |
| 104 | + </span> |
| 105 | + </div> |
| 106 | +</template> |
| 107 | + |
| 108 | +<style> |
| 109 | +[data-vaul-handle] { |
| 110 | + display: block; |
| 111 | + position: relative; |
| 112 | + opacity: .7; |
| 113 | + background: #e2e2e4; |
| 114 | + margin-left: auto; |
| 115 | + margin-right: auto; |
| 116 | + height: 5px; |
| 117 | + width: 32px; |
| 118 | + border-radius: 1rem; |
| 119 | + touch-action: pan-y; |
| 120 | +} |
| 121 | +
|
| 122 | +[data-vaul-handle]:active, [data-vaul-handle]:hover { |
| 123 | + opacity: 1; |
| 124 | +} |
| 125 | +
|
| 126 | +[data-vaul-handle-hitarea] { |
| 127 | + position: absolute; |
| 128 | + left: 50%; |
| 129 | + top: 50%; |
| 130 | + transform: translate(-50%,-50%); |
| 131 | + width: max(100%, 2.75rem); |
| 132 | + height: max(100%, 2.75rem); |
| 133 | + touch-action: inherit; |
| 134 | +} |
| 135 | +</style> |
0 commit comments