aforemny / elm-mdc

Elm port of the Material Components for the Web CSS/JS library
https://aforemny.github.io/elm-mdc
Apache License 2.0
354 stars 43 forks source link

Proposal to clean up keyboard input handling code #209

Closed berenddeboer closed 4 years ago

berenddeboer commented 5 years ago

Having keyboard handling appear in view code seems mixing concerns.

Often it does a lot of stuff calculations for a model update, then sends the results as a message.

This can be improved a bit by simply sending the command without doing calculations, but the keyboard code is still pretty unreadable with the json decoding going on. So perhaps we should take some inspiration from a new Elm keyboard package and support writing this like:

[ onKeydown [(Escape, CloseDialog) ]]

Or more complex:

[ onKeydown 
    [ (Enter, SelectListItem index)
    , (ArrowDown, FocusNextListItem index) 
    , (ArrowUp, FocusPreviousListItem index) 
    ]
]

We probably do not need to import Skinney/keyboard-events, but could add some code to Options.elm to support this.

Some difficulty is that in the update function we don't have access to the list items, so we may need to pass that along. Also setting preventDefault might depend on calculation.

berenddeboer commented 4 years ago

Added Options.OnAKeyDown which seems to be quite sufficient for the purposes of this library.