Skip to content

Commit 825df15

Browse files
committed
fix: Previous and next buttons and added slide number for better navigation among pages
1 parent a6e1c04 commit 825df15

2 files changed

Lines changed: 105 additions & 23 deletions

File tree

community-website/pages/learn-git-and-github.tsx

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable @next/next/no-img-element */
22
import type { NextPage } from "next";
3-
import { useEffect, useRef } from "react";
3+
import { useEffect, useRef, useReducer } from "react";
44
import Head from "next/head";
55
import Link from "next/link";
66
import ConfettiGenerator from "confetti-js";
@@ -12,49 +12,87 @@ import specificStyles from "../styles/LearnGitAndGitHub.module.css";
1212
const 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();

community-website/styles/LearnGitAndGitHub.module.css

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,48 @@
111111
margin-top: 30px;
112112
}
113113

114+
.slideIndicators {
115+
display: flex;
116+
justify-content: center;
117+
align-items: center;
118+
gap: 8px;
119+
margin-top: 24px;
120+
flex-wrap: wrap;
121+
padding: 0 20px;
122+
}
123+
124+
.slideIndicator {
125+
width: 24px;
126+
height: 24px;
127+
border-radius: 12px;
128+
background: white;
129+
cursor: pointer;
130+
transition: all 0.3s ease;
131+
border: 1px solid #e2e8f0;
132+
padding: 0;
133+
display: flex;
134+
align-items: center;
135+
justify-content: center;
136+
font-size: 12px;
137+
font-weight: 500;
138+
color: #4a5568;
139+
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
140+
}
141+
142+
.slideIndicator:hover {
143+
transform: translateY(-2px);
144+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
145+
border-color: #cbd5e0;
146+
}
147+
148+
.slideIndicator.active {
149+
background: #2d3748;
150+
color: white;
151+
border-color: #2d3748;
152+
transform: translateY(-1px);
153+
box-shadow: 0 4px 6px rgba(45, 55, 72, 0.3);
154+
}
155+
114156
.navBtn {
115157
background: rgba(255, 255, 255, 0.2);
116158
border: 2px solid black;
@@ -268,4 +310,4 @@
268310

269311
:global(.dark) .slide-counter {
270312
background: rgba(0, 0, 0, 0.2);
271-
}
313+
}

0 commit comments

Comments
 (0)