Raees678

Raees678

Unexpected behaviour of phx-click-away inside LiveComponent

I have a sample LiveView app with 2 LiveViews and 1 LiveComponent. LiveView 1 is live_rendered from the app layout so that it appears on every page (kinda like a sidebar). LiveView 2 is displayed when the page is routed to. LiveView 2 has a LiveComponent embedded inside it. The LiveComponent has a phx-click-away handle and a phx-target of @myself on it.

When my click is outside the LiveComponent but my click is inside LiveView 2, the click-away event fires, and the handle_event is called. However when I click on LiveView 1, the browser console records the error: no component found matching phx-target of 1, and the handle_event for the click-away is never called. I am trying to understand the behavior of phx-click-away, when it is inside a live-component, since this is unexpected. If the phx-click-away were inside LiveView 2, then it would fire when I clicked on LiveView 1.

How can I replicate the same behaviour of phx-click-away when it is fired on a LiveComponent, (especially when the LiveComponent is something like a dropdown that manages its own state, and needs phx-target={@myself})?

Here is some sample code to showing changes I made to reproduce this from the starter phoenix project.

router.ex:

... rest of router code
live "/", SecondLive
...

app.html.heex:

<main class="flex h-screen">
  <%= live_render(@socket, ClickawayWeb.FirstLive, id: "first", sticky: true) %>
  <.flash_group flash={@flash} />
  <%= @inner_content %>
</main>

live/first_live.ex:

defmodule ClickawayWeb.FirstLive do
  use ClickawayWeb, :live_view

  @impl true
  def mount(_params, _session, socket) do
    {:ok, socket, layout: false}
  end

  @impl true
  def render(assigns) do
    ~H"""
    <div class="bg-blue-300 flex-1 p-4 h-full">
      First (LiveView embedded in layout)
    </div>
    """
  end
end

live/second_live.ex:

defmodule ClickawayWeb.SecondLive do
  use ClickawayWeb, :live_view

  @impl true
  def render(assigns) do
    ~H"""
    <div class="bg-red-300 flex-1 p-4">
      Second  (LiveView on page) <.live_component module={ClickawayWeb.ThirdLive} id="third" />
    </div>
    """
  end
end

live/third_live.ex:

defmodule ClickawayWeb.ThirdLive do
  use ClickawayWeb, :live_component

  @impl true
  def handle_event("clicked-away-third", _, socket) do
    IO.inspect("clicked-away-third")
    {:noreply, socket}
  end

  @impl true
  def render(assigns) do
    ~H"""
    <div class="bg-green-300 flex-1 p-4" phx-click-away="clicked-away-third" phx-target={@myself}>
      Third (LiveComponent in second)
    </div>
    """
  end
end

Most Liked

soup

soup

In dispatchClickAway, we find all elements with phx-click-away, then map over them calling, withinOwners of the event target (which is the sticky view since we clicked in that),

https://github.com/phoenixframework/phoenix_live_view/blob/378e8493ca1e1340a4fc8ade8f328db1fb12c524/assets/js/phoenix_live_view/live_socket.js#L658-L670

Where withinOwners calls to owner

https://github.com/phoenixframework/phoenix_live_view/blob/378e8493ca1e1340a4fc8ade8f328db1fb12c524/assets/js/phoenix_live_view/live_socket.js#L438-L440

https://github.com/phoenixframework/phoenix_live_view/blob/378e8493ca1e1340a4fc8ade8f328db1fb12c524/assets/js/phoenix_live_view/live_socket.js#L433-L436

Owner calls closest view on the element (which is your sticky view since we pass in e.target), so owners <- withinOwners returns the stickyview and we then push the click event to the server with the sticky view + the component element, but since the component does not exist in the sticky view we see the error.

Probably you could just write

this.withinOwners(el, view => {

As I think we would only want to dispatch the event to the phx-click-away owner, not the click target owner?

I will post an issue I guess.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement