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
Components
Stimulus Scroll Reveal
A Stimulus controller that animates an element when it becomes visible.
Installation
Install the package
$ yarn add @stimulus-components/scroll-reveal
Register the controller in your application
app/javascript/controllers/index.jsimport { Application } from '@hotwired/stimulus' import ScrollReveal from '@stimulus-components/scroll-reveal' const application = Application.start() application.register('scroll-reveal', ScrollReveal)
Usage
<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:
.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;
}
Extending Controller
You can use inheritance to extend the functionality of any Stimulus component:
import ScrollReveal from '@stimulus-components/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.