Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for handle only interaction #71

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/famous-lamps-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vaul-vue": patch
---

add support for handle only interaction
19 changes: 17 additions & 2 deletions packages/vaul-vue/src/DrawerContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
keyboardIsOpen,
closeDrawer,
direction,
handleOnly,
} = injectDrawerRootContext()

const snapPointHeight = computed(() => {
Expand Down Expand Up @@ -45,6 +46,20 @@ function handlePointerDownOutside(event: Event) {
closeDrawer()
}

function handlePointerDown(event: PointerEvent) {
if (handleOnly.value)
return

onPress(event)
}

function handleOnDrag(event: PointerEvent) {
if (handleOnly.value)
return

onDrag(event)
}

watch(
isOpen,
(open) => {
Expand All @@ -65,8 +80,8 @@ watch(
:vaul-drawer-direction="direction"
:vaul-drawer-visible="isVisible ? 'true' : 'false'"
:style="{ '--snap-point-height': snapPointHeight }"
@pointerdown="onPress"
@pointermove="onDrag"
@pointerdown="handlePointerDown"
@pointermove="handleOnDrag"
@pointerup="onRelease"
@pointer-down-outside="handlePointerDownOutside"
@escape-key-down="(event) => {
Expand Down
106 changes: 106 additions & 0 deletions packages/vaul-vue/src/DrawerHandle.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<script setup lang="ts">
import { ref } from 'vue'
import { injectDrawerRootContext } from './context'
import type { DrawerHandleProps } from './controls'

const props = withDefaults(defineProps<DrawerHandleProps>(), {
preventCycle: false,
})

const LONG_HANDLE_PRESS_TIMEOUT = 250
const DOUBLE_TAP_TIMEOUT = 120

const { onPress, onDrag, handleRef, handleOnly, isVisible, snapPoints, activeSnapPoint, isDragging, dismissible, closeDrawer }
= injectDrawerRootContext()

const closeTimeoutId = ref<number | null>(null)
const shouldCancelInteraction = ref(false)

function handleStartCycle() {
// Stop if this is the second click of a double click
if (shouldCancelInteraction.value) {
handleCancelInteraction()
return
}

window.setTimeout(() => {
handleCycleSnapPoints()
}, DOUBLE_TAP_TIMEOUT)
}

function handleCycleSnapPoints() {
// Prevent accidental taps while resizing drawer
if (isDragging.value || props.preventCycle || shouldCancelInteraction.value) {
handleCancelInteraction()
return
}

// Make sure to clear the timeout id if the user releases the handle before the cancel timeout
handleCancelInteraction()

if (!snapPoints.value || snapPoints.value.length === 0) {
if (!dismissible.value)
closeDrawer()

return
}

const isLastSnapPoint = activeSnapPoint.value === snapPoints.value[snapPoints.value.length - 1]

if (isLastSnapPoint && dismissible.value) {
closeDrawer()
return
}

const currentSnapIndex = snapPoints.value.findIndex(point => point === activeSnapPoint.value)

if (currentSnapIndex === -1)
return // activeSnapPoint not found in snapPoints

const nextSnapPointIndex = isLastSnapPoint ? 0 : currentSnapIndex + 1

activeSnapPoint.value = snapPoints.value[nextSnapPointIndex]
}

function handleStartInteraction() {
closeTimeoutId.value = window.setTimeout(() => {
// Cancel click interaction on a long press
shouldCancelInteraction.value = true
}, LONG_HANDLE_PRESS_TIMEOUT)
}

function handleCancelInteraction() {
if (closeTimeoutId.value)
window.clearTimeout(closeTimeoutId.value)

shouldCancelInteraction.value = false
}

function handlePointerDown(event: PointerEvent) {
if (handleOnly.value)
onPress(event)
handleStartInteraction()
}

function handleOnDrag(event: PointerEvent) {
if (handleOnly.value)
onDrag(event)
}
</script>

<template>
<div
ref="handleRef"
:data-vaul-drawer-visible="isVisible ? 'true' : 'false'"
data-vaul-handle=""
aria-hidden="true"
@click="handleStartCycle"
@pointercancel="handleCancelInteraction"
@pointerdown="handlePointerDown"
@pointermove="handleOnDrag"
>
<span data-vaul-handle-hitarea="" aria-hidden="true">
<slot />
</span>
</div>
</template>
1 change: 1 addition & 0 deletions packages/vaul-vue/src/DrawerRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const props = withDefaults(defineProps<DrawerRootProps>(), {
modal: true,
scrollLockTimeout: SCROLL_LOCK_TIMEOUT,
direction: 'bottom',
handleOnly: false,
})

const emit = defineEmits<DrawerRootEmits>()
Expand Down
2 changes: 2 additions & 0 deletions packages/vaul-vue/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface DrawerRootContext {
isVisible: Ref<boolean>
drawerRef: Ref<ComponentPublicInstance | null>
overlayRef: Ref<ComponentPublicInstance | null>
handleRef: Ref<ComponentPublicInstance | null>
isDragging: Ref<boolean>
dragStartTime: Ref<Date | null>
isAllowedToDrag: Ref<boolean>
Expand All @@ -36,6 +37,7 @@ export interface DrawerRootContext {
emitRelease: (open: boolean) => void
emitOpenChange: (o: boolean) => void
nested: Ref<boolean>
handleOnly: Ref<boolean>
}

export const [injectDrawerRootContext, provideDrawerRootContext]
Expand Down
11 changes: 11 additions & 0 deletions packages/vaul-vue/src/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export type DrawerRootProps = {
defaultOpen?: boolean
nested?: boolean
direction?: DrawerDirection
handleOnly?: boolean
} & (WithFadeFromProps | WithoutFadeFromProps)

export interface UseDrawerProps {
Expand All @@ -57,6 +58,7 @@ export interface UseDrawerProps {
closeThreshold: Ref<number>
scrollLockTimeout: Ref<number>
direction: Ref<DrawerDirection>
handleOnly: Ref<boolean>
}

export interface DrawerRootEmits {
Expand Down Expand Up @@ -96,6 +98,10 @@ export interface Drawer {
closeDrawer: () => void
}

export interface DrawerHandleProps {
preventCycle?: boolean
}

function usePropOrDefaultRef<T>(prop: Ref<T | undefined> | undefined, defaultRef: Ref<T>): Ref<T> {
return prop && !!prop.value ? (prop as Ref<T>) : defaultRef
}
Expand All @@ -117,6 +123,7 @@ export function useDrawer(props: UseDrawerProps & DialogEmitHandlers): DrawerRoo
activeSnapPoint,
fadeFromIndex,
direction,
handleOnly,
} = props

const isOpen = ref(open.value ?? false)
Expand Down Expand Up @@ -149,6 +156,8 @@ export function useDrawer(props: UseDrawerProps & DialogEmitHandlers): DrawerRoo
ref<(number | string)[] | undefined>(undefined),
)

const handleRef = ref<ComponentPublicInstance | null>(null)

// const onCloseProp = ref<(() => void) | undefined>(undefined)
// const onOpenChangeProp = ref<((open: boolean) => void) | undefined>(undefined)
// const onDragProp = ref<((event: PointerEvent, percentageDragged: number) => void) | undefined>(
Expand Down Expand Up @@ -676,6 +685,7 @@ export function useDrawer(props: UseDrawerProps & DialogEmitHandlers): DrawerRoo
drawerRef,
drawerHeightRef,
overlayRef,
handleRef,
isDragging,
dragStartTime,
isAllowedToDrag,
Expand All @@ -700,5 +710,6 @@ export function useDrawer(props: UseDrawerProps & DialogEmitHandlers): DrawerRoo
emitRelease,
emitOpenChange,
nested,
handleOnly,
}
}
2 changes: 2 additions & 0 deletions packages/vaul-vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import DrawerRoot from './DrawerRoot.vue'
import DrawerRootNested from './DrawerRootNested.vue'
import DrawerOverlay from './DrawerOverlay.vue'
import DrawerContent from './DrawerContent.vue'
import DrawerHandle from './DrawerHandle.vue'

export type {
DrawerRootEmits,
Expand All @@ -18,6 +19,7 @@ export {
DrawerRootNested,
DrawerOverlay,
DrawerContent,
DrawerHandle,
}

export {
Expand Down
18 changes: 18 additions & 0 deletions playground/e2e/with-handle.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect, test } from '@playwright/test'
import { ANIMATION_DURATION } from './constants'

test.beforeEach(async ({ page }) => {
await page.goto('/test/with-handle')
})

test.describe('With handle', () => {
test('click should cycle to the next snap point', async ({ page }) => {
await page.waitForTimeout(ANIMATION_DURATION)

await expect(page.getByTestId('content')).toBeVisible()
await expect(page.getByTestId('active-snap-index')).toHaveText('0')

await page.getByTestId('handle').click()
await expect(page.getByTestId('active-snap-index')).toHaveText('1')
})
})
6 changes: 4 additions & 2 deletions playground/src/components/DemoDrawer.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { DrawerContent, DrawerOverlay, DrawerPortal, DrawerRoot, DrawerTrigger } from 'vaul-vue'
import { DrawerContent, DrawerHandle, DrawerOverlay, DrawerPortal, DrawerRoot, DrawerTrigger } from 'vaul-vue'
</script>

<template>
Expand All @@ -15,7 +15,9 @@ import { DrawerContent, DrawerOverlay, DrawerPortal, DrawerRoot, DrawerTrigger }
class="bg-gray-100 flex flex-col rounded-t-[10px] h-full mt-24 max-h-[96%] fixed bottom-0 left-0 right-0"
>
<div class="p-4 bg-white rounded-t-[10px] flex-1">
<div class="mx-auto w-12 h-1.5 flex-shrink-0 rounded-full bg-gray-300 mb-8" />
<DrawerHandle>
<div class="mx-auto w-12 h-1.5 flex-shrink-0 rounded-full bg-gray-300 mb-8" />
</DrawerHandle>
<div class="max-w-md mx-auto">
<h2 id="radix-:R3emdaH1:" class="font-medium mb-4">
Drawer for Vue.
Expand Down
4 changes: 4 additions & 0 deletions playground/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ const router = createRouter({
path: 'without-scaled-background',
component: () => import('../views/tests/WithoutScaledBackgroundView.vue'),
},
{
path: 'with-handle',
component: () => import('../views/tests/WithHandleView.vue'),
},
{
path: 'with-scaled-background',
component: () => import('../views/tests/WithScaledBackgroundView.vue'),
Expand Down
Loading