Components

Stimulus Scroll Reveal

A Stimulus controller that animates an element when it becomes visible.


Installation

  1. Install the package

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

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

Usage

app/views/index.html
<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:

app/views/index.html
<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

AttributeDefaultDescriptionOptional
data-scroll-reveal-class-value'in'These classes are added on the element when it becomes visible.
data-scroll-reveal-threshold-value0.1The 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:

app/javascript/stylesheets/application.css
.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:

app/javascript/controllers/scroll_reveal_controller.js
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.