3

My drag drop code is working fine with mouse, but not on a touch screen. Should the below work or is there a mistake here?

The draggable item:

<div class="draggable-area" draggable="true" 
   @ondragstart="@(() => HandleDragStart())"
   @ontouchstart="@(() => HandleDragStart())">

protected async virtual Task HandleDragStart()
{
    // set the draggable object
    _draggableItem = this;
}

The droppable area (in a different component):

<div class="droppable-area"
     @ondrop="HandleDrop" 
     @ondrop:stopPropagation="true"
     ondragover="event.preventDefault();">

protected async override Task HandleDrop()
{
    // handle the drop
    Console.WriteLine($"Dropped item: {_draggableItem}");
}
James Harcourt
  • 6,017
  • 4
  • 22
  • 42

1 Answers1

1

The solution I used in one of my projects is to implement drag and drop only for mouse using the HTML Drag and Drop API and then use dragdroptouch polyfill to add support for touch screens. The library needs a simple modification in order to work with Blazor as discussed here.

Dimitris Maragkos
  • 8,932
  • 2
  • 8
  • 26