Slide 1

Slide 1 text

HTML5 Drag and Drop API is a hot steaming @ohanhi Futurice WWWeeklies Dec 4th 2015

Slide 2

Slide 2 text

http://www.unicode.org/charts/PDF/Unicode-6.0/U60-1F300.pdf

Slide 3

Slide 3 text

Drag and drop (DnD) is a first class citizen in HTML5! The spec defines an event- based mechanism, JavaScript API, and additional markup for declaring that just about any type of element be draggable on a page. I don't think anyone can argue against native browser support for a particular feature. Native browser DnD means faster, more responsive web apps. - Eric Bidelman, September 30th, 2010

Slide 4

Slide 4 text

Making things draggable is super easy

Slide 5

Slide 5 text

Making elements dropzones is weird

Slide 6

Slide 6 text

There’s a bunch of useful events drag dragover dragstart dragend https://developer.mozilla.org/en-US/docs/Web/API/DragEvent#Event_types dragenter dragleave dragexit drop

Slide 7

Slide 7 text

There’s a bunch of useful events drag dragover dragstart dragend https://developer.mozilla.org/en-US/docs/Web/API/DragEvent#Event_types dragenter dragleave dragexit drop “Normal” events

Slide 8

Slide 8 text

There’s a bunch of useful events drag dragover dragstart dragend https://developer.mozilla.org/en-US/docs/Web/API/DragEvent#Event_types dragenter dragleave dragexit drop Fired X times per second (demo)

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Maybe I can use the stream of drag events as a signal!

Slide 11

Slide 11 text

Let’s take a look at the drag event target - The element being dragged. currentTarget - The node that had the event listener attached. … and nothing else.

Slide 12

Slide 12 text

What do we know from drag? ● Which DOM element is dragged ● Where the event listener was attached ● What is under the drag (dropzone) What we don’t know?

Slide 13

Slide 13 text

Okay so what do we know from drop? ● Which DOM element was dropped on ● Where the event listener was attached ● Which DOM element was dragged What we don’t know?

Slide 14

Slide 14 text

Umm… How do we connect these then?

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

Working around the problem ● Encode necessary data in the DOM ● Dragstart: either set the dataTransfer or app state ● Dragging: update app state based on dropzone ● Drop: get the app state