Giter Site home page Giter Site logo

maecapozzi / react-scroll-activator Goto Github PK

View Code? Open in Web Editor NEW
19.0 2.0 2.0 2 MB

A React Component that watches for a scroll event and triggers behavior

License: MIT License

HTML 1.58% JavaScript 98.42%
react user-scrolls scrolls stick watches trigger events

react-scroll-activator's Introduction

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

react-scroll-activator's People

Contributors

maecapozzi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.