@auriel/peppertino 0.1.1 → 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.
Files changed (45) hide show
  1. package/CHANGELOG.txt +136 -0
  2. package/README.md +343 -30
  3. package/build.js +27 -12
  4. package/dist/carousel.js +152 -0
  5. package/dist/carousel.js.map +7 -0
  6. package/dist/header.js +69 -0
  7. package/dist/header.js.map +7 -0
  8. package/dist/main.css +2780 -0
  9. package/dist/main.css.map +1 -0
  10. package/dist/modal.js +112 -0
  11. package/dist/modal.js.map +7 -0
  12. package/dist/peppertino.js +637 -0
  13. package/dist/peppertino.js.map +7 -0
  14. package/dist/select.js +99 -0
  15. package/dist/select.js.map +7 -0
  16. package/dist/tabs.js +79 -0
  17. package/dist/tabs.js.map +7 -0
  18. package/dist/validation.js +130 -0
  19. package/dist/validation.js.map +7 -0
  20. package/index.html +1137 -22
  21. package/package.json +1 -1
  22. package/src/js/carousel.js +148 -0
  23. package/src/js/header.js +43 -4
  24. package/src/js/modal.js +134 -0
  25. package/src/js/peppertino.js +9 -0
  26. package/src/js/select.js +103 -0
  27. package/src/js/tabs.js +94 -0
  28. package/src/js/validation.js +132 -0
  29. package/src/scss/base.scss +26 -6
  30. package/src/scss/button.scss +87 -20
  31. package/src/scss/card.scss +121 -0
  32. package/src/scss/carousel.scss +313 -0
  33. package/src/scss/footer.scss +224 -0
  34. package/src/scss/forms/fieldset.scss +127 -0
  35. package/src/scss/forms/form-instructions.scss +133 -0
  36. package/src/scss/forms/label.scss +115 -0
  37. package/src/scss/forms/text-field.scss +240 -0
  38. package/src/scss/forms/validation.scss +215 -0
  39. package/src/scss/forms.scss +8 -0
  40. package/src/scss/header.scss +27 -6
  41. package/src/scss/main.scss +7 -0
  42. package/src/scss/modal.scss +188 -0
  43. package/src/scss/section.scss +1 -1
  44. package/src/scss/select.scss +210 -0
  45. package/src/scss/tabs.scss +156 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@auriel/peppertino",
3
- "version": "0.1.1",
3
+ "version": "0.3.0",
4
4
  "description": "An Apple-inspired CSS framework with a spicy red accent. Clean geometry, restrained elegance, zero Cupertino lawsuits.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -0,0 +1,148 @@
