-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathPaginator.astro
More file actions
187 lines (162 loc) · 4.3 KB
/
Copy pathPaginator.astro
File metadata and controls
187 lines (162 loc) · 4.3 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
---
import type { Page } from "astro";
import MaskIcon from "./icons/MaskIcon.astro";
import { icons } from "@/utils/icons";
interface Props {
page: Page<unknown>;
}
const { page } = Astro.props;
const { t, dir, localizeNumber } = Astro.locals;
const current = page.currentPage;
const last = page.lastPage;
const firstUrl = current === 1 ? page.url.current : page.url.first!;
const base = firstUrl.replace(/\/$/, "");
const getHrefForPage = (n: number) => (n === 1 ? firstUrl : `${base}/${n}/`);
type Item = { type: "page"; number: number } | { type: "ellipsis" };
const items: Item[] = [];
if (last <= 5) {
// Show all pages if 5 or fewer
for (let n = 1; n <= last; n++) {
items.push({ type: "page", number: n });
}
} else {
const visible = new Set(
[1, last, current, current - 1, current + 1].filter(
(n) => n >= 1 && n <= last,
),
);
const sorted = [...visible].sort((a, b) => a - b);
for (const [i, n] of sorted.entries()) {
const prev = i > 0 ? sorted[i - 1] : undefined;
// Add ellipsis or fill single gap
if (prev !== undefined) {
const gap = n - prev;
if (gap === 2) {
items.push({ type: "page", number: prev + 1 });
} else if (gap > 2) {
items.push({ type: "ellipsis" });
}
}
items.push({ type: "page", number: n });
}
}
const hasPrev = current > 1;
const hasNext = current < last;
---
{
last > 1 && (
<nav
class="pagination"
aria-label={t("common:components.paginator.pagination")}
>
<a
href={hasPrev ? page.url.prev : undefined}
class="pagination-arrow"
aria-label={t("common:components.paginator.previousPage")}
aria-disabled={!hasPrev}
rel={hasPrev ? "prev" : undefined}
>
<MaskIcon
src={icons.mask("chevron-left")}
size="1.25em"
rtl={dir === "rtl"}
/>
</a>
<ul class="pagination-list">
{items.map((item) => {
if (item.type === "ellipsis") {
return (
<li class="pagination-separator" aria-hidden="true">
<span class="pagination-ellipsis">…</span>
</li>
);
}
const n = item.number;
const href = getHrefForPage(n);
const isCurrent = n === current;
return (
<li>
<a
href={href}
class="pagination-page"
aria-label={t("common:components.paginator.page", { n })}
aria-current={isCurrent ? "page" : undefined}
>
{localizeNumber(n)}
</a>
</li>
);
})}
</ul>
<a
href={hasNext ? page.url.next : undefined}
class="pagination-arrow"
aria-label={t("common:components.paginator.nextPage")}
aria-disabled={!hasNext}
rel={hasNext ? "next" : undefined}
>
<MaskIcon
src={icons.mask("chevron-right")}
size="1.25em"
rtl={dir === "rtl"}
/>
</a>
</nav>
)
}
<style>
.pagination {
display: flex;
align-items: center;
justify-content: center;
gap: var(--space-xs);
margin-top: var(--space-2xl);
}
.pagination-list {
display: flex;
align-items: center;
gap: var(--space-2xs);
list-style: none;
padding: 0;
margin: 0;
}
.pagination-arrow,
.pagination-page,
.pagination-ellipsis {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 2.5rem;
height: 2.5rem;
border-radius: var(--radius-full);
text-decoration: none;
font-size: var(--font-size-md);
color: var(--heading-color);
transition: background-color 0.15s ease;
}
.pagination-arrow {
color: var(--paragraph-main);
&:hover:not([aria-disabled="true"]) {
background: var(--card-hover-color);
}
&[aria-disabled="true"] {
opacity: 0.35;
pointer-events: none;
cursor: default;
}
}
.pagination-page {
&:hover {
background: var(--card-hover-color);
}
&[aria-current="page"] {
font-weight: var(--font-weight-semibold);
background: var(--footer-color);
pointer-events: none;
}
}
.pagination-ellipsis {
pointer-events: none;
color: var(--paragraph-subtitle);
}
</style>