0.4.2 • Published 5 years ago

@cantonjs/react-scroll-view v0.4.2

Weekly downloads
138
License
MIT
Repository
github
Last release
5 years ago

This project is currently in beta and APIs are subject to change before v1.0 release.

react-scroll-view WIP

CircleCI Build Status

React scroll component using Intersection Observer API in favor of scroll events.

Features

  • Support sticky for cross browsers (including Mobile Safari)
  • Support refresh control
  • Support onEndReach(), onScrollStart() and onScrollEnd() events
  • Easy to observe the intersections between ScrollView and children

Installation

$ yarn add @cantonjs/react-scroll-view intersection-observer

Intersection Observer API is required. You should include the polyfill yourself for cross-browser support

Getting Started

import "intersection-observer";
import React, { Component } from "react";
import { ScrollView } from "@cantonjs/react-scroll-view";

export default class App extends Component {
  handleEndReached = () => {
    console.log("load more");
  };

  render() {
    return (
      <ScrollView onEndReached={this.handleEndReached} style={{ height: '100vh' }}>
        <h1>React Scroll View</h1>
        <p>Awseome!</p>
      </ScrollView>
    );
  }
}

References

ScrollView Component

import { ScrollView } from "@cantonjs/react-scroll-view";

Scroll view component

Props

PropertyDescriptionType
styleThese styles will be applied to the ScrollView containerObject
classNameThe className will be applied to the ScrollView containerstring
contentContainerStyleThese styles will be applied to the ScrollView content container which wraps all of the child viewsObject
contentContainerClassNameThe className will be applied to the ScrollView content container which wraps all of the child viewsstring
onScrollFires at most once per frame during scrollingFunction
onScrollStartCalled once when the scroll startsFunction
onScrollEndCalled once when the scroll endsFunction
onEndReachedCalled once when the scroll position gets within endReachedThreshold of the rendered contentFunction
endReachedThresholdHow far from the end (in pixels) the bottom to trigger the onEndReached callbackNumber
isHorizontalWhen true, the ScrollView's children are arranged horizontally in a row instead of vertically in a column. Defaults to falseBoolean
disabledWhen true, the view cannot be scrolled. Defaults to falseBoolean
refreshControlA RefreshControl component, used to provide pull-to-refresh functionality for the ScrollView. Only works for vertical ScrollViewsElement
innerRefUse this to access the dom (DIV) refFunction

The rest of the props are exactly the same as the original DOM attributes.

Methods

MethodDescription
scrollTo(val)Scrolls to a given value immediately

ScrollObserver Component

import { ScrollObserver } from "@cantonjs/react-scroll-view";

Wrap any child component and observe it when in ScrollView.

Props

PropertyDescriptionType
onEnterFires once when the children nodes enterFunction
onLeaveFires once when the children nodes leaveFunction
innerRefUse this to access the internal refFunction
childrenChildren must be a function. Will receive an object with isIntersecting and ref propsFunction

Children function

The children prop will receive the following object shape:

PropertyDescriptionType
refMust be passed down to your component's ref in order to obtain a proper node to observeFunction
isIntersectingWill be true if the target element intersects with the ScrollViewBoolean

Example

import "intersection-observer";
import React, { Component } from "react";
import { ScrollView, ScrollObserver } from "@cantonjs/react-scroll-view";

export default class App extends Component {
  render() {
    return (
      <ScrollView style={{ height: '100vh' }}>
        <h1>React Scroll View</h1>
        <p>Awseome!</p>
        <ScrollObserver>
          {({ ref, isIntersecting }) =>
            <img
              ref={ref}
              src="/img.jpg"
              alt="lazy image"
              style={{ display: isIntersecting ? 'inline-block' : 'none' }}
            />
          }
        </ScrollObserver>
      </ScrollView>
    );
  }
}

StickySection Component

import { StickySection } from "@cantonjs/react-scroll-view";

Section component with a sticky header.

Props

PropertyDescriptionType
stickySticky header node elementNode/Function

The rest of the props are exactly the same as the original DOM attributes.

Example

import "intersection-observer";
import React, { Component } from "react";
import { ScrollView, ScrollSection } from "@cantonjs/react-scroll-view";

export default class App extends Component {
  render() {
    return (
      <ScrollView style={{ height: '100vh' }}>
        <StickySection sticky={<h1>A</h1>}>
          <ul>
            <li>Adelia Pisano</li>
            <li>Alayna Loredo</li>
          </ul>
        </StickySection>
        <StickySection sticky={<h1>B</h1>}>
          <ul>
            <li>Brant Hunsberger</li>
          </ul>
        </StickySection>
        <StickySection sticky={<h1>C</h1>}>
          <ul>
            <li>Carl Wetzler</li>
            <li>Cherry Greeno</li>
            <li>Cris Kepley</li>
          </ul>
        </StickySection>
      </ScrollView>
    );
  }
}

RefreshControl Component

import { RefreshControl } from "@cantonjs/react-scroll-view";

RefreshControl component

Props

PropertyDescriptionType
onRefreshCalled when the view starts refreshingFunction
isRefreshingWhether the view should be indicating an active refreshBoolean
colorThe refreshControl icon colorString

The rest of the props are exactly the same as the original DOM attributes.

Methods

MethodDescription
requestRefresh()Request to call onRefresh()

Example

import "intersection-observer";
import React, { Component } from "react";
import { ScrollView, RefreshControl } from "@cantonjs/react-scroll-view";

export default class App extends Component {
  state = {
    isRefreshing: false,
  };
  
  handleRefresh = () => {
    this.setState({
      isRefreshing: true,
    });
  };

  render() {
    const { isRefreshing } = this.state;
    return (
      <ScrollView
        style={{ height: '100vh' }}
        refreshControl={
          <RefreshControl
            onRefresh={this.handleRefresh}
            isRefreshing={isRefreshing}
          />
        }
      >
        <h1>React Scroll View</h1>
        <p>Awseome!</p>
      </ScrollView>
    );
  }
}

License

MIT