GithubHelp home page GithubHelp logo

roxwhale / react-native-snap-carousel Goto Github PK

View Code? Open in Web Editor NEW

This project forked from meliorence/react-native-snap-carousel

0.0 3.0 0.0 248 KB

Swiper component for React Native with previews, snapping effect and RTL support. Compatible with Android & iOS.

JavaScript 79.53% Python 4.74% Java 3.61% Objective-C 12.12%

react-native-snap-carousel's Introduction

react-native-snap-carousel

Swiper component for React Native with previews, snapping effect and RTL support. Compatible with Android & iOS. Pull requests are very welcome!

Table of contents

  1. Showcase
  2. Breaking change
  3. Usage
  4. Props
  5. Methods
  6. Properties
  7. Example
  8. Tips and tricks
  9. RTL support
  10. TODO
  11. Credits

Showcase

You can try these examples live in Archriss' showcase app on Android and iOS.

react-native-snap-carousel react-native-snap-carousel react-native-snap-carousel

Since it has been asked multiple times, please note that we do not plan on Open-Sourcing the code of our showcase app. Still, we've put together an example for you to play with, and you can find some insight about our map implementation in this comment.

App currently uses version 1.4.0 of the plugin. Especially, this means that you should expect slider's layout to break with RTL devices (see #38) since support was added in version 2.1.0.

Breaking change

Since version 2.0.0, items are now direct children of the component. As a result, props items and renderItem have been removed.

Usage

$ npm install --save react-native-snap-carousel
import Carousel from 'react-native-snap-carousel';

    // Example with different children
    render () {
        <Carousel
          ref={(carousel) => { this._carousel = carousel; }}
          sliderWidth={sliderWidth}
          itemWidth={itemWidth}
        >
            <View style={styles.slide1} />
            <Text style={styles.slide2} />
            <Image style={styles.slide3} />
        </Carousel>
    }

    // Example of appending the same component multiple times while looping through an array of data
    render () {
        const slides = this.state.entries.map((entry, index) => {
            return (
                <View key={`entry-${index}`} style={styles.slide}>
                    <Text style={styles.title}>{ entry.title }</Text>
                </View>
            );
        });

        <Carousel
          ref={(carousel) => { this._carousel = carousel; }}
          sliderWidth={sliderWidth}
          itemWidth={itemWidth}
        >
            { slides }
        </Carousel>
    }

Props

Required

Prop Description Type Default
itemWidth Width in pixels of your slides, must be the same for all of them Number Required
sliderWidth Width in pixels of your slider Number Required

Behavior

Prop Description Type Default
activeSlideOffset From slider's center, minimum slide distance to be scrolled before being set to active Number 25
enableMomentum See momentum Boolean false
enableSnap If enabled, releasing the touch will scroll to the center of the nearest/active item Number true
firstItem Index of the first item to display Number 0
shouldOptimizeUpdates whether to implement a shouldComponentUpdate strategy to minimize updates Boolean true
snapOnAndroid Snapping on android is kinda choppy, especially when swiping quickly so you can disable it Boolean true
swipeThreshold Delta x when swiping to trigger the snap Number 20

Autoplay

Prop Description Type Default
autoplay Trigger autoplay on mount Boolean false
autoplayDelay Delay before enabling autoplay on startup & after releasing the touch Number 5000
autoplayInterval Delay in ms until navigating to the next item Number 3000

Style and animation

Prop Description Type Default
animationFunc Animated animation to use. Provide the name of the method String timing
animationOptions Animation options to be merged with the default ones. Can be used w/ animationFunc Object { easing: Easing.elastic(1) }
carouselHorizontalPadding Override container's inner padding (needed for slides's centering). Warning: be aware that overriding the default value can mess with carousel's behavior. Number (sliderWidth - itemWidth) / 2
containerCustomStyle Optional styles for Scrollview's global wrapper ScrollView Style Object {}
contentContainerCustomStyle Optional styles for Scrollview's items container ScrollView Style Object {}
inactiveSlideOpacity Value of the opacity effect applied to inactive slides Number 1
inactiveSlideScale Value of the 'scale' transform applied to inactive slides Number 0.9
slideStyle Optional style for each item's container (the one whose scale and opacity are animated) Animated View Style Object {}

