1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
import { type ForwardRefRenderFunction, forwardRef } from 'react';
import { ButtonLink, Icon, Nav, type NavProps } from '../../../atoms';
import { NavItem, NavList } from '../../../molecules';
import styles from './pagination.module.scss';
export type PaginationItemKind = 'backward' | 'forward' | 'number';
type RenderPaginationItemAriaLabelProps = {
/**
* Does the item represent the current page?
*/
isCurrentPage?: boolean;
/**
* The item kind.
*/
kind: PaginationItemKind;
/**
* The linked page number.
*/
pageNumber: number;
};
export type RenderPaginationItemAriaLabel = (
props: RenderPaginationItemAriaLabelProps
) => string;
export type RenderPaginationLink = (page: number) => string;
export type PaginationProps = Omit<NavProps, 'children'> & {
/**
* The currently active page number.
*/
current: number;
/**
* Should the pagination be centered?
*
* @default false
*/
isCentered?: boolean;
/**
* Function used to provide an accessible label to pagination items.
*/
renderItemAriaLabel: RenderPaginationItemAriaLabel;
/**
* Function used to create the href provided for each page link.
*/
renderLink: RenderPaginationLink;
/**
* The number of pages to show on each side of the current page.
*
* @default 1
*/
siblings?: number;
/**
* The total number of pages.
*/
total: number;
};
type GetPagesProps = Pick<PaginationProps, 'current' | 'total'> & {
displayRange: number;
};
const getPages = ({ current, displayRange, total }: GetPagesProps) =>
Array.from({ length: total }, (_, index) => {
const page = index + 1;
const isFirstPage = page === 1;
const isLastPage = page === total;
const isOutOfRangeFromStart = page < current - displayRange && !isFirstPage;
const isOutOfRangeFromEnd = page > current + displayRange && !isLastPage;
const isOutOfRange = isOutOfRangeFromStart || isOutOfRangeFromEnd;
const ellipsisId = isOutOfRangeFromStart
? 'start-ellipsis'
: 'end-ellipsis';
return {
id: isOutOfRange ? ellipsisId : `page-${page}`,
number: isOutOfRangeFromStart || isOutOfRangeFromEnd ? null : page,
};
}).filter(
(page, index, allPages) =>
index === 0 || page.number !== allPages[index - 1]?.number
);
const PaginationWithRef: ForwardRefRenderFunction<
HTMLElement,
PaginationProps
> = (
{
className = '',
current,
isCentered = false,
renderItemAriaLabel,
renderLink,
siblings = 1,
total,
...props
},
ref
) => {
const paginationClass = [
styles.wrapper,
styles[isCentered ? 'wrapper--centered' : ''],
className,
].join(' ');
const displayRange =
current === 1 || current === total ? siblings + 1 : siblings;
const hasPreviousPage = current > 1;
const hasNextPage = current < total;
const pages = getPages({ current, displayRange, total });
const ellipsis = '\u2026' as const;
return (
<Nav {...props} className={paginationClass} ref={ref}>
<NavList
className={styles.list}
isInline
// eslint-disable-next-line react/jsx-no-literals
spacing="xs"
>
{hasPreviousPage ? (
<NavItem className={styles.item}>
<ButtonLink
aria-label={renderItemAriaLabel({
kind: 'backward',
pageNumber: current - 1,
})}
// eslint-disable-next-line react/jsx-no-literals
kind="secondary"
to={renderLink(current - 1)}
>
<Icon
aria-hidden
// eslint-disable-next-line react/jsx-no-literals
shape="arrow"
// eslint-disable-next-line react/jsx-no-literals
orientation="left"
/>
</ButtonLink>
</NavItem>
) : null}
{pages.map((page) => {
const isCurrentPage = page.number === current;
return (
<NavItem className={styles.item} key={page.id}>
<ButtonLink
aria-current={isCurrentPage ? 'page' : undefined}
aria-label={
page.number
? renderItemAriaLabel({
isCurrentPage,
kind: 'number',
pageNumber: page.number,
})
: undefined
}
isDisabled={page.number === null || isCurrentPage}
// eslint-disable-next-line react/jsx-no-literals
kind="secondary"
to={page.number ? renderLink(page.number) : ''}
>
{page.number ?? ellipsis}
</ButtonLink>
</NavItem>
);
})}
{hasNextPage ? (
<NavItem className={styles.item}>
<ButtonLink
aria-label={renderItemAriaLabel({
kind: 'forward',
pageNumber: current + 1,
})}
// eslint-disable-next-line react/jsx-no-literals
kind="secondary"
to={renderLink(current + 1)}
>
<Icon
aria-hidden
// eslint-disable-next-line react/jsx-no-literals
shape="arrow"
// eslint-disable-next-line react/jsx-no-literals
orientation="right"
/>
</ButtonLink>
</NavItem>
) : null}
</NavList>
</Nav>
);
};
export const Pagination = forwardRef(PaginationWithRef);
|