Skip to content

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

Closed

Description

@ofirgellerSM

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.

Activity

  1. vladluca commented on Jan 18, 2022

    @vladluca

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

  2. awgv commented on Jan 20, 2022

    @awgv

    @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>
  3. ani-ali commented on Jan 2, 2024

    @ani-ali
     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

  4. chirag-9121 commented on Sep 11, 2024

    @chirag-9121

    @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?

  5. aboveyunhai commented on Nov 8, 2024

    @aboveyunhai

    @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?

    const {  isDragging } = useSortable();
    
    <div onClick={() => { if(!isDragging){ console.log("click"); } }></div>
  6. chirag-9121 commented on Nov 9, 2024

    @chirag-9121

    @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?

    const {  isDragging } = useSortable();
    
    <div onClick={() => { if(!isDragging){ console.log("click"); } }></div>

    The thing is shadcn's components are built on Radix, so the events gets triggered when the pointer is down, i.e. onPointerDown.

    So I simply added this to the trigger component and the issue got resolved.

    onPointerDown={e => e.preventDefault()}
    
  7. aboveyunhai commented on Nov 9, 2024

    @aboveyunhai

    @chirag-9121
    Ideally you should do a strict isDragging check for whatever function is being called.
    preventDefault() in general is not a good idea as it will completely block the bubble phase.
    But depending on what you are doing, it might not have difference.

  8. berksafran-wt commented on Dec 3, 2024

    @berksafran-wt

    It works for me. Thanks @chirag-9121 🙏

  9. damassi commented on Dec 22, 2024

    @damassi

    Would be worth adding this solution clearly to the sortable docs 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions