forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useDraggable): new function (vitest-dev#727)
- Loading branch information
Showing
9 changed files
with
274 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { defineComponent, h, reactive, ref, unref } from 'vue-demi' | ||
import { useDraggable, UseDraggableOptions, useStorage } from '@vueuse/core' | ||
|
||
export interface UseDraggableProps extends UseDraggableOptions { | ||
/** | ||
* When provided, use `useStorage` to preserve element's position | ||
*/ | ||
storageKey?: string | ||
|
||
/** | ||
* Storage type | ||
* | ||
* @default 'local' | ||
*/ | ||
storageType?: 'local' | 'session' | ||
} | ||
|
||
export const UseDraggable = defineComponent<UseDraggableProps>({ | ||
name: 'UseDraggable', | ||
props: [ | ||
'storageKey', | ||
'initialValue', | ||
'exact', | ||
'preventDefault', | ||
'pointerTypes', | ||
] as unknown as undefined, | ||
setup(props, { slots }) { | ||
const target = ref() | ||
const initialValue = props.storageKey | ||
? useStorage( | ||
props.storageKey, | ||
unref(props.initialValue) || { x: 0, y: 0 }, | ||
props.storageType === 'session' ? sessionStorage : localStorage, | ||
) | ||
: props.initialValue || { x: 0, y: 0 } | ||
|
||
const data = reactive(useDraggable(target, { | ||
...props, | ||
initialValue, | ||
})) | ||
|
||
return () => { | ||
if (slots.default) | ||
return h('div', { ref: target, style: data.style }, slots.default(data)) | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<script setup lang="ts"> | ||
import { ref } from 'vue-demi' | ||
import { UseDraggable as Draggable } from './component' | ||
import { useDraggable } from '.' | ||
const el = ref<HTMLElement | null>(null) | ||
const { x, y, style } = useDraggable(el, { | ||
initialValue: { x: window.innerWidth / 4.2, y: 80 }, | ||
}) | ||
const innerWidth = window.innerWidth | ||
</script> | ||
|
||
<template> | ||
<div class="h-30"> | ||
<div | ||
ref="el" | ||
p="x-4 y-2" | ||
border="~ gray-400 rounded" | ||
shadow="~ hover:lg" | ||
class="fixed bg-$c-bg select-none cursor-move z-10" | ||
:style="style" | ||
> | ||
👋 Drag me! | ||
<div class="text-sm opacity-50"> | ||
I am at {{ Math.round(x) }}, {{ Math.round(y) }} | ||
</div> | ||
</div> | ||
|
||
<Draggable | ||
v-slot="{ x, y }" | ||
p="x-4 y-2" | ||
border="~ gray-400 rounded" | ||
shadow="~ hover:lg" | ||
class="fixed bg-$c-bg select-none cursor-move z-10" | ||
:initial-value="{ x: innerWidth / 3.9, y: 150 }" | ||
storage-key="vueuse-draggable-pos" | ||
storage-type="session" | ||
> | ||
Headless component | ||
<div class="text-xs opacity-50"> | ||
Position persisted in sessionStorage | ||
</div> | ||
<div class="text-sm opacity-50"> | ||
{{ Math.round(x) }}, {{ Math.round(y) }} | ||
</div> | ||
</Draggable> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
category: Sensors | ||
--- | ||
|
||
# useDraggable | ||
|
||
Make elements draggable. | ||
|
||
## Usage | ||
|
||
```html | ||
<script setup lang="ts"> | ||
import { ref } from 'vue' | ||
import { useDraggable } from '.' | ||
const el = ref<HTMLElement | null>(null) | ||
// `style` will be a helper computed for `left: ?px; top: ?px;` | ||
const { x, y, style } = useDraggable(el, { | ||
initialValue: { x: 40, y: 40 }, | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div ref="el" :style="style" style="position: fixed"> | ||
Drag me! I am at {{x}}, {{y}} | ||
</div> | ||
</template> | ||
``` | ||
|
||
## Component | ||
|
||
```html | ||
<UseDraggable :initialValue="{ x: 10, y: 10 }" v-slot="{ x, y }"> | ||
Drag me! I am at {{x}}, {{y}} | ||
</UseDraggable> | ||
``` | ||
|
||
For component usage, additional props `storageKey` and `storageType` can be passed to the component and enable the persistence of the element position. | ||
|
||
```html | ||
<UseDraggable storage-key="vueuse-draggable" storage-type="session"> | ||
Refresh the page and I am still in the same position! | ||
</UseDraggable> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { Ref, ref, unref, computed } from 'vue' | ||
import { MaybeRef, toRefs } from '@vueuse/shared' | ||
import { useEventListener } from '../useEventListener' | ||
import { MaybeElementRef } from '../unrefElement' | ||
import { PointerType, Position } from '../_types' | ||
import { defaultWindow } from '../_configurable' | ||
|
||
export interface UseDraggableOptions { | ||
/** | ||
* Only start the dragging when click on the element directly | ||
* | ||
* @default false | ||
*/ | ||
exact?: MaybeRef<boolean> | ||
|
||
/** | ||
* Prevent events defaults | ||
* | ||
* @default false | ||
*/ | ||
preventDefault?: MaybeRef<boolean> | ||
|
||
/** | ||
* Element to attach `pointermove` and `pointerup` events to. | ||
* | ||
* @default window | ||
*/ | ||
draggingElement?: MaybeElementRef | ||
|
||
/** | ||
* Pointer types that listen to. | ||
* | ||
* @default ['mouse', 'touch', 'pen'] | ||
*/ | ||
pointerTypes?: PointerType[] | ||
|
||
/** | ||
* Initial position of the element. | ||
* | ||
* @default { x: 0, y: 0} | ||
*/ | ||
initialValue?: MaybeRef<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 | ||
} | ||
|
||
/** | ||
* Make elements draggable. | ||
* | ||
* @see https://vueuse.org/useDraggable | ||
* @param el | ||
* @param options | ||
*/ | ||
export function useDraggable(el: MaybeElementRef, options: UseDraggableOptions = {}) { | ||
const draggingElement = options.draggingElement ?? defaultWindow | ||
const position: Ref<Position> = ref(options.initialValue ?? { x: 0, y: 0 }) | ||
const pressedDelta = ref<Position>() | ||
|
||
const filterEvent = (e: PointerEvent) => { | ||
if (options.pointerTypes) | ||
return options.pointerTypes.includes(e.pointerType as PointerType) | ||
return true | ||
} | ||
const preventDefault = (e: PointerEvent) => { | ||
if (unref(options.preventDefault)) | ||
e.preventDefault() | ||
} | ||
const start = (e: PointerEvent) => { | ||
if (!filterEvent(e)) | ||
return | ||
if (unref(options.exact) && e.target !== el.value) | ||
return | ||
const react = el.value!.getBoundingClientRect() | ||
const pos = { | ||
x: e.pageX - react.left, | ||
y: e.pageY - react.top, | ||
} | ||
if (options.onStart?.(pos, e) === false) | ||
return | ||
pressedDelta.value = pos | ||
preventDefault(e) | ||
} | ||
const move = (e: PointerEvent) => { | ||
if (!filterEvent(e)) | ||
return | ||
if (!pressedDelta.value) | ||
return | ||
position.value = { | ||
x: e.pageX - pressedDelta.value.x, | ||
y: e.pageY - pressedDelta.value.y, | ||
} | ||
options.onMove?.(position.value, e) | ||
preventDefault(e) | ||
} | ||
const end = (e: PointerEvent) => { | ||
if (!filterEvent(e)) | ||
return | ||
pressedDelta.value = undefined | ||
preventDefault(e) | ||
} | ||
|
||
useEventListener(el, 'pointerdown', start, true) | ||
useEventListener(draggingElement, 'pointermove', move, true) | ||
useEventListener(draggingElement, 'pointerup', end, true) | ||
|
||
return { | ||
...toRefs(position), | ||
position, | ||
isDragging: computed(() => !!pressedDelta.value), | ||
style: computed(() => `left:${position.value.x}px;top:${position.value.y}px;`), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters