Skip to content

Elm-lang select/menu using the browser focus. Minial approach: ~200 lines of code

License

Notifications You must be signed in to change notification settings

colepoirier/select-menu

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Select/menu for Elm

Yet another implementation of select/menu component. This one is based on two principles:

  • use the browser focus capablities
  • don't store the lists (that can be long sometimes)

This allows to make the library pretty small (~200 lines of code currently).

This module handles:

  • list of links causing redirection when selected - menu mode
  • list of buttons carring value - select mode

There's a keyboard managment included: up/down navigation, escape hides dropdown, enter selects.

Example

To see live example go to the example page and be sure to check out the example code to get a better idea on how the configs work.

import Html exposing (Html, div, button, text)
import Menu exposing (Menu)


type alias Model =
    {menu: Menu}


init : Model
init =
    {menu = Menu.init "unique-identifier"}


type Msg 
    = MenuMsg Menu.Msg
    | Selected String


-- Use it as dropdown menu with html links

simpleMenu: Model -> Html msg
simpleMenu { menu } =
    div [ ]
        [ button (Menu.trigger MenuMsg menu) [ text "Videos" ]
        , Menu.view menu [ class "mt-0 sm:mt-4" ] 
            [ Menu.link "/#popular" [] [ text "Popular" ]
            , Menu.link "/#viewed" [] [ text "Most viewed" ]
            , Menu.link "/#rated" [] [ text "Best rated" ]
            ]
            |> Html.map MenuMsg
        ]

-- .. or use it as a select with Selected messsage

simpleSelect: Model -> Html msg
simpleSelect { menu } =
    div [ Menu.onSelect Selected menu ]
        [ button (Menu.trigger MenuMsg menu) 
            [ text "What's your breakfast type?" 
            ]
        , Menu.view menu []
            [ Menu.button "C" [] [ text "Continental" ]
            , Menu.button "E" [] [ text "English" ]
            , Menu.button "A" [] [ text "American" ]
            ]
        ]

About

Elm-lang select/menu using the browser focus. Minial approach: ~200 lines of code

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Elm 100.0%