Draggable: What is the equivalent of dataTransfer.setData and dataTransfer.getData

Created on 5 Dec 2017  路  3Comments  路  Source: Shopify/draggable

Actually I am building an interface that needs to pass information from one component to another via drag and drop. But the interface does not need to move HTML elements around, just information attached to the draggable elements.

To do this, I am using DataTransfer.setData() and DataTransfer.getData() APIs.

So, what is the equivalent of those in this library?

question

Most helpful comment

@mbarakaja Thanks for bringing this up! There is no API to do this internally with draggable, but there are workarounds to this issue.

You could do this via HTMLElement.dataset or HTMLElement attributes on drag:start and drag:stop. (set on drag start and read on drag stop)

Alternatively you could use the DragSensor, which uses the browsers Drag and Drop API, you could something like:

const draggable = new Draggable(containers, {
  draggable: 'some-selector',
  // Adds the native drag sensor, which is not included by default
  sensors: [DragSensor],
});

// Removes the mouse sensor, which is included by default
draggable.removeSensor(MouseSensor);

draggable.on('drag:start', ({sensorEvent}) => {
  // originalEvent is now a native Browser Drag event with DataTransfer object
  const originalEvent = sensorEvent.originalEvent;

  originalEvent.dataTransfer.setData(/* ... */);
})

draggable.on('drag:stop', ({sensorEvent}) => {
  const originalEvent = sensorEvent.originalEvent;

  console.log(originalEvent.dataTransfer.getData(/* ... */));
})

There are currently no plans to add a native DataTransfer API for Draggable, but this may change depending on demand.

I hope this helped

All 3 comments

@mbarakaja Thanks for bringing this up! There is no API to do this internally with draggable, but there are workarounds to this issue.

You could do this via HTMLElement.dataset or HTMLElement attributes on drag:start and drag:stop. (set on drag start and read on drag stop)

Alternatively you could use the DragSensor, which uses the browsers Drag and Drop API, you could something like:

const draggable = new Draggable(containers, {
  draggable: 'some-selector',
  // Adds the native drag sensor, which is not included by default
  sensors: [DragSensor],
});

// Removes the mouse sensor, which is included by default
draggable.removeSensor(MouseSensor);

draggable.on('drag:start', ({sensorEvent}) => {
  // originalEvent is now a native Browser Drag event with DataTransfer object
  const originalEvent = sensorEvent.originalEvent;

  originalEvent.dataTransfer.setData(/* ... */);
})

draggable.on('drag:stop', ({sensorEvent}) => {
  const originalEvent = sensorEvent.originalEvent;

  console.log(originalEvent.dataTransfer.getData(/* ... */));
})

There are currently no plans to add a native DataTransfer API for Draggable, but this may change depending on demand.

I hope this helped

Let me know if that answered your question @mbarakaja .
Closing this for now

Hello @tsov, sorry for not texting you back. Your post was really helpful. I ended up using the native drag and drop API.

Draggable is fantastic, but is overkill for what I need for now.

Thank you very much.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kauffecup picture kauffecup  路  5Comments

dchenk picture dchenk  路  3Comments

philippkuehn picture philippkuehn  路  4Comments

890f2151c2be69c51db72017546d00fd picture 890f2151c2be69c51db72017546d00fd  路  4Comments

xmatusk3 picture xmatusk3  路  5Comments