arsduo / elm-dom-drag-drop / Dom.DragDrop

This library makes it easy to implement HTML5 drag-and-drop operations with Elm and [

State


type State draggableIdType dropTargetIdType

An opaque container for state data.

Querying State

isCurrentDropTarget : State draggableIdType dropTargetIdType -> dropTargetIdType -> Basics.Bool

This method will tell you whether a given item is currently being hovered over to allow you to provide a visual hint.

currentlyDraggedObject : State draggableIdType dropTargetIdType -> Maybe draggableIdType

This method will return the currently dragged item (if any). Note that this will return the id (data) that corresponds to the Dom.Element node being dragged, rather than the actual DOM node itself.

dropTargetExists : State draggableIdType dropTargetIdType -> Basics.Bool

This method will tell you whether the dragged element (if any) is currently over a drop zone.

initialState : State draggableIdType dropTargetIdType

The initial state on page load, with nothing being dragged or dropped.

Updating State

startDragging : State draggableIdType dropTargetIdType -> draggableIdType -> State draggableIdType dropTargetIdType

When the dragStarted message is received by your app, call this method to update the state with the newly-dragged object.

stopDragging : State draggableIdType dropTargetIdType -> State draggableIdType dropTargetIdType

When dragging stops because either the dragEnded or dropped message were received or the user has done something else in your application, call this method to update the state appropriately.

updateDropTarget : State draggableIdType dropTargetIdType -> dropTargetIdType -> State draggableIdType dropTargetIdType

When the user drags an element over a potential drop zone and the dropTargetChanged message is received by your app, call this method to update the state with the currently targeted drop zone.

Specifying Messages


type alias Messages msg draggableIdType dropTargetIdType =
{ dragStarted : draggableIdType -> msg
, dropTargetChanged : dropTargetIdType -> msg
, dragEnded : msg
, dropped : draggableIdType -> dropTargetIdType -> msg 
}

Messages the Dom.DragDrop framework will send to your application as events occur. It is up to your application to call the appropriate Dom.DragDrop update function and store the result in your model.

We track four messages:

Let's take a look at what this looks like for a sample set of types and messages:

-- some basic ID that each draggable element has
type Id
    = Id Int


-- we can drop onto an existing element or at the end of the list
type DropTarget
    = OntoElement Id
    | EndOfList

type Msg
    = MoveStarted Id
    | MoveTargetChanged DropTarget
    | MoveCanceled
    | MoveCompleted Id DropTarget

dragDropMessages : Dom.DragDrop.Messages Id DropTarget
dragDropMessages =
    { dragStarted = MoveStarted
    , dropTargetChanged = MoveTargetChanged
    , dragEnded = MoveCanceled
    , dropped = MoveCompleted
    }

Updating the UI

makeDraggable : State draggableIdType dropTargetIdType -> draggableIdType -> Messages msg draggableIdType dropTargetIdType -> Dom.Element msg -> Dom.Element msg

makeDraggable makes an element draggable. When an element is being dragged, it will gain the "being-dragged" CSS class, with which you can control the display of the moving element.

makeDroppable : State draggableIdType dropTargetIdType -> dropTargetIdType -> Messages msg draggableIdType dropTargetIdType -> Dom.Element msg -> Dom.Element msg

makeDroppable marks an element as a place that a dragged object can be dropped onto. If the dragged object is currently hovering over the droppable element, it gains the CSS class "drop-target" to allow for appropriate visual indication.