Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autoplay functionality #22

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ From version **4.0.0** it has been moved to *Functional component* and fix separ
| itemContainerStyle | Style for each carousel container item | View style | {} | No |
| onScrollEnd | Fired while scrollview end of scrolling | Function | ()=> {} | No |
| initialIndex | Initial starting focused item of list | Number | 0 | No |
| autoPlay | Adds autoplay functionality | Boolean | false | No |
| autoPlayInterval | Use with autoPlay. Sets the interval of the slides when autoPlay is activated. | Number | 4000 | No |



Expand Down
32 changes: 32 additions & 0 deletions src/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ function Carousel(props, ref) {
inActiveOpacity = 0.8,
inverted = false,
initialIndex = 0,
autoPlay = false,
autoPlayInterval = 4000,
bounces = true,
showsHorizontalScrollIndicator = false,
keyExtractor = (item, index) => index.toString(),
Expand All @@ -50,6 +52,7 @@ function Carousel(props, ref) {
} = props;
const scrollViewRef = useRef(null);
const currentIndexRef = useRef(initialIndex);
const [isAutoPlay, setIsAutoPlay] = useState(autoPlay);
const scrollXRef = useRef(0);
const scrollXBeginRef = useRef(0);
const xOffsetRef = useRef(new Animated.Value(0));
Expand All @@ -60,6 +63,19 @@ function Carousel(props, ref) {
const containerStyle = [styles.container, { width: containerWidth }, style];
const dataLength = data ? data.length : 0;

let interval;
useEffect(() => {
if (!isAutoPlay) {
return;
}
interval = setInterval(() => {
generateAutoPlay()
}, autoPlayInterval);
return () => {
clearInterval(interval);
}
}, [isAutoPlay])

useConstructor(() => {
setScrollHandler();
});
Expand Down Expand Up @@ -111,6 +127,12 @@ function Carousel(props, ref) {
});
}

function generateAutoPlay() {
scrollToIndex(
(currentIndexRef.current + 1) % (data.length - 1)
);
}

function handleOnScrollBeginDrag() {
onScrollBeginDrag && onScrollBeginDrag();
scrollXBeginRef.current = scrollXRef.current;
Expand All @@ -134,6 +156,14 @@ function Carousel(props, ref) {
}
}

function handleTouchStart() {
setIsAutoPlay(false);
}

function handleTouchEnd() {
setIsAutoPlay(true);
}

function getItemTotalMarginBothSide() {
const compensatorOfSeparatorByScaleEffect = (1 - inActiveScale) * itemWidth;
return separatorWidth - compensatorOfSeparatorByScaleEffect / 2;
Expand Down Expand Up @@ -254,6 +284,8 @@ function Carousel(props, ref) {
{...otherProps}
ref={scrollViewRef}
data={data}
onTouchStart={handleTouchStart}
onTouchEnd={handleTouchEnd}
style={containerStyle}
horizontal={true}
inverted={inverted}
Expand Down