MCPcopy
hub / github.com/xiaolin/react-image-gallery / useAutoPlay

Function useAutoPlay

src/components/hooks/useAutoPlay.ts:39–169  ·  view source on GitHub ↗
({
  autoPlay = false,
  slideInterval = DEFAULT_SLIDE_INTERVAL,
  slideDuration = DEFAULT_SLIDE_DURATION,
  infinite = true,
  totalSlides,
  currentIndex,
  canSlideRight,
  slideToIndexCore,
  slideToIndexWithStyleReset,
  onPlay,
  onPause,
}: UseAutoPlayProps)

Source from the content-addressed store, hash-verified

37 * Uses refs to avoid stale closures in setInterval
38 */
39export function useAutoPlay({
40 autoPlay = false,
41 slideInterval = DEFAULT_SLIDE_INTERVAL,
42 slideDuration = DEFAULT_SLIDE_DURATION,
43 infinite = true,
44 totalSlides,
45 currentIndex,
46 canSlideRight,
47 slideToIndexCore,
48 slideToIndexWithStyleReset,
49 onPlay,
50 onPause,
51}: UseAutoPlayProps): UseAutoPlayReturn {
52 const [isPlaying, setIsPlaying] = useState(false);
53 const playPauseIntervalRef = useRef<ReturnType<typeof setInterval> | null>(
54 null
55 );
56
57 // Ref to hold the latest pauseOrPlay function (avoids stale closure in setInterval)
58 const pauseOrPlayRef = useRef<(() => void) | null>(null);
59
60 const pauseOrPlay = useCallback(() => {
61 if (!infinite && !canSlideRight()) {
62 // Stop at the end if not infinite
63 if (playPauseIntervalRef.current) {
64 clearInterval(playPauseIntervalRef.current);
65 playPauseIntervalRef.current = null;
66 setIsPlaying(false);
67 if (onPause) {
68 onPause(currentIndex);
69 }
70 }
71 } else {
72 const nextIndex = currentIndex + 1;
73 // Handle 2 slides the same way as manual sliding
74 if (totalSlides === 2) {
75 slideToIndexWithStyleReset(nextIndex);
76 } else {
77 slideToIndexCore(nextIndex);
78 }
79 }
80 }, [
81 infinite,
82 canSlideRight,
83 currentIndex,
84 totalSlides,
85 slideToIndexCore,
86 slideToIndexWithStyleReset,
87 onPause,
88 ]);
89
90 // Keep ref updated with latest pauseOrPlay
91 pauseOrPlayRef.current = pauseOrPlay;
92
93 const play = useCallback(
94 (shouldCallOnPlay = true) => {
95 if (!playPauseIntervalRef.current) {
96 setIsPlaying(true);

Callers 2

ImageGallery.tsxFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected