1
1
import React from "react" ;
2
2
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" ;
5
3
6
4
const Pagination = ( { postsPerPage, totalPosts, paginate, currentPage } ) => {
7
- const pageNumbers = [ ] ;
8
5
const totalPages = Math . ceil ( totalPosts / postsPerPage ) ;
9
6
10
- for ( let i = 1 ; i <= totalPages ; i ++ ) {
11
- pageNumbers . push ( i ) ;
12
- }
13
-
14
7
const handlePrevious = ( ) => {
15
8
if ( currentPage > 1 ) {
16
9
paginate ( currentPage - 1 ) ;
@@ -24,70 +17,82 @@ const Pagination = ({ postsPerPage, totalPosts, paginate, currentPage }) => {
24
17
} ;
25
18
26
19
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 ) ;
37
25
}
38
26
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 ) ;
41
49
}
42
50
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 ;
52
52
} ;
53
53
54
- const visiblePageNumbers = getVisiblePageNumbers ( ) ;
54
+ const visiblePages = getVisiblePageNumbers ( ) ;
55
55
56
56
return (
57
57
< ResourcePageWrapper >
58
58
< div className = "pagination-container" >
59
59
< button
60
60
onClick = { handlePrevious }
61
- className = { `pagination-btn prev-btn ${ currentPage === 1 ? " disabled-btn" : "" } ` }
61
+ className = { `pagination-btn prev-btn ${ currentPage === 1 ? ' disabled-btn' : '' } ` }
62
62
disabled = { currentPage === 1 }
63
63
>
64
- < FaChevronLeft className = "arrow-icon" /> Previous
64
+ Previous
65
65
</ button >
66
66
67
67
< 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
+ ) : (
71
74
< 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" }
75
78
>
76
- { item }
79
+ { page }
77
80
</ button >
78
- ) ) }
81
+ )
82
+ ) }
79
83
</ div >
80
84
81
85
< button
82
86
onClick = { handleNext }
83
- className = { `pagination-btn next-btn ${ currentPage === totalPages ? " disabled-btn" : "" } ` }
87
+ className = { `pagination-btn next-btn ${ currentPage === totalPages ? ' disabled-btn' : '' } ` }
84
88
disabled = { currentPage === totalPages }
85
89
>
86
- Next < FaChevronRight className = "arrow-icon" />
90
+ Next
87
91
</ button >
88
92
</ div >
89
93
</ ResourcePageWrapper >
90
94
) ;
91
95
} ;
92
96
93
- export default Pagination ;
97
+ export default Pagination ;
98
+
0 commit comments