1
+ // PeppertinoCSS — Carousel
2
+
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
+
12
+ if (!track || !slides.length) return;
13
+
14
+ const total = slides.length;
15
+ let current = 0;
16
+
17
+ if (!root.getAttribute('role')) root.setAttribute('role', 'region');
18
+ if (!root.getAttribute('aria-roledescription')) root.setAttribute('aria-roledescription', 'carousel');
19
+ if (!root.getAttribute('aria-label') && !root.getAttribute('aria-labelledby')) {
20
+ root.setAttribute('aria-label', root.dataset.label || `Carousel ${rootIdx + 1}`);
21
+ }
22
+
23
+ track.setAttribute('aria-live', 'off');
24
+
25
+ slides.forEach((slide, i) => {
26
+ slide.setAttribute('role', 'group');
27
+ if (!slide.getAttribute('aria-roledescription')) slide.setAttribute('aria-roledescription', 'slide');
28
+ if (!slide.getAttribute('aria-label')) slide.setAttribute('aria-label', `${i + 1} of ${total}`);
29
+ });
30
+
31
+ let announcer = root.querySelector('.pp-carousel__announcer');
32
+ if (!announcer) {
33
+ announcer = document.createElement('div');
34
+ announcer.className = 'pp-carousel__announcer';
35
+ root.appendChild(announcer);
36
+ }
37
+ announcer.setAttribute('aria-live', 'polite');
38
+ announcer.setAttribute('aria-atomic', 'true');
39
+
40
+ dots.forEach((dot, i) => {
41
+ if (!dot.id) dot.id = `pp-carousel-${rootIdx}-dot-${i}`;
42
+ if (!dot.getAttribute('aria-label')) dot.setAttribute('aria-label', `Go to slide ${i + 1}`);
43
+ });
44
+
45
+ if (prevBtn && !prevBtn.getAttribute('aria-label')) prevBtn.setAttribute('aria-label', 'Previous slide');
46
+ if (nextBtn && !nextBtn.getAttribute('aria-label')) nextBtn.setAttribute('aria-label', 'Next slide');
47
+
48
+ function syncUI(idx) {
49
+ current = idx;
50
+
51
+ slides.forEach((slide, i) => slide.setAttribute('aria-hidden', String(i !== idx)));
52
+
53
+ dots.forEach((dot, i) => {
54
+ dot.classList.toggle('is-active', i === idx);
55
+ dot.setAttribute('aria-current', String(i === idx));
56
+ dot.setAttribute('tabindex', i === idx ? '0' : '-1');
57
+ });
58
+
59
+ if (prevBtn) {
60
+ prevBtn.disabled = idx === 0;
61
+ prevBtn.setAttribute('aria-disabled', String(idx === 0));
62
+ }
63
+ if (nextBtn) {
64
+ nextBtn.disabled = idx === total - 1;
65
+ nextBtn.setAttribute('aria-disabled', String(idx === total - 1));
66
+ }
67
+
68
+ const label = slides[idx].getAttribute('aria-label') || `Slide ${idx + 1}`;
69
+ announcer.textContent = '';
70
+ requestAnimationFrame(() => { announcer.textContent = label; });
71
+ }
72
+
73
+ const observer = new IntersectionObserver(
74
+ (entries) => {
75
+ entries.forEach((entry) => {
76
+ if (entry.isIntersecting) {
77
+ const idx = slides.indexOf(entry.target);
78
+ if (idx !== -1 && idx !== current) syncUI(idx);
79
+ }
80
+ });
81
+ },
82
+ { root: track, threshold: 0.5 }
83
+ );
84
+ slides.forEach((slide) => observer.observe(slide));
85
+
86
+ function goTo(idx) {
87
+ const clamped = Math.max(0, Math.min(total - 1, idx));
88
+ slides[clamped].scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'start' });
89
+ syncUI(clamped);
90
+ }
91
+
92
+ if (prevBtn) prevBtn.addEventListener('click', () => goTo(current - 1));
93
+ if (nextBtn) nextBtn.addEventListener('click', () => goTo(current + 1));
94
+
95
+ dots.forEach((dot, i) => {
96
+ dot.addEventListener('click', () => goTo(i));
97
+ dot.addEventListener('keydown', (e) => {
98
+ if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
99
+ e.preventDefault(); goTo((i + 1) % total); dots[(i + 1) % total].focus();
100
+ }
101
+ if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
102
+ e.preventDefault(); goTo((i - 1 + total) % total); dots[(i - 1 + total) % total].focus();
103
+ }
104
+ if (e.key === 'Home') { e.preventDefault(); goTo(0); dots[0].focus(); }
105
+ if (e.key === 'End') { e.preventDefault(); goTo(total - 1); dots[total - 1].focus(); }
106
+ });
107
+ });
108
+
109
+ if (autoplayBtn) {
110
+ autoplayBtn.addEventListener('click', () => {
111
+ const playing = root.classList.contains('pp-carousel--playing');
112
+ root.classList.toggle('pp-carousel--playing', !playing);
113
+ root.classList.toggle('pp-carousel--paused', playing);
114
+ autoplayBtn.setAttribute('aria-pressed', String(!playing));
115
+ autoplayBtn.setAttribute('aria-label', playing ? 'Start autoplay' : 'Pause autoplay');
116
+ root.dispatchEvent(new CustomEvent('pp-carousel:autoplay', { bubbles: true, detail: { playing: !playing } }));
117
+ });
118
+ }
119
+
120
+ track.setAttribute('tabindex', '0');
121
+ track.addEventListener('keydown', (e) => {
122
+ if (e.key === 'ArrowRight') { e.preventDefault(); goTo(current + 1); }
123
+ if (e.key === 'ArrowLeft') { e.preventDefault(); goTo(current - 1); }
124
+ if (e.key === 'Home') { e.preventDefault(); goTo(0); }
125
+ if (e.key === 'End') { e.preventDefault(); goTo(total - 1); }
126
+ });
127
+
128
+ syncUI(0);
129
+
130
+ return { goTo, current: () => current, total };
131
+ }
132
+
133
+ const instances = {};
134
+ document.querySelectorAll('.pp-carousel').forEach((el, i) => {
135
+ const id = el.id || `pp-carousel-${i}`;
136
+ const api = initCarousel(el, i);
137
+ if (api) instances[id] = api;
138
+ });
139
+
140
+ window.ppCarousel = {
141
+ get(ref) {
142
+ if (typeof ref === 'string') return instances[ref];
143
+ if (ref instanceof Element) return instances[ref.id] || Object.values(instances).find((_, k) => document.getElementById(Object.keys(instances)[k]) === ref);
144
+ return null;
145
+ },
146
+ instances,
147
+ };
148
+ })();
package/src/js/header.js CHANGED
@@ -3,11 +3,42 @@
3
3
  const toggle = header.querySelector('.pp-header__toggle');
