1.4.7 • Published 6 years ago

rn-deck-swiper v1.4.7

Weekly downloads
3
License
ISC
Repository
github
Last release
6 years ago

react-native-deck-swiper

contributions welcome npm version

Installation

npm install react-native-deck-swiper --save

Overview

  • Rotation animation
  • Opacity animation
  • Zoom animation
  • Overlay labels
  • Show next card while swiping
  • Swipe event callbacks
  • Trigger swipe animations programmatically
  • Jump to a card index
  • Swipe to the previous card
  • Swipe back to the previous card with a custom animation
  • Underlaying cards offset

Preview

App preview App preview2

Props

Card props

Propstypedescriptionrequireddefault
cardsarrayarray of data for the cards to be renderedrequired
renderCardfunc(cardData)function to render the card based on the datarequired
cardIndexnumbercardIndex to start with0
infiniteboolkeep swiping indefinitelyfalse
horizontalSwipeboolenable/disable horizontal swipingtrue
verticalSwipeboolenable/disable vertical swipingtrue
showSecondCardboolenable/disable second card while swipingtrue

Event callbacks

Propstypedescriptiondefault
onSwipedAllfuncfunction to be called when all cards have been swiped() => {}
onSwipedfuncfunction to be called when a card is swiped. it receives the swiped card index(cardIndex) => {}
onSwipedLeftfuncfunction to be called when a card is swiped left. it receives the swiped card index(cardIndex) => {}
onSwipedRightfuncfunction to be called when a card is swiped right. it receives the swiped card index(cardIndex) => {}
onSwipedTopfuncfunction to be called when a card is swiped top. it receives the swiped card index(cardIndex) => {}
onSwipedBottomfuncfunction to be called when a card is swiped bottom. it receives the swiped card index(cardIndex) => {}
onTapCardfuncfunction to be called when tapping a card. it receives the tapped card index(cardIndex) => {}
onTapCardDeadZonenumbermaximum amount of movement before a tap is no longer recognized as a tap5

Swipe animation props

Propstypedescriptiondefault
verticalThresholdnumbervertical swipe thresholdheight / 5
horizontalThresholdnumberhorizontal swipe thresholdwidth / 4
swipeAnimationDurationnumberduration of the swipe animation350
disableBottomSwipebooldisable bottom swipefalse
disableLeftSwipebooldisable left swipefalse
disableRightSwipebooldisable right swipefalse
disableTopSwipebooldisable top swipefalse

Zoom animation props

Propstypedescriptiondefault
secondCardZoomnumbersecond card zoom0.97
zoomAnimationDurationnumberduration of the zoom animation100
zoomFrictionnumberzoom spring animation friction7

Rotation animation props

Propstypedescriptiondefault
inputRotationRangearrayx values range for the rotation output-width / 2, 0, width / 2
outputRotationRangearrayrotation values for the x values in inputRotationRange"-10deg", "0deg", "10deg"

Opacity animation props

Propstypedescriptiondefault
animateCardOpacityboolanimate card opacityfalse
inputCardOpacityRangeXarraypan x card opacity input range-width / 2, -width / 3, 0, width / 3, width / 2
outputCardOpacityRangeXarrayopacity values for the values in inputCardOpacityRangeX0.8, 1, 1, 1, 0.8
inputCardOpacityRangeYarraypan y card opacity input range-height / 2, -height / 3, 0, height / 3, height / 2
outputCardOpacityRangeYarrayopacity values for the values in inputCardOpacityRangeY0.8, 1, 1, 1, 0.8
animateOverlayLabelsOpacityboolanimate card overlay labels opacityfalse
inputOverlayLabelsOpacityRangeXarraypan x overlay labels opacity input range-width / 3, -width / 4, 0, width / 4, width / 3
outputOverlayLabelsOpacityRangeXarrayopacity values for the values in inputOverlayLabelsOpacityRangeX1, 0, 0, 0, 1
inputOverlayLabelsOpacityRangeYarraypan x overlay labels opacity input range-height / 4, -height / 5, 0, height / 5, height / 4
outputOverlayLabelsOpacityRangeYarrayopacity values for the values in inputOverlayLabelsOpacityRangeY1, 0, 0, 0, 1
overlayOpacityVerticalThresholdnumbervertical threshold for overlay labelheight / 5
overlayOpacityHorizontalThresholdnumberhorizontal threshold for overlay labelwidth / 4

2 steps of inputOverlayLabelsOpacityRangeX and inputOverlayLabelsOpacityRangeY should match horizontalThreshold and verticalThreshold, respectively.

Swipe overlay labels

Propstypedescriptiondefault
overlayLabelsobjectswipe labels title and stylenull, see below for format
overlayLabelStyleobjectswipe labels stylenull, see below for format
overlayLabelWrapperStyleobjectoverlay label wrapper stylesee below for default

