basecoat-cli 0.1.0 → 0.2.0-beta.1

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,150 +1,199 @@
1
- window.basecoat = window.basecoat || {};
2
- window.basecoat.registerSelect = function(Alpine) {
3
- if (Alpine.components && Alpine.components.select) return;
4
-
5
- Alpine.data('select', (name = null, initialValue = null) => ({
6
- open: false,
7
- name: null,
8
- options: [],
9
- disabledOptions: [],
10
- focusedIndex: null,
11
- selectedLabel: null,
12
- selectedValue: null,
13
- query: '',
1
+ (() => {
2
+ const initSelect = (selectComponent) => {
3
+ const trigger = selectComponent.querySelector(':scope > [popovertarget]');
4
+ const selectedValue = trigger.querySelector(':scope > span');
5
+ const popover = selectComponent.querySelector(':scope > [popover]');
6
+ const listbox = popover.querySelector('[role="listbox"]');
7
+ const input = selectComponent.querySelector(':scope > input[type="hidden"]');
8
+ const filter = selectComponent.querySelector('header input[type="text"]');
9
+ if (!trigger || !popover || !listbox || !input) return;
14
10
 
15
- init() {
16
- this.$nextTick(() => {
17
- if (name) this.name = name;
18
- this.options = Array.from(this.$el.querySelectorAll('[role=option]:not([aria-disabled])'));
19
- this.disabledOptions = Array.from(this.$el.querySelectorAll('[role=option][aria-disabled=true]'));
20
- if (this.options.length === 0) return;
21
- if (initialValue) {
22
- const option = this.options.find(opt => opt.getAttribute('data-value') === initialValue);
23
- this.selectOption(option, false);
24
- this.focusedIndex = this.options.indexOf(option);
25
- } else {
26
- this.selectOption(this.options[0], false);
27
- }
28
- });
29
- },
30
- focusOption() {
31
- if (this.options.length === 0) return;
32
-
33
- if (this.focusedIndex >= this.options.length) {
34
- this.focusedIndex = this.options.length - 1;
35
- } else if (this.focusedIndex < 0 || this.focusedIndex === null) {
36
- this.focusedIndex = 0;
11
+ const options = Array.from(listbox.querySelectorAll('[role="option"]'));
12
+ let visibleOptions = [...options];
13
+ let activeIndex = -1;
14
+
15
+ const updateValue = (option) => {
16
+ if (option) {
17
+ selectedValue.innerHTML = option.dataset.label || option.innerHTML;
18
+ input.value = option.dataset.value;
19
+ listbox.querySelector('[role="option"][aria-selected="true"]')?.removeAttribute('aria-selected');
20
+ option.setAttribute('aria-selected', 'true');
37
21
  }
38
- this.options.forEach(opt => opt.removeAttribute('data-focus'));
39
- const option = this.options[this.focusedIndex];
40
- option.setAttribute('data-focus', '');
41
- option.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
42
- },
43
- focusOnSelectedOption() {
44
- if (this.options.length === 0 || this.selectedValue === null) return;
45
- const option = this.options.find(opt => opt.getAttribute('data-value') === this.selectedValue);
22
+ };
23
+
24
+ const selectOption = (option) => {
46
25
  if (!option) return;
47
- this.focusedIndex = this.options.indexOf(option);
48
- this.focusOption();
49
- },
50
- moveOptionFocus(delta) {
51
- if (this.options.length === 0) return;
52
-
53
- if (!this.open) {
54
- this.open = true;
55
- this.focusOnSelectedOption();
56
- } else {
57
- this.focusedIndex = this.focusedIndex === null
58
- ? 0
59
- : this.focusedIndex + delta;
60
- }
61
- this.focusOption();
62
- },
63
- handleOptionClick(event) {
64
- const option = event.target.closest('[role=option]');
65
- if (option && option.getAttribute('aria-disabled') !== 'true') {
66
- this.selectOption(option);
67
- this.open = false;
68
- this.$nextTick(() => this.$refs.trigger.focus());
26
+
27
+ updateValue(option);
28
+
29
+ trigger.removeAttribute('aria-activedescendant');
30
+ options.forEach(opt => opt.classList.remove('active'));
31
+ activeIndex = -1;
32
+ popover.hidePopover();
33
+ };
34
+
35
+ if (filter) {
36
+ const filterOptions = () => {
37
+ const searchTerm = filter.value.trim().toLowerCase();
38
+
39
+ if (activeIndex > -1) {
40
+ options[activeIndex].classList.remove('active');
41
+ trigger.removeAttribute('aria-activedescendant');
42
+ activeIndex = -1;
43
+ }
44
+
45
+ visibleOptions = [];
46
+ options.forEach(option => {
47
+ const optionText = (option.dataset.label || option.textContent).trim().toLowerCase();
48
+ const matches = optionText.includes(searchTerm);
49
+ option.setAttribute('aria-hidden', String(!matches));
50
+ if (matches) {
51
+ visibleOptions.push(option);
52
+ }
53
+ });
54
+ };
55
+
56
+ filter.addEventListener('input', filterOptions);
57
+ }
58
+
59
+ let initialOption = options.find(opt => input.value && opt.dataset.value === input.value);
60
+ if (!initialOption && options.length > 0) initialOption = options[0];
61
+
62
+ updateValue(initialOption);
63
+
64
+ const handleKeyNavigation = (e) => {
65
+ if (!['ArrowDown', 'ArrowUp', 'Enter', 'Home', 'End'].includes(e.key)) {
66
+ return;
69
67
  }
70
- },
71
- handleOptionMousemove(event) {
72
- const option = event.target.closest('[role=option]');
73
- if (option && option.getAttribute('aria-disabled') !== 'true') {
74
- this.focusedIndex = this.options.indexOf(option);
75
- this.focusOption();
68
+
69
+ if (!popover.matches(':popover-open')) {
70
+ if (e.currentTarget === trigger && e.key !== 'Enter') {
71
+ e.preventDefault();
72
+ trigger.click();
73
+ }
74
+ return;
76
75
  }
77
- },
78
- handleOptionEnter(event) {
79
- this.selectOption(this.options[this.focusedIndex]);
80
- this.open = !this.open;
81
- },
82
- selectOption(option, dispatch = true) {
83
- if (this.options.length === 0 || !option || option.disabled || option.getAttribute('data-value') == null) {
76
+
77
+ e.preventDefault();
78
+
79
+ if (e.key === 'Enter') {
80
+ if (activeIndex > -1) {
81
+ selectOption(options[activeIndex]);
82
+ }
84
83
  return;
85
84
  }
86
85
 
87
- this.options.forEach(opt => {
88
- opt.setAttribute('aria-selected', opt === option);
89
- });
90
- this.selectedLabel = option.innerHTML;
91
- this.selectedValue = option.getAttribute('data-value');
92
- if (dispatch) {
93
- this.$dispatch('select:change', {
94
- value: this.selectedValue,
95
- label: this.selectedLabel
96
- });
97
- this.$dispatch('change');
86
+ if (visibleOptions.length === 0) return;
87
+
88
+ const currentVisibleIndex = activeIndex > -1 ? visibleOptions.indexOf(options[activeIndex]) : -1;
89
+ let nextVisibleIndex = currentVisibleIndex;
90
+
91
+ switch (e.key) {
92
+ case 'ArrowDown':
93
+ if (currentVisibleIndex < visibleOptions.length - 1) {
94
+ nextVisibleIndex = currentVisibleIndex + 1;
95
+ }
96
+ break;
97
+ case 'ArrowUp':
98
+ if (currentVisibleIndex > 0) {
99
+ nextVisibleIndex = currentVisibleIndex - 1;
100
+ } else if (currentVisibleIndex === -1) {
101
+ nextVisibleIndex = 0; // Start from top if nothing is active
102
+ }
103
+ break;
104
+ case 'Home':
105
+ nextVisibleIndex = 0;
106
+ break;
107
+ case 'End':
108
+ nextVisibleIndex = visibleOptions.length - 1;
109
+ break;
110
+ }
111
+
112
+ if (nextVisibleIndex !== currentVisibleIndex) {
113
+ if (currentVisibleIndex > -1) {
114
+ visibleOptions[currentVisibleIndex].classList.remove('active');
115
+ }
116
+
117
+ const newActiveOption = visibleOptions[nextVisibleIndex];
118
+ newActiveOption.classList.add('active');
119
+ activeIndex = options.indexOf(newActiveOption);
120
+
121
+ if (newActiveOption.id) {
122
+ trigger.setAttribute('aria-activedescendant', newActiveOption.id);
123
+ }
124
+ newActiveOption.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
125
+ }
126
+ };
127
+
128
+ trigger.addEventListener('keydown', handleKeyNavigation);
129
+ if (filter) {
130
+ filter.addEventListener('keydown', handleKeyNavigation);
131
+ }
132
+
133
+ listbox.addEventListener('click', (e) => {
134
+ const clickedOption = e.target.closest('[role="option"]');
135
+ if (clickedOption) {
136
+ selectOption(clickedOption);
98
137
  }
99
- },
100
- filterOptions(query) {
101
- if (query.length > 0) {
102
- this.disabledOptions.forEach(opt => { opt.setAttribute('aria-hidden', 'true'); });
103
- } else {
104
- this.disabledOptions.forEach(opt => { opt.removeAttribute('aria-hidden'); });
138
+ });
139
+
140
+ popover.addEventListener('toggle', (e) => {
141
+ trigger.setAttribute('aria-expanded', e.newState === 'open');
142
+
143
+ if (e.newState === 'open') {
144
+ if (filter) filter.focus();
145
+
146
+ const selectedOption = listbox.querySelector('[role="option"][aria-selected="true"]');
147
+ let startingOption = null;
148
+
149
+ if (selectedOption && visibleOptions.includes(selectedOption)) {
150
+ startingOption = selectedOption;
151
+ } else if (visibleOptions.length > 0) {
152
+ startingOption = visibleOptions[0];
153
+ }
154
+
155
+ if (activeIndex > -1) options[activeIndex]?.classList.remove('active');
156
+
157
+ if (startingOption) {
158
+ activeIndex = options.indexOf(startingOption);
159
+ startingOption.classList.add('active');
160
+ if (startingOption.id) {
161
+ trigger.setAttribute('aria-activedescendant', startingOption.id);
162
+ }
163
+ startingOption.scrollIntoView({ block: 'nearest' });
164
+ } else {
165
+ activeIndex = -1;
166
+ }
167
+ } else if (e.newState === 'closed') {
168
+ if (filter) {
169
+ filter.value = '';
170
+ visibleOptions = [...options];
171
+ options.forEach(opt => opt.setAttribute('aria-hidden', 'false'));
172
+ }
173
+
174
+ trigger.removeAttribute('aria-activedescendant');
175
+ if (activeIndex > -1) options[activeIndex]?.classList.remove('active');
176
+ activeIndex = -1;
105
177
  }
106
- this.options.forEach(opt => {
107
- opt.removeAttribute('aria-hidden');
108
- if (opt.getAttribute('data-value') != null && !opt.innerHTML.toLowerCase().includes(query.toLowerCase())) {
109
- opt.setAttribute('aria-hidden', 'true');
178
+ });
179
+
180
+ selectComponent.dataset.selectInitialized = true;
181
+ };
182
+
183
+ document.querySelectorAll('div.select:not([data-select-initialized])').forEach(initSelect);
184
+
185
+ const observer = new MutationObserver((mutations) => {
186
+ mutations.forEach((mutation) => {
187
+ mutation.addedNodes.forEach((node) => {
188
+ if (node.nodeType === Node.ELEMENT_NODE) {
189
+ if (node.matches('div.select:not([data-select-initialized])')) {
190
+ initSelect(node);
191
+ }
192
+ node.querySelectorAll('div.select:not([data-select-initialized])').forEach(initSelect);
110
193
  }
111
194
  });
112
- },
113
-
114
- $trigger: {
115
- '@click'() { this.open = !this.open; this.focusOnSelectedOption(); },
116
- '@keydown.escape.prevent'() {
117
- this.open = false;
118
- this.$refs.trigger.focus();
119
- },
120
- '@keydown.down.prevent'() { this.moveOptionFocus(+1); },
121
- '@keydown.up.prevent'() { this.moveOptionFocus(-1); },
122
- '@keydown.home.prevent'() { this.focusOption(0) },
123
- '@keydown.end.prevent'() { this.focusOption(this.options.length - 1) },
124
- '@keydown.enter.prevent'() {
125
- if (this.open) this.handleOptionEnter();
126
- else this.open = true;
127
- },
128
- ':aria-expanded'() { return this.open },
129
- 'x-ref': 'trigger'
130
- },
131
- $content: {
132
- '@click'(e) { this.handleOptionClick(e) },
133
- '@mouseover'(e) { this.handleOptionMousemove(e) },
134
- ':aria-hidden'() { return !this.open },
135
- 'x-cloak': ''
136
- },
137
- $filter: {
138
- '@input'(e) { this.filterOptions(e.target.value) },
139
- '@keydown.escape.prevent'() {
140
- this.open = false;
141
- this.$refs.trigger.focus();
142
- },
143
- '@keydown.down.prevent'() { this.moveOptionFocus(+1); },
144
- '@keydown.up.prevent'() { this.moveOptionFocus(-1); },
145
- '@keydown.home.prevent'() { this.focusOption(0) },
146
- '@keydown.end.prevent'() { this.focusOption(this.options.length - 1) },
147
- '@keydown.enter.prevent'() { this.handleOptionEnter() },
148
- }
149
- }));
150
- };
195
+ });
196
+ });
197
+
198
+ observer.observe(document.body, { childList: true, subtree: true });
199
+ })();
@@ -0,0 +1 @@
1
+ (()=>{const e=e=>{const t=e.querySelector(":scope > [popovertarget]"),r=t.querySelector(":scope > span"),a=e.querySelector(":scope > [popover]"),i=a.querySelector('[role="listbox"]'),o=e.querySelector(':scope > input[type="hidden"]'),n=e.querySelector('header input[type="text"]');if(!(t&&a&&i&&o))return;const s=Array.from(i.querySelectorAll('[role="option"]'));let c=[...s],l=-1;const d=e=>{e&&(r.innerHTML=e.dataset.label||e.innerHTML,o.value=e.dataset.value,i.querySelector('[role="option"][aria-selected="true"]')?.removeAttribute("aria-selected"),e.setAttribute("aria-selected","true"))},u=e=>{e&&(d(e),t.removeAttribute("aria-activedescendant"),s.forEach((e=>e.classList.remove("active"))),l=-1,a.hidePopover())};if(n){const e=()=>{const e=n.value.trim().toLowerCase();l>-1&&(s[l].classList.remove("active"),t.removeAttribute("aria-activedescendant"),l=-1),c=[],s.forEach((t=>{const r=(t.dataset.label||t.textContent).trim().toLowerCase().includes(e);t.setAttribute("aria-hidden",String(!r)),r&&c.push(t)}))};n.addEventListener("input",e)}let v=s.find((e=>o.value&&e.dataset.value===o.value));!v&&s.length>0&&(v=s[0]),d(v);const p=e=>{if(!["ArrowDown","ArrowUp","Enter","Home","End"].includes(e.key))return;if(!a.matches(":popover-open"))return void(e.currentTarget===t&&"Enter"!==e.key&&(e.preventDefault(),t.click()));if(e.preventDefault(),"Enter"===e.key)return void(l>-1&&u(s[l]));if(0===c.length)return;const r=l>-1?c.indexOf(s[l]):-1;let i=r;switch(e.key){case"ArrowDown":r<c.length-1&&(i=r+1);break;case"ArrowUp":r>0?i=r-1:-1===r&&(i=0);break;case"Home":i=0;break;case"End":i=c.length-1}if(i!==r){r>-1&&c[r].classList.remove("active");const e=c[i];e.classList.add("active"),l=s.indexOf(e),e.id&&t.setAttribute("aria-activedescendant",e.id),e.scrollIntoView({block:"nearest",behavior:"smooth"})}};t.addEventListener("keydown",p),n&&n.addEventListener("keydown",p),i.addEventListener("click",(e=>{const t=e.target.closest('[role="option"]');t&&u(t)})),a.addEventListener("toggle",(e=>{if(t.setAttribute("aria-expanded","open"===e.newState),"open"===e.newState){n&&n.focus();const e=i.querySelector('[role="option"][aria-selected="true"]');let r=null;e&&c.includes(e)?r=e:c.length>0&&(r=c[0]),l>-1&&s[l]?.classList.remove("active"),r?(l=s.indexOf(r),r.classList.add("active"),r.id&&t.setAttribute("aria-activedescendant",r.id),r.scrollIntoView({block:"nearest"})):l=-1}else"closed"===e.newState&&(n&&(n.value="",c=[...s],s.forEach((e=>e.setAttribute("aria-hidden","false")))),t.removeAttribute("aria-activedescendant"),l>-1&&s[l]?.classList.remove("active"),l=-1)})),e.dataset.selectInitialized=!0};document.querySelectorAll("div.select:not([data-select-initialized])").forEach(e);new MutationObserver((t=>{t.forEach((t=>{t.addedNodes.forEach((t=>{t.nodeType===Node.ELEMENT_NODE&&(t.matches("div.select:not([data-select-initialized])")&&e(t),t.querySelectorAll("div.select:not([data-select-initialized])").forEach(e))}))}))})).observe(document.body,{childList:!0,subtree:!0})})();
@@ -1,25 +1,110 @@
1
- window.basecoat = window.basecoat || {};
2
- window.basecoat.registerSidebar= function(Alpine) {
3
- if (Alpine.components && Alpine.components.sidebar) return;
4
-
5
- Alpine.data('sidebar', (initialOpen = true, initialMobileOpen = false) => ({
6
- open: window.innerWidth >= 768
7
- ? initialOpen
8
- : initialMobileOpen,
9
-
10
- init() {
11
- this.$nextTick(() => {
12
- this.$el.removeAttribute('data-uninitialized');
1
+ (() => {
2
+ // Monkey patching the history API to detect client-side navigation
3
+ if (!window.history.__basecoatPatched) {
4
+ const originalPushState = window.history.pushState;
5
+ window.history.pushState = function(...args) {
6
+ originalPushState.apply(this, args);
7
+ window.dispatchEvent(new Event('basecoat:locationchange'));
8
+ };
9
+
10
+ const originalReplaceState = window.history.replaceState;
11
+ window.history.replaceState = function(...args) {
12
+ originalReplaceState.apply(this, args);
13
+ window.dispatchEvent(new Event('basecoat:locationchange'));
14
+ };
15
+
16
+ window.history.__basecoatPatched = true;
17
+ }
18
+
19
+ const initSidebar = (sidebarComponent) => {
20
+ const initialOpen = sidebarComponent.dataset.initialOpen !== 'false';
21
+ const initialMobileOpen = sidebarComponent.dataset.initialMobileOpen === 'true';
22
+ const breakpoint = parseInt(sidebarComponent.dataset.breakpoint) || 768;
23
+
24
+ let open = breakpoint > 0
25
+ ? (window.innerWidth >= breakpoint ? initialOpen : initialMobileOpen)
26
+ : initialOpen;
27
+
28
+ const updateCurrentPageLinks = () => {
29
+ const currentPath = window.location.pathname.replace(/\/$/, '');
30
+ sidebarComponent.querySelectorAll('a').forEach(link => {
31
+ if (link.hasAttribute('data-ignore-current')) return;
32
+
33
+ const linkPath = new URL(link.href).pathname.replace(/\/$/, '');
34
+ if (linkPath === currentPath) {
35
+ link.setAttribute('aria-current', 'page');
36
+ } else {
37
+ link.removeAttribute('aria-current');
38
+ }
13
39
  });
14
- },
15
-
16
- $main: {
17
- '@sidebar:open.window'(e) { this.open = true },
18
- '@sidebar:close.window'(e) { this.open = false },
19
- '@sidebar:toggle.window'(e) { this.open = !this.open },
20
- '@click'(e) { if (e.target === this.$el) this.open = false },
21
- ':aria-hidden'() { return !this.open },
22
- ':inert'() { return !this.open }
23
- },
24
- }));
25
- };
40
+ };
41
+
42
+ const updateState = () => {
43
+ sidebarComponent.setAttribute('aria-hidden', !open);
44
+ if (open) {
45
+ sidebarComponent.removeAttribute('inert');
46
+ } else {
47
+ sidebarComponent.setAttribute('inert', '');
48
+ }
49
+ };
50
+
51
+ const setState = (state) => {
52
+ open = state;
53
+ updateState();
54
+ };
55
+
56
+ const sidebarId = sidebarComponent.id;
57
+
58
+ window.addEventListener('sidebar:open', (e) => {
59
+ if (!e.detail?.id || e.detail.id === sidebarId) setState(true);
60
+ });
61
+ window.addEventListener('sidebar:close', (e) => {
62
+ if (!e.detail?.id || e.detail.id === sidebarId) setState(false);
63
+ });
64
+ window.addEventListener('sidebar:toggle', (e) => {
65
+ if (!e.detail?.id || e.detail.id === sidebarId) setState(!open);
66
+ });
67
+
68
+ sidebarComponent.addEventListener('click', (e) => {
69
+ const target = e.target;
70
+ const nav = sidebarComponent.querySelector('nav');
71
+
72
+ const isMobile = window.innerWidth < breakpoint;
73
+
74
+ if (isMobile && (target.closest('a, button') && !target.closest('[data-keep-mobile-sidebar-open]'))) {
75
+ if (document.activeElement) document.activeElement.blur();
76
+ setState(false);
77
+ return;
78
+ }
79
+
80
+ if (target === sidebarComponent || (nav && !nav.contains(target))) {
81
+ if (document.activeElement) document.activeElement.blur();
82
+ setState(false);
83
+ }
84
+ });
85
+
86
+ window.addEventListener('popstate', updateCurrentPageLinks);
87
+ window.addEventListener('basecoat:locationchange', updateCurrentPageLinks);
88
+
89
+ updateState();
90
+ updateCurrentPageLinks();
91
+ sidebarComponent.dataset.sidebarInitialized = true;
92
+ };
93
+
94
+ document.querySelectorAll('.sidebar:not([data-sidebar-initialized])').forEach(initSidebar);
95
+
96
+ const observer = new MutationObserver((mutations) => {
97
+ mutations.forEach((mutation) => {
98
+ mutation.addedNodes.forEach((node) => {
99
+ if (node.nodeType === Node.ELEMENT_NODE) {
100
+ if (node.matches('.sidebar:not([data-sidebar-initialized])')) {
101
+ initSidebar(node);
102
+ }
103
+ node.querySelectorAll('.sidebar:not([data-sidebar-initialized])').forEach(initSidebar);
104
+ }
105
+ });
106
+ });
107
+ });
108
+
109
+ observer.observe(document.body, { childList: true, subtree: true });
110
+ })();
@@ -0,0 +1 @@
1
+ (()=>{if(!window.history.__basecoatPatched){const e=window.history.pushState;window.history.pushState=function(...t){e.apply(this,t),window.dispatchEvent(new Event("basecoat:locationchange"))};const t=window.history.replaceState;window.history.replaceState=function(...e){t.apply(this,e),window.dispatchEvent(new Event("basecoat:locationchange"))},window.history.__basecoatPatched=!0}const e=e=>{const t="false"!==e.dataset.initialOpen,i="true"===e.dataset.initialMobileOpen,a=parseInt(e.dataset.breakpoint)||768;let n=a>0?window.innerWidth>=a?t:i:t;const d=()=>{const t=window.location.pathname.replace(/\/$/,"");e.querySelectorAll("a").forEach((e=>{if(e.hasAttribute("data-ignore-current"))return;new URL(e.href).pathname.replace(/\/$/,"")===t?e.setAttribute("aria-current","page"):e.removeAttribute("aria-current")}))},o=()=>{e.setAttribute("aria-hidden",!n),n?e.removeAttribute("inert"):e.setAttribute("inert","")},r=e=>{n=e,o()},s=e.id;window.addEventListener("sidebar:open",(e=>{e.detail?.id&&e.detail.id!==s||r(!0)})),window.addEventListener("sidebar:close",(e=>{e.detail?.id&&e.detail.id!==s||r(!1)})),window.addEventListener("sidebar:toggle",(e=>{e.detail?.id&&e.detail.id!==s||r(!n)})),e.addEventListener("click",(t=>{const i=t.target,n=e.querySelector("nav");if(window.innerWidth<a&&i.closest("a, button")&&!i.closest("[data-keep-mobile-sidebar-open]"))return document.activeElement&&document.activeElement.blur(),void r(!1);(i===e||n&&!n.contains(i))&&(document.activeElement&&document.activeElement.blur(),r(!1))})),window.addEventListener("popstate",d),window.addEventListener("basecoat:locationchange",d),o(),d(),e.dataset.sidebarInitialized=!0};document.querySelectorAll(".sidebar:not([data-sidebar-initialized])").forEach(e);new MutationObserver((t=>{t.forEach((t=>{t.addedNodes.forEach((t=>{t.nodeType===Node.ELEMENT_NODE&&(t.matches(".sidebar:not([data-sidebar-initialized])")&&e(t),t.querySelectorAll(".sidebar:not([data-sidebar-initialized])").forEach(e))}))}))})).observe(document.body,{childList:!0,subtree:!0})})();
@@ -1,64 +1,75 @@
1
- window.basecoat = window.basecoat || {};
2
- window.basecoat.registerTabs = function(Alpine) {
3
- if (Alpine.components && Alpine.components.tabs) return;
4
-
5
- Alpine.data('tabs', (initialTabIndex = 0) => ({
6
- activeTabIndex: initialTabIndex,
7
- tabs: [],
8
- panels: [],
1
+ (() => {
2
+ const initTabs = (tabsComponent) => {
3
+ const tablist = tabsComponent.querySelector('[role="tablist"]');
4
+ if (!tablist) return;
9
5
 
10
- init() {
11
- this.$nextTick(() => {
12
- this.tabs = Array.from(this.$el.querySelectorAll(':scope > [role=tablist] [role=tab]:not([disabled])'));
13
- this.panels = Array.from(this.$el.querySelectorAll(':scope > [role=tabpanel]'));
14
- if (this.tabs.length > 0) {
15
- this.selectTab(this.tabs[initialTabIndex], false);
16
- }
17
- });
18
- },
19
- nextTab() {
20
- if (this.tabs.length === 0) return;
21
- let newIndex = (this.activeTabIndex + 1) % this.tabs.length;
22
- this.selectTab(this.tabs[newIndex]);
23
- },
24
- prevTab() {
25
- if (this.tabs.length === 0) return;
26
- let newIndex = (this.activeTabIndex - 1 + this.tabs.length) % this.tabs.length;
27
- this.selectTab(this.tabs[newIndex]);
28
- },
29
- selectTab(tab, focus = true) {
30
- if (!tab || this.tabs.length === 0) return;
6
+ const tabs = Array.from(tablist.querySelectorAll('[role="tab"]'));
7
+ const panels = tabs.map(tab => document.getElementById(tab.getAttribute('aria-controls'))).filter(Boolean);
31
8
 
32
- this.tabs.forEach((t, index) => {
33
- const isSelected = t === tab;
34
- t.setAttribute('aria-selected', isSelected);
35
- t.setAttribute('tabindex', isSelected ? '0' : '-1');
36
- if (isSelected) {
37
- this.activeTabIndex = index;
38
- this.activeTab = t;
39
- if (focus) {
40
- t.focus();
41
- }
42
- }
9
+ const selectTab = (tabToSelect) => {
10
+ tabs.forEach((tab, index) => {
11
+ tab.setAttribute('aria-selected', 'false');
12
+ tab.setAttribute('tabindex', '-1');
13
+ if (panels[index]) panels[index].hidden = true;
43
14
  });
44
15
 
45
- const panelId = tab.getAttribute('aria-controls');
46
- if (!panelId) return;
16
+ tabToSelect.setAttribute('aria-selected', 'true');
17
+ tabToSelect.setAttribute('tabindex', '0');
18
+ const activePanel = document.getElementById(tabToSelect.getAttribute('aria-controls'));
19
+ if (activePanel) activePanel.hidden = false;
20
+ };
47
21
 
48
- this.panels.forEach(panel => {
49
- panel.hidden = (panel.getAttribute('id') !== panelId);
50
- });
51
- },
22
+ tablist.addEventListener('click', (e) => {
23
+ const clickedTab = e.target.closest('[role="tab"]');
24
+ if (clickedTab) selectTab(clickedTab);
25
+ });
26
+
27
+ tablist.addEventListener('keydown', (e) => {
28
+ const currentTab = e.target;
29
+ if (!tabs.includes(currentTab)) return;
30
+
31
+ let nextTab;
32
+ const currentIndex = tabs.indexOf(currentTab);
52
33
 
53
- $tablist: {
54
- ['@click'](event) {
55
- const clickedTab = event.target.closest('[role=tab]');
56
- if (clickedTab) {
57
- this.selectTab(clickedTab);
34
+ switch (e.key) {
35
+ case 'ArrowRight':
36
+ nextTab = tabs[(currentIndex + 1) % tabs.length];
37
+ break;
38
+ case 'ArrowLeft':
39
+ nextTab = tabs[(currentIndex - 1 + tabs.length) % tabs.length];
40
+ break;
41
+ case 'Home':
42
+ nextTab = tabs[0];
43
+ break;
44
+ case 'End':
45
+ nextTab = tabs[tabs.length - 1];
46
+ break;
47
+ default:
48
+ return;
49
+ }
50
+
51
+ e.preventDefault();
52
+ selectTab(nextTab);
53
+ nextTab.focus();
54
+ });
55
+
56
+ tabsComponent.dataset.tabsInitialized = true;
57
+ };
58
+
59
+ document.querySelectorAll('.tabs:not([data-tabs-initialized])').forEach(initTabs);
60
+
61
+ const observer = new MutationObserver((mutations) => {
62
+ mutations.forEach((mutation) => {
63
+ mutation.addedNodes.forEach((node) => {
64
+ if (node.nodeType === Node.ELEMENT_NODE) {
65
+ if (node.matches('.tabs:not([data-tabs-initialized])')) {
66
+ initTabs(node);
58
67
  }
59
- },
60
- ['@keydown.arrow-right.prevent']() { this.nextTab() },
61
- ['@keydown.arrow-left.prevent']() { this.prevTab() },
62
- },
63
- }));
64
- };
68
+ node.querySelectorAll('.tabs:not([data-tabs-initialized])').forEach(initTabs);
69
+ }
70
+ });
71
+ });
72
+ });
73
+
74
+ observer.observe(document.body, { childList: true, subtree: true });
75
+ })();
@@ -0,0 +1 @@
1
+ (()=>{const e=e=>{const t=e.querySelector('[role="tablist"]');if(!t)return;const a=Array.from(t.querySelectorAll('[role="tab"]')),r=a.map((e=>document.getElementById(e.getAttribute("aria-controls")))).filter(Boolean),n=e=>{a.forEach(((e,t)=>{e.setAttribute("aria-selected","false"),e.setAttribute("tabindex","-1"),r[t]&&(r[t].hidden=!0)})),e.setAttribute("aria-selected","true"),e.setAttribute("tabindex","0");const t=document.getElementById(e.getAttribute("aria-controls"));t&&(t.hidden=!1)};t.addEventListener("click",(e=>{const t=e.target.closest('[role="tab"]');t&&n(t)})),t.addEventListener("keydown",(e=>{const t=e.target;if(!a.includes(t))return;let r;const o=a.indexOf(t);switch(e.key){case"ArrowRight":r=a[(o+1)%a.length];break;case"ArrowLeft":r=a[(o-1+a.length)%a.length];break;case"Home":r=a[0];break;case"End":r=a[a.length-1];break;default:return}e.preventDefault(),n(r),r.focus()})),e.dataset.tabsInitialized=!0};document.querySelectorAll(".tabs:not([data-tabs-initialized])").forEach(e);new MutationObserver((t=>{t.forEach((t=>{t.addedNodes.forEach((t=>{t.nodeType===Node.ELEMENT_NODE&&(t.matches(".tabs:not([data-tabs-initialized])")&&e(t),t.querySelectorAll(".tabs:not([data-tabs-initialized])").forEach(e))}))}))})).observe(document.body,{childList:!0,subtree:!0})})();