Miss any of our Open RFC calls?Watch the recordings here! »

@sustainhawaii/react-swipy

0.0.18 • Public • Published

fork of React Swipy (react-swipy) by goncy

Build Status Coverage Status

A simple, horizontally-swipeable React HOC

Installation

# NPM 
npm install --save sustainhawaii/react-swipy
# Yarn 
yarn add sustainhawaii/react-swipy

Differences in this fork

  • Removed rotation from swipe animation for a more standard swipe.
  • Added optional "direction" prop to restrict swiping to left or right.

Why

I needed a an HOC to swipe left, right or both on a react component with a callbacks for the swipe events.

How

See in CodeSandbox

import React, {Component} from "react";
import Swipeable from "react-swipy"
 
import Card from "./components/Card";
import Button from "./components/Button";
 
const wrapperStyles = {position: "relative", width: "250px", height: "250px"};
const actionsStyles = {
  display: "flex",
  justifyContent: "space-between",
  marginTop: 12,
};
 
class App extends Component {
  state = {
    cards: ["First", "Second", "Third"],
  };
 
  remove = () =>
    this.setState(({cards}) => ({
      cards: cards.slice(1, cards.length),
    }));
 
  render() {
    const {cards} = this.state;
 
    return (
      <div>
        <div style={wrapperStyles}>
          {cards.length > 0 ? (
            <div style={wrapperStyles}>
              <Swipeable
                buttons={({left, right}) => (
                  <div style={actionsStyles}>
                    <Button onClick={left}>Reject</Button>
                    <Button onClick={right}>Accept</Button>
                  </div>
                )}
                onAfterSwipe={this.remove}
              >
                <Card>{cards[0]}</Card>
              </Swipeable>
              {cards.length > 1 && <Card zIndex={-1}>{cards[1]}</Card>}
            </div>
          ) : (
            <Card zIndex={-2}>No more cards</Card>
          )}
        </div>
      </div>
    );
  }
}
 
export default App;

Props

limit

Number

Offset in px swiped to consider as swipe

min

Number

Offset when opacity fade should start

direction

String

Swipe direction restricted to ('left' or 'right'); defaults to 'both'

onBeforeSwipe

Function

Callback executed before swiping, it receives 3 parameters:

  • A function that, when called, executes the swipe ('left' or 'right' can be passed to force direction)
  • A function that will cancel the swipe
  • The direction of the swipe

onSwipe

Function

Callback executed right after swipe, it receives 'left' or 'right' as first parameter

onAfterSwipe

Function

Callback executed when animation ends

children

Node

Content of the card

buttons

Function

Function that returns a left and right function that will force a swipe

Install

npm i @sustainhawaii/react-swipy

DownloadsWeekly Downloads

0

Version

0.0.18

License

ISC

Unpacked Size

33.1 kB

Total Files

7

Last publish

Collaborators

  • avatar