-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
index.ts
211 lines (187 loc) · 5.13 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { computed, ref } from 'vue-demi'
import type { MaybeRefOrGetter } from '@vueuse/shared'
import { isClient, toRefs, toValue } from '@vueuse/shared'
import { useEventListener } from '../useEventListener'
import type { PointerType, Position } from '../types'
import { defaultWindow } from '../_configurable'
export interface UseDraggableOptions {
/**
* Only start the dragging when click on the element directly
*
* @default false
*/
exact?: MaybeRefOrGetter<boolean>
/**
* Prevent events defaults
*
* @default false
*/
preventDefault?: MaybeRefOrGetter<boolean>
/**
* Prevent events propagation
*
* @default false
*/
stopPropagation?: MaybeRefOrGetter<boolean>
/**
* Whether dispatch events in capturing phase
*
* @default true
*/
capture?: boolean
/**
* Element to attach `pointermove` and `pointerup` events to.
*
* @default window
*/
draggingElement?: MaybeRefOrGetter<HTMLElement | SVGElement | Window | Document | null | undefined>
/**
* Element for calculating bounds (If not set, it will use the event's target).
*
* @default undefined
*/
containerElement?: MaybeRefOrGetter<HTMLElement | SVGElement | null | undefined>
/**
* Handle that triggers the drag event
*
* @default target
*/
handle?: MaybeRefOrGetter<HTMLElement | SVGElement | null | undefined>
/**
* Pointer types that listen to.
*
* @default ['mouse', 'touch', 'pen']
*/
pointerTypes?: PointerType[]
/**
* Initial position of the element.
*
* @default { x: 0, y: 0 }
*/
initialValue?: MaybeRefOrGetter<Position>
/**
* Callback when the dragging starts. Return `false` to prevent dragging.
*/
onStart?: (position: Position, event: PointerEvent) => void | false
/**
* Callback during dragging.
*/
onMove?: (position: Position, event: PointerEvent) => void
/**
* Callback when dragging end.
*/
onEnd?: (position: Position, event: PointerEvent) => void
/**
* Axis to drag on.
*
* @default 'both'
*/
axis?: 'x' | 'y' | 'both'
}
/**
* Make elements draggable.
*
* @see https://vueuse.org/useDraggable
* @param target
* @param options
*/
export function useDraggable(
target: MaybeRefOrGetter<HTMLElement | SVGElement | null | undefined>,
options: UseDraggableOptions = {},
) {
const {
pointerTypes,
preventDefault,
stopPropagation,
exact,
onMove,
onEnd,
onStart,
initialValue,
axis = 'both',
draggingElement = defaultWindow,
containerElement,
handle: draggingHandle = target,
} = options
const position = ref<Position>(
toValue(initialValue) ?? { x: 0, y: 0 },
)
const pressedDelta = ref<Position>()
const filterEvent = (e: PointerEvent) => {
if (pointerTypes)
return pointerTypes.includes(e.pointerType as PointerType)
return true
}
const handleEvent = (e: PointerEvent) => {
if (toValue(preventDefault))
e.preventDefault()
if (toValue(stopPropagation))
e.stopPropagation()
}
const start = (e: PointerEvent) => {
if (!filterEvent(e))
return
if (toValue(exact) && e.target !== toValue(target))
return
const container = toValue(containerElement)
const containerRect = container?.getBoundingClientRect?.()
const targetRect = toValue(target)!.getBoundingClientRect()
const pos = {
x: e.clientX - (container ? targetRect.left - containerRect!.left + container.scrollLeft : targetRect.left),
y: e.clientY - (container ? targetRect.top - containerRect!.top + container.scrollTop : targetRect.top),
}
if (onStart?.(pos, e) === false)
return
pressedDelta.value = pos
handleEvent(e)
}
const move = (e: PointerEvent) => {
if (!filterEvent(e))
return
if (!pressedDelta.value)
return
const container = toValue(containerElement)
const targetRect = toValue(target)!.getBoundingClientRect()
let { x, y } = position.value
if (axis === 'x' || axis === 'both') {
x = e.clientX - pressedDelta.value.x
if (container)
x = Math.min(Math.max(0, x), container.scrollWidth - targetRect!.width)
}
if (axis === 'y' || axis === 'both') {
y = e.clientY - pressedDelta.value.y
if (container)
y = Math.min(Math.max(0, y), container.scrollHeight - targetRect!.height)
}
position.value = {
x,
y,
}
onMove?.(position.value, e)
handleEvent(e)
}
const end = (e: PointerEvent) => {
if (!filterEvent(e))
return
if (!pressedDelta.value)
return
pressedDelta.value = undefined
onEnd?.(position.value, e)
handleEvent(e)
}
if (isClient) {
const config = { capture: options.capture ?? true }
useEventListener(draggingHandle, 'pointerdown', start, config)
useEventListener(draggingElement, 'pointermove', move, config)
useEventListener(draggingElement, 'pointerup', end, config)
}
return {
...toRefs(position),
position,
isDragging: computed(() => !!pressedDelta.value),
style: computed(
() => `left:${position.value.x}px;top:${position.value.y}px;`,
),
}
}
export type UseDraggableReturn = ReturnType<typeof useDraggable>