AodhanHayter
I have a list (LiveView stream) which on mount is populated, it is then subsequently prepended to via PubSub using stream.insert. When prepending a new item to the list I want to nicely animate it’s entry.
I’m able to add this animation using phx-mounted and JS.transition, but my problem is the initial lifecycle mounts as it causes this animation to flash for the server rendered list on the initial mounts. I only want this animation to occur when a new item comes in after the initial mount.
My question is if anyone has figured out an elegant way to handle this. My first thought is counting renders and conditionally applying the transition but was hoping for a more declarative way. The markup is as follows…
<ul id="jobs" phx-update="stream" class="p-4 flex flex-col gap-4">
<li
:for={{id, j} <- @streams.jobs}
id={id}
class="border-2 p-2 h-fit flex flex-row justify-between overflow-hidden"
phx-mounted={
JS.transition(
{"first:ease-in duration-500", "first:opacity-0 first:p-0 first:h-0", "first:opacity-100"},
time: 500
)
}
>
<p>Name: <%= j.name %></p>
<p>ID: <%= j.job_id %></p>
<p>Status: <%= j.state %></p>
</li>
</ul>
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley:
Mutonex gamedesign was finish...
New
Hello
I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex











Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
tomg
You could do something like this:
Where you assign
inserttofalseon initial mount andtruebefore the first insert.AodhanHayter
This is essentially what I ended up doing. Have a flag for that list, flip the flag appropriately to enable/disable animation. No render counting required as I was initially thinking.
rhcarvalho
This solves the animation when the item is inserted.
Any similar trick to conditionally trigger a
phx-remove?I want to avoid a useless animation when navigating away from a page, and only animate when removing items from a list. Current setup is a dropdown to select number of visible items that fires an event via phx-change, then the event handler updates query params (to save client state) and does a push_patch. The list is assigned in
handle_paramsaccording to the query params (how many items to show).ppff01
While struggling with this too, I stumbled upon this thread and thought I should share my solution.
Basically my idea is to trigger UI events from their source (when the server wants to update the stream) and I think it’s less hacky than what’s proposed in this thread.
Here is my
handle_infoimplementation, based on phoenix 1.8 usage of PubSub:An important idea is not to call
stream_deleteon deletion, you’ll see why just below. (By the way this thing withsocket.assigns.filtersis because I use Flop and keep my filters in assigns to createreturn_tourls which keep the filtering.)And here is the hook on each table row:
Note that you can’t send an event to a specific hook (or at least I didn’t find out how), so you need to filter its destination directly inside the hook.
About events:
stream_deletewithout removing the element from the actual stream. But that’s not a big deal in my opinion since it will be refreshed sooner or later. Also note that you could also keep the HTML element, it would be hidden anyway thanks tojs().hide().Hope this helps!
rhcarvalho
Nice solution
! Explicit server-pushed events give much more granular control than
phx-mountedandphx-remove.I think this is very correct.
Sometimes I choose to use a global event listener, so that I have a single listener that applies the action to an element given a selector, instead of multiple instances of the hook. Note that when server sent events bubble up to the
windowthey get thephx:prefix (JavaScript interoperability — Phoenix LiveView v1.2.5).For example, in
app.js:On the server side:
A more generic listener could even exec any set of JS commands stored in an attribute (or sent along the event payload):
On the server side:
Or (see Make `JS.t()` a public data structure, or json serializable):
ppff01
Very interesting, thank you!
Edit: that’s what I ended up doing to reduce JS load on the browser (also it allows to send any transition to any DOM element, which is nice).
spicychickensauce
By the way, View Transitions actually make these kind of transitions trivial, see this example here: Same-document view transitions for single-page applications | View Transitions | Chrome for Developers
Currently not supported in live_view yet, but if my proposal can get enough attention, we might make it possible soon: