Is there a way to have a LiveComponent figure out what path to return to from context (e.g. when closing a modal)?

I have a LiveComponent that I want to be easily portable to different views. It’s a modal pop-up for creating a new user.

Imagine you’re at accounts/:name

You click to open the “user-add” modal, which does a push_patch to accounts/:name/user-add

When done adding the user, you want it to push_patch back to accounts/:name

But say you want to have this component in accounts/:name/edit as well, and, similarly, when done adding the user, have it return from accounts/:name/edit/user-add back to accounts/:name/edit

I can do this by giving the component an assigns variable called @return_path or something like that, but I’m wondering if there is a way where you don’t explicitly have to set the assigns with this variable, and maybe the component can figure out from context which path it was called from and then just return to that path when closing.

That’s the way to go.

best example coming from phoenix itself


  @impl true
  def handle_event("close", _, socket) do
    {:noreply, push_patch(socket, to: socket.assigns.return_to)}

components are attached to the page not called from. You could investigate __MODULE__ but which url in the massive module. It is an ambigious search