11/* eslint-disable @next/next/no-img-element */
22import type { NextPage } from "next" ;
3- import { useEffect , useRef } from "react" ;
3+ import { useEffect , useRef , useReducer } from "react" ;
44import Head from "next/head" ;
55import Link from "next/link" ;
66import ConfettiGenerator from "confetti-js" ;
@@ -12,49 +12,87 @@ import specificStyles from "../styles/LearnGitAndGitHub.module.css";
1212const LearnGitAndGitHub : NextPage = ( ) => {
1313 const currentSlide = useRef ( 0 ) ;
1414 const slidesRef = useRef < NodeListOf < Element > > ( ) ;
15+ const renderSlideIndicators = ( ) => {
16+ if ( ! slidesRef . current ) return null ;
17+ const totalSlides = slidesRef . current . length ;
18+
19+ return (
20+ < div className = { specificStyles . slideIndicators } >
21+ { Array . from ( { length : totalSlides } , ( _ , index ) => (
22+ < button
23+ key = { index }
24+ className = { `${ specificStyles . slideIndicator } ${
25+ index === currentSlide . current ? specificStyles . active : ""
26+ } `}
27+ onClick = { ( ) => showSlide ( index ) }
28+ aria-label = { `Go to slide ${ index + 1 } ` }
29+ title = { `Go to slide ${ index + 1 } ` }
30+ >
31+ < span > { index + 1 } </ span >
32+ </ button >
33+ ) ) }
34+ </ div >
35+ ) ;
36+ } ;
37+
1538 const renderButtonsComponent = ( ) => {
39+ if ( ! slidesRef . current ) return null ;
40+ const totalSlides = slidesRef . current . length ;
41+ const isLastSlide = currentSlide . current === totalSlides - 1 ;
42+
1643 return (
17- < div className = { specificStyles . navigation } >
18- < button
19- className = { specificStyles . navBtn }
20- id = "prev-btn"
21- onClick = { previousSlide }
22- >
23- ← Previous
24- </ button >
25- < button
26- className = { specificStyles . navBtn }
27- id = "next-btn"
28- onClick = { nextSlide }
44+ < >
45+ < div
46+ className = { `${ specificStyles . navigation } ${
47+ isLastSlide ? specificStyles . singleButton : ""
48+ } `}
2949 >
30- Next →
31- </ button >
32- </ div >
50+ < button
51+ className = { specificStyles . navBtn }
52+ id = "prev-btn"
53+ onClick = { previousSlide }
54+ >
55+ < span style = { { position : 'relative' , zIndex : 1 } } > ← Previous</ span >
56+ </ button >
57+ { ! isLastSlide && (
58+ < button
59+ className = { specificStyles . navBtn }
60+ id = "next-btn"
61+ onClick = { nextSlide }
62+ >
63+ < span style = { { position : 'relative' , zIndex : 1 } } > Next →</ span >
64+ </ button >
65+ ) }
66+ </ div >
67+ { renderSlideIndicators ( ) }
68+ </ >
3369 ) ;
3470 } ;
3571
3672 // Show slide
73+ const [ , forceUpdate ] = useReducer ( ( x : number ) => x + 1 , 0 ) ;
74+
3775 const showSlide = ( n : number ) => {
3876 if ( ! slidesRef . current ) return ;
3977 const slides = slidesRef . current ;
4078 const totalSlides = slides . length ;
4179
4280 slides [ currentSlide . current ] . classList . remove ( specificStyles . active ) ;
4381
44- currentSlide . current = n ;
45- if ( currentSlide . current >= totalSlides ) currentSlide . current = 0 ;
46- if ( currentSlide . current < 0 ) currentSlide . current = totalSlides - 1 ;
82+ // Ensure slide number stays within bounds without wrapping
83+ currentSlide . current = Math . min ( Math . max ( 0 , n ) , totalSlides - 1 ) ;
4784
4885 slides [ currentSlide . current ] . classList . add ( specificStyles . active ) ;
4986
5087 const slideNumberEl = document . getElementById ( "slide-number" ) ;
5188 const prevBtn = document . getElementById ( "prev-btn" ) as HTMLButtonElement ;
52- const nextBtn = document . getElementById ( "next-btn" ) as HTMLButtonElement ;
5389
5490 if ( slideNumberEl )
5591 slideNumberEl . textContent = String ( currentSlide . current + 1 ) ;
5692 if ( prevBtn ) prevBtn . disabled = currentSlide . current === 0 ;
57- if ( nextBtn ) nextBtn . disabled = currentSlide . current === totalSlides - 1 ;
93+
94+ // Force re-render to update navigation buttons
95+ forceUpdate ( ) ;
5896 } ;
5997
6098 const nextSlide = ( ) => showSlide ( currentSlide . current + 1 ) ;
@@ -73,7 +111,9 @@ const LearnGitAndGitHub: NextPage = () => {
73111 const handleKeyDown = ( e : KeyboardEvent ) => {
74112 if ( e . key === "ArrowRight" || e . key === " " ) {
75113 e . preventDefault ( ) ;
76- nextSlide ( ) ;
114+ if ( currentSlide . current < ( slidesRef . current ?. length || 0 ) - 1 ) {
115+ nextSlide ( ) ;
116+ }
77117 } else if ( e . key === "ArrowLeft" ) {
78118 e . preventDefault ( ) ;
79119 previousSlide ( ) ;
0 commit comments