Skip to content

Commit

Permalink
feat: update drag drop ref function
Browse files Browse the repository at this point in the history
  • Loading branch information
hcg1023 committed Mar 14, 2022
1 parent ef38236 commit a4f4805
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/hooks/useDrag/useDrag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ export function useDrag<
DragObject = unknown,
DropResult = unknown,
CollectedProps = unknown,
ConnectDragSourceOptions = unknown,
ConnectDragPreviewOption = unknown
>(
specArg: FactoryOrInstance<
DragSourceHookSpec<DragObject, DropResult, CollectedProps>
>
): [
Ref<CollectedProps>,
Ref<ConnectDragSource>,
Ref<ConnectDragSource<ConnectDragSourceOptions>>,
Ref<ConnectDragPreview<ConnectDragPreviewOption>>
] {
const spec = useOptionalFactory(specArg)
Expand Down
5 changes: 3 additions & 2 deletions lib/hooks/useDrop/useDrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import { computed, Ref, unref } from 'vue'
export function useDrop<
DragObject = unknown,
DropResult = unknown,
CollectedProps = unknown
CollectedProps = unknown,
ConnectDropTargetOptions = unknown
>(
specArg: FactoryOrInstance<
DropTargetHookSpec<DragObject, DropResult, CollectedProps>
>
): [Ref<CollectedProps>, Ref<ConnectDropTarget>] {
): [Ref<CollectedProps>, Ref<ConnectDropTarget<ConnectDropTargetOptions>>] {
const spec = useOptionalFactory(specArg)
const monitor = useDropTargetMonitor<DragObject, DropResult>()
const connector = useDropTargetConnector(computed(() => unref(spec).options))
Expand Down
14 changes: 7 additions & 7 deletions lib/types/connectors.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ComponentPublicInstance, VNode } from 'vue'
export type ConnectableElement = Element | null

export type ConnectableElement = Element | ComponentPublicInstance | null

export type ConnectDragSource = ConnectableElement
export type ConnectDropTarget = ConnectableElement
export type ConnectDragPreview<Options> = (
type ConnectNode<Options> = (
elementOrNode: ConnectableElement,
options?: Options
) => VNode
) => Element

export type ConnectDragSource<Options> = ConnectNode<Options>
export type ConnectDropTarget<Options> = ConnectNode<Options>
export type ConnectDragPreview<Options> = ConnectNode<Options>
16 changes: 16 additions & 0 deletions src/views/Box.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script setup lang="ts">
import { ref } from 'vue'
const props = defineProps<{ title: string }>()
const el = ref()
defineExpose({
el,
})
</script>

<template>
<div ref="el" class="box">
{{ title }}
</div>
</template>
14 changes: 13 additions & 1 deletion src/views/Example.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts" setup>
import { useDrag, useDrop } from 'vue3-dnd'
import { computed, unref } from 'vue'
import Box from './Box.vue'
const [dropCollect, drop] = useDrop(() => ({
accept: 'Box',
Expand Down Expand Up @@ -36,6 +37,10 @@ const [collect, drag] = useDrag(() => ({
const isDragging = computed(() => collect.value.isDragging)
const opacity = computed(() => (unref(isDragging) ? 0.4 : 1))
const setRef = (instance: any) => {
drag.value(instance.el)
}
</script>

<template>
Expand All @@ -51,7 +56,14 @@ const opacity = computed(() => (unref(isDragging) ? 0.4 : 1))
</div>
</div>
<div :style="{ overflow: 'hidden', clear: 'both' }">
<div :ref="drag" class="box" role="Box" :style="{ opacity }">Box</div>
<Box
:ref="setRef"
class="box"
role="Box"
:style="{ opacity }"
title="Box"
></Box>
<!-- <div :ref="drag" class="box" role="Box" :style="{ opacity }">Box</div>-->
</div>
</div>
</template>
Expand Down

0 comments on commit a4f4805

Please sign in to comment.