项目作者: exelban

项目描述 :
Swipe bindings for react
高级语言: TypeScript
项目地址: git://github.com/exelban/react-swipe-component.git
创建时间: 2016-10-09T18:39:34Z
项目社区:https://github.com/exelban/react-swipe-component

开源协议:Apache License 2.0

下载


react-swipe-component

Download Count

Demo image

Swipe bindings for react.
Demo

Install

  1. yarn add react-swipe-component

Or with npm:

  1. npm install react-swipe-component --save

Usage

Example

  1. import React from "react"
  2. import ReactDOM from "react-dom"
  3. import {Swipe, Position} from "react-swipe-component"
  4. class Demo extends React.Component<{}, {}>{
  5. render() {
  6. return <Swipe
  7. nodeName="div"
  8. className="test"
  9. onSwipeEnd={this.onSwipeEnd}
  10. onSwipedLeft={this.onSwipeLeftListener}
  11. onSwipedRight={this.onSwipeRightListener}
  12. onSwipedDown={this.onSwipeDownListener}
  13. onSwipedUp={this.onSwipeUpListener}
  14. onSwipe={this.onSwipeListener}>
  15. Demo
  16. </Swipe>
  17. }
  18. onSwipeEnd = () => {
  19. console.log("Swipe Ended")
  20. }
  21. onSwipeLeftListener = () => {
  22. console.log("Swiped left")
  23. }
  24. onSwipeRightListener = () => {
  25. console.log("Swiped right")
  26. }
  27. onSwipeUpListener = () => {
  28. console.log("Swiped Up")
  29. }
  30. onSwipeDownListener = () => {
  31. console.log("Swiped down")
  32. }
  33. onSwipeListener = (p) => {
  34. if (p.x !== 0) {
  35. console.log(`Swipe x: ${p.x}`)
  36. }
  37. if (p.y !== 0) {
  38. console.log(`Swipe y: ${p.y}`)
  39. }
  40. }
  41. }
  42. ReactDOM.render(<Demo></Demo>, document.getElementById("app"))

Props

nodeName is a string which determines the html element/node that this react component binds its touch events to then returns. The default value is ‘div’.

node is a option if you’d like to pass a node instead of nodeName(e.g. styled-components).

className is a string which determines the html element/node class.

style is a object which determines the style for element.

delta is the amount of px before we start firing events. Also affects how far onSwipedUp, onSwipedRight, onSwipedDown, and onSwipedLeft need to be before they fire events. The default value is 50.

detectMouse is allow you to turn off swipe listener for mouse event. The default value is true.

detectTouch is allow you to turn on swipe listener for touch event. The default value is false.

preventDefault is whether to prevent the browser’s touchmove event. Sometimes you would like the target to scroll natively. The default value is false.

stopPropagation prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.

onSwipingUp, onSwipingRight, onSwipingDown, onSwipingLeft, are called with the event as well as the absolute delta of where the swipe started and where it’s currently at. Return distance from starting point.

onSwipedUp, onSwipedRight, onSwipedDown, onSwipedLeft are called with the event as well as the x distance, + or -, from where the swipe started to where it ended. These only fire at the end of a touch event.

onSwipe are called when the swipe started. Return distance from starting point [x,y]. One value will be 0. If value is non 0 it means that it’s main swipe axis.

onSwipeEnd are called when the swipe ended.

onTransitionEnd event is fired when a CSS transition has completed.

Types
  1. interface Props {
  2. nodeName?: string,
  3. node?: React.ReactNode,
  4. className?: string,
  5. style?: Object,
  6. detectTouch?: boolean,
  7. detectMouse?: boolean,
  8. delta: number,
  9. preventDefault?: boolean,
  10. stopPropagation?: boolean,
  11. children?: any,
  12. onSwipe: (p: Position) => void
  13. onSwipingLeft: (x: number) => void,
  14. onSwipingRight: (x: number) => void,
  15. onSwipingUp: (y: number) => void,
  16. onSwipingDown: (y: number) => void,
  17. onSwipedLeft: () => void,
  18. onSwipedRight: () => void,
  19. onSwipedUp: () => void,
  20. onSwipedDown: () => void,
  21. onSwipeEnd: () => void,
  22. onTransitionEnd: () => void,
  23. }
  24. interface Position {
  25. x: number,
  26. y: number,
  27. }

Developing

Library

  1. git clone git@github.com:exelban/react-swipe-component.git
  2. cd react-swipe-component
  3. yarn install
  4. yarn build

Demo

If You want to test a package on demo page:

  1. cd docs
  2. yarn install
  3. yarn dev

What’s new

v3.0.0 (BREAKING CHANGES)

  1. - rewrited library in typescript
  2. - removed eslint
  3. - removed flow
  4. - update all dependencies
  5. - added stopPropagation
  6. - changed returning values structure
  7. - updated example

v2.1.0

  1. - updated some dependencies
  2. - removed unnecessary comments
  3. - fixed docs script for build

v2.0.0

  1. - updated all dependencies
  2. - added flow types
  3. - added eslint
  4. - added onSwipeEnd to example
  5. - renamed ./lib/Swipe to ./lib/index
  6. - moved to Babel 7 for compiling

v1.4.0

  1. - fixed Google Chrome preventDefault error in console
  2. - small fixed with main example
  3. - started using webpack to compile to ES5
  4. - updated dependency

License

Apache License 2.0