-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathPagination.tsx
More file actions
237 lines (211 loc) · 6.64 KB
/
Copy pathPagination.tsx
File metadata and controls
237 lines (211 loc) · 6.64 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { ChevronRightIcon } from "@heroicons/react/24/outline";
import { ChevronLeftIcon } from "@heroicons/react/24/solid";
import { Link, useLocation } from "@remix-run/react";
import { cn } from "~/utils/cn";
import { LinkButton } from "./Buttons";
/** Pass `hasNextPage` when the total page count is unknown; use `showPageNumbers={false}` in that case. */
export function PaginationControls({
currentPage,
totalPages,
hasNextPage,
showPageNumbers = true,
}: {
currentPage: number;
totalPages: number;
/** When set, navigation uses this instead of `totalPages`. */
hasNextPage?: boolean;
showPageNumbers?: boolean;
}) {
const location = useLocation();
const hasNextMode = hasNextPage !== undefined;
const showPagination = hasNextMode ? currentPage > 1 || hasNextPage : totalPages > 1;
const nextDisabled = hasNextMode ? !hasNextPage : currentPage === totalPages;
if (!showPagination) {
return null;
}
return (
<nav className="flex items-center gap-1" aria-label="Pagination">
{showPageNumbers ? (
<>
<LinkButton
to={pageUrl(location, currentPage - 1)}
variant="secondary/small"
LeadingIcon={ChevronLeftIcon}
shortcut={{ key: "j" }}
tooltip="Previous"
disabled={currentPage === 1}
className={cn("px-2", currentPage > 1 ? "group" : "")}
/>
{calculatePageLinks(currentPage, totalPages).map((page, i) => (
<PageLinkComponent page={page} key={i} location={location} />
))}
<LinkButton
to={pageUrl(location, currentPage + 1)}
variant="secondary/small"
TrailingIcon={ChevronRightIcon}
shortcut={{ key: "k" }}
tooltip="Next"
disabled={nextDisabled}
className={cn("px-2", !nextDisabled ? "group" : "")}
/>
</>
) : (
<div className="flex items-center">
<div className={cn("peer/prev order-1", currentPage === 1 && "pointer-events-none")}>
<LinkButton
to={pageUrl(location, currentPage - 1)}
variant="secondary/small"
LeadingIcon={ChevronLeftIcon}
shortcut={{ key: "j" }}
tooltip="Previous"
disabled={currentPage === 1}
className={cn(
"flex items-center rounded-r-none border-r-0 pl-2 pr-[0.5625rem]",
currentPage === 1 && "cursor-not-allowed opacity-50"
)}
/>
</div>
<div
className={cn(
"order-2 h-6 w-px bg-charcoal-600 transition-colors peer-hover/next:bg-charcoal-550 peer-hover/prev:bg-charcoal-550",
currentPage === 1 && nextDisabled && "opacity-30"
)}
/>
<div className={cn("peer/next order-3", nextDisabled && "pointer-events-none")}>
<LinkButton
to={pageUrl(location, currentPage + 1)}
variant="secondary/small"
TrailingIcon={ChevronRightIcon}
shortcut={{ key: "k" }}
tooltip="Next"
disabled={nextDisabled}
className={cn(
"flex items-center rounded-l-none border-l-0 pl-[0.5625rem] pr-2",
nextDisabled && "cursor-not-allowed opacity-50"
)}
/>
</div>
</div>
)}
</nav>
);
}
function pageUrl(location: ReturnType<typeof useLocation>, page: number): string {
const search = new URLSearchParams(location.search);
search.set("page", String(page));
return location.pathname + "?" + search.toString();
}
const baseClass =
"flex items-center justify-center border border-transparent min-w-6 h-6 px-1 text-xs font-medium transition text-text-dimmed rounded-sm focus-visible:focus-custom";
const unselectedClass = "hover:bg-tertiary hover:text-text-bright";
const selectedClass = "border-charcoal-600 bg-tertiary text-text-bright hover:bg-charcoal-600/50";
function PageLinkComponent({
page,
location,
}: {
page: PageLink;
location: ReturnType<typeof useLocation>;
}) {
if (page.type === "specific") {
return (
<Link
to={pageUrl(location, page.page)}
className={cn(baseClass, page.isCurrent ? selectedClass : unselectedClass)}
>
{page.page}
</Link>
);
} else {
return <span className={baseClass}>...</span>;
}
}
type PageLink = EllipsisPageLink | SpecificPageLink;
type EllipsisPageLink = {
type: "ellipses";
};
type SpecificPageLink = {
type: "specific";
page: number;
isCurrent: boolean;
};
// If there are less than or equal to 6 pages, just show all the pages.
// If there are more than 5 pages, show the first 3, the current page, and the last 3.
function calculatePageLinks(currentPage: number, totalPages: number): Array<PageLink> {
const pageLinks: Array<PageLink> = [];
if (totalPages <= 10) {
for (let i = 1; i <= totalPages; i++) {
pageLinks.push({
type: "specific",
page: i,
isCurrent: i === currentPage,
});
}
} else {
if (currentPage <= 3) {
for (let i = 1; i <= 4; i++) {
pageLinks.push({
type: "specific",
page: i,
isCurrent: i === currentPage,
});
}
pageLinks.push({
type: "ellipses",
});
for (let i = totalPages - 3; i <= totalPages; i++) {
pageLinks.push({
type: "specific",
page: i,
isCurrent: i === currentPage,
});
}
} else if (currentPage >= totalPages - 3) {
for (let i = 1; i <= 3; i++) {
pageLinks.push({
type: "specific",
page: i,
isCurrent: i === currentPage,
});
}
pageLinks.push({
type: "ellipses",
});
for (let i = totalPages - 4; i <= totalPages; i++) {
pageLinks.push({
type: "specific",
page: i,
isCurrent: i === currentPage,
});
}
} else {
for (let i = 1; i <= 3; i++) {
pageLinks.push({
type: "specific",
page: i,
isCurrent: i === currentPage,
});
}
pageLinks.push({
type: "ellipses",
});
for (let i = currentPage - 1; i <= currentPage + 1; i++) {
pageLinks.push({
type: "specific",
page: i,
isCurrent: i === currentPage,
});
}
pageLinks.push({
type: "ellipses",
});
for (let i = totalPages - 2; i <= totalPages; i++) {
pageLinks.push({
type: "specific",
page: i,
isCurrent: i === currentPage,
});
}
}
}
return pageLinks;
}