Harlock.Tabs (harlock v0.4.0)

Copy Markdown View Source

Key-event helper for the Harlock.Elements.tabs/1 widget.

The widget is a dumb renderer — the app holds the active tab id in its model. When the tabs element has focus, route key events through apply_key/3 to compute the next active id:

def update({:key, _, _} = event, %{tab: active} = model) do
  case Harlock.Focus.current() do
    :tabs ->
      case Harlock.Tabs.apply_key(event, active, [{:a, "Alpha"}, {:b, "Beta"}]) do
        {:select, id} -> %{model | tab: id}
        :noop -> model
      end

    _ ->
      model
  end
end

Bindings:

  • Left / Right — cycle tabs (with wrap)
  • Home / End — first / last
  • anything else — :noop

Auto-routing (v0.4)

When a tabs element carries a :focusable id, the runtime calls apply_key/3 automatically and delivers the result to update/2 as {:harlock_select, focus_id, new_id} — the app only writes where the active tab id lives on the model:

tabs(focusable: :nav, items: [{:a, "Alpha"}, {:b, "Beta"}], active: m.tab)

def update({:harlock_select, :nav, id}, m), do: %{m | tab: id}

Calling apply_key/3 directly from update/2 is still supported (and required for tabs widgets without a :focusable id, or with handle_keys: false).

Summary

Functions

Map a {:key, key, mods} event to a selection change.

Types

event()

@type event() :: {:select, id()} | :noop

id()

@type id() :: any()

item()

@type item() :: {id(), String.t()}

Functions

apply_key(arg1, active, items)

@spec apply_key({:key, any(), [atom()]}, id(), [item()]) :: event()

Map a {:key, key, mods} event to a selection change.