adrienpoly / infinite-scroll-stimulus-js

Infinite scroll with Stimulus.js, Stimulus-use and Pagy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Infinite scroll

Example for using intersection observers to create an infinite scroll in a rails App.

This demo leverage stimulus-use new appear behaviour to simplify the js controller

Stimulus controller

import { Controller } from "stimulus";
import { useIntersection } from "stimulus-use";

export default class extends Controller {
  connect() {
    useIntersection(this, {
      rootMargin: "100px",
    });
  }

  appear() {
    this.loadMore(this.nextUrl);
  }

  loadMore(url) {
    Rails.ajax({
      type: "GET",
      url: url,
      success: (_data, _status, xhr) => {
        this.element.outerHTML = xhr.response;
      },
    });
  }

  get nextUrl() {
    return this.data.get("nextUrl");
  }
}

About

Infinite scroll with Stimulus.js, Stimulus-use and Pagy


Languages

Language:Ruby 71.9%Language:HTML 22.2%Language:JavaScript 3.8%Language:CSS 2.1%