4
4
  const nav = header.querySelector('.pp-header__nav');
5
5
 
6
+ // Signal CSS that JS controls mobile dropdowns
7
+ header.querySelectorAll('.pp-header__dropdown').forEach((d, i) => {
8
+ d.classList.add('is-js-controlled');
9
+ // Ensure each dropdown has an id for aria-controls
10
+ if (!d.id) d.id = `pp-dropdown-${i}`;
11
+ });
12
+
13
+ // Wire up nav-item toggles with aria
14
+ header.querySelectorAll('.pp-header__nav-item').forEach((item) => {
15
+ const dropdown = item.querySelector('.pp-header__dropdown');
16
+ if (!dropdown) return;
17
+
18
+ const link = item.querySelector('.pp-header__nav-link');
19
+ if (link) {
20
+ link.setAttribute('aria-haspopup', 'true');
21
+ link.setAttribute('aria-expanded', 'false');
22
+ if (dropdown.id) link.setAttribute('aria-controls', dropdown.id);
23
+ }
24
+
25
+ dropdown.setAttribute('role', 'menu');
26
+ dropdown.querySelectorAll('.pp-header__dropdown-link').forEach((a) => {
27
+ a.setAttribute('role', 'menuitem');
28
+ });
29
+ });
30
+
6
31
  // Mobile hamburger
7
32
  if (toggle && nav) {
8
- toggle.addEventListener('click', () => {
33
+ if (!nav.id) nav.id = 'pp-header-nav';
34
+ toggle.setAttribute('aria-controls', nav.id);
35
+ nav.setAttribute('aria-hidden', 'true');
36
+
37
+ toggle.addEventListener('click', (e) => {
38
+ e.stopPropagation();
9
39
  const isOpen = nav.classList.toggle('is-open');
10
40
  toggle.setAttribute('aria-expanded', String(isOpen));
41
+ nav.setAttribute('aria-hidden', String(!isOpen));
11
42
  });
12
43
 
13
44
  // Close drawer on outside click
@@ -15,6 +46,7 @@
15
46
  if (!header.contains(e.target)) {
16
47
  nav.classList.remove('is-open');
17
48
  toggle.setAttribute('aria-expanded', 'false');
49
+ nav.setAttribute('aria-hidden', 'true');
18
50
  }
19
51
  });
20
52
  }
