wktdev

wktdev

Phoenix 1.7 generator comes with a modal in “CoreComponents”.
The instructions do not explain how to toggle it.

The base syntax is:

  <.modal
  id="user-modal"
  show >

The show attribute is what triggers the popup.

In JavaScript ,to trigger a modal you typically have a boolean to toggle the “show” attribute value. In Elixir I have no idea how to do that nor do I know if that is the approach that is expected of the developer to make it work.

The documentation is here:

      <.modal id="confirm-modal">
        Are you sure?
        <:confirm>OK</:confirm>
        <:cancel>Cancel</:cancel>
      </.modal>

  JS commands may be passed to the `:on_cancel` and `on_confirm` attributes
  for the caller to react to each button press, for example:

      <.modal id="confirm" on_confirm={JS.push("delete")} on_cancel={JS.navigate(~p"/posts")}>
        Are you sure you?
        <:confirm>OK</:confirm>
        <:cancel>Cancel</:cancel>
      </.modal>

The example does not explicitly show the show attribute, In the working example below from the phoenix generator, it is used.



<.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>

Showing Posts 1 to 9

BradS2S

BradS2S

Bind the show attribute to a flag, ‘show_modal’

Then you can use a LiveView handle_event function that responds to a phx-update event, and updates a boolean show_modal flag.

You can set the default value if the show_modal flag when you mount

wktdev

wktdev OP

I have no idea what a “flag” is.

When I search I get this:

EDIT:

I found this:

Feature flags, or feature toggles, are boolean values associated to a name . They should be used to control whether some application feature is enabled or disabled, and they are meant to be modified at runtime while an application is running. This is usually done by the people who control the application.

BradS2S

BradS2S

Usually it just means a Boolean variable. That’s how I use it. So wax on, wax off kind of deal


# In your LiveView module

defmodule MyApp.MyLiveView do
  use Phoenix.LiveView

  # Initialize show_modal to false
  def mount(_params, _session, socket) do
    {:ok, assign(socket, show_modal: false)}
  end

  # Handle the phx-update event to toggle show_modal
  def handle_event("toggle_modal", _params, socket) do
    {:noreply, assign(socket, show_modal: !socket.assigns.show_modal)}
  end
end

wktdev

wktdev OP

Do you have a remedial example?
Every time I search for any of this stuff I get a giant jungle of text and nothing is clear and simple.

I searched for an example and I got some persons entire repository:

https://github.com/tompave/fun_with_flags

wktdev

wktdev OP

How do I assign the event handler to the modal so it works?

edit, ignore comment above:

What I meant to say is, after I attached the event handler to a button, how do I bind all this to the modal?

I understand how to use assigns to set variables. The main problem I am having is I don’t know how to toggle the state of the actual modal.

wktdev

wktdev OP

For anyone asking the same question it’s two lines of code and requires JavaScript. I spent a day and a half going in circles over this nonsense. It has nothing to do with the show command. The show/hide feature pertains to the ID assignment and that it needs the JS module.




  <.modal
    id="user-modal"
    >
  </.modal>


<button phx-click={show_modal("user-modal")}>  Click me </button>
bemesa21

bemesa21

I saw that you solved your problem, but I was already writing an answer for you. I’ll leave it here in case this example works for someone else.

A function show_modal/2 is also defined in CoreComponents, the only thing you need to pass to this function is the id of your modal. In this function, JS commands are used to display the modal and apply some transitions to it.

So you just need to define the id of your modal:

<.modal
    :for={user <- @users}
    id={"delete-modal-#{user.id}"}
    on_confirm={
      JS.push("delete-user", value: %{id: user.id})
      |> hide_modal("delete-modal-#{user.id}")
      |> hide("#user-#{user.id}")
    }
>
  Are you sure you want to delete "<%= user.name %>"?
  <:cancel>Cancel</:cancel>
  <:confirm>Delete</:confirm>
</.modal>

and call that function:

<.link
  id={"delete-user-#{user.id}"}
  phx-click={show_modal("delete-modal-#{user.id}")}
>
codeanpeace

codeanpeace

I totally understand it can be frustrating and I know it’s not likely your intention, but let’s not take it out on the project and its contributors. What might seem like “nonsense” when you’re frustrated usually begins to make a lot of sense with a little bit of curiosity, patience, and experience. For example…

… following the show attribute could have gotten you to those two lines of code much sooner. If you look at the function that defines the modal in the CoreComponents module, you’ll see the how the boolean show attribute acts as a flag for the show_modal/1 function that takes an @id as an argument and wraps the Phoenix.LiveView.JS.show/2 function among other JS commands.

# CoreComponents
  attr :show, :boolean, default: false
  ...
  def modal(assigns) do
    ~H"""
    <div
      id={@id}
      phx-mounted={@show && show_modal(@id)}
      ...
    >
      ...
    """
  end
  ...
  def show_modal(js \\ %JS{}, id) when is_binary(id) do
    js
    |> JS.show(to: "#{id}")
    |> JS.show(
      to: "#{id}-bg",
      transition: {"transition-all transform ease-out duration-300", "opacity-0", "opacity-100"}
    )
    |> show("#{id}-container")
    |> JS.add_class("overflow-hidden", to: "body")
    |> JS.focus_first(to: "#{id}-content")
  end
soyjeansoy

soyjeansoy

Massive thank youuuuu!!! the show_modal function was already aliased!

The reason for that is: I looked at lib/your_app_web/your_app_web.ex,

unquote(html_helpers()) ← this

defp html_helpers do
  quote do
    # HTML escaping functionality
    import Phoenix.HTML
    # Core UI components and translation
    import HangmanWeb.CoreComponents
    import HangmanWeb.Gettext

    # Shortcut for generating JS commands
    alias Phoenix.LiveView.JS

    # Routes generation with the ~p sigil
    unquote(verified_routes())
  end
end

Took me lots of hours why JS.show_modal is undefined on the page :person_facepalming:t5:

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews