Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding the ability to style dragged div #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/src/Box.elm
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ view model =
[]
, DnD.dragged
model.draggable
Nothing
dragged
]

Expand Down
1 change: 1 addition & 0 deletions example/src/DragOnly.elm
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ view model =
[ dnd.draggable "drag-n-drop" [] [ text "hello" ]
, DnD.dragged
model
Nothing
dragged
]

Expand Down
1 change: 1 addition & 0 deletions example/src/SortingList.elm
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ view model =
]
, DnD.dragged
model.draggable
Nothing
(box "dotted" << Tuple.second)
]
)
Expand Down
17 changes: 17 additions & 0 deletions example/src/Table.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Html
import Html.Attributes exposing (style)
import Html exposing (..)
import DnD
import Mouse


main =
Expand Down Expand Up @@ -148,13 +149,29 @@ view model =
)
, DnD.dragged
model.draggableLeft
(Just draggedStyle)
dragged
, DnD.dragged
model.draggableRight
Nothing
dragged
]


draggedStyle : Mouse.Position -> Attribute m
draggedStyle p =
let
px p =
(toString p) ++ "px"
in
style
[ "position" => "absolute"
, "left" => px p.x
, "top" => px p.y
, "border" => "1px dotted #eee"
]


box : Item -> Html Msg
box item =
p [] [ text item.text ]
Expand Down
20 changes: 15 additions & 5 deletions src/DnD.elm
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,21 @@ dragged item =

DnD.dragged
model.draggable
Nothing
box
```
-}
dragged : Draggable a m -> (a -> Html m) -> Html m
dragged (Draggable model) view =
model
|> Maybe.map (\{ meta, position } -> div [ draggedStyle position ] [ view meta ])
|> Maybe.withDefault (text "")
dragged : Draggable a m -> Maybe (Mouse.Position -> Html.Attribute m) -> (a -> Html m) -> Html m
dragged (Draggable model) ownStyle view =
let
styler =
case ownStyle of
Nothing ->
draggedStyle

Just fn ->
fn
in
model
|> Maybe.map (\{ meta, position } -> div [ styler position ] [ view meta ])
|> Maybe.withDefault (text "")