diff --git a/packages/core/useDraggable/component.ts b/packages/core/useDraggable/component.ts index d2d35669ead..fd099301839 100644 --- a/packages/core/useDraggable/component.ts +++ b/packages/core/useDraggable/component.ts @@ -1,4 +1,4 @@ -import { defineComponent, h, reactive, ref } from 'vue-demi' +import { computed, defineComponent, h, reactive, ref } from 'vue-demi' import type { UseDraggableOptions } from '@vueuse/core' import { isClient, useDraggable, useStorage } from '@vueuse/core' import { resolveUnref } from '@vueuse/shared' @@ -29,9 +29,11 @@ export const UseDraggable = defineComponent({ 'stopPropagation', 'pointerTypes', 'as', + 'handle', ] as unknown as undefined, setup(props, { slots }) { const target = ref() + const handle = computed(() => props.handle ?? target.value) const initialValue = props.storageKey ? useStorage( props.storageKey, @@ -46,6 +48,7 @@ export const UseDraggable = defineComponent({ const data = reactive(useDraggable(target, { ...props, + handle, initialValue, })) diff --git a/packages/core/useDraggable/demo.vue b/packages/core/useDraggable/demo.vue index ea09d11f02f..777f0307bf0 100644 --- a/packages/core/useDraggable/demo.vue +++ b/packages/core/useDraggable/demo.vue @@ -5,6 +5,7 @@ import { useDraggable } from '@vueuse/core' import { UseDraggable as Draggable } from './component' const el = ref(null) +const handle = ref(null) const innerWidth = isClient ? window.innerWidth : 200 @@ -53,5 +54,26 @@ const { x, y, style } = useDraggable(el, { {{ Math.round(x) }}, {{ Math.round(y) }} + + +
+ 👋 Drag here! +
+
+ Handle that triggers the drag event +
+
+ I am at {{ Math.round(x) }}, {{ Math.round(y) }} +
+
diff --git a/packages/core/useDraggable/index.ts b/packages/core/useDraggable/index.ts index 606955c95d4..5b08395ca6e 100644 --- a/packages/core/useDraggable/index.ts +++ b/packages/core/useDraggable/index.ts @@ -34,6 +34,13 @@ export interface UseDraggableOptions { */ draggingElement?: MaybeComputedRef + /** + * Handle that triggers the drag event + * + * @default target + */ + handle?: MaybeComputedRef + /** * Pointer types that listen to. * @@ -44,7 +51,7 @@ export interface UseDraggableOptions { /** * Initial position of the element. * - * @default { x: 0, y: 0} + * @default { x: 0, y: 0 } */ initialValue?: MaybeComputedRef @@ -73,6 +80,7 @@ export interface UseDraggableOptions { */ export function useDraggable(target: MaybeComputedRef, options: UseDraggableOptions = {}) { const draggingElement = options.draggingElement ?? defaultWindow + const draggingHandle = options.handle ?? target const position = ref(resolveUnref(options.initialValue) ?? { x: 0, y: 0 }) const pressedDelta = ref() @@ -127,7 +135,7 @@ export function useDraggable(target: MaybeComputedRef