1.1.23 • Published 6 years ago

react-native-swipeable-cards v1.1.23

Weekly downloads
27
License
MIT
Repository
github
Last release
6 years ago

Swipe Cards for React Native

Maintained Version of @meteor-factory's package.

React Native Swipe Cards

Quick Start

  1. npm install --save react-native-swipeable-cards
  2. Create a module e.g. SwipeCards.js
  3. Import it import SwipeCards from 'react-native-swipeable-cards'
  4. Render it <SwipeCards style={{flex: 1}} />
// SwipeCards.js
'use strict';

import React, { Component } from 'react';
import {StyleSheet, Text, View, Image} from 'react-native';

import SwipeCards from 'react-native-swipeable-cards';

class Card extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <View style={[styles.card, {backgroundColor: this.props.backgroundColor}]}>
        <Text>{this.props.text}</Text>
      </View>
    )
  }
}

class NoMoreCards extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <View>
        <Text style={styles.noMoreCardsText}>No more cards</Text>
      </View>
    )
  }
}

export default class extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      cards: [
        {text: 'Tomato', backgroundColor: 'red'},
        {text: 'Aubergine', backgroundColor: 'purple'},
        {text: 'Courgette', backgroundColor: 'green'},
        {text: 'Blueberry', backgroundColor: 'blue'},
        {text: 'Umm...', backgroundColor: 'cyan'},
        {text: 'orange', backgroundColor: 'orange'},
      ]
    };
  }

  onSwipeRight (card) {
    console.log(`Yup for ${card.text}`)
  }
  onSwipeLeft (card) {
    console.log(`Nope for ${card.text}`)
  }
  onSwipeUp (card) {
    console.log(`Maybe for ${card.text}`)
  }
  render() {
    // If you want a stack of cards instead of one-per-one view, activate stack mode
    // stack={true}
    return (
      <SwipeCards
        cards={this.state.cards}
        renderCard={(cardData) => <Card {...cardData} />}
        renderNoMoreCards={() => <NoMoreCards />}

        onSwipeRight={this.handleYup}
        onSwipeLeft={this.handleNope}
        onSwipeUp={this.handleMaybe}
        hasMaybeAction
      />
    )
  }
}

const styles = StyleSheet.create({
  card: {
    justifyContent: 'center',
    alignItems: 'center',
    width: 300,
    height: 300,
  },
  noMoreCardsText: {
    fontSize: 22,
  }
})

More complex example

'use strict';

import React, { Component } from 'react';
import {StyleSheet, Text, View, Image} from 'react-native';

import SwipeCards from 'react-native-swipeable-cards';

class Card extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <View style={styles.card}>
        <Image style={styles.thumbnail} source={{uri: this.props.image}} />
        <Text style={styles.text}>This is card {this.props.name}</Text>
        <Button type='outline' title='Nah' 
        style={SwipeStyles.rejectButton} 
        onPress={() => {
            this.props.swiper._forceNextCard()
        }}
      />
      </View>
    )
  }
}

class NoMoreCards extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <View style={styles.noMoreCards}>
        <Text>No more cards</Text>
      </View>
    )
  }
}

const cards = [
  {name: '1', image: 'https://media.giphy.com/media/GfXFVHUzjlbOg/giphy.gif'},
  {name: '2', image: 'https://media.giphy.com/media/irTuv1L1T34TC/giphy.gif'},
  {name: '3', image: 'https://media.giphy.com/media/LkLL0HJerdXMI/giphy.gif'},
  {name: '4', image: 'https://media.giphy.com/media/fFBmUMzFL5zRS/giphy.gif'},
  {name: '5', image: 'https://media.giphy.com/media/oDLDbBgf0dkis/giphy.gif'},
  {name: '6', image: 'https://media.giphy.com/media/7r4g8V2UkBUcw/giphy.gif'},
  {name: '7', image: 'https://media.giphy.com/media/K6Q7ZCdLy8pCE/giphy.gif'},
  {name: '8', image: 'https://media.giphy.com/media/hEwST9KM0UGti/giphy.gif'},
  {name: '9', image: 'https://media.giphy.com/media/3oEduJbDtIuA2VrtS0/giphy.gif'},
]

