PhoenixKitWeb.Live.Settings.UrlTabs (phoenix_kit v2.45.0)

Copy Markdown View Source

Keeps the active tab of a settings page in the URL (?tab=sessions).

A tab held only in the socket is lost on refresh, can't be linked to and isn't restored by the browser's Back button. The settings pages render every pane in one LiveView (and often one form) and hide the inactive ones, so the tabs patch: the LiveView stays mounted, nothing is reloaded, unsaved edits in a hidden pane survive, and only the URL changes.

A page keeps one list of tab maps (%{id:, label:, icon:}, the shape PhoenixKitWeb.Components.Core.NavTabs takes) and uses it twice:

def handle_params(params, _url, socket) do
  {:noreply, assign(socket, :active_tab, UrlTabs.active(params, tabs()))}
end

# template
<.nav_tabs
  active_tab={@active_tab}
  tabs={UrlTabs.patch_links(tabs(), "/admin/settings/users")}
  variant={:border}
/>

The first tab is the default and its URL carries no query. An unknown ?tab= opens the default rather than rendering nothing.

Summary

Functions

The id of the tab params["tab"] names, or the first tab's.

tabs with a :patch link added to each, under base_path (the canonical /admin/... path — Routes.path/1 prefixes and localizes it).

Functions

active(arg1, tabs)

@spec active(map(), [map()]) :: String.t()

The id of the tab params["tab"] names, or the first tab's.

patch_links(tabs, base_path)

@spec patch_links([map()], String.t()) :: [map()]

tabs with a :patch link added to each, under base_path (the canonical /admin/... path — Routes.path/1 prefixes and localizes it).