Getting Started
Components
- Animated Number
- Auto Submit
- Carousel
- Character Counter
- Chartjs
- Checkbox Select All
- Clipboard
- Color Picker
- Content Loader
- Dialog
- Dropdown
- Glow
- Lightbox
- Notification
- Password Visibility
- Places Autocomplete
- Popover
- Prefetch
- Rails Nested Form
- Read More
- Remote Rails
- Reveal Controller
- Scroll Progress
- Scroll Reveal
- Scroll To
- Sortable
- Sound
- Textarea Autogrow
- Timeago
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
Install the package
$ yarn add @stimulus-components/content-loader
Register the controller in your application
app/javascript/controllers/index.jsimport { Application } from '@hotwired/stimulus' import ContentLoader from '@stimulus-components/content-loader' const application = Application.start() application.register('content-loader', ContentLoader)
Example
Content Loader
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
get :comments, to: 'posts#comments'
end
<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
Attribute | Default | Description | Optional |
---|---|---|---|
data-content-loader-url-value | undefined | URL to fetch the content. | ❌ |
data-content-loader-refresh-interval-value | undefined | Interval in milliseconds to reload content. | ✅ |
data-content-loader-lazy-loading-value | undefined | Fetch content when element is visible. | ✅ |
data-content-loader-lazy-loading-root-margin-value | 0px | rootMargin option for Intersection Observer. | ✅ |
data-content-loader-lazy-loading-threshold-value | 0 | threshold option for Intersection Observer. | ✅ |
data-content-loader-load-scripts-value | false | Load inline scripts from the content. | ✅ |
Extending Controller
You can use inheritance to extend the functionality of any Stimulus component:
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.