forked from monero-project/monero-site
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageTabs.astro
More file actions
294 lines (273 loc) · 6.92 KB
/
Copy pathPageTabs.astro
File metadata and controls
294 lines (273 loc) · 6.92 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
---
interface Props {
labels: string[]; // One label per panel slot (panel-0 ... panel-9)
links?: string[]; // Optional array of URLs, if provided, renders as navigation links instead of same-page tabs
id: string; // Required id
initial?: number; // 0-based default tab
activeIndex?: number; // 0-based active tab for navigation mode
}
const { labels = [], links, id, initial = 0, activeIndex }: Props = Astro.props;
const { t } = Astro.locals;
if (!Array.isArray(labels) || labels.length === 0) {
throw new Error("<PageTabs> needs a non-empty labels array.");
}
if (labels.length > 10) {
throw new Error("<PageTabs> supports up to 10 tabs.");
}
if (links && links.length !== labels.length) {
throw new Error("<PageTabs> links array must match labels array length.");
}
if (links && labels.some((_, i) => Astro.slots.has(`panel-${i}`))) {
throw new Error("<PageTabs> cannot have both links and slot content.");
}
if (typeof id !== "string" || id.trim() === "") {
throw new Error("<PageTabs> requires a non-empty id.");
}
const uid = id.trim();
const I = (n: number) =>
Math.min(Math.max(0, Number(n) || 0), labels.length - 1);
const active = I(initial);
---
<div class="tabs" id={uid}>
{
links ? (
<div class="tab-header">
{labels.map((label, i) => (
<a
class:list={["tab-label", activeIndex === i && "active"]}
href={links[i]}
>
{label}
</a>
))}
</div>
) : (
<>
<fieldset>
<legend class="sr-only">
{t("common:components.pageTabs.tabSelection")}
</legend>
{labels.map((_, i) => (
<input
type="radio"
name={uid}
id={`${uid}-${i}`}
checked={active === i}
aria-controls={`${uid}-panel-${i}`}
/>
))}
</fieldset>
<div class="tab-header">
{labels.map((label, i) => (
<label class="tab-label" for={`${uid}-${i}`}>
{label}
</label>
))}
</div>
<div class="tab-content">
{labels[0] && (
<div
class="panel"
id={`${uid}-panel-0`}
role="tabpanel"
aria-labelledby={`${uid}-0`}
>
<slot name="panel-0" />
</div>
)}
{labels[1] && (
<div
class="panel"
id={`${uid}-panel-1`}
role="tabpanel"
aria-labelledby={`${uid}-1`}
>
<slot name="panel-1" />
</div>
)}
{labels[2] && (
<div
class="panel"
id={`${uid}-panel-2`}
role="tabpanel"
aria-labelledby={`${uid}-2`}
>
<slot name="panel-2" />
</div>
)}
{labels[3] && (
<div
class="panel"
id={`${uid}-panel-3`}
role="tabpanel"
aria-labelledby={`${uid}-3`}
>
<slot name="panel-3" />
</div>
)}
{labels[4] && (
<div
class="panel"
id={`${uid}-panel-4`}
role="tabpanel"
aria-labelledby={`${uid}-4`}
>
<slot name="panel-4" />
</div>
)}
{labels[5] && (
<div
class="panel"
id={`${uid}-panel-5`}
role="tabpanel"
aria-labelledby={`${uid}-5`}
>
<slot name="panel-5" />
</div>
)}
{labels[6] && (
<div
class="panel"
id={`${uid}-panel-6`}
role="tabpanel"
aria-labelledby={`${uid}-6`}
>
<slot name="panel-6" />
</div>
)}
{labels[7] && (
<div
class="panel"
id={`${uid}-panel-7`}
role="tabpanel"
aria-labelledby={`${uid}-7`}
>
<slot name="panel-7" />
</div>
)}
{labels[8] && (
<div
class="panel"
id={`${uid}-panel-8`}
role="tabpanel"
aria-labelledby={`${uid}-8`}
>
<slot name="panel-8" />
</div>
)}
{labels[9] && (
<div
class="panel"
id={`${uid}-panel-9`}
role="tabpanel"
aria-labelledby={`${uid}-9`}
>
<slot name="panel-9" />
</div>
)}
</div>
</>
)
}
</div>
<style lang="scss">
.tabs {
width: 100%;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
fieldset {
border: none;
padding: 0;
margin: 0;
}
.tabs input[type="radio"] {
position: absolute;
opacity: 0;
pointer-events: none;
}
.tab-header {
display: flex;
align-items: stretch;
box-sizing: border-box;
width: max-content;
max-width: 100%;
border: 1px solid var(--border-color);
border-radius: var(--radius-full);
overflow-x: auto;
overflow-y: hidden;
margin-bottom: var(--space-2xl);
scrollbar-width: thin;
-webkit-overflow-scrolling: touch;
scroll-snap-type: x mandatory;
&::-webkit-scrollbar {
height: 4px;
}
&::-webkit-scrollbar-track {
background: transparent;
}
&::-webkit-scrollbar-thumb {
border-radius: var(--radius-full);
}
}
.tab-label {
flex: 0 0 auto;
display: flex;
align-items: center;
justify-content: center;
padding-block: var(--space-md);
padding-inline: clamp(1rem, 0.6rem + 2vw, 2rem);
cursor: pointer;
font-weight: var(--font-weight-semibold);
font-size: clamp(0.75rem, 0.75rem + 0.5vw, 1rem);
user-select: none;
text-decoration: none;
border-radius: var(--radius-full);
white-space: nowrap;
scroll-snap-align: center;
color: var(--heading-color);
&:hover {
background: var(--card-hover-color);
}
&.active {
background: var(--footer-color);
}
}
.panel {
display: none;
}
@media (max-width: 900px) {
.tab-header {
width: 100%;
}
.tab-label {
flex: 1;
}
}
@for $i from 0 through 9 {
fieldset:has(input[id$="-#{$i}"]:checked)
~ .tab-header
label[for$="-#{$i}"] {
background: var(--footer-color);
}
fieldset:has(input[id$="-#{$i}"]:focus-visible)
~ .tab-header
label[for$="-#{$i}"] {
outline: 2px solid currentColor;
}
fieldset:has(input[id$="-#{$i}"]:checked)
~ .tab-content
.panel[id$="-panel-#{$i}"] {
display: block;
}
}
</style>