1- import React from 'react' ;
1+ import React , { useEffect , useState } from 'react' ;
22
33import { ArrowIcon } from './icons' ;
44import { PAGE_SIZE_OPTIONS } from '../constants/pagination' ;
@@ -55,40 +55,90 @@ export const Pagination: React.FC<PaginationProps> = ({
5555 setPageSize,
5656 totalItems
5757} ) => {
58- // Generate page numbers to show
58+ // Track screen width for responsive pagination
59+ const [ screenWidth , setScreenWidth ] = useState ( window . innerWidth ) ;
60+
61+ // Update screen width on window resize
62+ useEffect ( ( ) => {
63+ const handleResize = ( ) => {
64+ setScreenWidth ( window . innerWidth ) ;
65+ } ;
66+
67+ window . addEventListener ( 'resize' , handleResize ) ;
68+
69+ // Cleanup event listener on component unmount
70+ return ( ) => {
71+ window . removeEventListener ( 'resize' , handleResize ) ;
72+ } ;
73+ } , [ ] ) ;
74+
75+ // Generate page numbers to show based on screen size
5976 const getVisiblePages = ( ) => {
6077 const pages : number [ ] = [ ] ;
6178
62- // If 5 or fewer pages, show all
63- if ( pageCount <= 5 ) {
79+ // Determine how many page numbers to show based on screen width
80+ let maxVisiblePages : number ;
81+ let showPageNumbers = true ;
82+
83+ if ( screenWidth <= 374 )
84+ // Very small screens: show only 2 page numbers + ellipsis
85+ maxVisiblePages = 2 ;
86+ else if ( screenWidth <= 542 )
87+ // Small screens: show 3 page numbers + ellipsis
88+ maxVisiblePages = 3 ;
89+ else if ( screenWidth < 1228 )
90+ // Medium screens: show 4 page numbers + ellipsis
91+ maxVisiblePages = 4 ;
92+ else
93+ // Large screens: show up to 6 page numbers
94+ maxVisiblePages = 6 ;
95+
96+ // For very small screens, don't show page numbers at all
97+ if ( screenWidth <= 415 )
98+ showPageNumbers = false ;
99+
100+ // If not showing page numbers, return empty array (only arrows will show)
101+ if ( ! showPageNumbers )
102+ return pages ;
103+
104+ // If total pages is less than or equal to max visible pages, show all
105+ if ( pageCount <= maxVisiblePages ) {
64106 for ( let i = 0 ; i < pageCount ; i ++ )
65107 pages . push ( i ) ;
66108
67109 return pages ;
68110 }
69111
70- // For more than 5 pages, show smart pagination
71- if ( pageIndex <= 2 ) {
72- // Near start: show 1, 2, 3, 4, 5, ..., last
73- for ( let i = 0 ; i < 5 ; i ++ )
112+ // Calculate how many pages to show on each side of current page
113+ const sidePages = Math . floor ( ( maxVisiblePages - 1 ) / 2 ) ;
114+
115+ // Near start
116+ if ( pageIndex <= sidePages ) {
117+ // Show first few pages + ellipsis + last page
118+ for ( let i = 0 ; i < maxVisiblePages - 1 ; i ++ )
74119 pages . push ( i ) ;
75120
76121 pages . push ( - 1 ) ; // Ellipsis marker
77122 pages . push ( pageCount - 1 ) ;
78- } else if ( pageIndex >= pageCount - 3 ) {
79- // Near end: show 1, ..., last-4, last-3, last-2, last-1, last
123+ }
124+ // Near end
125+ else if ( pageIndex >= pageCount - sidePages - 1 ) {
126+ // Show first page + ellipsis + last few pages
80127 pages . push ( 0 ) ;
81128 pages . push ( - 1 ) ; // Ellipsis marker
82- for ( let i = pageCount - 5 ; i < pageCount ; i ++ )
83- pages . push ( i ) ;
84129
85- } else {
86- // Middle: show 1, ..., current-1, current, current+1, ..., last
130+ for ( let i = pageCount - ( maxVisiblePages - 1 ) ; i < pageCount ; i ++ )
131+ pages . push ( i ) ;
132+ }
133+ // Middle
134+ else {
135+ // Show first page + ellipsis + current and neighbors + ellipsis + last page
87136 pages . push ( 0 ) ;
88137 pages . push ( - 1 ) ; // Ellipsis marker
89- pages . push ( pageIndex - 1 ) ;
90- pages . push ( pageIndex ) ;
91- pages . push ( pageIndex + 1 ) ;
138+
139+ for ( let i = pageIndex - sidePages ; i <= pageIndex + sidePages ; i ++ )
140+ pages . push ( i ) ;
141+
92142 pages . push ( - 1 ) ; // Ellipsis marker
93143 pages . push ( pageCount - 1 ) ;
94144 }
@@ -108,7 +158,10 @@ export const Pagination: React.FC<PaginationProps> = ({
108158 position : 'absolute'
109159 } } >
110160 { /* Total Items Text */ }
111- < div style = { { ...componentStyles . pagination . totalItems , position : 'static' } } >
161+ < div
162+ className = "pagination-total-items"
163+ style = { { ...componentStyles . pagination . totalItems , position : 'static' } }
164+ >
112165 Total { totalItems } items
113166 </ div >
114167
0 commit comments