This component is available on GitHub.
Stimulus Scroll Reveal
A Stimulus controller that animates an element when it becomes visible.
Installation
$ yarn add stimulus-scroll-reveal
And use it in your JS file:
// Probably in app/javascript/controllers/index.js
import { Application } from '@hotwired/stimulus'
import ScrollReveal from 'stimulus-scroll-reveal'
const application = Application.start()
application.register('scroll-reveal', ScrollReveal)
π Demo
See demo βUsage
In your view:
<div data-controller="scroll-reveal">
<p data-scroll-reveal-target="item" class="reveal">Hello</p>
<p data-scroll-reveal-target="item" class="reveal" data-delay="250ms">World!</p>
</div>
With custom options:
<div
data-controller="scroll-reveal"
data-scroll-reveal-class-value="active fade"
data-scroll-reveal-threshold-value="0.9"
data-scroll-reveal-root-margin-value="10px"
>
<p data-scroll-reveal-target="item" class="reveal">With custom</p>
<p data-scroll-reveal-target="item" class="reveal">options!</p>
</div>
π Configuration
Attribute | Default | Description | Optional |
---|---|---|---|
data-scroll-reveal-class-value | 'in' | These classes are added on the element when it becomes visible. | β |
data-scroll-reveal-threshold-value | 0.1 | The threshold option for the IntersectionObserver. | β |
data-scroll-reveal-root-margin-value | '0px' | The rootMargin option for the IntersectionObserver. | β |
It's up to you to create the CSS transition animation. This controller basically simply adds a class to an element when it becomes visible.
For instance:
<style>
.reveal {
opacity: 0;
transform: translateY(20px);
transition: 1s cubic-bezier(0.5, 0, 0, 1);
transition-property: opacity, transform;
}
.reveal.in {
opacity: 1;
transform: none;
}
</style>
π Extending Controller
You can use inheritance to extend the functionality of any Stimulus component:
import ScrollReveal from 'stimulus-scroll-reveal'
export default class extends ScrollReveal {
connect() {
super.connect()
console.log('Do what you want here.')
}
// Override this method to change the IntersectionObserver behavior
intersectionObserverCallback(entries, observer) {
//
}
// Options used for the IntersectionObserver
get intersectionObserverOptions() {
return {}
}
// You can override this getter to set your default options here.
get defaultOptions() {
return {
class: 'active',
threshold: 0.5,
rootMargin: '100px',
}
}
}
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.
βοΈ 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.