tubedude

tubedude

Enhancing Client-Side Navigation in Phoenix Live View

Hey,
Earlier i posted a question, but after some research I think a proposal is due.

Working on a Phoenix Live View app, I needed client-side navigation from a select element without server trips. Initially, i tried phx-click on each option element but it was never triggered.

I was able to wrap the select element on a form, and using the phx-change it was able to send the change to the server, but for a simple navigation, I don’t feel a server trip is the right way to implement it.

I was able to make it work by creating a custom event, and rebuild the encodeJS and call execution on it.

JavaScript:

window.addEventListener("live_view_navigate", event => {
  const path = event.target.value;
  liveSocket.execJS(event.target, `[['patch', {"href": "${path}"}]]`);
});

HTML:

<select phx-change={JS.dispatch("live_view_navigate")}>
  <%= for option <- @options do %>
    <option value={option.path}><%= option.label %></option>
  <% end %>
</select>

This gets the job done but leans on JavaScript, which feels off in a LiveView context.

Proposal

Let’s simplify. How about a built-in Live View solution that receives a new option that points to the attribute in which the actual path is? Like this:

<select phx-change={JS.push(using: "value")}>
  <option value="1">First</option>
  <option value="2">Second</option>
</select>

Choosing the first option here would mimic JS.push(to: "1"). The implementation in phoenix_live_view.js could be somethig like:

exec_patch(eventType, phxEvent, view, sourceEl, el, { href, replace, using = null }) {
  let targetValue = using ? sourceEl.getAttribute(using) : href;
  view.liveSocket.pushHistoryPatch(targetValue, replace ? "replace" : "push", sourceEl);
}

This change aims to streamline client-side navigation without external JavaScript or server interactions. There are a few other changes to be done in the codebase, but I’d be happy to submit a PR.

What do you think?

First Post!

cmo

cmo

I think it is a mistake to try and avoid writing JavaScript at all costs when we are developing web pages.

You would need a way for the developer to choose whether it is patch, navigate or href navigation.

How does using a select for navigation affect the the lives of assistive technology users?

Last Post!

cmo

cmo

You’re right, people do use select for nav. Personally, I like to keep things simple and expected so generally use nav, li and a. Navigation menus should be pretty :slight_smile:

Where Next?

Popular in Proposals: Ideas Top

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
Aduril
Hello there, Whenever I setup a new project, there is a small function I always add: reply/1. What does it do? In a LiveView mount, han...
New
sodapopcan
So after complaining about this for the third or fourth time on this forum, I figured I should make a proposal. TL;DR with can be hard t...
New
altdsoy
Hi there :wave:, I hope everyone is doing well… Tl;Dr: Is there a use case to have Heroicons from an actual hex.pm package instead of G...
New
superchris
Hello! After seeing a couple of posts from Chris and Jose about supporting web components in LiveView, I thought it would be great to ini...
New
byhemechi
Many web frameworks (e.g. Remix, Gatsby) have an option for their link components that begins the navigation request on hover so that whe...
New
cheerfulstoic
I feel like Elixir is getting big enough and old enough that I’m starting to experience problems with conflicting dependencies. An examp...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement