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

Simple drag and drop list #29

Open
prpanto opened this issue Mar 15, 2024 · 0 comments
Open

Simple drag and drop list #29

prpanto opened this issue Mar 15, 2024 · 0 comments

Comments

@prpanto
Copy link

prpanto commented Mar 15, 2024

Request: Open discussions to talk without open issues

After hours I cant manage to work with this lib. I dont know why and I dont understand how works properly. This lib is awesome but pure documentation, it need more examples.

I think drag directive have problem and it didnt work at all. Maybe I will give a try another time.
I use useGesture but didnt work.

<template>
  <div ref="container" class="h-screen flex flex-col justify-center items-center space-y-10">
    <span v-for="n in 3" :key="n" class="flex justify-center items-center size-12 bg-emerald-600 rounded text-lg font-bold select-none cursor-grab">{{ n }}</span>
  </div>
</template>

<script setup>
import { useSpring, useMotionProperties } from '@vueuse/motion'
import { useDrag } from '@vueuse/gesture'
import clamp from 'lodash/clamp'

const container = ref()
const boxes = computed(() => Array.from(container.value.children).map((box) => ({
  element: box,
  y: box.getBoundingClientRect().y,
  x: box.getBoundingClientRect().x,
})))

onMounted(() => {
  boxes.value.map((box, index) => {
    const { motionProperties } = useMotionProperties(box.element, {
      cursor: 'grab',
      x: 0,
      y: 0
    })

    const { set } = useSpring(motionProperties, {
      damping: 50,
      stiffness: 1000,
    })

    useDrag(
      ({ movement: [x, y], dragging }) => {
        console.log(x, y, dragging);
        if (!dragging) {
          set({ y: 0, cursor: 'grab' })
          
          return
        }

        const currentY = clamp(Math.round((index * 100 + y) / 100), 0, child.length - 1)
        console.log(currentY)
        set({ cursor: 'grabbing', currentY })
      },
      { domTarget: box.element }
    )
  })
})
</script>
@prpanto prpanto changed the title Simple drag and drop Simple drag and drop list Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant