I’ve been online trying to learn how to create a Modal in Phoenix LiveView. I’ve been doing it for hours and I have no idea.
Do I create a LiveView and embed one LiveVIew into the other with one acting as the modal?
If so, how do I embed a LiveView into the other? I read the docs and they are not clear and direct. I used the example code and I have no desire to jump through another hour of errors just to embed a file into another.
That doc doesn’t say how to build the Modal - just that you can. The rest of that text is a terse brick that requires a translator. I’m too dumb to understand it.
I’m sure it’s not that difficult.
As it stands I have a LiveVIew with a button and and when I click it, faux data is sent to the database. So that works.
I know how to write a form and submit the data to the database. I need to know what to focus on to complete the rest. When the user clicks the button, a form modal should appear. The form is filled out and submitted by a user. The data is captured and manipulated in the parent LiveView and sent off to the database.
The generator shows the code below. There is no documentation for it anywhere.
<.modal
:if={@live_action in [:new, :edit]}
id="user-modal"
show
on_cancel={JS.navigate(~p"/users")}
>
<.live_component
module={AppxWeb.UserLive.FormComponent}
id={@user.id || :new}
title={@page_title}
action={@live_action}
user={@user}
patch={~p"/users"}
/>
</.modal>
Other than the <.modal> tag, id and show attributes I don’t understand any of it.
The show attribute creates the pop up effect but I don’t know how to toggle it dynamically. The example is too complicated.
EDIT
I’m looking up socket.assigns.live_action
If I can understand that, I think the rest might be simple.
EDIT
Looks like socket.assigns.live_action isn’t in the documentation.
I’d suggest reading the sections on attributes and slots on Phoenix.Component module docs and then searching for where the modal is defined in your project aka def modal.
Note that while the guide documentations are generally very good, they seem to often lag the module documentation since LiveView is still evolving rapidly.
You weren’t wrong, searching for <.modal> in the HEX docs would not have returned anything useful at the moment. There’s nothing really special about <.modal>, modal is just the name of a function in a module that got imported. If the module wasn’t imported, it’d look like <SomeModule.modal> or <SomeModule.some_other_function>.
Now if you had searched for .modal in the Phoenix repo itself, you may have found the phx.gen.live templates that add the modal function definition to your project.