This component is available on GitHub.

Stimulus Content Loader

A Stimulus controller to asynchronously load HTML from an url.

Video Tutorial

Dave Kimura from Drifting Ruby has released a presentation video on how to use this package with a real life example.

πŸ‘‰ Take a look: Deferred Content Loading

Installation

$ yarn add stimulus-content-loader

And use it in your JS file:

// Probably in app/javascript/controllers/index.js

import { Application } from '@hotwired/stimulus'
import ContentLoader from 'stimulus-content-loader'

const application = Application.start()
application.register('content-loader', ContentLoader)

πŸ‘€ Demo

See demo

Usage

In your controller:

class PostsController < ApplicationController
  def comments
    render partial: 'posts/comments', locals: { comments: @post.comments }
  end
end

In your routes:

Rails.application.routes.draw do
ressources
  get :comments, to: 'posts#comments'
end

In your view:

<div data-controller="content-loader" data-content-loader-url-value="<%= comments_path %>">
  <i class="fas fa-spinner fa-spin"></i>
  Loading comments... This content will be replaced by the content of the `posts/comments` partial generated by Rails.
</div>

<div
  data-controller="content-loader"
  data-content-loader-url-value="<%= comments_path %>"
  data-content-loader-refresh-interval-value="5000"
>
  This content will be reloaded every 5 seconds.
</div>

<div
  data-controller="content-loader"
  data-content-loader-url-value="/message.html"
  data-content-loader-load-scripts-value="true"
>
  This content will be replaced by the content of the `/message.html` page in your public folder.
</div>

<div
  data-controller="content-loader"
  data-content-loader-url-value="/message.html"
  data-content-loader-lazy-loading-value=""
>
  This content will be replaced only when the element become visible thanks to Intersection Observers.
</div>

<div
  data-controller="content-loader"
  data-content-loader-url-value="/message.html"
  data-content-loader-lazy-loading-value=""
  data-content-loader-lazy-loading-root-margin-value="30px"
  data-content-loader-lazy-loading-threshold-value="0.4"
>
  You can customize the Intersection Observer options.
</div>

<div
  data-controller="content-loader"
  data-content-loader-url-value="/message.html"
  data-content-loader-lazy-loading-value=""
  data-content-loader-refresh-interval-value="5000"
>
  You can combine lazy loading and refresh interval. The timer will start only after the first fetch.
</div>

πŸ›  Configuration

AttributeDefaultDescriptionOptional
data-content-loader-url-valueundefinedURL to fetch the content.❌
data-content-loader-refresh-interval-valueundefinedInterval in milliseconds to reload content.βœ…
data-content-loader-lazy-loading-valueundefinedFetch content when element is visible.βœ…
data-content-loader-lazy-loading-root-margin-value0pxrootMargin option for Intersection Observer.βœ…
data-content-loader-lazy-loading-threshold-value0threshold option for Intersection Observer.βœ…
data-content-loader-load-scripts-valuefalseLoad inline scripts from the content.βœ…

πŸŽ› Extending Controller

You can use inheritance to extend the functionality of any Stimulus component:

import ContentLoader from 'stimulus-content-loader'

export default class extends ContentLoader {
  connect() {
    super.connect()
    console.log('Do what you want here.')
  }
}

This controller will automatically have access to targets defined in the parent class.

If you override the connect, disconnect or any other methods from the parent, you'll want to call super.method() to make sure the parent functionality is executed.

Credits

This controller is inspired by the official Stimulus example.

β˜•οΈ Sponsors

Stimulus Component is an MIT licensed open source project and completely free to use. However, the amount of effort needed to maintain and develop new features for the project is not sustainable without proper financial backing. You can support Stimulus Components development on Github Sponsors. πŸ™

πŸ‘· Contributing

Do not hesitate to contribute to the project by adapting or adding features ! Bug reports or pull requests are welcome.

Don’t forget to drop a 🌟 on Github to support the project.

πŸ“ License

This project is released under the MIT license.