Simple swiper / slider. Works both on React-Native and React-Native-Web.
npm install react-native-web-swiperSimple swiper / slider. Works both on React-Native and React-Native-Web.
Hybrid Snack: https://snack.expo.io/@oxyii/react-native-web-swiper
``bash`
$ npm i react-native-web-swiper --save
`jsx
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Swiper from 'react-native-web-swiper';
const styles = StyleSheet.create({
container: {
flex: 1,
},
slideContainer: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
slide1: {
backgroundColor: 'rgba(20,20,200,0.3)',
},
slide2: {
backgroundColor: 'rgba(20,200,20,0.3)',
},
slide3: {
backgroundColor: 'rgba(200,20,20,0.3)',
},
});
export default class Screen extends React.Component {
render() {
return (
);
}
}
`
`jsx`
from={1} {/ initial slide is second /}
loop {/ back to first slide after last /}
timeout={2} {/ autoplay duration (2 secs) /}
springConfig={{ speed: 11 }} {/ RN Animated.spring config /}
minDistanceForAction={0.15} {/ Swipe less that 15% keep active slide /}
positionFixed {/ Fix mobile safari vertical bounces /}
controlsProps={{
DotComponent: ({ index, activeIndex, isActive, onPress }) =>
}}
>
{/ ... /}
The slide automatically gets props.isActive, props.activeIndex and props.index.
`jsx
import React from 'react';
import { Text, View } from 'react-native';
import Swiper from 'react-native-web-swiper';
type Props = {
index?: number,
activeIndex?: number,
}
export const SomeSlide = (props: Props) => (
)
export default () => (
)
`
This is possible because Swiper used cloneElement and inject internally the activeIndex and index props to each slide. This also means that all slides will re-render on swipe, since the activeIndex prop value changes on swipe.
---
| Prop | Default | Type | Description |
| :------------------- |:------------:| :--------------------:| :-----------|
| vertical | false | boolean | Swiper vertical layout |0
| from | | number | Initial slide index |false
| loop | | boolean | Set to true to enable continuous loop mode |0
| timeout | | number | Delay between auto play transitions (in second). Set negative value for reverse autoplay :satisfied:. Autoplay disabled by default |() => true
| gesturesEnabled | | function | Function that returns boolean value. Must return false to disable swiping mechanism. Does not disable Prev / Next buttons |Animated.spring
| springConfig | | | Tune spring animation on autoplay, touch release or slides changes via buttons |5
| minDistanceToCapture | | number | Initiate animation after swipe this distance. It fix gesture collisions inside ScrollView |0.2
| minDistanceForAction | | number | Minimal part of swiper width (or height for vertical) must be swiped for changing index. Otherwise animation restore current slide. Default value 0.2 means that 20% must be swiped for change index |false
| positionFixed | | boolean | Swiper inner container position fixed instead relative. Fix mobile safari vertical bounce |ViewPropTypes.style
| containerStyle | | | Outer (root) container style |ViewPropTypes.style
| innerContainerStyle | | | Inner container style |ViewPropTypes.style
| swipeAreaStyle | | | Swipe area style |ViewPropTypes.style
| slideWrapperStyle | | | Each slide wrapper style |true
| controlsEnabled | | boolean | Dots and control buttons visible and enabled |React.Component
| Controls | | | Custom controls component |function
| onAnimationStart | | | Any swiper animation start |function
| onAnimationEnd | | | Any swiper animation end |function
| onIndexChanged | | | Called when active index changed |object
| controlsProps | | | see below |
Over the swiper we need to create a controls layer. But this layer will block the possibility of swiper layer control.
We created 9 controls placeholders to solve this problem:
top-left, top, top-right, left, center, right, bottom-left, bottom and bottom-right.
You can adjust controls position by placing into relevant placeholder:
`jsx`
controlsProps={{
prevTitle: 'prev button title',
nextTitle: 'next button title',
dotsTouchable: true, {/ touch over dot will make swiper move to rel slide /}
dotsPos: 'top',
prevPos: false, {/ hide prev button /}
nextPos: 'top-right',
cellsStyle: {
'top': { padding: 5, backgroundColor: 'rgba(0, 0, 0, .3)' },
'top-left': { / any custom placeholder style / },
},
cellsContent: {
'bottom-right':
}
}}
/>
| Prop | Default | Type | Description |
| :------------------- |:------------:| :-----------------------:| :-----------|
| cellsStyle | | object | Controls corners placeholders styles. Allowed keys is: top-left, top, top-right, left, center, right, bottom-left, bottom and bottom-right, allowed values is ViewPropTypes.style |object
| cellsContent | | | Controls corners placeholders additional content. Allowed keys is: top-left, top, top-right, left, center, right, bottom-left, bottom and bottom-right, allowed values is string OR React element |'bottom'
| dotsPos | OR 'right' if vertical | boolean OR enum('top-left', 'top', 'top-right', 'left', 'center', 'right', 'bottom-left', 'bottom', 'bottom-right') | Dots position |'bottom-left'
| prevPos | OR 'top-right' if vertical | boolean OR enum('top-left', 'top', 'top-right', 'left', 'center', 'right', 'bottom-left', 'bottom', 'bottom-right') | Prev button position |'bottom-right'
| nextPos | | boolean OR enum('top-left', 'top', 'top-right', 'left', 'center', 'right', 'bottom-left', 'bottom', 'bottom-right') | Next button position |'Prev'
| prevTitle | | string | Prev button title |'Next'
| nextTitle | | string | Next button title |Text.propTypes.style
| prevTitleStyle | | | Customize prev button title |Text.propTypes.style
| nextTitleStyle | | | Customize next button title |React.Component
| PrevComponent | | | Custom prev button component |React.Component
| NextComponent | | | Custom next button component |element
| firstPrevElement | | | Custom prev element on first slide (if not loop) |element
| lastNextElement | | | Custom next element on last slide (if not loop) |false
| dotsTouchable | | boolean | Touches over dots will move swiper to relative slide |ViewPropTypes.style
| dotsWrapperStyle | | | Dots wrapper View style |object
| dotProps | | | react-native-elements Badge props |object
| dotActiveStyle | | | Additional style to active dot. Will be added to dot badgeStyle |React.Component
| DotComponent | | | Custom dot component |
Store a reference to the Swiper in your component by using the ref prop
provided by React (see docs):
`jsx
const swiperRef = useRef(null);
...
...
/>
`
Then you can manually trigger swiper from anywhere:
`jsx``
() => {
swiperRef.current.goTo(1);
swiperRef.current.goToPrev();
swiperRef.current.goToNext();
const index = swiperRef.current.getActiveIndex();
};