-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy patharia-role-reference.html
More file actions
691 lines (626 loc) · 26.2 KB
/
Copy patharia-role-reference.html
File metadata and controls
691 lines (626 loc) · 26.2 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
<!--
ARIA Role Reference
Single-file client-side reference for common WAI-ARIA roles: purpose,
attributes, semantic HTML alternatives, examples, and best practices.
Built for One File Tools.
-->
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ARIA Role Reference - One File Tools</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link href="https://fonts.googleapis.com/css2?family=Space+Grotesk:wght@600;700&family=Inter:wght@400;500;600&family=JetBrains+Mono:wght@400;500;600&display=swap" rel="stylesheet" />
<style>
:root {
--bg: #030712;
--grid-line: #111827;
--panel: rgba(17, 24, 39, 0.7);
--panel-border: #1f2937;
--accent: #818cf8;
--accent-hover: #6366f1;
--accent-glow: rgba(129, 140, 248, 0.15);
--text: #f9fafb;
--text-muted: #9ca3af;
--text-dim: #4b5563;
--radius-sm: 12px;
--radius-md: 20px;
--font-sans: "Inter", system-ui, sans-serif;
--font-mono: "JetBrains Mono", monospace;
--font-display: "Space Grotesk", sans-serif;
--color-fair: #f59e0b;
--color-strong: #10b981;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: var(--font-sans);
background-color: var(--bg);
background-image: linear-gradient(var(--grid-line) 1px, transparent 1px), linear-gradient(90deg, var(--grid-line) 1px, transparent 1px);
background-size: 32px 32px;
color: var(--text);
padding: 48px 20px 80px;
min-height: 100vh;
display: flex;
justify-content: center;
}
.app {
width: 100%;
max-width: 900px;
}
.hero {
text-align: center;
margin-bottom: 28px;
}
.eyebrow {
font-family: var(--font-mono);
font-size: 12px;
letter-spacing: 0.2em;
color: var(--accent);
text-transform: uppercase;
margin-bottom: 12px;
font-weight: 500;
}
.wordmark {
font-family: var(--font-display);
font-weight: 700;
font-size: clamp(26px, 5vw, 38px);
letter-spacing: -0.02em;
margin-bottom: 12px;
}
.tagline {
color: var(--text-muted);
font-size: 15px;
line-height: 1.5;
max-width: 560px;
margin: 0 auto;
}
.controls {
display: flex;
gap: 10px;
margin-bottom: 20px;
flex-wrap: wrap;
}
.search-input {
flex: 1;
min-width: 200px;
background: var(--panel);
border: 1px solid var(--panel-border);
border-radius: 10px;
color: var(--text);
font-family: var(--font-sans);
font-size: 15px;
padding: 13px 16px;
}
.search-input:focus {
outline: none;
border-color: var(--accent);
box-shadow: 0 0 0 3px var(--accent-glow);
}
.category-select {
background: var(--panel);
border: 1px solid var(--panel-border);
border-radius: 10px;
color: var(--text);
font-family: var(--font-sans);
font-size: 14px;
padding: 13px 14px;
}
.category-select:focus {
outline: none;
border-color: var(--accent);
box-shadow: 0 0 0 3px var(--accent-glow);
}
.layout {
display: grid;
grid-template-columns: 260px 1fr;
gap: 20px;
align-items: start;
}
.role-list {
background: var(--panel);
border: 1px solid var(--panel-border);
border-radius: var(--radius-md);
padding: 10px;
max-height: 560px;
overflow-y: auto;
list-style: none;
}
.role-list li {
margin-bottom: 4px;
}
.role-btn {
width: 100%;
text-align: left;
background: transparent;
border: 1px solid transparent;
border-radius: 8px;
color: var(--text-muted);
font-family: var(--font-mono);
font-size: 13px;
padding: 9px 12px;
cursor: pointer;
transition: all 0.15s;
}
.role-btn:hover {
background: rgba(129, 140, 248, 0.08);
color: var(--text);
}
.role-btn.active {
background: var(--accent);
color: #030712;
font-weight: 600;
}
.role-btn:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 1px;
}
.role-cat {
font-size: 10px;
color: var(--text-dim);
text-transform: uppercase;
letter-spacing: 0.08em;
padding: 12px 12px 6px;
font-family: var(--font-mono);
}
.no-results {
color: var(--text-dim);
font-size: 13px;
padding: 14px 12px;
text-align: center;
}
.detail {
background: var(--panel);
border: 1px solid var(--panel-border);
border-radius: var(--radius-md);
padding: 26px;
min-height: 400px;
}
.detail h2 {
font-family: var(--font-mono);
font-size: 22px;
color: var(--accent);
margin-bottom: 4px;
}
.detail .role-category {
font-family: var(--font-mono);
font-size: 11px;
color: var(--text-dim);
text-transform: uppercase;
letter-spacing: 0.08em;
margin-bottom: 18px;
}
.detail-section {
margin-bottom: 22px;
}
.detail-section h3 {
font-family: var(--font-mono);
font-size: 12px;
color: var(--text-muted);
text-transform: uppercase;
letter-spacing: 0.1em;
margin-bottom: 8px;
}
.detail-section p {
font-size: 15px;
line-height: 1.6;
color: var(--text);
}
.chip-row {
display: flex;
flex-wrap: wrap;
gap: 6px;
}
.chip {
font-family: var(--font-mono);
font-size: 12px;
color: var(--text);
background: rgba(3, 7, 18, 0.5);
border: 1px solid var(--panel-border);
border-radius: 6px;
padding: 5px 9px;
}
.code-block {
position: relative;
background: rgba(3, 7, 18, 0.6);
border: 1px solid var(--panel-border);
border-radius: var(--radius-sm);
padding: 14px 16px;
font-family: var(--font-mono);
font-size: 13px;
line-height: 1.6;
color: #d1d5db;
white-space: pre-wrap;
word-break: break-word;
}
.copy-btn {
position: absolute;
top: 10px;
right: 10px;
background: rgba(31, 41, 55, 0.8);
border: 1px solid var(--panel-border);
color: var(--text-muted);
border-radius: 6px;
padding: 4px 10px;
font-family: var(--font-sans);
font-size: 12px;
cursor: pointer;
}
.copy-btn:hover {
background: rgba(55, 65, 81, 0.9);
color: var(--text);
}
.semantic-note {
background: rgba(16, 185, 129, 0.07);
border: 1px solid rgba(16, 185, 129, 0.3);
border-radius: var(--radius-sm);
padding: 12px 14px;
font-size: 14px;
line-height: 1.55;
}
.semantic-note code {
font-family: var(--font-mono);
color: var(--color-strong);
}
.mistake-list {
list-style: none;
display: flex;
flex-direction: column;
gap: 8px;
}
.mistake-list li {
display: flex;
gap: 8px;
font-size: 14px;
line-height: 1.5;
color: var(--text-muted);
}
.mistake-list li::before {
content: "⚠️";
flex-shrink: 0;
}
.footer-note {
text-align: center;
color: var(--text-dim);
font-size: 13px;
margin-top: 24px;
}
.footer-note a {
color: var(--text-muted);
}
.toast {
position: fixed;
bottom: 24px;
left: 50%;
transform: translateX(-50%) translateY(100px);
background: var(--text);
color: var(--bg);
padding: 12px 22px;
border-radius: 99px;
font-family: var(--font-sans);
font-weight: 600;
font-size: 14px;
opacity: 0;
transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
z-index: 100;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}
.toast.show {
transform: translateX(-50%) translateY(0);
opacity: 1;
}
@media (max-width: 720px) {
.layout {
grid-template-columns: 1fr;
}
.role-list {
max-height: 220px;
}
.detail {
padding: 20px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="hero">
<div class="eyebrow">Accessibility</div>
<h1 class="wordmark">ARIA Role Reference</h1>
<p class="tagline">A fast, offline reference for common WAI-ARIA roles — purpose, key attributes, semantic HTML alternatives, examples, and pitfalls.</p>
</header>
<div class="controls">
<input type="search" class="search-input" id="searchInput" placeholder="Search roles (e.g. button, dialog, tab)…" aria-label="Search ARIA roles" autocomplete="off" />
<select class="category-select" id="categorySelect" aria-label="Filter by category">
<option value="all">All categories</option>
</select>
</div>
<div class="layout">
<ul class="role-list" id="roleList" aria-label="ARIA roles"></ul>
<section class="detail" id="detail" aria-live="polite"></section>
</div>
<p class="footer-note">Part of <a href="https://github.com/praveenscience/One-File-Tools">One File Tools</a>. A curated quick reference — always confirm against the <span>W3C WAI-ARIA spec</span> for edge cases.</p>
</div>
<div class="toast" id="toast" role="status" aria-live="polite"></div>
<script>
/* ==========================================================
ARIA Role Reference
Vanilla JS, no dependencies. Data is a curated subset of the
most commonly used WAI-ARIA roles — not the full spec.
========================================================== */
// Each role: { name, category, purpose, attributes[], semantic, example, mistakes[] }
const ROLES = [
{
name: "button",
category: "Widget",
purpose: "An interactive element that triggers an action when activated (click, Enter, or Space).",
attributes: ["aria-pressed", "aria-expanded", "aria-disabled", "aria-label"],
semantic: "Use the native <button> element instead — it is focusable, keyboard-operable, and announced correctly for free.",
example: '<div role="button" tabindex="0" aria-pressed="false">\n Toggle\n</div>\n\n<!-- Preferred -->\n<button type="button">Toggle</button>',
mistakes: ['Forgetting tabindex="0" so keyboard users can\'t focus it.', "Not handling Enter and Space key events.", "Using it when a real <button> would work."]
},
{
name: "dialog",
category: "Window",
purpose: "A modal or non-modal window overlaid on the page, such as a confirmation or form popup.",
attributes: ["aria-modal", "aria-labelledby", "aria-describedby"],
semantic: "Use the native <dialog> element with showModal() where possible for built-in focus trapping and Esc handling.",
example: '<div role="dialog" aria-modal="true" aria-labelledby="title">\n <h2 id="title">Confirm delete</h2>\n <button>Cancel</button>\n <button>Delete</button>\n</div>',
mistakes: ["Not trapping focus inside the dialog.", "Missing aria-labelledby/aria-label for the dialog name.", "Not returning focus to the trigger on close."]
},
{
name: "alert",
category: "Live Region",
purpose: "An important, time-sensitive message that is announced immediately by assistive tech.",
attributes: ["aria-live (implicit assertive)", "aria-atomic"],
semantic: "No exact native equivalent; use sparingly for genuinely urgent messages.",
example: '<div role="alert">\n Your session will expire in 1 minute.\n</div>',
mistakes: ['Using it for non-urgent status (use role="status" instead).', "Rendering it empty on load then filling it (announce timing issues).", "Overusing alerts, which overwhelms users."]
},
{
name: "status",
category: "Live Region",
purpose: 'A polite live region for advisory, non-urgent status updates (e.g. "Saved").',
attributes: ["aria-live (implicit polite)", "aria-atomic"],
semantic: "The <output> element carries an implicit status role in many contexts.",
example: '<div role="status">\n 3 items added to cart\n</div>',
mistakes: ["Expecting immediate interruption (it waits until idle).", "Toggling display:none, which can suppress announcements."]
},
{
name: "navigation",
category: "Landmark",
purpose: "A section containing navigation links to other pages or parts of the page.",
attributes: ["aria-label", "aria-labelledby"],
semantic: "Use the native <nav> element — it has an implicit navigation role.",
example: '<nav aria-label="Primary">\n <a href="/">Home</a>\n <a href="/about">About</a>\n</nav>',
mistakes: ["Multiple navs without distinct aria-labels.", "Wrapping non-navigation content in it."]
},
{
name: "main",
category: "Landmark",
purpose: "The primary content of the document, unique per page.",
attributes: ["aria-label", "aria-labelledby"],
semantic: "Use the native <main> element — implicit main role.",
example: "<main>\n <h1>Article title</h1>\n <p>…</p>\n</main>",
mistakes: ["Having more than one main landmark per page.", "Putting repeated site chrome (headers/footers) inside it."]
},
{
name: "tab",
category: "Widget",
purpose: "A single tab in a tablist that, when activated, displays its associated tabpanel.",
attributes: ["aria-selected", "aria-controls", "tabindex"],
semantic: 'No native element; must be composed with role="tablist" and role="tabpanel".',
example: '<div role="tablist">\n <button role="tab" aria-selected="true" aria-controls="p1" id="t1">One</button>\n <button role="tab" aria-selected="false" aria-controls="p2" id="t2">Two</button>\n</div>\n<div role="tabpanel" id="p1" aria-labelledby="t1">…</div>',
mistakes: ["Not implementing arrow-key navigation between tabs.", "Missing aria-controls linking the tab to its panel.", "Leaving all tabs in the tab order (use roving tabindex)."]
},
{
name: "tabpanel",
category: "Widget",
purpose: "The content container associated with a tab.",
attributes: ["aria-labelledby", "tabindex"],
semantic: 'No native element; pair with role="tab" and role="tablist".',
example: '<div role="tabpanel" id="p1" aria-labelledby="t1" tabindex="0">\n Panel content\n</div>',
mistakes: ["Not labelling the panel via aria-labelledby pointing to its tab.", "Showing multiple panels at once."]
},
{
name: "checkbox",
category: "Widget",
purpose: "A checkable input with a true/false (or mixed) state.",
attributes: ["aria-checked", "aria-disabled", "tabindex"],
semantic: 'Use <input type="checkbox"> — native state, keyboard, and label association.',
example: '<div role="checkbox" aria-checked="false" tabindex="0">\n Subscribe\n</div>\n\n<!-- Preferred -->\n<label><input type="checkbox"> Subscribe</label>',
mistakes: ["Forgetting to toggle aria-checked in script.", "No Space-key handler.", "Reinventing what the native input gives free."]
},
{
name: "menu",
category: "Widget",
purpose: "A list of choices/actions, typically an application menu (not site navigation).",
attributes: ["aria-labelledby", "aria-orientation"],
semantic: 'No native element; do NOT use for regular site nav links — that\'s role="navigation".',
example: '<ul role="menu">\n <li role="menuitem" tabindex="-1">Cut</li>\n <li role="menuitem" tabindex="-1">Copy</li>\n</ul>',
mistakes: ['Using role="menu" for website navigation bars.', "Missing arrow-key navigation and roving tabindex."]
},
{
name: "tooltip",
category: "Widget",
purpose: "A contextual popup describing an element, shown on hover or focus.",
attributes: ["aria-describedby (on the trigger)"],
semantic: "No native element; the trigger references the tooltip via aria-describedby.",
example: '<button aria-describedby="tip">Help</button>\n<div role="tooltip" id="tip">\n Opens the help center\n</div>',
mistakes: ["Only showing on hover (must also show on keyboard focus).", "Putting interactive content inside the tooltip.", "Not dismissible with Esc."]
},
{
name: "progressbar",
category: "Widget",
purpose: "Displays the progress of a task that takes time to complete.",
attributes: ["aria-valuenow", "aria-valuemin", "aria-valuemax", "aria-label"],
semantic: "Use the native <progress> element where possible.",
example: '<div role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100" aria-label="Upload">\n</div>',
mistakes: ["Not updating aria-valuenow as progress changes.", "Omitting an accessible name.", "Using it for indeterminate spinners without care."]
},
{
name: "search",
category: "Landmark",
purpose: "A landmark region containing search functionality for the site or page.",
attributes: ["aria-label", "aria-labelledby"],
semantic: 'No native element yet; commonly applied to a <form role="search">.',
example: '<form role="search">\n <label for="q">Search</label>\n <input id="q" type="search" />\n</form>',
mistakes: ["Applying it to non-search forms.", "Having multiple search landmarks without labels."]
},
{
name: "banner",
category: "Landmark",
purpose: "Site-oriented header content, typically the masthead (logo, primary heading).",
attributes: ["aria-label"],
semantic: "Use the native <header> at the top level of the page — implicit banner role.",
example: '<header>\n <img src="logo.svg" alt="Acme" />\n</header>',
mistakes: ["Nesting a <header> inside <article>/<section> and expecting banner (it isn't).", "Having more than one banner per page."]
},
{
name: "contentinfo",
category: "Landmark",
purpose: "Footer information about the document (copyright, links, contact).",
attributes: ["aria-label"],
semantic: "Use the native <footer> at the top level — implicit contentinfo role.",
example: "<footer>\n <p>© 2026 Acme</p>\n</footer>",
mistakes: ["Multiple contentinfo landmarks per page.", "Nesting <footer> inside sectioning content expecting the landmark."]
},
{
name: "switch",
category: "Widget",
purpose: "An on/off toggle, like a light switch — a checkbox with on/off rather than checked semantics.",
attributes: ["aria-checked", "tabindex", "aria-label"],
semantic: 'Use <input type="checkbox" role="switch"> to keep native behavior with switch semantics.',
example: '<button role="switch" aria-checked="true">\n Wi-Fi\n</button>',
mistakes: ["Confusing it with checkbox when a simple check is more appropriate.", "Not toggling aria-checked in script."]
},
{
name: "region",
category: "Landmark",
purpose: "A significant, generic section of content that users may want to navigate to.",
attributes: ["aria-label", "aria-labelledby"],
semantic: "A <section> with an accessible name (aria-label/labelledby) exposes an implicit region.",
example: '<section aria-labelledby="h">\n <h2 id="h">Weather</h2>\n</section>',
mistakes: ["Adding region without an accessible name (then it's not a landmark).", "Overusing it, creating landmark noise."]
}
];
const searchInput = document.getElementById("searchInput");
const categorySelect = document.getElementById("categorySelect");
const roleList = document.getElementById("roleList");
const detail = document.getElementById("detail");
const toast = document.getElementById("toast");
let selectedRole = ROLES[0].name;
/** Escapes HTML so code examples render as text, not markup. */
function escapeHtml(str) {
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
}
/** Populates the category dropdown from the role data. */
function populateCategories() {
const cats = Array.from(new Set(ROLES.map((r) => r.category))).sort();
cats.forEach((cat) => {
const opt = document.createElement("option");
opt.value = cat;
opt.textContent = cat;
categorySelect.appendChild(opt);
});
}
/** Returns roles matching the current search + category filters. */
function getFilteredRoles() {
const query = searchInput.value.trim().toLowerCase();
const cat = categorySelect.value;
return ROLES.filter((role) => {
const matchesCat = cat === "all" || role.category === cat;
const matchesQuery = !query || role.name.toLowerCase().includes(query) || role.category.toLowerCase().includes(query) || role.purpose.toLowerCase().includes(query);
return matchesCat && matchesQuery;
});
}
/** Renders the filtered role list, grouped by category. */
function renderList() {
const roles = getFilteredRoles();
roleList.innerHTML = "";
if (roles.length === 0) {
const li = document.createElement("li");
li.className = "no-results";
li.textContent = "No roles match your search.";
roleList.appendChild(li);
return;
}
let lastCat = null;
roles.forEach((role) => {
if (role.category !== lastCat) {
const catLi = document.createElement("li");
catLi.className = "role-cat";
catLi.textContent = role.category;
roleList.appendChild(catLi);
lastCat = role.category;
}
const li = document.createElement("li");
const btn = document.createElement("button");
btn.type = "button";
btn.className = "role-btn" + (role.name === selectedRole ? " active" : "");
btn.textContent = 'role="' + role.name + '"';
btn.addEventListener("click", () => {
selectedRole = role.name;
renderList();
renderDetail();
});
li.appendChild(btn);
roleList.appendChild(li);
});
}
/** Renders the detail panel for the currently selected role. */
function renderDetail() {
const role = ROLES.find((r) => r.name === selectedRole);
if (!role) return;
const attrChips = role.attributes.map((a) => '<span class="chip">' + escapeHtml(a) + "</span>").join("");
const mistakes = role.mistakes.map((m) => "<li>" + escapeHtml(m) + "</li>").join("");
detail.innerHTML = '<h2>role="' + escapeHtml(role.name) + '"</h2>' + '<div class="role-category">' + escapeHtml(role.category) + " role</div>" + '<div class="detail-section"><h3>Purpose</h3><p>' + escapeHtml(role.purpose) + "</p></div>" + '<div class="detail-section"><h3>Common attributes</h3><div class="chip-row">' + attrChips + "</div></div>" + '<div class="detail-section"><h3>Semantic HTML alternative</h3><div class="semantic-note">' + role.semantic.replace(/<([a-z0-9]+)>/gi, "<code><$1></code>").replace(/role="([^"]+)"/g, '<code>role="$1"</code>') + "</div></div>" + '<div class="detail-section"><h3>Example</h3><div class="code-block"><button class="copy-btn" type="button" id="copyExample">Copy</button>' + escapeHtml(role.example) + "</div></div>" + '<div class="detail-section"><h3>Common mistakes</h3><ul class="mistake-list">' + mistakes + "</ul></div>";
const copyBtn = document.getElementById("copyExample");
if (copyBtn) {
copyBtn.addEventListener("click", () => {
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard
.writeText(role.example)
.then(() => showToast("Example copied!"))
.catch(() => showToast("Copy failed — select the text manually"));
} else {
showToast("Copy failed — clipboard unavailable");
}
});
}
}
/** Shows a brief toast message. */
function showToast(message) {
toast.textContent = message;
toast.classList.add("show");
setTimeout(() => toast.classList.remove("show"), 2000);
}
searchInput.addEventListener("input", () => {
const roles = getFilteredRoles();
// Keep the selection in view if it still matches; otherwise jump to the first result.
if (roles.length && !roles.some((r) => r.name === selectedRole)) {
selectedRole = roles[0].name;
renderDetail();
}
renderList();
});
categorySelect.addEventListener("change", () => {
const roles = getFilteredRoles();
if (roles.length && !roles.some((r) => r.name === selectedRole)) {
selectedRole = roles[0].name;
renderDetail();
}
renderList();
});
// ---- Init ----
populateCategories();
renderList();
renderDetail();
</script>
</body>
</html>