zporter

zporter

I have a LiveView page in which I would like to render a stream of items differently depending on viewport size. Currently, I use Tailwind classes like hidden and lg:inline-table to render a <table> on wider viewports and lg:hidden and block to render a list of cards on narrow viewports. In order to render both lists within the same DOM, I need to give each element a unique identifier. This requires me to setup two streams pointed to the same data source and configured to generate DOM identifiers differently: stream(:rows) and stream(:cards).

Using two streams in this way works well until I want to use the InfiniteScroll hook. On LiveView version 0.19.3, using the phx-viewport-bottom attribute on both of the stream elements results in triggering more rows to be added as soon as a scroll action is detected. This is due to the hidden list element’s last child being higher up in the viewport. A small change to the onScroll event handler in the InfiniteScroll hook to exit early if the element is hidden (if (this.el.offsetParent === null) { return };) fixes the scroll behavior for the page and items aren’t loaded until the last child of the visible element is on the screen.

Have others run into similar issues? Does the proposed solution make sense or should I be considering other options? I’m happy to open a Pull Request with the aforementioned change, but I wanted to solicit ideas here first. Thank you!

Showing Posts 1 to 2

jason.o

jason.o

I know this is an old post but I am implementing something very similar, and my solution is basically rendering the table or the card view depending on the screen size. (e.g., <table :if={@screen_layout == :desktop} />) It’s not perfect, but works decently.

byhemechi

byhemechi

Just for anyone landing on this thread from search i figured i’d put this here

Instead of using phx-viewport-bottom you could use an Intersection Observer positioned after the streamed elements.

What I usually do is create a custom element that “clicks” a child button once it is in view. Here’s an example of how that could be implemented

class InfiniteScrollTrigger extends HTMLElement {
  observer = new IntersectionObserver(([entry]) => this.trigger(entry), {
    threshold: 1,
  });

  trigger(entry) {
    this.querySelector("button")?.click();
  }

  connectedCallback() {
    this.observer.observe(this);
    this.style.display = "contents";
  }

  disconnectedCallback() {
    this.observer.disconnect();
  }
}


customElements.define("infinite-scroll-trigger", InfiniteScrollTrigger);
  <infinite-scroll-trigger event="load_more" value={@cursor}>
    <.button phx-click="load_more" value={@cursor} phx-disable-with="Loading...">
      Load more
    </.button>
  </infinite-scroll-trigger>

Alternatively, you could make a hook which you apply to a div or something.
I prefer the wrapper method as it makes loading state much simpler, you just use the built in phx attributes


hooks.InfiniteScrollTrigger = {
  mounted() {
    this.observer = new IntersectionObserver(([{ isIntersecting }]) => {
      if (!this.el.hasAttribute("disabled")) {
        this.pushEvent(this.el.getAttribute("phx-intersect"), {
          value: this.el.getAttribute("value"),
        });
      }
    });
    this.observer.observe(this.el);
  },
  beforeDestroy() {
    this.obsever.disconnect();
  },
};
— 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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
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

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
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews