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 1 commit 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
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
28 changes: 28 additions & 0 deletions packages/vaul-vue/src/DrawerHandle.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script setup lang="ts">
import { injectDrawerRootContext } from './context'

const {
onPress,
onDrag,
handleOnly,
} = injectDrawerRootContext()

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

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

<template>
<div
@pointerdown="handlePointerDown"
@pointermove="handleOnDrag"
>
<slot />
</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 @@ -25,6 +25,7 @@ const props = withDefaults(defineProps<DrawerRootProps>(), {
modal: true,
scrollLockTimeout: SCROLL_LOCK_TIMEOUT,
direction: 'bottom',
handleOnly: false,
})

const emit = defineEmits<DrawerRootEmits>()
Expand Down
1 change: 1 addition & 0 deletions packages/vaul-vue/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface DrawerRootContext {
emitRelease: (open: boolean) => void
emitOpenChange: (o: boolean) => void
nested: Ref<boolean>
handleOnly: Ref<boolean>
}

export const [injectDrawerRootContext, provideDrawerRootContext]
Expand Down
4 changes: 4 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 @@ -116,6 +118,7 @@ export function useDrawer(props: UseDrawerProps & DialogEmitHandlers): DrawerRoo
activeSnapPoint,
fadeFromIndex,
direction,
handleOnly,
} = props

const isOpen = ref(open.value ?? false)
Expand Down Expand Up @@ -699,5 +702,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
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