项目作者: jinjor

项目描述 :
Flexible context menu for Elm
高级语言: Elm
项目地址: git://github.com/jinjor/elm-contextmenu.git
创建时间: 2016-11-07T03:32:17Z
项目社区:https://github.com/jinjor/elm-contextmenu

开源协议:BSD 3-Clause "New" or "Revised" License

下载


elm-contextmenu

Flexible context menu for Elm (Demo)

Warning

On the migration from Elm 0.18 to 0.19, the legacy Color type has changed to just a type alias of String like #aaa, rgb(100,100,200). Also, some icon libraries that uses Color type (i.e. FontAwesome, MaterialIcons) cannot be used anymore. So now you need to make a function typed as String -> Int -> Html msg. It should work but I haven’t tested yet.

I also think the implementation can be improved using new Browser API, but I cannot spend my time to try it. The styling method can be improved too. I would really appreciate if someone do that. Don’t hesitate to fork this package or make your own from scratch! (This article may help.)

How to use

This component works with The Elm Architecture.

1. Model

  1. type alias Model =
  2. { contextMenu : ContextMenu Context
  3. , config : ContextMenu.Config
  4. , message : String
  5. }

2. Msg

  1. type Msg
  2. = ContextMenuMsg (ContextMenu.Msg Context)
  3. | Item Int

3. Initialize

  1. init : Flags -> (Model, Cmd Msg)
  2. init flags =
  3. let
  4. (contextMenu, msg) = ContextMenu.init
  5. in
  6. ( { contextMenu = contextMenu
  7. }
  8. , Cmd.map ContextMenuMsg msg
  9. )

4. Update

  1. update : Msg -> Model -> (Model, Cmd Msg)
  2. update msg model =
  3. case msg of
  4. ContextMenuMsg msg ->
  5. let
  6. (contextMenu, cmd) =
  7. ContextMenu.update msg model.contextMenu
  8. in
  9. ( { model | contextMenu = contextMenu }
  10. , Cmd.map ContextMenuMsg cmd
  11. )

5. Subscribe

  1. subscriptions : Model -> Sub Msg
  2. subscriptions model =
  3. Sub.map ContextMenuMsg (ContextMenu.subscriptions model.contextMenu)

6. View

  1. view : Model -> Html Msg
  2. view model =
  3. div
  4. [ ContextMenu.open ContextMenuMsg "context1" ]
  5. [ ContextMenu.view
  6. ContextMenu.defaultConfig
  7. ContextMenuMsg
  8. toItemGroups
  9. toItemGroups model.contextMenu
  10. ]
  11. toItemGroups : String -> List (List Item)
  12. toItemGroups context =
  13. if context == "context1" then
  14. [ [ (ContextMenu.item "Hey", Item 1)
  15. , (ContextMenu.item "Yo!", Item 2)
  16. ]
  17. ]
  18. else
  19. []

License

BSD-3-Clause