Skip to content

Commit 30d1164

Browse files
author
Tharanishwaran
committed
fix: responsive design for pagination buttons
Signed-off-by: Tharanishwaran <your-email@example.com>
1 parent 663995f commit 30d1164

File tree

2 files changed

+181
-99
lines changed

2 files changed

+181
-99
lines changed
Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
import React from "react";
22
import { ResourcePageWrapper } from "./resourceGrid.style";
3-
import { FaChevronLeft } from "@react-icons/all-files/fa/FaChevronLeft";
4-
import { FaChevronRight } from "@react-icons/all-files/fa/FaChevronRight";
53

64
const Pagination = ({ postsPerPage, totalPosts, paginate, currentPage }) => {
7-
const pageNumbers = [];
85
const totalPages = Math.ceil(totalPosts / postsPerPage);
96

10-
for (let i = 1; i <= totalPages; i++) {
11-
pageNumbers.push(i);
12-
}
13-
147
const handlePrevious = () => {
158
if (currentPage > 1) {
169
paginate(currentPage - 1);
@@ -24,70 +17,82 @@ const Pagination = ({ postsPerPage, totalPosts, paginate, currentPage }) => {
2417
};
2518

2619
const getVisiblePageNumbers = () => {
27-
if (totalPages <= 5) {
28-
return pageNumbers;
29-
}
30-
31-
const visiblePages = [];
32-
33-
visiblePages.push(1);
34-
35-
if (currentPage > 3) {
36-
visiblePages.push("...");
20+
const isMobile = typeof window !== 'undefined' && window.innerWidth <= 575;
21+
const maxVisible = isMobile ? 3 : 5;
22+
23+
if (totalPages <= maxVisible) {
24+
return Array.from({ length: totalPages }, (_, i) => i + 1);
3725
}
3826

39-
for (let i = Math.max(2, currentPage - 1); i <= Math.min(currentPage + 1, totalPages - 1); i++) {
40-
visiblePages.push(i);
27+
const pages = [];
28+
29+
if (isMobile) {
30+
// Mobile: Show fewer pages
31+
if (currentPage <= 2) {
32+
pages.push(1, 2, 3);
33+
if (totalPages > 3) pages.push("...");
34+
} else if (currentPage >= totalPages - 1) {
35+
if (totalPages > 3) pages.push("...");
36+
pages.push(totalPages - 2, totalPages - 1, totalPages);
37+
} else {
38+
pages.push("...", currentPage - 1, currentPage, currentPage + 1, "...");
39+
}
40+
} else {
41+
// Desktop: Original logic
42+
pages.push(1);
43+
if (currentPage > 3) pages.push("...");
44+
for (let i = Math.max(2, currentPage - 1); i <= Math.min(currentPage + 1, totalPages - 1); i++) {
45+
pages.push(i);
46+
}
47+
if (currentPage < totalPages - 2) pages.push("...");
48+
if (totalPages > 1) pages.push(totalPages);
4149
}
4250

43-
if (currentPage < totalPages - 2) {
44-
visiblePages.push("...");
45-
}
46-
47-
if (totalPages > 1) {
48-
visiblePages.push(totalPages);
49-
}
50-
51-
return visiblePages;
51+
return pages;
5252
};
5353

54-
const visiblePageNumbers = getVisiblePageNumbers();
54+
const visiblePages = getVisiblePageNumbers();
5555

5656
return (
5757
<ResourcePageWrapper>
5858
<div className="pagination-container">
5959
<button
6060
onClick={handlePrevious}
61-
className={`pagination-btn prev-btn ${currentPage === 1 ? "disabled-btn" : ""}`}
61+
className={`pagination-btn prev-btn ${currentPage === 1 ? 'disabled-btn' : ''}`}
6262
disabled={currentPage === 1}
6363
>
64-
<FaChevronLeft className="arrow-icon" /> Previous
64+
Previous
6565
</button>
6666

6767
<div className="page-numbers">
68-
{visiblePageNumbers.map((item, index) => (
69-
item === "..." ?
70-
<span key={`ellipsis-${index}`} className="ellipsis">...</span> :
68+
{visiblePages.map((page, idx) =>
69+
page === "..." ? (
70+
<span key={`ellipsis-${idx}`} className="ellipsis">
71+
...
72+
</span>
73+
) : (
7174
<button
72-
key={item}
73-
onClick={() => paginate(item)}
74-
className={item === currentPage ? "page-btn active-btn" : "page-btn"}
75+
key={page}
76+
onClick={() => paginate(page)}
77+
className={page === currentPage ? "page-btn active-btn" : "page-btn"}
7578
>
76-
{item}
79+
{page}
7780
</button>
78-
))}
81+
)
82+
)}
7983
</div>
8084

8185
<button
8286
onClick={handleNext}
83-
className={`pagination-btn next-btn ${currentPage === totalPages ? "disabled-btn" : ""}`}
87+
className={`pagination-btn next-btn ${currentPage === totalPages ? 'disabled-btn' : ''}`}
8488
disabled={currentPage === totalPages}
8589
>
86-
Next <FaChevronRight className="arrow-icon" />
90+
Next
8791
</button>
8892
</div>
8993
</ResourcePageWrapper>
9094
);
9195
};
9296

93-
export default Pagination;
97+
export default Pagination;
98+

0 commit comments

Comments
 (0)