Qqwy

Qqwy

TypeCheck Core Team

Phoenix LiveView request/proposal: mouse events

Development on Phoenix LiveView is of course in full swing. However, here are a couple of features I currently miss and proposals to add them to Phoenix LiveView.

Mouse Events

Currently, LiveView supports phx-click which triggers whenever there is a JavaScript click event.
In multiple projects I have encountered cases with a large need for other types of interaction. Specifically, custom form elements frequently require things to happen on mousedown, when dragging, or when doubleclicking.

I propose that the following events are added to LiveView (whose behaviour is similar to phx-click in that the same extra information is sent, as well as the phx-value-*s that are set on that particular element):

  • phx-mousedown
  • phx-mousemove
  • phx-mouseup
  • phx-dblclick

Supporting other click-like events

I believe above list covers the most common interactions. Of course, there exist many more mouse-related (as well as more general touch-related) events.
On top of above list of events, I therefore propose that it should be made easier to hook into the existing code that sends a phx-click-like event to the server.

Reason:

If someone were to create custom JavaScript for such an event right now, the would not only be re-inventing the logic that LiveView itself uses. The bigger issue is that their logic will not share the updates/changes to the code LiveView itself has, forever lagging behind with the implementation.
To be more precise, the following is very difficult (already right now) to accurately reproduce in a custom JS hook:

  • The new event should include all phx-value-*s using the same naming conventions/conversions as a normal (built-in) event.
  • The new event should include the same meta information with the request.
  • The new event should adhere to supplied phx-debounce and phx-throttle settings.
  • The new event should trigger on the correctly specified phx-target.
  • This list will grow and change with future features being added to LiveView.

Proposal

Some standardized way to add a new type of click-like event to a Phoenix LiveView instance would be a solution, such as adding a configuration field that accepts a list of event-names or alternatively an object of event name keys with callback function values, where the callback function is able to do things before an event is dispatched and can yield to the event dispatching logic at some point (either by using ES6 features or by e.g. just passing in the function that can do the work as argument to the callback function so it can call it whenever it likes).

Most Liked

samaaron

samaaron

OK, for those looking to work with mouse down events, here’s the things I had to learn/do:

  1. Create the HTML element in the call to render with an id and phx-hook attributes,
  2. Modify my app.js to work with the hook and specifically handle the element’s mouse down event
  3. Add a new event handler in my LiveView component to receive the event.

Let’s look at each in turn…

1. Creating the HTML element

Somewhere in your render you need to have a HTML element you’d like to listen to events on. I’m using a button here. Your element needs to have a unique ID (this can be anything but is typically hyphen-separated such as "lovely-button") and also a phx-hook attribute which can also be anything but is typically CamelCase such as YoButton:

<button id="lovely-button" phx-hook="YoButton">yo button!</button>

2. Handling the JS event

Now we have our HTML element, we need to add the appropriate javascript event handler. This will be called when the button is clicked and will then tell the server via the pushEvent function.

First we need to edit our app.js to include a Hooks object which includes an internal YoButton object (this needs to be named similarly to the phx-hook attribute above).

LiveView gives us a mounted callback to use which is called when all the server and client initialisation is completed, so we know things are ready to go.

Inside this callback, we can add our event handler which listens for 'mousedown' events and then uses pushEvent to tell the server:

let Hooks = {}

Hooks.YoButton = {
  mounted(){
    this.el.addEventListener('mousedown', e => {
     this.pushEvent("mouseymouse");
    });
  }
}

We also need to teach our liveSocket about the Hooks:

let liveSocket = new LiveSocket("/live", Socket, {
  hooks: Hooks,
  params: {_csrf_token: csrfToken},
  ....

3. Receiving the event on the server

This is probably the easiest bit. In our LiveView component we just need a new handler function:

def handle_event("mouseymouse", _params, socket) do
   # do something interesting!
  {:noreply, socket}
end

I hope that this helps someone trying to do something similar :slight_smile:

Anko

Anko

came here after reading this post by John Carmack https://twitter.com/ID_AA_Carmack/status/1787850053912064005

basically he advocates for mouse down making interfaces a lot snappier. I have tried it and it does really seem to work… Maybe it’s worth reviving this conversation?

kartheek

kartheek

LiveView is evolving similar to Java Server Faces. Java Server Faces is more component oriented specification of server side rendering of views. Oracle ADF Faces is JSF based framework which had extensive support for building rich client applications.

ADF Faces has 2 types of events server-side and client-side - client side events were meant to be handled on browser side and server side events which will be sent to server for handling on it on server side. Table - 6-1, 6-2, 6-3, 6-4 have different events generated by ADF Faces.

Leaving this here for reference and prior art.

Where Next?

Popular in Discussions Top

andre1sk
A big advantage to Elixir is all the distributed goodness but for many applications running on multiple nodes having integrated Etcd, Zoo...
New
WildYorkies
It seems that the more I read, the more I find Elixir users speaking about all the ways that Elixir is not good for x, y, and z use cases...
New
sashaafm
I’m trying to evaluate the best combo/stack for a BEAM Web app. Right now I’m exploring Yaws a bit, after having dealt with Phoenix for a...
New
owaisqayum
I have a sample string sentence = "Hello, world ... 123 *** ^%&amp;*())^% %%:&gt;" From this string, I want to only keep the integers, ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
eteeselink
Hi all, In the last days, two things happened: A blog post titled “They might never tell you it’s broken” made the rounds. It’s about ...
New
chulkilee
Here are the list of HTTP client libraries/wrappers, and some thoughts on HTTP client in general. I’d like to hear from others how they w...
New
opsb
We’re considering our architecture from a viewpoint of scaling our traffic heavily over the next 6 months. Our current deployment is runn...
New
sashaafm
Piggy backing a bit on @dvcrn topic BEAM optimization for functions with static return type?, I’ve been trying to understand in a deeper ...
New
Qqwy
Looking at the stacks that existing large companies have used, WhatsApp internally uses Mnesia to store the messages, while Discord uses ...
New

Other popular topics 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
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43806 214
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127536 1222
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

We're in Beta

About us Mission Statement