overlayLabelStyle

{
  fontSize: 45,
  fontWeight: 'bold',
  borderRadius: 10,
  padding: 10,
  overflow: 'hidden'
}

overlayLabelWrapperStyle default props:

{
  position: 'absolute',
  backgroundColor: 'transparent',
  zIndex: 2,
  flex: 1,
  width: '100%',
  height: '100%'
} 

overlayLabels default props :

{
  bottom: {
	element: <Text>BLEAH</Text> /* Optional */
	title: 'BLEAH',
    style: {
      label: {
        backgroundColor: 'black',
        borderColor: 'black',
        color: 'white',
        borderWidth: 1
      },
      wrapper: {
        flexDirection: 'column',
        alignItems: 'center',
        justifyContent: 'center'
      }
    }
  },
  left: {
	element: <Text>NOPE</Text> /* Optional */
	title: 'NOPE',
    style: {
      label: {
        backgroundColor: 'black',
        borderColor: 'black',
        color: 'white',
        borderWidth: 1
      },
      wrapper: {
        flexDirection: 'column',
        alignItems: 'flex-end',
        justifyContent: 'flex-start',
        marginTop: 30,
        marginLeft: -30
      }
    }
  },
  right: {
	element: <Text>LIKE</Text> /* Optional */
	title: 'LIKE',
    style: {
      label: {
        backgroundColor: 'black',
        borderColor: 'black',
        color: 'white',
        borderWidth: 1
      },
      wrapper: {
        flexDirection: 'column',
        alignItems: 'flex-start',
        justifyContent: 'flex-start',
        marginTop: 30,
        marginLeft: 30
      }
    }
  },
  top: {
	element: <Text>SUPER</Text> /* Optional */
	title: 'SUPER LIKE',
    style: {
      label: {
        backgroundColor: 'black',
        borderColor: 'black',
        color: 'white',
        borderWidth: 1
      },
      wrapper: {
        flexDirection: 'column',
        alignItems: 'center',
        justifyContent: 'center'
      }
    }
  }
}

Swipe back to previous card props

Make sure you set showSecondCard={false} for smoother and proper transitions while going back to previous card.

Propstypedescriptiondefault
goBackToPreviousCardOnSwipeLeftboolprevious card is rendered on left swipefalse
goBackToPreviousCardOnSwipeRightboolprevious card is rendered on right swipefalse
goBackToPreviousCardOnSwipeTopboolprevious card is rendered on top swipefalse
goBackToPreviousCardOnSwipeBottomboolprevious card is rendered on bottom swipefalse

Style props

Propstypedescriptiondefault
backgroundColorstringbackground color for the view containing the cards'#4FD0E9'
marginTopnumbermarginTop for the swiper container0
marginBottomnumbermarginBottom for the swiper container0
cardVerticalMarginnumbercard vertical margin60
cardHorizontalMarginnumbercard horizontal margin20
childrenOnTopboolrender children on top or notfalse
cardStylenodeoverride swipable card style{}

Methods

To trigger imperative animations, you can use a reference to the Swiper component.

Propsargumentsdescription
swipeLeftmustDecrementCardIndex = falseswipe left to the next card
swipeRightmustDecrementCardIndex = falseswipe right to the next card
swipeTopmustDecrementCardIndex = falseswipe top to the next card
swipeBottommustDecrementCardIndex = falseswipe bottom to the next card
jumpToCardIndexcardIndexset the current card index

Usage example

render () {
    <View style={styles.container}>
        <Swiper
            cards={['DO', 'MORE', 'OF', 'WHAT', 'MAKES', 'YOU', 'HAPPY']}
            renderCard={(card) => {
                return (
                    <View style={styles.card}>
                        <Text style={styles.text}>{card}</Text>
                    </View>
                )
            }}
            onSwiped={(cardIndex) => {console.log(cardIndex)}}
            onSwipedAll={() => {console.log('onSwipedAll')}}
            cardIndex={0}
            backgroundColor={'#4FD0E9'}>
            <Button
                onPress={() => {console.log('oulala')}}
                title="Press me">
                You can press me
            </Button>
        </Swiper>
    </View>
}

Demo inside the Exemples Folder

Stylesheet example

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#F5FCFF'
  },
  card: {
    flex: 1,
    borderRadius: 4,
    borderWidth: 2,
    borderColor: '#E8E8E8',
    justifyContent: 'center',
    backgroundColor: 'white',
  },
  text: {
    textAlign: 'center',
    fontSize: 50,
    backgroundColor: 'transparent'
  }
})

Todo(contributions are welcome)

Underlaying card offset to achieve a stack effect