This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
Easily allow users to drop files into the browser
version 1.0.2
license BSD3
native-modules False
elm-version 0.18.0 <= v < 0.19.0
Tag 2.0.1
Committed At 2017-10-12 21:02:06 UTC
elm-lang/svg 2.0.0 <= v < 3.0.0 2.0.0
elm-lang/html 2.0.0 <= v < 3.0.0 2.0.0
elm-lang/core 5.1.1 <= v < 6.0.0 5.1.1



#Elm Dropzone Provides helpers to make it easy to add a "dropzone" into a webapp and pass the the dropped files onto the FileReader library to read. The library does not yet have a dependency on FileReader because FileReader has not passed native review yet. At the moment using it with FileReader is the primary intended use case. A full example of how to use this library is provided in the examples folder. Please note that you have to clone the filereader library manually into the examples folder. There are 3 basic steps to get DropZone working in your application:

Store the DropZone.Model as part of your model

type alias Model =
    { dropZone : DropZone.Model -- this is the dropzone model you need to store
    , ... -- other parts of your model

init : Model
init =
    { dropZone = DropZone.init
    , ... -- init other parts

Update the DropZone.Model as part of your update function

type Msg
    = DropZoneMsg (DropZone.DropZoneMessage (List FileReader.NativeFile))
    | .. -- other actions

update : Msg -> Model -> (Model, Cmd Msg)
update message model =
    case message of
        -- Drop is the only message that you will want to handle yourself as well
        DropZoneMessage (Drop files) ->
            ( { model -- Make sure to update the DropZone model
              | dropZone = DropZone.update (Drop files) model.dropZone
              , .. -- maybe store the files in your own model
            , Cmd.batch <|
       (readTextFile << .blob) files
              -- in this example the dropped files are all read
              -- with FileReader.readTextFile
        DropZoneMessage a ->
            -- These are the other DropZone actions that are not exposed,
            -- but you still need to hand it to DropZone.update so
            -- the DropZone model stays consistent
            ( { model | dropZone = DropZone.update a model.dropZone }
            , Cmd.none
        .. -- other action handling here

Render the view for your dropzone

Here the important thing is that you are in full control of rendering your dropzone. All you need to do to make sure it works as a DropZone is add the attributes you get from a call to dropZoneEventHandlers. If you like you can use the isHovering method to render your dropzone differently when the user is hovering over it with a DnD operation. dropZoneEventHandlers takes a Json.decoder to extract the "payload" from the native JS drop event. We use the FileReader.parseDroppedFiles here to extract a List of native JS File objects.

-- Write a function that renders your dropzone and use dropzoneEventHandlers to
-- turn it into a dropzone.
dropZoneView : Model -> Html msg
dropZoneView model =
      ( (hoveringDependentStyles model.dropZoneModel)
      :: dropZoneEventHandlers FileReader.parseDroppedFiles DropZoneMsg)

hoveringDependentStyles : DropZone.Model -> Html.Attribute msg
hoveringDependentStyles dropZoneModel =
  if (DropZone.isHovering dropZoneModel) then
    style [( "border", "3px dashed red")]
    style [( "border", "3px dashed steelblue")]

by Daniel Bachler, Simon Hampton