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(useDraggable): add disabled parameter #3613

Merged
merged 17 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/core/useDraggable/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ export const UseDraggable = /* #__PURE__ */ defineComponent<UseDraggableProps>({
'onStart',
'onMove',
'onEnd',
'disabled',
] as unknown as undefined,
setup(props, { slots }) {
const target = ref()
const handle = computed(() => props.handle ?? target.value)
const disabled = computed(() => !!props.disabled)
const storageValue = props.storageKey && useStorage(
props.storageKey,
toValue(props.initialValue) || { x: 0, y: 0 },
Expand All @@ -61,6 +63,7 @@ export const UseDraggable = /* #__PURE__ */ defineComponent<UseDraggableProps>({
handle,
initialValue,
onEnd,
disabled,
}))

return () => {
Expand Down
15 changes: 14 additions & 1 deletion packages/core/useDraggable/demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,25 @@ const handle = ref<HTMLElement | null>(null)

const innerWidth = isClient ? window.innerWidth : 200

const disabled = ref(false)
const { x, y, style } = useDraggable(el, {
initialValue: { x: innerWidth / 4.2, y: 80 },
preventDefault: true,
disabled,
})
</script>

<template>
<div>
<div class="text-xs">
<label class="checkbox">
<input
:checked="disabled" type="checkbox" name="enabled"
@input="($event.target as HTMLInputElement)!.checked ? disabled = true : disabled = false "
>
<span>Disabled drag and drop</span>
</label>
</div>
<p class="italic op50 text-center">
Check the floating boxes
</p>
Expand All @@ -42,9 +53,10 @@ const { x, y, style } = useDraggable(el, {
shadow="~ hover:lg"
class="fixed bg-$vp-c-bg select-none cursor-move z-24"
:initial-value="{ x: innerWidth / 3.9, y: 150 }"
:prevent-default="true"
prevent-default
storage-key="vueuse-draggable-pos"
storage-type="session"
:disabled="disabled"
>
Renderless component
<div class="text-xs opacity-50">
Expand All @@ -64,6 +76,7 @@ const { x, y, style } = useDraggable(el, {
:initial-value="{ x: innerWidth / 3.6, y: 240 }"
:prevent-default="true"
:handle="handle"
:disabled="disabled"
>
<div ref="handle" class="cursor-move">
👋 Drag here!
Expand Down
13 changes: 10 additions & 3 deletions packages/core/useDraggable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ export interface UseDraggableOptions {
* @default 'both'
*/
axis?: 'x' | 'y' | 'both'

/**
* Disabled drag and drop.
*
* @default false
*/
disabled?: MaybeRefOrGetter<boolean>
}

/**
Expand Down Expand Up @@ -138,7 +145,7 @@ export function useDraggable(
}

const start = (e: PointerEvent) => {
if (!filterEvent(e))
if (toValue(options.disabled) || !filterEvent(e))
return
if (toValue(exact) && e.target !== toValue(target))
return
Expand All @@ -156,7 +163,7 @@ export function useDraggable(
handleEvent(e)
}
const move = (e: PointerEvent) => {
if (!filterEvent(e))
if (toValue(options.disabled) || !filterEvent(e))
return
if (!pressedDelta.value)
return
Expand All @@ -183,7 +190,7 @@ export function useDraggable(
handleEvent(e)
}
const end = (e: PointerEvent) => {
if (!filterEvent(e))
if (toValue(options.disabled) || !filterEvent(e))
return
if (!pressedDelta.value)
return
Expand Down