Sometimes, there are sets of interactive elements that we want users to be able to navigate through with arrow keys rather than with tabs, and we want the final focus change to wrap. This module makes it easier to set up this focus and wrapping behavior.
view : { toId : item -> id, focus : id -> msg, view : List (Accessibility.Styled.Key.Event msg) -> item -> Accessibility.Styled.Html msg, leftRight : Basics.Bool, upDown : Basics.Bool } -> List item -> List (Accessibility.Styled.Html msg)
Helper for creating a list of elements with looping arrow-key navigation.
Your view
function will be called for each item with the corresponding keyboard
event handlers, as well as the item itself.
e.g.
FocusLoop.view
{ id = .id
, focus = Focus
, leftRight = True
, upDown = True
, view = viewFocusableItem
}
[ { id = 1, name = "Foo" } ]
viewFocusableItem item arrowKeyHandlers =
div
[ Key.onKeyDownPreventDefault arrowKeyHandlers ]
[ text item.name ]
Does your list support adding and removing items? If so, check out FocusLoop.Lazy
which
will prevent recalculation of event handlers for every item when the list changes.
addEvents : { focus : a -> msg, leftRight : Basics.Bool, upDown : Basics.Bool } -> List a -> List ( a, List (Accessibility.Styled.Key.Event msg) )
Zip a list of items with its corresponding keyboard events.