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

Question: Respond to a click on the dragable item #591

Closed
ofirgellerSM opened this issue Jan 17, 2022 · 4 comments
Closed

Question: Respond to a click on the dragable item #591

ofirgellerSM opened this issue Jan 17, 2022 · 4 comments

Comments

@ofirgellerSM
Copy link

If the user clicks on the item and does not hold the mouse key I would like to run some function.

Normally I would add an "onClick" handler to the element, but it seems like the listeners are colliding with that method.

  • I don't mind adding a delay to the start of the drag if that is necessary.
@vladluca
Copy link

vladluca commented Jan 18, 2022

@ofirgellerSM You can find a solution to your problem here: #477

@awgv
Copy link

awgv commented Jan 20, 2022

@ofirgellerSM The API allows constraining the activation via a delay or distance. Something like that:

// All of these are imported from '@dnd-kit/core'.

const sensors = useSensors(
  useSensor(PointerSensor, {
    activationConstraint: {
      distance: 8,
    },
  })
)

<DndContext sensors={sensors}>
  // […]
</DndContext>

@Anishali2
Copy link

 const pointerSensor = useSensor(PointerSensor, {
    activationConstraint: {
      distance: 0.01
    }
  })
  const mouseSensor = useSensor(MouseSensor)
  const touchSensor = useSensor(TouchSensor)
  const keyboardSensor = useSensor(KeyboardSensor)

  const sensors = useSensors(
    mouseSensor,
    touchSensor,
    keyboardSensor,
    pointerSensor
  )

this works perfectly with touch and drag

@chirag-9121
Copy link

@awgv's answer worked for me. But I encountered another problem. When I try to drag an element, the click functionality gets triggered and opens the popover component while also dragging the element. How can I avoid the click once a component is dragged?

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

6 participants