@@ -23,20 +55,27 @@
23
55
  header.querySelectorAll('.pp-header__nav-item').forEach((item) => {
24
56
  const dropdown = item.querySelector('.pp-header__dropdown');
25
57
  if (!dropdown) return;
58
+ const link = item.querySelector('.pp-header__nav-link');
26
59
 
27
60
  item.addEventListener('click', (e) => {
28
61
  if (window.innerWidth >= 768) return;
29
62
  e.stopPropagation();
30
- item.classList.toggle('is-open');
63
+ const isOpen = item.classList.toggle('is-open');
64
+ if (link) link.setAttribute('aria-expanded', String(isOpen));
31
65
  });
32
66
  });
33
67
 
34
- // Close mobile dropdowns on resize back to desktop
68
+ // Reset on resize to desktop
35
69
  window.addEventListener('resize', () => {
36
70
  if (window.innerWidth >= 768) {
37
71
  nav?.classList.remove('is-open');
38
72
  toggle?.setAttribute('aria-expanded', 'false');
39
- header.querySelectorAll('.pp-header__nav-item').forEach((i) => i.classList.remove('is-open'));
73
+ nav?.setAttribute('aria-hidden', 'true');
74
+ header.querySelectorAll('.pp-header__nav-item').forEach((i) => {
75
+ i.classList.remove('is-open');
76
+ const link = i.querySelector('.pp-header__nav-link');
77
+ if (link?.hasAttribute('aria-expanded')) link.setAttribute('aria-expanded', 'false');
78
+ });
40
79
  }
41
80
  });
42
81
  }
