coen.bakker

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

coen.bakker

I also upgrade to the latest version of LiveView (0.20.9) just now, to rule out that being the reason.

sodapopcan

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

sodapopcan

Sooooo strange. Possibly a bug but trying it in a new app is a good idea.

Last Post!

MitchellJeppson

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.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
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
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

We're in Beta

About us Mission Statement