Coder Social home page Coder Social logo

react-iscroll's Introduction

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>
  )
}

function component.getIScroll()

Return iScroll instance if initialized

function component.withIScroll([waitForInit], callback)

Run callback with iScroll instance as argument if instance is initialized. You can pass true as first argument for call callback after iscroll is initialized

  onSomethingClick: function(ev) {
    ev.preventDefault()
    this.refs.iscroll.withIScroll(function(iscroll) {
      iscroll.destroy()
    })
  },

  render: function() {
    return(
      <div>
        <a class="#" onClick={this.onSomethingClick}>Do something</a>
        <ReactIScroll ref="iscroll"
                      iscroll={iScroll}
                      onRefresh={this.onRefresh}>
          <div>Long content...</div>
        </ReactIScroll>
      </div>
    )
  }

Example

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

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

To-Do

Done

  • 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)

  • Publish to npm

  • Convert source code into Babel

  • 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.

Licence

React iScroll is released under the MIT License.

react-iscroll's People

Contributors

jordancardwell avatar kmalakoff avatar omasback 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.