Callbacks

Prop Description Type Default
onSnapToItem(slideIndex) Callback fired when navigating to an item Function undefined

ScrollView

In addition to these props, you can use any prop from the ScrollView component.

Here are a few useful ones:removeClippedSubviews, showsHorizontalScrollIndicator, overScrollMode (android), bounces (ios), decelerationRate (ios), scrollEventThrottle (ios)

Methods

Reference to the component

In order to use the following methods, you need to create a reference to the carousel's instance. There are two ways of doing it.

ref as a callback attribute (recommended)

<Carousel
  // other props
  ref={(carousel) => { this._carousel = carousel; }}
/>

// methods can then be called this way
onPress={() => { this._carousel.snapToNext(); }}

ref as a string attribute (legacy)

<Carousel
  // other props
  ref={'carousel'}
/>

// methods can then be called this way
onPress={() => { this.refs.carousel.snapToNext(); }}

Available methods

  • startAutoplay (instantly = false) Start the autoplay manually
  • stopAutoplay () Stop the autoplay manually
  • snapToItem (index, animated = true) Snap to an item manually
  • snapToNext (animated = true) Snap to next item manually
  • snapToPrev (animated = true) Snap to previous item manually

Properties

You need a reference to the carousel's instance (see above if needed).

  • currentIndex Current active item (int, starts at 0)

Example

You can find the following example in the /example folder.

react-native-snap-carousel

Tips and tricks

Momentum

Since 1.5.0, the snapping effect can now be based on momentum instead of when you're releasing your finger. It means that the component will wait until the ScrollView isn't moving anymore to snap. By default, the inertia isn't too high on Android. However, we had to tweak the default iOS value a bit to make sure the snapping isn't delayed for too long. You can adjust this value to your needs thanks to this prop.

As a rule of thumb, we recommend setting enableMomentum to false (default) and decelerationRate to 'fast' when you are displaying only one main slide (as in the showcase above), and to use true and 0.9 otherwise. This should help providing a better snap feeling.

Margin between slides

If you need some extra horizontal margin between slides (besides the one resulting from the scale effect), you should add it as paddingHorizontal on the slide container. Make sure to take this into account when calculating item's width.

const sliderWidth = Dimensions.get('window').width * 0.75;
const slideWidth = 250;
const horizontalMargin = 20;
const itemWidth = slideWidth + horizontalMargin * 2;

const styles = Stylesheet.create({
    slide: {
        width: itemWidth
        // other styles for your item's container
    }
};

<Carousel
  sliderWidth={sliderWidth}
  itemWidth={itemWidth}
>
    ...
</Carousel>

RTL support

Experimental feature

Since version 2.1.0, the plugin is compatible with RTL layouts. Our implementation relies on miscellaneous hacks that work around a React Native bug with horizontal ScrollView.

As such, this feature should be considered experimental since it might break with newer versions of React Native.

Known issue

There is one kown issue with RTL layouts: during init, the last slide will shortly be seen. You can work around this by delaying slider's visibility with a small timer (FYI, version 0.43.0 of React Native introduced a display style prop that could either be set to flex or none).

TODO

  • Implement 'loop' mode
  • Implement 'preload' mode
  • Add vertical implementation
  • Handle changing props on-the-fly
  • Handle device orientation event
  • Handle autoplay properly when updating children's length
  • Add RTL support
  • Improve momemtum handling
  • Improve snap on Android
  • Handle passing 1 item only
  • Fix centering

Credits

Written by Maxime Bertonnier and Benoît Delmaire at Archriss.

react-native-snap-carousel's People

Contributors

bd-arc avatar chitezh avatar exilz avatar jonarod avatar rgabs avatar superical avatar

Watchers

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