Qqwy

Qqwy

TypeCheck Core Team

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

First 10 of 13 Posts Switch mode

Qqwy

Qqwy OP

TypeCheck Core Team

By the way, this proposal is tangentially related to this issue.

Qqwy

Qqwy OP

TypeCheck Core Team

@chrismccord, @josevalim, @snewcomer: feedback would be much appreciated! :blush:

chrismccord

chrismccord

Creator of Phoenix

LV master allows you to define a metadata callback to populate the payload however you’d like. You receive the JS event + DOM target :slight_smile:

Qqwy

Qqwy OP

TypeCheck Core Team

Thank you! This is indeed great to make phx-click and phx-keydown more versatile.
But does it enable other kinds of mouse events? (In my quick perusal of the Git diff of the Merge Request containing the change you mentioned I was not able to find information about that, but I might have missed something.)

josevalim

josevalim

Creator of Elixir

Adding new events will always be a compromise, as we want to avoid having an excessive amount of “data attributes”. It is probably best for now to implement those as hooks.

Your argument that using a hook won’t provide all features as Phoenix is true but note you don’t have to support all features, only the ones you need. Also, by using a hook, you can actually stay ahead and do things that LV doesn’t currently. :slight_smile:

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.

Qqwy

Qqwy OP

TypeCheck Core Team

Thank you for your response! What you say is true, but it was not enough to remove the gut feeling that something is missing. I took a few days to think about this matter in more depth and to formulate my thoughts. What it boils down to, is this:

It assumes that the person writing the JavaScript for the hook and the person using the hook will be the same. If a hook only partially supports the things that LiveView does, then it is difficult to reuse that hook or publish it in a library.
Essentially, the cognitive overhead for understanding and using something that is only able to partially integrate with LiveView is much higher. I think this is where my gut feeling against ‘ad-hoc hooks’ comes from.

Does that make sense?

samaaron

samaaron

I came here hoping to find phx-mouse-down as click events on mouse up (which appears to be the default) aren’t at all intuitive for my usecase.

I guess I can look into doing this (although I have absolutely no idea at this stage so it’s back to google for me). If I manage to figure it out, I’ll come back and add a little example here so others don’t have to search as much :slight_smile:

chrismccord

chrismccord

Creator of Phoenix

for mousedown a hook + pushEvent is your best bet

samaaron

samaaron

Yep, that’s precisely what I’m trying my best to figure out how to do. My issue is that I have zero javascript background, so knowing how to work effectively (or pretty much at all) with hooks is currently a huge blindspot for me.

I’m sure I’ll get there eventually…

[Update] - I managed it. Thanks everyone for your help and support :slight_smile: Will post here later with a short description of what was necessary.

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement