coen.bakker
Scroll position changes on stream_insert/4 when using LiveView streams
When I stream_insert/4 into an existing stream on a live view, it causes the scroll position to change.
For example, in this simply live view the “update” event causes the page to scroll to the top.
defmodule MyAppWeb.TempLive do
use MyAppWeb, :live_view
import ...
alias ...
def mount(_, _, socket) do
{:ok, stream(socket, :posts, get_post_feed())}
end
def render(assigns) do
~H"""
<div
id="stream"
phx-update="stream"
class="flex flex-col gap-10"
>
<%= for {dom_id, post} <- @streams.posts do %>
<div id={dom_id}>
<p><%= post.message %></p>
<button phx-click="update" phx-value-id={post.id}>Click</button>
</div>
<% end %>
</div>
"""
end
def handle_event("update", %{"id" => id}, socket) do
{:noreply, stream_insert(socket, :posts, %Post{id: id, message: "Updated message"})}
end
end
I looked around in the TodoTrek repo (GitHub - chrismccord/todo_trek · GitHub) to see if I’ve missed some client side hook for managing scroll position or something along those lines. But I didn’t find anything to explain why I get the scroll jumping while other seem to don’t.
Most Liked
coen.bakker
I also upgrade to the latest version of LiveView (0.20.9) just now, to rule out that being the reason.
sodapopcan
I tried your code and can’t reproduce it. What browser is it failing in for ? I’m using Chromium (Arc and I even dusted off Chrome to give it a try). I also tried in Safari.
sodapopcan
Last Post!
MitchellJeppson
I had a similar problem and the cause for me was the element with phx-update and phx-viewport-bottom and phx-viewport-top was not the scrolling element. The parent element was the one that was doing the scrolling. Once I fixed up the css to force the correct element to scroll it worked as expected.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









