Skip to content

Instantly share code, notes, and snippets.

@roovo
Last active February 4, 2019 20:23
Show Gist options
  • Save roovo/f2c3540c0c9917b0dbbc6e8e666adcda to your computer and use it in GitHub Desktop.
Save roovo/f2c3540c0c9917b0dbbc6e8e666adcda to your computer and use it in GitHub Desktop.
module Page.Helper.Dropdown exposing (Dropdown, Msg(..), init, isDroppedDown, itemHasBeenClicked, update)
-- TYPES
type Dropdown
= Internal { show : Bool, itemClicked : Bool }
init : Dropdown
init =
Internal { show = False, itemClicked = False }
-- INFO
itemHasBeenClicked : Dropdown -> Bool
itemHasBeenClicked (Internal status) =
status.itemClicked
isDroppedDown : Dropdown -> Bool
isDroppedDown (Internal status) =
status.show
-- UPDATE
type Msg
= ItemMouseDown
| LostFocus
| ToggleShow
update : Msg -> Dropdown -> Dropdown
update msg (Internal status) =
case msg of
ItemMouseDown ->
Internal { status | itemClicked = True }
LostFocus ->
if status.itemClicked then
Internal status
else
Internal { status | show = False }
ToggleShow ->
Internal
{ status
| show = not status.show
, itemClicked = False
}
import Page.Helper.Dropdown as Dropdown
type Msg
= GotMenuMsg Dropdown.Msg
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case ( msg, model ) of
( GotMenuMsg subMsg, _ ) ->
let
subModel =
Dropdown.update subMsg (Session.menu (toSession model))
in
( mapSessionConfig (\c -> { c | menu = subModel }) model
, Cmd.none
)
view : Model -> Document Msg
view model =
let
viewPage page toMsg config =
let
{ title, body } =
Page.view GotMenuMsg (Session.viewer (toSession model)) page config
in
{ title = title
, body = List.map (Html.map toMsg) body
}
import Page.Helper.Dropdown as Dropdown
view : (Dropdown.Msg -> msg) -> Session -> { title : String, content : Element msg } -> Document msg
view menuMsg session { title, content, modal } =
import Page.Helper.Dropdown as Dropdown exposing (Dropdown)
type Session
= Guest Nav.Key Config
type alias Config =
{ menu : Dropdown
}
menu : Session -> Dropdown
menu =
.menu << config
config : Session -> Config
config session =
case session of
Guest _ conf ->
conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment