All Projects β†’ maecapozzi β†’ react-scroll-activator

maecapozzi / react-scroll-activator

Licence: MIT License
A React Component that watches for a scroll event and triggers behavior

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to react-scroll-activator

Scrolltrigger
Let your page react to scroll changes.
Stars: ✭ 3,547 (+18568.42%)
Mutual labels:  trigger, user-scrolls
animusjs
πŸŽ† AnimusJS is the solution for combine JS and CSS animations.
Stars: ✭ 42 (+121.05%)
Mutual labels:  trigger, user-scrolls
spa-bus
πŸ”₯Tools for multilevel components to pass values in any SPA
Stars: ✭ 15 (-21.05%)
Mutual labels:  events, trigger
event
The implementation of the pattern observer
Stars: ✭ 45 (+136.84%)
Mutual labels:  events
VBA-Userform-EventListener
πŸŽ‰ A very easy way to add event listeners to a userform.
Stars: ✭ 17 (-10.53%)
Mutual labels:  events
psql-streamer
Stream database events from PostgreSQL to Kafka
Stars: ✭ 37 (+94.74%)
Mutual labels:  events
yii2-forms
Forms CRUD - formbuilder, generator code
Stars: ✭ 32 (+68.42%)
Mutual labels:  events
certificates
πŸŽ“ Generate event certificates easily
Stars: ✭ 50 (+163.16%)
Mutual labels:  events
preact-delegate
Preact delegate DOM events
Stars: ✭ 17 (-10.53%)
Mutual labels:  events
eventigo-web
Events aggregator and newsletter
Stars: ✭ 22 (+15.79%)
Mutual labels:  events
events
Event emitter with asynchronous events.
Stars: ✭ 12 (-36.84%)
Mutual labels:  events
use-bus
React hook to subscribe and dispatch events accros React components
Stars: ✭ 51 (+168.42%)
Mutual labels:  events
events
πŸ“… Upcoming events, conferences, meetups related to Open Source Design
Stars: ✭ 53 (+178.95%)
Mutual labels:  events
vcenter-event-broker-appliance
The VMware Event Broker Appliance Fling enables customers to unlock the hidden potential of events in their SDDC to easily create event-driven automation.
Stars: ✭ 120 (+531.58%)
Mutual labels:  events
OpenSleigh
OpenSleigh is a Saga management library for .NET Core.
Stars: ✭ 198 (+942.11%)
Mutual labels:  events
Quizzie
Open Sourced Quiz Portal which can be used for any event / competition with a custom leaderboard.
Stars: ✭ 31 (+63.16%)
Mutual labels:  events
events
Tiny type-safe event emitter
Stars: ✭ 25 (+31.58%)
Mutual labels:  events
JavaUltimateTools
A Large Repository Of Awesome Code For Java.
Stars: ✭ 24 (+26.32%)
Mutual labels:  events
wishbone
A Python framework to build composable event pipeline servers with minimal effort.
Stars: ✭ 42 (+121.05%)
Mutual labels:  events
nativescript-wear-os
Consolidated repo for WearOS with NativeScript
Stars: ✭ 27 (+42.11%)
Mutual labels:  watches

react-scroll-activator

react-scroll-activator watches for a scroll event inside of a container or on the window. When certain user-defined rules are met, it passes an activatedState prop to a render prop component, triggering whatever behavior the developer chooses on the child.

License: MIT CircleCI

The Problem

You want an element to "stick" to the top of the window when a user scrolls in a page. Or maybe you want to hide an element as a user scrolls. Basically, you want to trigger the behavior of a component as a user scrolls.

The Solution

react-scroll-activator is a straightforward React component that watches for a scroll event inside any container or on the window. If a user scrolls, (and a series of conditions are met), the ScrollActivator component sends an activatedState prop to a render prop component, triggering the render prop component's behavior.

Table of Contents

Installation

npm install react-scroll-activator

Usage

β€’ Basic

You can either use the ScrollActivator component on the window, or on any container that scrolls.

On the window

class StickyElement extends React.Component {
  shouldComponentBeSticky = () => {
    return window.scrollY > 120
  }

  isSticky = activatedState => {
    if (activatedState === 'isActivated') {
      return { position: 'fixed' }
    } else {
      return { position: 'relative' }
    }
  }

  render () {
    return (
      <ScrollActivator onScroll={this.shouldComponentBeSticky}>
        {activatedState => (
          <div style={this.isSticky(activatedState)}>
            <h1>Hi</h1>
          </div>
        )}
      </ScrollActivator>
    }
  )
}

In a modal

Let's say the modal's classname is .any-class-name:

class StickyElement extends React.Component {
  handleScrollCallback = (e, topOffset) => {
    this.containerSelector = document.querySelector('.any-class-name')
    return (
      e.target.scrollTop >
        this.containerSelector.getBoundingClientRect().top + topOffset
    )
  }

  render () {
    return (
      <div className='any-class-name'>
        <ScrollActivator
          onScroll={this.handleScrollCallback}
          containerSelector='.any-class-name'
        >
          {activatedState => <StickyElement isSticky={activatedState} />}
        </ScrollActivator>
      </div>
    )
  }
}

In this example, ScrollActivator is wrapped around a StickyElement which is the component that will stick to the top of the container as the user scrolls. The ScrollActivator will pass activatedState to the child component, which the child component can then use to activate certain behavior. In the case of this example, the StickyElement will stick to the top of the component.

To actually make sure you are setting rules, add a handleScrollCallback function that resembles the one below to the class in which you are invoking ScrollActivator. You'll pass this to the ScrollActivator component onScroll.

FAQ

Inspiration

I built this because I needed to make a banner stick to the top of a container, but I didn't have access to the window.

Alternatives

Contributors

  • Mae Capozzi

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].