/react-iscroll

React component for wrapping iScroll http://iscrolljs.com/

Primary LanguageJavaScriptMIT LicenseMIT

React iScroll

React component for wrapping iScroll library.

What is iScroll?

iScroll is a high performance, small footprint, dependency free, multi-platform javascript scroller.

-- iScroll's homepage

Works on mobile and desktop, supports zooming, pagging, parallax scrolling, carousels and is incredibly small (4kb compress gzipped).

Why React compoment?

React components are great way how to compose your application. And they are great how to handle third party libraries. You can wrap complex logic around library and expose simple API, which are react users used to.

Install

npm install react-iscroll

Usage

Simple example app. Allow scrolling on long list and catch event when scrolling starts.

var React = require('react'),
    ReactIScroll = require('react-iscroll'),
    iScroll = require('iscroll');

var ExampleApp = React.createClass({
  getDefaultProps: function() {
    options: {
      mouseWheel: true,
      scrollbars: true
    },
    wrapperStyle: {
      height:'100%',
      overflow:'hidden'
    }
  },
  onScrollStart: function() {
    console.log("iScroll starts scrolling")
  },
  render: function() {
    var i = 0, len = 1000, listOfLi = [];

    for(i; i < len; i++) {
      listOfLi.push(<li key={i}>Row {i+1}</li>)
    }

    return (
      <div style={this.props.wrapperStyle}>
        <h1>Example of scrollable list</h1>
        <ReactIScroll iscroll={iScroll}
                      options={this.props.options}
                      onScrollStart={this.onScrollStart}>
          <ul>
            {listOfLi}
          </ul>
        </ReactIScroll>
      </div>
    )
  }
})

Configuration (API)

Basic configuration. Just component with iScroll library. You can pick build which you want.

var iScroll = require('iscroll/build/iscroll-lite')

<ReactIScroll iscroll={iScroll}>
  <div>Long content...</div>
</ReactIScroll>

You can customize iScroll options with options property. Supports all from iScroll manual

var iScroll = require('iscroll/build/iscroll-probe')
var options = {
  mouseWheel: true,
  scrollbars: true,
  freeScroll: true,
  invertWheelDirection: true,
  momentum: false,
  indicators: {...}
}

<ReactIScroll iscroll={iScroll}
              options={options}>
  <div>Long content...</div>
</ReactIScroll>

Component supports all iScroll events. All of them passed iScroll instance to callback.

var iScroll = require('iscroll/build/iscroll-probe')

<ReactIScroll iscroll={iScroll}
              onBeforeScrollStart={this.onBeforeScrollStart}
              onScrollCancel={this.onScrollCancel}
              onScrollStart={this.onScrollStart}
              onScroll={this.onScroll}
              onScrollEnd={this.onScrollEnd}
              onFlick={this.onFlick}
              onZoomStart={this.onZoomStart}
              onZoomEnd={this.onZoomEnd}>
  <div>Long content...</div>
</ReactIScroll>

Plus there is one special event 'onRefresh' which is triggered when iScroll is refreshed. You can then get state of iScroll like iscroll.hasVerticalScroll, iscroll.x or iscroll.scale.

Watch out when updating state by value from iScroll. Always update state only when value changed to prevent circular updating (stack level too deep)

var iScroll = require('iscroll/build/iscroll-lite')

onRefresh: function(iScrollInstance) {
  var yScroll = iScrollInstance.y;

  console.log("vertical position:" + yScroll)

  if(this.state.y != yScroll) {
    this.setState({y: yScroll})
  }
},
render: function() {
  return (
    <ReactIScroll iscroll={iScroll}
                  onRefresh={this.onRefresh}>
      <div>Long content...</div>
    </ReactIScroll>
  )
}

Example

There is example application. You can run it with this commands:

npm install
npm run example
open http://localhost:8080/

To-Do

  • Make this README.md :)
  • Trigger onRefresh event when iscroll is internally refreshed (e.g. on window resize)
  • Do not require('iscroll') by itself. Instead pass it in props (there is few different versions of iscroll and you want to pick correct one for you)
  • Add tests
  • Think about shouldComponentUpdate. Now it is always true because this.props.children are new object everytime and can't be compared via == or ===. Maybe there is some way how to cheaply compare them.
  • Standalone build
  • Publish to npm

Licence

Reditor is released under the MIT License.