@@ -0,0 +1,134 @@
1
+ (function () {
2
+ const FOCUSABLE = 'button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
3
+ let lastFocused = null;
4
+
5
+ document.querySelectorAll('.pp-modal-backdrop').forEach((backdrop) => {
6
+ backdrop.classList.add('is-animated');
7
+ backdrop.setAttribute('aria-hidden', 'true');
8
+
9
+ // Role and modal semantics (skip if native <dialog>)
10
+ if (!(backdrop instanceof HTMLDialogElement)) {
11
+ backdrop.setAttribute('role', 'dialog');
12
+ backdrop.setAttribute('aria-modal', 'true');
13
+ }
14
+
15
+ // Wire aria-labelledby to the modal title if present
16
+ const modal = backdrop.querySelector('.pp-modal');
17
+ const title = modal?.querySelector('.pp-modal__title');
18
+ if (title) {
19
+ if (!title.id) title.id = `pp-modal-title-${backdrop.id}`;
20
+ backdrop.setAttribute('aria-labelledby', title.id);
21
+ }
22
+
23
+ // Wire aria-describedby to description if present
24
+ const desc = modal?.querySelector('.pp-modal__description');
25
+ if (desc) {
26
+ if (!desc.id) desc.id = `pp-modal-desc-${backdrop.id}`;
27
+ backdrop.setAttribute('aria-describedby', desc.id);
28
+ }
29
+ });
30
+
31
+ function getFocusable(backdrop) {
32
+ return [...backdrop.querySelectorAll(FOCUSABLE)];
33
+ }
34
+
35
+ function trapFocus(e, backdrop) {
36
+ const focusable = getFocusable(backdrop);
37
+ if (!focusable.length) return;
38
+ const first = focusable[0];
39
+ const last = focusable[focusable.length - 1];
40
+
41
+ if (e.key === 'Tab') {
42
+ if (e.shiftKey) {
43
+ if (document.activeElement === first) { e.preventDefault(); last.focus(); }
44
+ } else {
45
+ if (document.activeElement === last) { e.preventDefault(); first.focus(); }
46
+ }
47
+ }
48
+ }
49
+
50
+ function openModal(id) {
51
+ const backdrop = document.getElementById(id);
52
+ if (!backdrop) return;
53
+
54
+ lastFocused = document.activeElement;
55
+
56
+ if (backdrop instanceof HTMLDialogElement) {
57
+ backdrop.showModal();
58
+ } else {
59
+ backdrop.classList.add('is-open');
60
+ }
61
+
62
+ backdrop.removeAttribute('aria-hidden');
63
+ backdrop.setAttribute('aria-hidden', 'false');
64
+
65
+ const focusable = getFocusable(backdrop);
66
+ if (focusable.length) focusable[0].focus();
67
+
68
+ backdrop._trapFocus = (e) => trapFocus(e, backdrop);
69
+ backdrop.addEventListener('keydown', backdrop._trapFocus);
70
+ }
71
+
72
+ function closeModal(id) {
73
+ const backdrop = document.getElementById(id);
74
+ if (!backdrop) return;
75
+
76
+ if (backdrop instanceof HTMLDialogElement) {
77
+ backdrop.close();
78
+ } else {
79
+ backdrop.classList.remove('is-open');
80
+ }
81
+
82
+ backdrop.setAttribute('aria-hidden', 'true');
83
+
84
+ if (backdrop._trapFocus) {
85
+ backdrop.removeEventListener('keydown', backdrop._trapFocus);
86
+ delete backdrop._trapFocus;
87
+ }
88
+
89
+ // Restore focus to the element that opened the modal
90
+ if (lastFocused && typeof lastFocused.focus === 'function') {
91
+ lastFocused.focus();
92
+ lastFocused = null;
93
+ }
94
+ }
95
+
96
+ function closeAll() {
97
+ document.querySelectorAll('.pp-modal-backdrop').forEach((b) => {
98
+ if (b instanceof HTMLDialogElement) b.close();
99
+ else b.classList.remove('is-open');
100
+ b.setAttribute('aria-hidden', 'true');
101
+ if (b._trapFocus) {
102
+ b.removeEventListener('keydown', b._trapFocus);
103
+ delete b._trapFocus;
104
+ }
105
+ });
106
+ if (lastFocused && typeof lastFocused.focus === 'function') {
107
+ lastFocused.focus();
108
+ lastFocused = null;
109
+ }
110
+ }
111
+
112
+ // Close on backdrop click outside .pp-modal
113
+ document.querySelectorAll('.pp-modal-backdrop').forEach((backdrop) => {
114
+ backdrop.addEventListener('click', (e) => {
115
+ if (e.target === backdrop) closeModal(backdrop.id);
116
+ });
117
+ });
118
+
119
+ // Escape key
120
+ document.addEventListener('keydown', (e) => {
121
+ if (e.key === 'Escape') closeAll();
122
+ });
123
+
124
+ // data-modal-open / data-modal-close triggers
125
+ document.querySelectorAll('[data-modal-open]').forEach((el) => {
126
+ el.addEventListener('click', () => openModal(el.dataset.modalOpen));
127
+ });
128
+
129
+ document.querySelectorAll('[data-modal-close]').forEach((el) => {
130
+ el.addEventListener('click', () => closeModal(el.dataset.modalClose));
131
+ });
132
+
133
+ window.ppModal = { open: openModal, close: closeModal, closeAll };
134
+ })();
@@ -0,0 +1,9 @@
1
+ // PeppertinoCSS — aggregator
2
+ // Imports all JS components so users can include a single <script> tag.
3
+
4
+ import './carousel.js';
5
+ import './header.js';
6
+ import './modal.js';
7
+ import './select.js';
8
+ import './tabs.js';
9
+ import './validation.js';
@@ -0,0 +1,103 @@
1
+ (function () {
2
+ function initSelect(wrapper, idx) {
3
+ const trigger = wrapper.querySelector('.pp-select__trigger');
4
+ const panel = wrapper.querySelector('.pp-select__panel');
5
+ const options = wrapper.querySelectorAll('.pp-select__option');
6
+ const valueEl = wrapper.querySelector('.pp-select__value');
7
+
8
+ if (!trigger || !panel) return;
9
+
10
+ // IDs for aria-controls / aria-activedescendant
11
+ const panelId = panel.id || `pp-select-panel-${idx}`;
12
+ panel.id = panelId;
13
+
14
+ options.forEach((opt, i) => {
15
+ if (!opt.id) opt.id = `pp-select-opt-${idx}-${i}`;
16
+ opt.setAttribute('role', 'option');
17
+ opt.setAttribute('aria-selected', opt.classList.contains('is-selected') ? 'true' : 'false');
18
+ });
19
+
20
+ panel.setAttribute('role', 'listbox');
21
+ trigger.setAttribute('role', 'combobox');
22
+ trigger.setAttribute('aria-haspopup', 'listbox');
23
+ trigger.setAttribute('aria-expanded', 'false');
24
+ trigger.setAttribute('aria-controls', panelId);
25
+
26
+ // Set initial aria-activedescendant to the selected option
27
+ const selected = wrapper.querySelector('.pp-select__option.is-selected');
28
+ if (selected) trigger.setAttribute('aria-activedescendant', selected.id);
29
+
30
+ function open() {
31
+ wrapper.classList.add('is-open');
32
+ trigger.setAttribute('aria-expanded', 'true');
33
+ panel.removeAttribute('hidden');
34
+ }
35
+
36
+ function close() {
37
+ wrapper.classList.remove('is-open');
38
+ trigger.setAttribute('aria-expanded', 'false');
39
+ panel.setAttribute('hidden', '');
40
+ }
41
+
42
+ function toggle() {
43
+ wrapper.classList.contains('is-open') ? close() : open();
44
+ }
45
+
46
+ // Initial state — JS takes over from :focus-within
47
+ panel.setAttribute('hidden', '');
48
+
49
+ trigger.addEventListener('click', (e) => {
50
+ e.stopPropagation();
51
+ toggle();
52
+ });
53
+
54
+ // Option selection
55
+ options.forEach((opt) => {
56
+ opt.addEventListener('click', () => {
57
+ options.forEach((o) => {
58
+ o.classList.remove('is-selected');
59
+ o.setAttribute('aria-selected', 'false');
60
+ });
61
+ opt.classList.add('is-selected');
62
+ opt.setAttribute('aria-selected', 'true');
63
+ trigger.setAttribute('aria-activedescendant', opt.id);
64
+
65
+ if (valueEl) valueEl.textContent = opt.textContent.trim();
66
+
67
+ const nativeSelect = wrapper.querySelector('select[data-pp-select]');
68
+ if (nativeSelect) nativeSelect.value = opt.dataset.value ?? opt.textContent.trim();
69
+
70
+ wrapper.dispatchEvent(new CustomEvent('pp-select:change', {
71
+ bubbles: true,
72
+ detail: { value: opt.dataset.value, label: opt.textContent.trim() },
73
+ }));
74
+
75
+ close();
76
+ trigger.focus();
77
+ });
78
+ });
79
+
80
+ // Keyboard navigation
81
+ trigger.addEventListener('keydown', (e) => {
82
+ if (e.key === 'ArrowDown') { e.preventDefault(); open(); options[0]?.focus(); }
83
+ if (e.key === 'Escape') close();
84
+ });
85
+
86
+ panel.addEventListener('keydown', (e) => {
87
+ const focusable = [...options];
88
+ const idx = focusable.indexOf(document.activeElement);
89
+
90
+ if (e.key === 'ArrowDown') { e.preventDefault(); focusable[idx + 1]?.focus(); }
91
+ if (e.key === 'ArrowUp') { e.preventDefault(); idx > 0 ? focusable[idx - 1]?.focus() : trigger.focus(); }
92
+ if (e.key === 'Escape') { close(); trigger.focus(); }
93
+ if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); document.activeElement?.click(); }
94
+ });
95
+
96
+ // Close on outside click
97
+ document.addEventListener('click', (e) => {
98
+ if (!wrapper.contains(e.target)) close();
99
+ });
100
+ }
101
+
102
+ document.querySelectorAll('.pp-select').forEach((el, i) => initSelect(el, i));
103
+ })();
package/src/js/tabs.js ADDED
@@ -0,0 +1,94 @@
1
+ (function () {
2
+ function initTabs(tabGroup, groupIdx) {
3
+ const items = tabGroup.querySelectorAll('.pp-tabs__item');
4
+ const targetAttr = 'data-tab-target';
5
+
6
+ // Ensure tablist role
7
+ if (!tabGroup.getAttribute('role')) tabGroup.setAttribute('role', 'tablist');
8
+
9
+ // Wire up tab roles and aria-controls
10
+ items.forEach((item, i) => {
11
+ item.setAttribute('role', 'tab');
12
+ if (!item.id) item.id = `pp-tab-${groupIdx}-${i}`;
13
+
14
+ const targetId = item.getAttribute(targetAttr);
15
+ if (targetId) {
16
+ item.setAttribute('aria-controls', targetId);
17
+ const panel = document.querySelector(`[data-tab-panel="${targetId}"]`);
18
+ if (panel) {
19
+ if (!panel.id) panel.id = targetId;
20
+ panel.setAttribute('role', 'tabpanel');
21
+ panel.setAttribute('aria-labelledby', item.id);
22
+ }
23
+ }
24
+ });
25
+
26
+ function activate(item) {
27
+ items.forEach((i) => {
28
+ i.classList.remove('is-active');
29
+ i.setAttribute('aria-selected', 'false');
30
+ i.setAttribute('tabindex', '-1');
31
+ });
32
+
33
+ item.classList.add('is-active');
34
+ item.setAttribute('aria-selected', 'true');
35
+ item.setAttribute('tabindex', '0');
36
+
37
+ // Show/hide associated panels if present
38
+ const targetId = item.getAttribute(targetAttr);
39
+ if (targetId) {
40
+ document.querySelectorAll('[data-tab-panel]').forEach((panel) => {
41
+ const isVisible = panel.getAttribute('data-tab-panel') === targetId;
42
+ panel.hidden = !isVisible;
43
+ panel.setAttribute('aria-hidden', String(!isVisible));
44
+ });
45
+ }
46
+
47
+ tabGroup.dispatchEvent(new CustomEvent('pp-tabs:change', {
48
+ bubbles: true,
49
+ detail: { value: item.dataset.tabTarget, label: item.textContent.trim() },
50
+ }));
51
+ }
52
+
53
+ // Click
54
+ items.forEach((item) => {
55
+ item.addEventListener('click', () => activate(item));
56
+ });
57
+
58
+ // Keyboard: left/right arrow navigation
59
+ tabGroup.addEventListener('keydown', (e) => {
60
+ const current = [...items].findIndex((i) => i === document.activeElement);
61
+ if (current === -1) return;
62
+
63
+ if (e.key === 'ArrowRight') {
64
+ e.preventDefault();
65
+ const next = items[(current + 1) % items.length];
66
+ activate(next);
67
+ next.focus();
68
+ }
69
+ if (e.key === 'ArrowLeft') {
70
+ e.preventDefault();
71
+ const prev = items[(current - 1 + items.length) % items.length];
72
+ activate(prev);
73
+ prev.focus();
74
+ }
75
+ if (e.key === 'Home') {
76
+ e.preventDefault();
77
+ activate(items[0]);
78
+ items[0].focus();
79
+ }
80
+ if (e.key === 'End') {
81
+ e.preventDefault();
82
+ activate(items[items.length - 1]);
83
+ items[items.length - 1].focus();
84
+ }
85
+ });
86
+
87
+ // Set initial tabindex
88
+ items.forEach((item) => {
89
+ item.setAttribute('tabindex', item.classList.contains('is-active') ? '0' : '-1');
90
+ });
91
+ }
92
+
93
+ document.querySelectorAll('.pp-tabs').forEach((el, i) => initTabs(el, i));
94
+ })();