PhoenixKitWeb.Components.Core.RoleSwitcher (phoenix_kit v2.26.0)

Copy Markdown View Source

The switcher for the role a user acts as (PhoenixKit.Users.ActiveRole).

Renders nothing unless the scope is narrowed — the switcher is on and the user holds two or more switchable roles. An impersonation session gets the switcher like any other: the role lives on the session token, so switching there never touches the borrowed account's own sessions. Every row is a plain form PUT to /users/session/role, like the account switcher: this renders in layouts, where a phx-click would land in whichever LiveView the page mounted.

Reads only the scope: the switchable roles are loaded once by PhoenixKit.Users.Auth.Scope.for_user/1, so rendering on every page costs no query.

Variants

  • :menu_section — a "Role" section for an account dropdown's <ul class="menu">. Always rendered where the switcher is visible, except that with the switcher placed in the header it shows only below the sm breakpoint, where the header control is hidden.
  • :header — a compact dropdown for a header bar, rendered only when role_switcher_location is "header", and only from sm up.

Render both, once each, and the location setting decides which one a visitor sees at which width:

<.role_switcher variant={:header} id="admin-role-switcher-header" scope={@scope} ... />
<.role_switcher variant={:menu_section} id="admin-role-switcher-menu" scope={@scope} ... />

Summary

Functions

Renders the role switcher.

Functions

role_switcher(assigns)

Renders the role switcher.

Attributes

  • :scope — the current scope. Renders nothing unless it is narrowed.
  • :id — unique per placement; prefixes every form id.
  • :variant — :menu_section (default) or :header.
  • :current_path — sent as return_to, so the switch comes back here when the new role can reach it.
  • :current_locale — keeps the form's action on the visitor's locale.
  • :location — :menu or :header. nil (default) reads the role_switcher_location setting; pass it to render without a settings read.

Attributes

  • scope (:any) - Defaults to nil.
  • id (:string) (required)
  • variant (:atom) - Defaults to :menu_section. Must be one of :menu_section, or :header.
  • current_path (:string) - Defaults to "".
  • current_locale (:string) - Defaults to nil.
  • location (:atom) - Defaults to nil.Must be one of nil, :menu, or :header.