const cards2 = [
  {name: '10', image: 'https://media.giphy.com/media/12b3E4U9aSndxC/giphy.gif'},
  {name: '11', image: 'https://media4.giphy.com/media/6csVEPEmHWhWg/200.gif'},
  {name: '12', image: 'https://media4.giphy.com/media/AA69fOAMCPa4o/200.gif'},
  {name: '13', image: 'https://media.giphy.com/media/OVHFny0I7njuU/giphy.gif'},
]

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      cards: cards,
      outOfCards: false
    }
  }

  cardSwipedRight (card) {
    console.log("LIKED!")
  }

  cardSwipedLeft (card) {
    console.log("DISLIKED!")
  }

  cardRemoved (index) {
    console.log(`The index is ${index}`);

    let CARD_REFRESH_LIMIT = 3

    if (this.state.cards.length - index <= CARD_REFRESH_LIMIT + 1) {
      console.log(`There are only ${this.state.cards.length - index - 1} cards left.`);

      if (!this.state.outOfCards) {
        console.log(`Adding ${cards2.length} more cards`)

        this.setState({
          cards: this.state.cards.concat(cards2),
          outOfCards: true
        })
      }

    }

  }

  render() {
    return (
      <SwipeCards
        cards={this.state.cards}
        ref = {(swiper) => this.swiper = swiper}
        loop={false}
        renderCard={(cardData) => <Card swiper={this.swiper} {...cardData} />}
        renderNoMoreCards={() => <NoMoreCards />}
        showRightOverlay={true}
        showLeftOverlay={true}
        stackDepth={3}
        stack={true}
        keyExtractor={(card) => {
          return card.name
        }}
        onSwipeRight={this.cardSwipedRight}
        onSwipeLeft={this.cardSwipedLeft}
        cardRemoved={(card) => this.cardRemoved(card)}
      />
    )
  }
}

const styles = StyleSheet.create({
  card: {
    alignItems: 'center',
    borderRadius: 5,
    overflow: 'hidden',
    borderColor: 'grey',
    backgroundColor: 'white',
    borderWidth: 1,
    elevation: 1,
  },
  thumbnail: {
    width: 300,
    height: 300,
  },
  text: {
    fontSize: 20,
    paddingTop: 10,
    paddingBottom: 10
  },
  noMoreCards: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  }
})

Props

Props nameTypeDescriptionDefault
cards*ArrayData that will be provided as props for the cards
renderCard*FunctionRenders the card with the current data
loopBooleanIf true, start again when run out of cardsfalse
onLoopFunctionCalled when card list returns to the beginning
renderNoMoreCardsFunctionRenders what is shown after swiped last card
showRightOverlayBooleanShows the 'Right Overlay' componenttrue
showLeftOverlayBooleanShows the 'Left Overlay'true
showUpOverlayBooleanShows the 'Up Overlay'true
swipeUpBooleanIncludes the possibility to swipe up and its componentsfalse
renderRightOverlayFunctionRenders the Right Overlay
renderLeftOverlayFunctionRenders Left Overlay
renderUpOverlayFunctionRenders Up Overlay
onSwipeRightFunctionCalled when card is 'passed' with that card's data
onSwipeLeftFunctionCalled when card is 'rejected' with that card's data
containerStylestyleOverride default style
overlayRightWrapperstyleOverride default style
overlayRightTextStylestyleOverride default style
overlayLeftWrapperstyleOverride default style
overlayLeftTextStylestyleOverride default style
overlayUpWrapperstyleOverride default style
overlayUpTextStylestyleOverride default style
overlayRightelementReact component to render on a Yes vote
overlayRightTextstringText to render on Yes voteLike
overlayLeftelementReact component to render on a No vote
overlayLeftTextstringText to render on No voteNope
overlayUpelementReact component to render on a Maybe vote
overlayUpTextstringText to render on Maybe voteMaybe
smoothTransitionBooleanDisables a slow transition fading the current card outfalse
cardKeyStringReact key to be used to for each card
dragYBooleanAllows dragging cards verticallytrue
stackBooleanEnables the stack modefalse
stackDepthNumberNumber of Cards for Stack to Container5
stackOffsetXNumberHorizontal offset between cards in stack25
stackOffsetYNumberVertical offset between cards in stack0
cardRemovedFunctionA callback passing the card reference that just got removed
onClickHandlerFunctionA callback clicking the cardalert('tap')
keyExtractorFunctionCallback to set Key prop on cardkey = index
rotationBooleanDisable card rotation during swipeTrue

*required

Todo (PRs welcome!)

  • Show next card underneath current card
  • Add more args to cardRemoved?
  • Update ReadMe
1.1.23

6 years ago

1.1.22

6 years ago

1.1.21

6 years ago

1.1.2

6 years ago

1.1.0

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago