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?

Where Next?

Popular in Proposals: Ideas Top

mssantosdev
Hey, everyone. I’ve been working with Phoenix Components for a few weeks, and they are great. One thing I use a lot when building custom...
New
sevensidedmarble
All the pieces are there to execute arbitrary JS commands from the server. You can put them on a data property and call them from the cli...
New
mortenlund
Hi! I would like to suggest a new callback in the lifecycle of the Live Component which is unmount. Sometimes it is nice to be able to ...
New
dkuku
This is a proposal to make the map key mismatch errors a bit better: Every time I have a typo It’s very challenging for me even when I u...
New
davydog187
I’ve been enjoying the new Streams API, but I keep needing to work around the lack of access to the underlying items. One issue that cons...
New
dimitarvp
To @jonatanklosko and @the-mikedavis: I see that there is a Rust crate at crates.io: Rust Package Registry but it is pointing at https:/...
New
dogweather
Hi Phoenix community! First off, huge thanks for an amazing framework - Phoenix has been a joy to work with. I have a small UX suggestio...
New
eagle-head
Hi everyone, I’ve been researching Content Security Policy Level 3 support in Phoenix and wanted to share my findings and a proposal for...
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
sevensidedmarble
I have a very simple suggestion: the generated config/dev.exs file should read from PORT at compile time to set the endpoints port. If ...
New

Other popular topics Top

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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
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

We're in Beta

About us Mission Statement