toggly_live_view

LiveView feature assigns, update subscriptions and declarative HEEx gates for Toggly. Usable with online definitions or offline defaults.

Requires Elixir 1.20+, OTP 29+, Phoenix 1.8 / LiveView 1.2+. Add {:toggly_live_view, "~> 0.1.0"}. Supervise one named Toggly client; core documentation covers configuration and filters.

use Phoenix.LiveView
import Toggly.LiveView, only: [feature: 1]
on_mount {Toggly.LiveView, {MyApp.Flags, ["new-dashboard", "api-v2"]}}

The hook reads socket.assigns.toggly_context, or the signed session's "toggly_context" map. Populate it from authenticated application state before the Toggly hook. HTTP Plug assigns do not automatically become LiveView session data. Raw URL parameters are not trusted identity.

<.feature flags={@toggly_flags} feature={["new-dashboard", "api-v2"]} requirement={:any}>
<p>New dashboard</p>
</.feature>
<.feature
flags={@toggly_flags}
feature={["new-dashboard", "api-v2"]}
requirement={:any}
negate={true}
>
<p>Classic dashboard</p>
</.feature>

Use a second feature block with negate={true} for disabled content, keeping the same flags, feature keys, requirement and default on both blocks. Negation applies after the all/any result is combined. Each block renders its ordinary children only when its result is true.

requirement defaults to :all; negate and default default to false. Empty features are false before negation. Flags are booleans, not multivariate experiment assignments. Gates render presentation branches and do not authorize access.

Use Toggly.LiveView.assign_feature_flags(socket, MyApp.Flags, keys, context: context) after a socket-local identity or entity change. The on_mount hook subscribes only for connected sockets, reevaluates using that socket's current context on updates, and resubscribes after a client restart. Process death removes server subscriptions; no manual terminate callback is required. Unrelated messages continue to your LiveView handler.

The socket receives only evaluated booleans. Do not send backend app keys or raw definitions to browsers. See the full Phoenix showcase for identity and Order.Vip contexts.

Run mix test --cover from the umbrella root. Tests use real connected LiveViews for concurrent identities, definition updates, client restarts and cleanup. MIT.