kccarter

kccarter

Improving LiveView JS interop to reduce the need for Hooks just to register for DOM events

This is likely a feature request unless we’re overlooking something, but it would be a nice improvement to the developer experience if there were LiveView Bindings for calling a Javascript function when a DOM event fires.

The existing Bindings offer phx-click for sending messages to the server, but not for calling local Javascript. phx-mounted is the closest binding, but then you have to define a bunch of Hooks just to register for regular DOM events.

The small set of JS Commands is helpful, but doesn’t allow us to call arbitrary functions.

Something like:

<div phx-click={JS.call(myJavascriptFunction(evt)}></div>

Most times, we just want an onClick, onInput or onChange handler to call some local JavaScript without any round trips to the server. So extending the Bindings to include phx-on... would be equally helpful.

Or perhaps:

<div phx-onInput={JS.call(...)}></div>
<div phx-onEvent={'input', JS.call(...)}></div>

The Client Hooks via phx-hook section explains exactly how to do what we want, but we’re curious if there’s an appetite for trying to improve this so that we don’t have to write all the Hook functions.

Most Liked

chrismccord

chrismccord

Creator of Phoenix
  # // app.js
  # window.addEventListener("js:call", e => window[e.detail.name](...e.detail.args))
  #
  # heex
  # <button phx-click={js_call("alert", ["clicked!"])}>Click me</button>
  #
  def js_call(name, args \\ []) do
    JS.dispatch("js:call", detail: %{name: name, args: args})
  end

LostKobrakai

LostKobrakai

You can do that already using Phoenix.LiveView.JS — Phoenix LiveView v1.2.5.

sodapopcan

sodapopcan

I feel the docs are a bit confusing. When given a client-side JS command, phx-click will not send any request to the server. Eg, phx-click={JS.show()}. However if you read the Bindings sections shared by @kccarter, you might think this is the case. Even when you know about JS commands, it’s still confusing wording. I can bring it up in the issue tracker unless someone thinks I’m really off-base here.

Where Next?

Popular in Proposals: Ideas Top

markevans
Hi! I’m excited about everything that’s going on re. gradual typing and am really pleased to see that Jose and the team seem to be think...
New
mikesax
On a Rails/Turbo site, the first page is typically loaded using http GET and then sockets are used navigate and replace HTML content for ...
New
snofang
In a typical business development task, having a function in a context module which accepts attributes of map type and passes them to Ect...
New
mythicalprogrammer
Hello, since the 1.8rc0 was out, DaisyUI was noted to have the benefit of light and dark mode. From the elixir subreddit, it seem a few ...
New
GenericJam
I’ve been messing around with image generation models recently and thought this could be wrapped in a library or people could just use th...
New
jakeprem
Goal: To make JS.patch and JS.navigate more interoperable with JS.push. Scenario: Imagine making a reusable Phoenix component and you wa...
New
sevensidedmarble
Hello all, Apologies if this has been proposed before I guess, but I have a very simple one: With the increasing importance of LV, I th...
New
Matsa59
Lets start by a story, I work with a team on a pretty big CMS with lots of modules. We split the cms using umbrella apps. So we have comp...
New
mxgrn
As one edits a Phoenix LiveView form in a modal, it’s very easy to accidentally press ‘Esc’ and lose all the edits. Similar to data-conf...
New
sodapopcan
This past weekend I’ve been doing some work on a PR to bring open_browser to PhoenixTest. For those who don’t know it, PhoenixTest is a ...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
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

We're in Beta

About us Mission Statement