why

why

Hello,

I like the idea of using a local js helper library like alpinejs when working with Liveview. I find it very useful for simple things like menu open/close where higher latency might be a tad too noticeable when solved in Liveview. What’s particularly nice is that I keep the ability to “liveview update” content inside an element opened via alpine.

Conveniently, support for libraries like alpinejs was added in Liveview a little while ago, documented here. To get to the relevant subsection, one needs to search for the paragraph that contains “onBeforeElUpdated”.

Unfortunately, there seems to be a security concern in alpine that makes me shy away from it at the moment. Hoping there will be a solution from the alpine team in the not too distant future, though :slight_smile:

In the meantime, my options seem to be:

  1. Use liveview JS hooks with phx-update=“ignore” for these situations. Works ok, but I lose the ability to “liveview update” content inside ignored elements.

  2. Use liveview JS hooks without phx-update=“ignore” but using the “updated” callback to rescue an open menu after a Liveview update closed it. Am in the process of experimenting with this, the “updated” callback is relatively easy to understand.

  3. Use the onBeforeElUpdated callback mentioned above like alpine does. Unfortunately, I can’t quite figure out how to use that, and was wondering if anybody might have an example? I’m looking at the morphdom docs at the moment, but it’s slow going…

Thank you!

Showing Posts 1 to 4

daveboo

daveboo

Any luck on #2 yet? I have this exact issue.

why

why OP

I didn’t have any luck with #2, but I ended up settling on #3, and with some experimentation got it to work.

It’s really very simple, the onBeforeElUpdated callback gives you the option to hook into the step when Liveview asks morphdom to update a specific DOM node with some change.

At the onBeforeElUpdated point, morphdom has two versions of the DOM node it is working with: the “to” version (what the node should become) and the “from” version (what the node is at the moment).

If, for example, you want to preserve all the styles in the classList that are there before Liveview wants to push its update, simply set the to.classList to the state of from.classList, like this:

...
dom: {
    onBeforeElUpdated(from, to){
      to.classList = from.classList
    }
  },
...

Important note: It seems like you need to add a unique ID to the node whose classList (or other property) you want to preserve with the above.

The above, is, of course, just a simplification, as you probably want to have some logic to have this happen only on nodes that meet certain criteria. Examples might be only those nodes whose styling is impacted by your local JS helper library, etc.

By the way, playing with this helped me understand how Liveview works much better, and gave me an appreciation for the awesome library that morphdom is :slightly_smiling_face:

You can, for example, see morphdom walking the DOM live, by doing something like this (note that the console.logs slow the process down quite a bit):

...
dom: {
    onBeforeElUpdated(from, to){
      console.log("****************")
      console.log("from state:")
      console.log(from)
      console.log("to state:")
      console.log(to)
    }
  },
...

Hope this helps!

Edit: On further thought, it might be that my problem with #2 was that I didn’t use an ID on the node. It might simply work if you give the node a unique ID…

daveboo

daveboo

So, would classList be the id of the HTML element that contains the classes that you would like to preserve?

idi527

idi527

Note that doing

onBeforeElUpdated(from, to){
  to.classList = from.classList
}

would copy the classlist of every patched element over. That might be not what you want since it might revert class changes made via liveview. You’d probably restrict the copying to certain elements only:

onBeforeElUpdated(from, to){
  if (from.classList.contains("before-update-copy-class")) {
    to.classList = from.classList
  }
}

I’ve also settled on that approach in a similar problem Liveview with Muuri grid (would phx-update="stop-ignore" make sense?).

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews