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"
  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'This class is 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:

<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.