@auriel/peppertino 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,39 +1,217 @@
1
1
  (() => {
2
+ // src/js/carousel.js
3
+ (function() {
4
+ function initCarousel(root, rootIdx) {
5
+ const track = root.querySelector(".pp-carousel__track");
6
+ const slides = [...root.querySelectorAll(".pp-carousel__slide")];
7
+ const dots = [...root.querySelectorAll(".pp-carousel__dot")];
8
+ const prevBtn = root.querySelector(".pp-carousel__prev");
9
+ const nextBtn = root.querySelector(".pp-carousel__next");
10
+ const autoplayBtn = root.querySelector(".pp-carousel__autoplay");
11
+ if (!track || !slides.length) return;
12
+ const total = slides.length;
13
+ let current = 0;
14
+ if (!root.getAttribute("role")) root.setAttribute("role", "region");
15
+ if (!root.getAttribute("aria-roledescription")) root.setAttribute("aria-roledescription", "carousel");
16
+ if (!root.getAttribute("aria-label") && !root.getAttribute("aria-labelledby")) {
17
+ root.setAttribute("aria-label", root.dataset.label || `Carousel ${rootIdx + 1}`);
18
+ }
19
+ track.setAttribute("aria-live", "off");
20
+ slides.forEach((slide, i) => {
21
+ slide.setAttribute("role", "group");
22
+ if (!slide.getAttribute("aria-roledescription")) slide.setAttribute("aria-roledescription", "slide");
23
+ if (!slide.getAttribute("aria-label")) slide.setAttribute("aria-label", `${i + 1} of ${total}`);
24
+ });
25
+ let announcer = root.querySelector(".pp-carousel__announcer");
26
+ if (!announcer) {
27
+ announcer = document.createElement("div");
28
+ announcer.className = "pp-carousel__announcer";
29
+ root.appendChild(announcer);
30
+ }
31
+ announcer.setAttribute("aria-live", "polite");
32
+ announcer.setAttribute("aria-atomic", "true");
33
+ dots.forEach((dot, i) => {
34
+ if (!dot.id) dot.id = `pp-carousel-${rootIdx}-dot-${i}`;
35
+ if (!dot.getAttribute("aria-label")) dot.setAttribute("aria-label", `Go to slide ${i + 1}`);
36
+ });
37
+ if (prevBtn && !prevBtn.getAttribute("aria-label")) prevBtn.setAttribute("aria-label", "Previous slide");
38
+ if (nextBtn && !nextBtn.getAttribute("aria-label")) nextBtn.setAttribute("aria-label", "Next slide");
39
+ function syncUI(idx) {
40
+ current = idx;
41
+ slides.forEach((slide, i) => slide.setAttribute("aria-hidden", String(i !== idx)));
42
+ dots.forEach((dot, i) => {
43
+ dot.classList.toggle("is-active", i === idx);
44
+ dot.setAttribute("aria-current", String(i === idx));
45
+ dot.setAttribute("tabindex", i === idx ? "0" : "-1");
46
+ });
47
+ if (prevBtn) {
48
+ prevBtn.disabled = idx === 0;
49
+ prevBtn.setAttribute("aria-disabled", String(idx === 0));
50
+ }
51
+ if (nextBtn) {
52
+ nextBtn.disabled = idx === total - 1;
53
+ nextBtn.setAttribute("aria-disabled", String(idx === total - 1));
54
+ }
55
+ const label = slides[idx].getAttribute("aria-label") || `Slide ${idx + 1}`;
56
+ announcer.textContent = "";
57
+ requestAnimationFrame(() => {
58
+ announcer.textContent = label;
59
+ });
60
+ }
61
+ const observer = new IntersectionObserver(
62
+ (entries) => {
63
+ entries.forEach((entry) => {
64
+ if (entry.isIntersecting) {
65
+ const idx = slides.indexOf(entry.target);
66
+ if (idx !== -1 && idx !== current) syncUI(idx);
67
+ }
68
+ });
69
+ },
70
+ { root: track, threshold: 0.5 }
71
+ );
72
+ slides.forEach((slide) => observer.observe(slide));
73
+ function goTo(idx) {
74
+ const clamped = Math.max(0, Math.min(total - 1, idx));
75
+ slides[clamped].scrollIntoView({ behavior: "smooth", block: "nearest", inline: "start" });
76
+ syncUI(clamped);
77
+ }
78
+ if (prevBtn) prevBtn.addEventListener("click", () => goTo(current - 1));
79
+ if (nextBtn) nextBtn.addEventListener("click", () => goTo(current + 1));
80
+ dots.forEach((dot, i) => {
81
+ dot.addEventListener("click", () => goTo(i));
82
+ dot.addEventListener("keydown", (e) => {
83
+ if (e.key === "ArrowRight" || e.key === "ArrowDown") {
84
+ e.preventDefault();
85
+ goTo((i + 1) % total);
86
+ dots[(i + 1) % total].focus();
87
+ }
88
+ if (e.key === "ArrowLeft" || e.key === "ArrowUp") {
89
+ e.preventDefault();
90
+ goTo((i - 1 + total) % total);
91
+ dots[(i - 1 + total) % total].focus();
92
+ }
93
+ if (e.key === "Home") {
94
+ e.preventDefault();
95
+ goTo(0);
96
+ dots[0].focus();
97
+ }
98
+ if (e.key === "End") {
99
+ e.preventDefault();
100
+ goTo(total - 1);
101
+ dots[total - 1].focus();
102
+ }
103
+ });
104
+ });
105
+ if (autoplayBtn) {
106
+ autoplayBtn.addEventListener("click", () => {
107
+ const playing = root.classList.contains("pp-carousel--playing");
108
+ root.classList.toggle("pp-carousel--playing", !playing);
109
+ root.classList.toggle("pp-carousel--paused", playing);
110
+ autoplayBtn.setAttribute("aria-pressed", String(!playing));
111
+ autoplayBtn.setAttribute("aria-label", playing ? "Start autoplay" : "Pause autoplay");
112
+ root.dispatchEvent(new CustomEvent("pp-carousel:autoplay", { bubbles: true, detail: { playing: !playing } }));
113
+ });
114
+ }
115
+ track.setAttribute("tabindex", "0");
116
+ track.addEventListener("keydown", (e) => {
117
+ if (e.key === "ArrowRight") {
118
+ e.preventDefault();
119
+ goTo(current + 1);
120
+ }
121
+ if (e.key === "ArrowLeft") {
122
+ e.preventDefault();
123
+ goTo(current - 1);
124
+ }
125
+ if (e.key === "Home") {
126
+ e.preventDefault();
127
+ goTo(0);
128
+ }
129
+ if (e.key === "End") {
130
+ e.preventDefault();
131
+ goTo(total - 1);
132
+ }
133
+ });
134
+ syncUI(0);
135
+ return { goTo, current: () => current, total };
136
+ }
137
+ const instances = {};
138
+ document.querySelectorAll(".pp-carousel").forEach((el, i) => {
139
+ const id = el.id || `pp-carousel-${i}`;
140
+ const api = initCarousel(el, i);
141
+ if (api) instances[id] = api;
142
+ });
143
+ window.ppCarousel = {
144
+ get(ref) {
145
+ if (typeof ref === "string") return instances[ref];
146
+ if (ref instanceof Element) return instances[ref.id] || Object.values(instances).find((_, k) => document.getElementById(Object.keys(instances)[k]) === ref);
147
+ return null;
148
+ },
149
+ instances
150
+ };
151
+ })();
152
+
2
153
  // src/js/header.js
3
154
  (function() {
4
155
  function initHeader(header) {
5
156
  const toggle = header.querySelector(".pp-header__toggle");
6
157
  const nav = header.querySelector(".pp-header__nav");
7
- header.querySelectorAll(".pp-header__dropdown").forEach((d) => {
158
+ header.querySelectorAll(".pp-header__dropdown").forEach((d, i) => {
8
159
  d.classList.add("is-js-controlled");
160
+ if (!d.id) d.id = `pp-dropdown-${i}`;
161
+ });
162
+ header.querySelectorAll(".pp-header__nav-item").forEach((item) => {
163
+ const dropdown = item.querySelector(".pp-header__dropdown");
164
+ if (!dropdown) return;
165
+ const link = item.querySelector(".pp-header__nav-link");
166
+ if (link) {
167
+ link.setAttribute("aria-haspopup", "true");
168
+ link.setAttribute("aria-expanded", "false");
169
+ if (dropdown.id) link.setAttribute("aria-controls", dropdown.id);
170
+ }
171
+ dropdown.setAttribute("role", "menu");
172
+ dropdown.querySelectorAll(".pp-header__dropdown-link").forEach((a) => {
173
+ a.setAttribute("role", "menuitem");
174
+ });
9
175
  });
10
176
  if (toggle && nav) {
177
+ if (!nav.id) nav.id = "pp-header-nav";
178
+ toggle.setAttribute("aria-controls", nav.id);
179
+ nav.setAttribute("aria-hidden", "true");
11
180
  toggle.addEventListener("click", (e) => {
12
181
  e.stopPropagation();
13
182
  const isOpen = nav.classList.toggle("is-open");
14
183
  toggle.setAttribute("aria-expanded", String(isOpen));
184
+ nav.setAttribute("aria-hidden", String(!isOpen));
15
185
  });
16
186
  document.addEventListener("click", (e) => {
17
187
  if (!header.contains(e.target)) {
18
188
  nav.classList.remove("is-open");
19
189
  toggle.setAttribute("aria-expanded", "false");
190
+ nav.setAttribute("aria-hidden", "true");
20
191
  }
21
192
  });
22
193
  }
23
194
  header.querySelectorAll(".pp-header__nav-item").forEach((item) => {
24
195
  const dropdown = item.querySelector(".pp-header__dropdown");
25
196
  if (!dropdown) return;
197
+ const link = item.querySelector(".pp-header__nav-link");
26
198
  item.addEventListener("click", (e) => {
27
199
  if (window.innerWidth >= 768) return;
28
200
  e.stopPropagation();
29
- item.classList.toggle("is-open");
201
+ const isOpen = item.classList.toggle("is-open");
202
+ if (link) link.setAttribute("aria-expanded", String(isOpen));
30
203
  });
31
204
  });
32
205
  window.addEventListener("resize", () => {
33
206
  if (window.innerWidth >= 768) {
34
207
  nav == null ? void 0 : nav.classList.remove("is-open");
35
208
  toggle == null ? void 0 : toggle.setAttribute("aria-expanded", "false");
36
- header.querySelectorAll(".pp-header__nav-item").forEach((i) => i.classList.remove("is-open"));
209
+ nav == null ? void 0 : nav.setAttribute("aria-hidden", "true");
210
+ header.querySelectorAll(".pp-header__nav-item").forEach((i) => {
211
+ i.classList.remove("is-open");
212
+ const link = i.querySelector(".pp-header__nav-link");
213
+ if (link == null ? void 0 : link.hasAttribute("aria-expanded")) link.setAttribute("aria-expanded", "false");
214
+ });
37
215
  }
38
216
  });
39
217
  }
@@ -42,21 +220,64 @@
42
220
 
43
221
  // src/js/modal.js
44
222
  (function() {
223
+ const FOCUSABLE = 'button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
224
+ let lastFocused = null;
45
225
  document.querySelectorAll(".pp-modal-backdrop").forEach((backdrop) => {
46
226
  backdrop.classList.add("is-animated");
47
227
  backdrop.setAttribute("aria-hidden", "true");
228
+ if (!(backdrop instanceof HTMLDialogElement)) {
229
+ backdrop.setAttribute("role", "dialog");
230
+ backdrop.setAttribute("aria-modal", "true");
231
+ }
232
+ const modal = backdrop.querySelector(".pp-modal");
233
+ const title = modal == null ? void 0 : modal.querySelector(".pp-modal__title");
234
+ if (title) {
235
+ if (!title.id) title.id = `pp-modal-title-${backdrop.id}`;
236
+ backdrop.setAttribute("aria-labelledby", title.id);
237
+ }
238
+ const desc = modal == null ? void 0 : modal.querySelector(".pp-modal__description");
239
+ if (desc) {
240
+ if (!desc.id) desc.id = `pp-modal-desc-${backdrop.id}`;
241
+ backdrop.setAttribute("aria-describedby", desc.id);
242
+ }
48
243
  });
244
+ function getFocusable(backdrop) {
245
+ return [...backdrop.querySelectorAll(FOCUSABLE)];
246
+ }
247
+ function trapFocus(e, backdrop) {
248
+ const focusable = getFocusable(backdrop);
249
+ if (!focusable.length) return;
250
+ const first = focusable[0];
251
+ const last = focusable[focusable.length - 1];
252
+ if (e.key === "Tab") {
253
+ if (e.shiftKey) {
254
+ if (document.activeElement === first) {
255
+ e.preventDefault();
256
+ last.focus();
257
+ }
258
+ } else {
259
+ if (document.activeElement === last) {
260
+ e.preventDefault();
261
+ first.focus();
262
+ }
263
+ }
264
+ }
265
+ }
49
266
  function openModal(id) {
50
267
  const backdrop = document.getElementById(id);
51
268
  if (!backdrop) return;
269
+ lastFocused = document.activeElement;
52
270
  if (backdrop instanceof HTMLDialogElement) {
53
271
  backdrop.showModal();
54
272
  } else {
55
273
  backdrop.classList.add("is-open");
56
274
  }
57
275
  backdrop.removeAttribute("aria-hidden");
58
- const firstFocusable = backdrop.querySelector('button, [href], input, [tabindex]:not([tabindex="-1"])');
59
- if (firstFocusable) firstFocusable.focus();
276
+ backdrop.setAttribute("aria-hidden", "false");
277
+ const focusable = getFocusable(backdrop);
278
+ if (focusable.length) focusable[0].focus();
279
+ backdrop._trapFocus = (e) => trapFocus(e, backdrop);
280
+ backdrop.addEventListener("keydown", backdrop._trapFocus);
60
281
  }
61
282
  function closeModal(id) {
62
283
  const backdrop = document.getElementById(id);
@@ -67,13 +288,29 @@
67
288
  backdrop.classList.remove("is-open");
68
289
  }
69
290
  backdrop.setAttribute("aria-hidden", "true");
291
+ if (backdrop._trapFocus) {
292
+ backdrop.removeEventListener("keydown", backdrop._trapFocus);
293
+ delete backdrop._trapFocus;
294
+ }
295
+ if (lastFocused && typeof lastFocused.focus === "function") {
296
+ lastFocused.focus();
297
+ lastFocused = null;
298
+ }
70
299
  }
71
300
  function closeAll() {
72
301
  document.querySelectorAll(".pp-modal-backdrop").forEach((b) => {
73
302
  if (b instanceof HTMLDialogElement) b.close();
74
303
  else b.classList.remove("is-open");
75
304
  b.setAttribute("aria-hidden", "true");
305
+ if (b._trapFocus) {
306
+ b.removeEventListener("keydown", b._trapFocus);
307
+ delete b._trapFocus;
308
+ }
76
309
  });
310
+ if (lastFocused && typeof lastFocused.focus === "function") {
311
+ lastFocused.focus();
312
+ lastFocused = null;
313
+ }
77
314
  }
78
315
  document.querySelectorAll(".pp-modal-backdrop").forEach((backdrop) => {
79
316
  backdrop.addEventListener("click", (e) => {
@@ -94,12 +331,26 @@
94
331
 
95
332
  // src/js/select.js
96
333
  (function() {
97
- function initSelect(wrapper) {
334
+ function initSelect(wrapper, idx) {
98
335
  const trigger = wrapper.querySelector(".pp-select__trigger");
99
336
  const panel = wrapper.querySelector(".pp-select__panel");
100
337
  const options = wrapper.querySelectorAll(".pp-select__option");
101
338
  const valueEl = wrapper.querySelector(".pp-select__value");
102
339
  if (!trigger || !panel) return;
340
+ const panelId = panel.id || `pp-select-panel-${idx}`;
341
+ panel.id = panelId;
342
+ options.forEach((opt, i) => {
343
+ if (!opt.id) opt.id = `pp-select-opt-${idx}-${i}`;
344
+ opt.setAttribute("role", "option");
345
+ opt.setAttribute("aria-selected", opt.classList.contains("is-selected") ? "true" : "false");
346
+ });
347
+ panel.setAttribute("role", "listbox");
348
+ trigger.setAttribute("role", "combobox");
349
+ trigger.setAttribute("aria-haspopup", "listbox");
350
+ trigger.setAttribute("aria-expanded", "false");
351
+ trigger.setAttribute("aria-controls", panelId);
352
+ const selected = wrapper.querySelector(".pp-select__option.is-selected");
353
+ if (selected) trigger.setAttribute("aria-activedescendant", selected.id);
103
354
  function open() {
104
355
  wrapper.classList.add("is-open");
105
356
  trigger.setAttribute("aria-expanded", "true");
@@ -114,8 +365,6 @@
114
365
  wrapper.classList.contains("is-open") ? close() : open();
115
366
  }
116
367
  panel.setAttribute("hidden", "");
117
- trigger.setAttribute("aria-expanded", "false");
118
- trigger.setAttribute("aria-haspopup", "listbox");
119
368
  trigger.addEventListener("click", (e) => {
120
369
  e.stopPropagation();
121
370
  toggle();
@@ -123,8 +372,13 @@
123
372
  options.forEach((opt) => {
124
373
  opt.addEventListener("click", () => {
125
374
  var _a;
126
- options.forEach((o) => o.classList.remove("is-selected"));
375
+ options.forEach((o) => {
376
+ o.classList.remove("is-selected");
377
+ o.setAttribute("aria-selected", "false");
378
+ });
127
379
  opt.classList.add("is-selected");
380
+ opt.setAttribute("aria-selected", "true");
381
+ trigger.setAttribute("aria-activedescendant", opt.id);
128
382
  if (valueEl) valueEl.textContent = opt.textContent.trim();
129
383
  const nativeSelect = wrapper.querySelector("select[data-pp-select]");
130
384
  if (nativeSelect) nativeSelect.value = (_a = opt.dataset.value) != null ? _a : opt.textContent.trim();
@@ -148,14 +402,14 @@
148
402
  panel.addEventListener("keydown", (e) => {
149
403
  var _a, _b, _c;
150
404
  const focusable = [...options];
151
- const idx = focusable.indexOf(document.activeElement);
405
+ const idx2 = focusable.indexOf(document.activeElement);
152
406
  if (e.key === "ArrowDown") {
153
407
  e.preventDefault();
154
- (_a = focusable[idx + 1]) == null ? void 0 : _a.focus();
408
+ (_a = focusable[idx2 + 1]) == null ? void 0 : _a.focus();
155
409
  }
156
410
  if (e.key === "ArrowUp") {
157
411
  e.preventDefault();
158
- idx > 0 ? (_b = focusable[idx - 1]) == null ? void 0 : _b.focus() : trigger.focus();
412
+ idx2 > 0 ? (_b = focusable[idx2 - 1]) == null ? void 0 : _b.focus() : trigger.focus();
159
413
  }
160
414
  if (e.key === "Escape") {
161
415
  close();
@@ -170,14 +424,29 @@
170
424
  if (!wrapper.contains(e.target)) close();
171
425
  });
172
426
  }
173
- document.querySelectorAll(".pp-select").forEach(initSelect);
427
+ document.querySelectorAll(".pp-select").forEach((el, i) => initSelect(el, i));
174
428
  })();
175
429
 
176
430
  // src/js/tabs.js
177
431
  (function() {
178
- function initTabs(tabGroup) {
432
+ function initTabs(tabGroup, groupIdx) {
179
433
  const items = tabGroup.querySelectorAll(".pp-tabs__item");
180
434
  const targetAttr = "data-tab-target";
435
+ if (!tabGroup.getAttribute("role")) tabGroup.setAttribute("role", "tablist");
436
+ items.forEach((item, i) => {
437
+ item.setAttribute("role", "tab");
438
+ if (!item.id) item.id = `pp-tab-${groupIdx}-${i}`;
439
+ const targetId = item.getAttribute(targetAttr);
440
+ if (targetId) {
441
+ item.setAttribute("aria-controls", targetId);
442
+ const panel = document.querySelector(`[data-tab-panel="${targetId}"]`);
443
+ if (panel) {
444
+ if (!panel.id) panel.id = targetId;
445
+ panel.setAttribute("role", "tabpanel");
446
+ panel.setAttribute("aria-labelledby", item.id);
447
+ }
448
+ }
449
+ });
181
450
  function activate(item) {
182
451
  items.forEach((i) => {
183
452
  i.classList.remove("is-active");
@@ -190,7 +459,9 @@
190
459
  const targetId = item.getAttribute(targetAttr);
191
460
  if (targetId) {
192
461
  document.querySelectorAll("[data-tab-panel]").forEach((panel) => {
193
- panel.hidden = panel.getAttribute("data-tab-panel") !== targetId;
462
+ const isVisible = panel.getAttribute("data-tab-panel") === targetId;
463
+ panel.hidden = !isVisible;
464
+ panel.setAttribute("aria-hidden", String(!isVisible));
194
465
  });
195
466
  }
196
467
  tabGroup.dispatchEvent(new CustomEvent("pp-tabs:change", {
@@ -216,12 +487,151 @@
216
487
  activate(prev);
217
488
  prev.focus();
218
489
  }
490
+ if (e.key === "Home") {
491
+ e.preventDefault();
492
+ activate(items[0]);
493
+ items[0].focus();
494
+ }
495
+ if (e.key === "End") {
496
+ e.preventDefault();
497
+ activate(items[items.length - 1]);
498
+ items[items.length - 1].focus();
499
+ }
219
500
  });
220
- items.forEach((item, i) => {
501
+ items.forEach((item) => {
221
502
  item.setAttribute("tabindex", item.classList.contains("is-active") ? "0" : "-1");
222
503
  });
223
504
  }
224
- document.querySelectorAll(".pp-tabs").forEach(initTabs);
505
+ document.querySelectorAll(".pp-tabs").forEach((el, i) => initTabs(el, i));
506
+ })();
507
+
508
+ // src/js/validation.js
509
+ (function() {
510
+ function getField(input) {
511
+ return input.closest(".pp-field");
512
+ }
513
+ function getMessageEl(field) {
514
+ return field ? field.querySelector(".pp-field__message") : null;
515
+ }
516
+ function getHintEl(field) {
517
+ return field ? field.querySelector(".pp-field__hint") : null;
518
+ }
519
+ function buildDescribedBy(field) {
520
+ const ids = [];
521
+ const hint = getHintEl(field);
522
+ const msg = getMessageEl(field);
523
+ if (hint && hint.id) ids.push(hint.id);
524
+ if (msg && msg.id) ids.push(msg.id);
525
+ return ids.join(" ");
526
+ }
527
+ function ensureIds(field, input) {
528
+ const base = input.id || `pp-field-${Math.random().toString(36).slice(2, 7)}`;
529
+ const hint = getHintEl(field);
530
+ const msg = getMessageEl(field);
531
+ if (hint && !hint.id) hint.id = `${base}-hint`;
532
+ if (msg && !msg.id) msg.id = `${base}-msg`;
533
+ }
534
+ function setError(input, message) {
535
+ const field = getField(input);
536
+ if (!field) return;
537
+ ensureIds(field, input);
538
+ field.classList.remove("pp-field--success", "pp-field--warning", "pp-field--validating");
539
+ field.classList.add("pp-field--error");
540
+ input.setAttribute("aria-invalid", "true");
541
+ let msg = getMessageEl(field);
542
+ if (!msg) {
543
+ msg = document.createElement("span");
544
+ msg.className = "pp-field__message";
545
+ msg.id = `${input.id || "field"}-msg`;
546
+ msg.setAttribute("role", "alert");
547
+ field.appendChild(msg);
548
+ }
549
+ msg.textContent = message;
550
+ input.setAttribute("aria-describedby", buildDescribedBy(field));
551
+ field.dispatchEvent(new CustomEvent("pp-field:error", { bubbles: true, detail: { input, message } }));
552
+ }
553
+ function clearError(input) {
554
+ const field = getField(input);
555
+ if (!field) return;
556
+ field.classList.remove("pp-field--error", "pp-field--validating");
557
+ input.setAttribute("aria-invalid", "false");
558
+ const msg = getMessageEl(field);
559
+ if (msg) msg.textContent = "";
560
+ input.setAttribute("aria-describedby", buildDescribedBy(field));
561
+ field.dispatchEvent(new CustomEvent("pp-field:clear", { bubbles: true, detail: { input } }));
562
+ }
563
+ function setSuccess(input, message) {
564
+ const field = getField(input);
565
+ if (!field) return;
566
+ ensureIds(field, input);
567
+ field.classList.remove("pp-field--error", "pp-field--warning", "pp-field--validating");
568
+ field.classList.add("pp-field--success");
569
+ input.setAttribute("aria-invalid", "false");
570
+ let msg = getMessageEl(field);
571
+ if (message) {
572
+ if (!msg) {
573
+ msg = document.createElement("span");
574
+ msg.className = "pp-field__message";
575
+ msg.id = `${input.id || "field"}-msg`;
576
+ field.appendChild(msg);
577
+ }
578
+ msg.textContent = message;
579
+ }
580
+ input.setAttribute("aria-describedby", buildDescribedBy(field));
581
+ }
582
+ function setValidating(input) {
583
+ const field = getField(input);
584
+ if (!field) return;
585
+ field.classList.remove("pp-field--error", "pp-field--success", "pp-field--warning");
586
+ field.classList.add("pp-field--validating");
587
+ input.removeAttribute("aria-invalid");
588
+ }
589
+ function setSummary(errors, summaryEl) {
590
+ if (!summaryEl) return;
591
+ const list = summaryEl.querySelector(".pp-validation-summary__list");
592
+ if (!list) return;
593
+ list.innerHTML = "";
594
+ if (!errors || errors.length === 0) {
595
+ summaryEl.classList.remove("is-visible");
596
+ summaryEl.setAttribute("aria-hidden", "true");
597
+ return;
598
+ }
599
+ errors.forEach(({ input, message }) => {
600
+ const inputEl = typeof input === "string" ? document.getElementById(input) : input;
601
+ const li = document.createElement("li");
602
+ li.className = "pp-validation-summary__item";
603
+ if (inputEl && inputEl.id) {
604
+ const a = document.createElement("a");
605
+ a.href = `#${inputEl.id}`;
606
+ a.textContent = message;
607
+ a.addEventListener("click", (e) => {
608
+ e.preventDefault();
609
+ inputEl.focus();
610
+ inputEl.scrollIntoView({ behavior: "smooth", block: "center" });
611
+ });
612
+ li.appendChild(a);
613
+ } else {
614
+ li.textContent = message;
615
+ }
616
+ list.appendChild(li);
617
+ });
618
+ summaryEl.classList.add("is-visible");
619
+ summaryEl.setAttribute("aria-hidden", "false");
620
+ summaryEl.setAttribute("tabindex", "-1");
621
+ summaryEl.focus();
622
+ }
623
+ document.querySelectorAll(".pp-field").forEach((field) => {
624
+ const input = field.querySelector(".pp-field__input, .pp-field__textarea");
625
+ if (!input) return;
626
+ ensureIds(field, input);
627
+ const refs = buildDescribedBy(field);
628
+ if (refs) {
629
+ const existing = input.getAttribute("aria-describedby") || "";
630
+ const merged = [...new Set([...existing.split(" "), ...refs.split(" ")].filter(Boolean))].join(" ");
631
+ input.setAttribute("aria-describedby", merged);
632
+ }
633
+ });
634
+ window.ppValidation = { setError, clearError, setSuccess, setValidating, setSummary };
225
635
  })();
226
636
  })();
227
637
  //# sourceMappingURL=peppertino.js.map