Components

Stimulus Content Loader

A Stimulus controller to asynchronously load HTML from an url.


If Turbo is activated within your application, you can get the same behavior using a <turbo-frame> without the necessity of an additional Stimulus controller.

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

  1. Install the package

    $ yarn add @stimulus-components/content-loader
    
  2. Register the controller in your application

    app/javascript/controllers/index.js
    import { Application } from '@hotwired/stimulus'
    import ContentLoader from '@stimulus-components/content-loader'
    
    const application = Application.start()
    application.register('content-loader', ContentLoader)
    

Usage

In your controller:

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

In your routes:

config/routes.rb
Rails.application.routes.draw do
  get :comments, to: 'posts#comments'
end
app/views/index.html.erb
<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:

app/javascript/controllers/content_loader_controller.js
import ContentLoader from '@stimulus-components/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.