JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 515
  • Score
    100M100P100Q94018F
  • License MIT

simple react native carousel with sensible defaults

Package Exports

  • react-native-sideswipe

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-native-sideswipe) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Sideswipe

A simple, cross-platform React Native swipeable carousel with sensible defaults

demo

Because I found all other options too heavy or not polished enough.

What Makes Your Solution So Special?

Nothing. It's just a tiny simple carousel with a pretty flexible API. If you need more check out another solution, if you need less you might not need a carousel because this whole thing is ~150 lines. 😎


API

Carousel component used to render carousel items via renderItem prop.

type CarouselProps = {
  // data for FlatList
  data: Array<*>,

  // render item method, similar to FlatList with some enhancments
  renderItem: CarouselRenderProps =>
    | Array<React$Element<*> | boolean>
    | React$Element<*>
    | null,

  // active index of the carousel
  index?: number,

  // width of each child
  itemWidth?: number,

  // should we capture touch event
  shouldCapture?: GestureState => boolean,

  // fired when the active index for the carousel changes
  onIndexChange?: number => void,

  // style for the FlatList element
  style?: Styles,

  // offset from start/end edges of carousel
  contentOffset?: number,
}
type CarouselRenderProps = {
  // index of item in data collection
  itemIndex: number,

  // active index of the carousel
  currentIndex: number,

  // total count of items in data collection
  itemCount: number,

  // item passed from FlatList
  item: *,
}

<AnimatedCarouselItem />

Helper component used to create an animated value for carousel items. Passes an Animated.Value via render prop. Children can use this animated value to make visual adjustments when item becomes the active item.

Animated.Value is 0 when not active, 1 when active.

type AnimatedCarouselItemProps = {
  // index of item in data collection
  itemIndex: number,

  // active index of the carousel
  currentIndex: number,

  // renders a carousel item, passing an animated value
  render: Animated.Value =>
    | Array<React$Element<*> | boolean>
    | React$Element<*>
    | null,

  // duration to use for animations (150)
  duration?: number,

  // Easing function to use for animations (Easing.linear)
  easing?: Function,

  // Use native drive for animations
  useNativeDriver?: boolean,
}

Usage:

yarn add react-native-sideswipe
import { Dimensions } from 'react-native';
import {
  Carousel,
  AnimatedCarouselItem,
} from 'react-native-sideswipe';

import CustomComponent from '...'
import data from '...'

export default class SweetCarousel extends Component {
  state = {
    currentIndex: 0,
  };

  render = () => {
    // center items on screen
    const { width } = Dimensions.get('window);
    const contentOffset = (width - CustomComponent.WIDTH) / 2;

    return (
      <Carousel
        index={this.state.currentIndex}
        itemWidth={CustomComponent.WIDTH}
        style={{ width }}
        data={data}
        contentOffset={contentOffset}
        onIndexChange={index =>
          this.setState(() => ({ currentIndex: index }))
        }
        renderItem={({ itemIndex, currentIndex, item }) => (
          <AnimatedCarouselItem
            itemIndex={itemIndex}
            currentIndex={currentIndex}
            render={animatedValue => (
              <CustomComponent
                {...item}
                animatedValue={animatedValue}
              />
            )}
          />
        )}
      />
    );
  };
}

Contributors

Thanks goes to these wonderful people (emoji key):


Kurtis Kemple

💻 📖

This project follows the all-contributors specification. Contributions of any kind welcome!


License

MIT