• Stars
    star
    138
  • Rank 264,508 (Top 6 %)
  • Language
    JavaScript
  • Created about 8 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

React HOC for the scrollMonitor

ScrollMonitor-React

This is a React component that provides an API to the scrollMonitor. It lets you create both watchers and scroll containers.

There is now a React hooks version.

It adds all the boolean properties from a watcher to this.props and takes all the method properties as properties.

Scrollmonitor-react is two higher order components. They're functions that you pass an original component and receive a new component that adds functionality to the original.

Basic Usage

Knowing when you're in the viewport

import React from 'react';
import { Watch } from 'scrollmonitor-react';

export default Watch(class MyComponent extends React.Component {

	render () {
		var text;
		if (this.props.isInViewport) {
			text = 'I AM in the viewport!';
		} else {
			text = 'You will never see this because it gets replaced when it enters the viewport.'
		}

		return (<span>
			{text}
			{this.props.children}
		</span>);
	}
});

Doing something when a watched child enters or exits the viewport

Provide methods with the scrollMonitor event names as props to your component.

import React from 'react';

import MyWatchedComponent from './the/example/above';

export default MyParentComponent extends React.Component {

	receiveStateChange (watcher) {
		console.log('state changed!', watcher)
	}

	render () {
		return (<MyWatchedComponent stateChange={this.receiveStateChange} />)
	}
}

Avoid starting the watcher automatically

By default the watcher is started on every enhanced component, but there can be situations where you want to start the watcher later so you can set the prop autoStart to false to avoid starting the watcher automatically.

API

this.props provided to your component

  • this.props.isInViewport - true if any part of the element is visible, false if not.
  • this.props.isFullyInViewport - true if the entire element is visible [1].
  • this.props.isAboveViewport - true if any part of the element is above the viewport.
  • this.props.isBelowViewport - true if any part of the element is below the viewport.
  • this.props.lockWatcher() - locks the watcher letting you move the element but watch the same place. See the scrollMonitor's documentation for more info.
  • this.props.unlockWatcher() - unlocks the watcher.
  • this.props.startWatcher() - starts the watcher if it's not running
  • this.props.stopWatcher() - stops the watcher if it's running

1. If the element is larger than the viewport isFullyInViewport is true when the element spans the entire viewport.

Properties you provide to the component

<MyWatchedComponent
	autoStart={true|false} // Decide if watcher should start when component is mounted. Default: true
	innerRef={(el, watcher, props) => this.foo = el} // allows you to gain access to the DOM element, the watcher, and the child's props
	stateChange={(watcher, childProps) => {}} // Called when any part of the state changes.
	visibilityChange={(watcher, childProps) => {}} // when the element partially enters or fully exits the viewport.
	enterViewport={(watcher, childProps) => {}} // when the element enters the viewport.
	fullyEnterViewport={(watcher, childProps) => {}} // when the element is completely in the viewport [1].
	exitViewport={(watcher, childProps) => {}} // when the element completely leaves the viewport.
	partiallyExitViewport={(watcher, childProps) => {}} // when the element goes from being fully in the viewport to only partially [2]
>
	<h1>Child components are fine too.</h1>
</MyWatchedComponent>

All callbacks receive two arguments: the watcher and the props of the child.

1. If the element is larger than the viewport fullyEnterViewport will be triggered when the element spans the entire viewport. 2. If the element is larger than the viewport partiallyExitViewport will be triggered when the element no longer spans the entire viewport.

Scroll Containers

The ScrollContainer HOC lets you create scrollMonitor Scroll Containers. It provides a scroll container on this.props that it must pass to its children.

import React from 'react';
import { render } from 'react-dom';
import { ScrollContainer } from 'scrollmonitor-react';

import MyWatchedContainer from 'the/example/above';

// Your container gets this.props.scrollContainer, which it must pass to the child components.
var Container = ScrollContainer(ContainerComponent extends React.Component {
	render () {
		i = 1;
		return (<div className="container-scroll">
			<MyWatchedContainer scrollContainer={this.props.scrollContainer}>{i++}</MyWatchedContainer>
			{...times a million}
		</div>);
	}
}

More Repositories

1

scrollmonitor

A simple and fast API to monitor elements as you scroll
TypeScript
3,296
star
2

scrollmonitor-parallax

A simple parallax API
JavaScript
135
star
3

redux-marionette

Because it's time to start that big migration
JavaScript
33
star
4

scrollmonitor-hooks

React Hooks for scrollmonitor.
JavaScript
27
star
5

polevault

An api to recieve gesture events from a Leap Motion device
JavaScript
26
star
6

cross-tab-middleware

Redux middleware for sending actions across open browser tabs
JavaScript
14
star
7

paperboy

A simple and lightweight event emitter mixin with advanced features.
JavaScript
13
star
8

apollo-server-service-worker

Put apollo server inside a service worker.
JavaScript
13
star
9

cross-tab-channel

A simple library for sending data across open browser tabs
JavaScript
10
star
10

dexie-hooks

Hooks for Dexie
TypeScript
9
star
11

leap-theremin

A theremin that works with the leap motion detector
JavaScript
5
star
12

imageSelector

A responsive image selector
HTML
4
star
13

fix-a-float

99.0000000000000005% of the time floating point math is perfect
JavaScript
3
star
14

bitcoin-bot

A simple bot that trades bitcoin
JavaScript
2
star
15

idaho

The 43rd State. Machine.
TypeScript
2
star
16

redux-action-wrapper

Simplifies your mapDispatchToProps significantly
JavaScript
2
star
17

entrokitty

Littering your box with files that have similar names? Use entrokitty to tell them apart.
JavaScript
2
star
18

altitude-finder

A simple web app to find your altitude while offline.
JavaScript
2
star
19

releasable

Adds retain/release to any object, letting you know when you can clean up.
JavaScript
1
star
20

ab-split

A simple A/B testing utility
JavaScript
1
star
21

prepend-selector-webpack-plugin

A webpack plugin to prepend all your CSS selectors with a string.
JavaScript
1
star
22

potterywebsite

Simple store made for pottery.
PHP
1
star
23

umpire-indicator

A simple umpire indicator app
JavaScript
1
star
24

epub-generator

A barebones epub generator that provides a full epub, or the files that make one.
TypeScript
1
star
25

unmask

A javascript tool to remove the background from an icon or logo
JavaScript
1
star
26

use-callback-ref

A friendlier, faster, less risky useCallback
TypeScript
1
star
27

mosaic

A tiled layout system
JavaScript
1
star
28

data-viz

An unfinished app for graphing data
JavaScript
1
star
29

storage.js

A simple interface to localStorage that adds expiration dates.
JavaScript
1
star
30

Elements-of-Typographic-Stylesheet

An extension for Chrome to improve the look of the web.
CSS
1
star
31

live-tongue-shape

An app to trace the shape of a tongue surface from an ultrasound image.
TypeScript
1
star