basecoat-cli 0.1.1 → 0.2.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.
@@ -0,0 +1 @@
1
+ (()=>{const e=e=>{const t=e.querySelector(":scope > button"),a=e.querySelector(":scope > [data-popover]"),n=a.querySelector('[role="menu"]');if(!t||!n||!a)return void console.error("Dropdown menu component is missing a trigger, a menu, or a popover content element.",e);let r=[],i=-1;const o=()=>{"false"!==t.getAttribute("aria-expanded")&&(t.setAttribute("aria-expanded","false"),t.removeAttribute("aria-activedescendant"),a.setAttribute("aria-hidden","true"),t.focus(),i=-1)},d=()=>{t.setAttribute("aria-expanded","true"),a.setAttribute("aria-hidden","false"),r=Array.from(n.querySelectorAll('[role^="menuitem"]:not([disabled])')),r.length>0&&s(0)},s=e=>{if(i>-1&&r[i]&&r[i].classList.remove("active"),i=e,i>-1&&r[i]){const e=r[i];e.classList.add("active"),t.setAttribute("aria-activedescendant",e.id)}else t.removeAttribute("aria-activedescendant")};t.addEventListener("click",(()=>{"true"===t.getAttribute("aria-expanded")?o():d()})),e.addEventListener("keydown",(e=>{const a="true"===t.getAttribute("aria-expanded");if("Escape"===e.key)return void(a&&o());if(!a)return void(["ArrowDown","ArrowUp","Enter"," "].includes(e.key)&&(e.preventDefault(),d()));if(0===r.length)return;let n=i;switch(e.key){case"ArrowDown":e.preventDefault(),n=i<r.length-1?i+1:0;break;case"ArrowUp":e.preventDefault(),n=i>0?i-1:r.length-1;break;case"Home":e.preventDefault(),n=0;break;case"End":e.preventDefault(),n=r.length-1;break;case"Enter":case" ":return e.preventDefault(),r[i]?.click(),void o()}n!==i&&s(n)})),n.addEventListener("click",(e=>{e.target.closest('[role^="menuitem"]')&&o()})),document.addEventListener("click",(t=>{e.contains(t.target)||o()})),e.dataset.dropdownMenuInitialized=!0};document.querySelectorAll(".dropdown-menu:not([data-dropdown-menu-initialized])").forEach(e);new MutationObserver((t=>{t.forEach((t=>{t.addedNodes.forEach((t=>{t.nodeType===Node.ELEMENT_NODE&&(t.matches(".dropdown-menu:not([data-dropdown-menu-initialized])")&&e(t),t.querySelectorAll(".dropdown-menu:not([data-dropdown-menu-initialized])").forEach(e))}))}))})).observe(document.body,{childList:!0,subtree:!0})})(),(()=>{const e=e=>{const t=e.querySelector(":scope > button"),a=e.querySelector(":scope > [data-popover]");if(!t||!a)return void console.error("Popover component is missing a trigger button or a content element.",e);const n=()=>{"false"!==t.getAttribute("aria-expanded")&&(t.setAttribute("aria-expanded","false"),a.setAttribute("aria-hidden","true"),t.focus())};t.addEventListener("click",(()=>{"true"===t.getAttribute("aria-expanded")?n():(()=>{const e=a.querySelector("[autofocus]");e&&a.addEventListener("transitionend",(()=>{e.focus()}),{once:!0}),t.setAttribute("aria-expanded","true"),a.setAttribute("aria-hidden","false")})()})),e.addEventListener("keydown",(e=>{"Escape"===e.key&&n()})),document.addEventListener("click",(t=>{e.contains(t.target)||n()})),e.dataset.popoverInitialized=!0};document.querySelectorAll(".popover:not([data-popover-initialized])").forEach(e);new MutationObserver((t=>{t.forEach((t=>{t.addedNodes.forEach((t=>{t.nodeType===Node.ELEMENT_NODE&&(t.matches(".popover:not([data-popover-initialized])")&&e(t),t.querySelectorAll(".popover:not([data-popover-initialized])").forEach(e))}))}))})).observe(document.body,{childList:!0,subtree:!0})})(),(()=>{const e=e=>{const t=e.querySelector(":scope > button"),a=t.querySelector(":scope > span"),n=e.querySelector(":scope > [data-popover]"),r=n.querySelector('[role="listbox"]'),i=e.querySelector(':scope > input[type="hidden"]'),o=e.querySelector('header input[type="text"]');if(!(t&&n&&r&&i))return;const d=Array.from(r.querySelectorAll('[role="option"]'));let s=[...d],c=-1;const l=e=>{e&&(a.innerHTML=e.dataset.label||e.innerHTML,i.value=e.dataset.value,r.querySelector('[role="option"][aria-selected="true"]')?.removeAttribute("aria-selected"),e.setAttribute("aria-selected","true"))},u=()=>{n.setAttribute("aria-hidden","true"),t.setAttribute("aria-expanded","false"),o&&(o.value="",s=[...d],d.forEach((e=>e.setAttribute("aria-hidden","false")))),t.removeAttribute("aria-activedescendant"),c>-1&&d[c]?.classList.remove("active"),c=-1},h=e=>{e&&(e.dataset.value&&l(e),u())};if(o){const e=()=>{const e=o.value.trim().toLowerCase();c>-1&&(d[c].classList.remove("active"),t.removeAttribute("aria-activedescendant"),c=-1),s=[],d.forEach((t=>{const a=(t.dataset.label||t.textContent).trim().toLowerCase().includes(e);t.setAttribute("aria-hidden",String(!a)),a&&s.push(t)}))};o.addEventListener("input",e)}let v=d.find((e=>i.value&&e.dataset.value===i.value));!v&&d.length>0&&(v=d[0]),l(v);const p=e=>{const a="false"===n.getAttribute("aria-hidden");if(!["ArrowDown","ArrowUp","Enter","Home","End","Escape"].includes(e.key))return;if(!a)return void("Enter"!==e.key&&"Escape"!==e.key&&(e.preventDefault(),t.click()));if(e.preventDefault(),"Escape"===e.key)return void u();if("Enter"===e.key)return void(c>-1&&h(s[c]));if(0===s.length)return;const r=c>-1?s.indexOf(d[c]):-1;let i=r;switch(e.key){case"ArrowDown":r<s.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=s.length-1}if(i!==r){r>-1&&s[r].classList.remove("active");const e=s[i];e.classList.add("active"),c=d.indexOf(e),e.id&&t.setAttribute("aria-activedescendant",e.id),e.scrollIntoView({block:"nearest",behavior:"smooth"})}};t.addEventListener("keydown",p),o&&o.addEventListener("keydown",p),t.addEventListener("click",(()=>{if("true"===t.getAttribute("aria-expanded"))u();else{n.setAttribute("aria-hidden","false"),t.setAttribute("aria-expanded","true"),o&&o.focus();const e=r.querySelector('[role="option"][aria-selected="true"]');e&&(c>-1&&d[c]?.classList.remove("active"),c=d.indexOf(e),e.classList.add("active"),e.id&&t.setAttribute("aria-activedescendant",e.id),e.scrollIntoView({block:"nearest"}))}})),r.addEventListener("click",(e=>{const t=e.target.closest('[role="option"]');t&&h(t)})),document.addEventListener("click",(t=>{e.contains(t.target)||u()})),n.setAttribute("aria-hidden","true"),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})})(),(()=>{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,a="true"===e.dataset.initialMobileOpen,n=parseInt(e.dataset.breakpoint)||768;let r=n>0?window.innerWidth>=n?t:a:t;const i=()=>{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",!r),r?e.removeAttribute("inert"):e.setAttribute("inert","")},d=e=>{r=e,o()},s=e.id;window.addEventListener("sidebar:open",(e=>{e.detail?.id&&e.detail.id!==s||d(!0)})),window.addEventListener("sidebar:close",(e=>{e.detail?.id&&e.detail.id!==s||d(!1)})),window.addEventListener("sidebar:toggle",(e=>{e.detail?.id&&e.detail.id!==s||d(!r)})),e.addEventListener("click",(t=>{const a=t.target,r=e.querySelector("nav");if(window.innerWidth<n&&a.closest("a, button")&&!a.closest("[data-keep-mobile-sidebar-open]"))return document.activeElement&&document.activeElement.blur(),void d(!1);(a===e||r&&!r.contains(a))&&(document.activeElement&&document.activeElement.blur(),d(!1))})),window.addEventListener("popstate",i),window.addEventListener("basecoat:locationchange",i),o(),i(),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})})(),(()=>{const e=e=>{const t=e.querySelector('[role="tablist"]');if(!t)return;const a=Array.from(t.querySelectorAll('[role="tab"]')),n=a.map((e=>document.getElementById(e.getAttribute("aria-controls")))).filter(Boolean),r=e=>{a.forEach(((e,t)=>{e.setAttribute("aria-selected","false"),e.setAttribute("tabindex","-1"),n[t]&&(n[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&&r(t)})),t.addEventListener("keydown",(e=>{const t=e.target;if(!a.includes(t))return;let n;const i=a.indexOf(t);switch(e.key){case"ArrowRight":n=a[(i+1)%a.length];break;case"ArrowLeft":n=a[(i-1+a.length)%a.length];break;case"Home":n=a[0];break;case"End":n=a[a.length-1];break;default:return}e.preventDefault(),r(n),n.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})})(),(()=>{let e;const t=new WeakMap;let a=!1;const n={success:'<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="m9 12 2 2 4-4"/></svg>',error:'<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="m15 9-6 6"/><path d="m9 9 6 6"/></svg>',info:'<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M12 16v-4"/><path d="M12 8h.01"/></svg>',warning:'<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3"/><path d="M12 9v4"/><path d="M12 17h.01"/></svg>'};function r(t){t.dataset.toasterInitialized||(e=t,e.addEventListener("mouseenter",o),e.addEventListener("mouseleave",d),e.addEventListener("click",(e=>{const t=e.target.closest(".toast footer a"),a=e.target.closest(".toast footer button");(t||a)&&s(e.target.closest(".toast"))})),e.querySelectorAll(".toast:not([data-toast-initialized])").forEach(i),e.dataset.toasterInitialized="true")}function i(e){if(e.dataset.toastInitialized)return;const n=parseInt(e.dataset.duration),r=-1!==n?n||("error"===e.dataset.category?5e3:3e3):-1,i={remainingTime:r,timeoutId:null,startTime:null};-1!==r&&(a?i.timeoutId=null:(i.startTime=Date.now(),i.timeoutId=setTimeout((()=>s(e)),r))),t.set(e,i),e.dataset.toastInitialized="true"}function o(){a||(a=!0,e.querySelectorAll('.toast:not([aria-hidden="true"])').forEach((e=>{if(!t.has(e))return;const a=t.get(e);a.timeoutId&&(clearTimeout(a.timeoutId),a.timeoutId=null,a.remainingTime-=Date.now()-a.startTime)})))}function d(){a&&(a=!1,e.querySelectorAll('.toast:not([aria-hidden="true"])').forEach((e=>{if(!t.has(e))return;const a=t.get(e);-1===a.remainingTime||a.timeoutId||(a.remainingTime>0?(a.startTime=Date.now(),a.timeoutId=setTimeout((()=>s(e)),a.remainingTime)):s(e))})))}function s(e){if(!t.has(e))return;const a=t.get(e);clearTimeout(a.timeoutId),t.delete(e),document.activeElement&&document.activeElement.blur(),e.setAttribute("aria-hidden","true"),e.addEventListener("transitionend",(()=>e.remove()),{once:!0})}const c=document.getElementById("toaster");c&&r(c),window.addEventListener("basecoat:toast",(t=>{if(!e)return void console.error("Cannot create toast: toaster container not found on page.");const a=function(e){const{category:t="info",title:a,description:r,action:i,cancel:o,duration:d,icon:s}=e,c=s||t&&n[t]||"",l=a?`<h2>${a}</h2>`:"",u=r?`<p>${r}</p>`:"",h=i?.href?`<a href="${i.href}" class="btn" data-toast-action>${i.label}</a>`:i?.onclick?`<button type="button" class="btn" data-toast-action onclick="${i.onclick}">${i.label}</button>`:"",v=o?`<button type="button" class="btn-outline h-6 text-xs px-2.5 rounded-sm" data-toast-cancel onclick="${o?.onclick}">${o.label}</button>`:"",p=`\n <div\n class="toast"\n role="${"error"===t?"alert":"status"}"\n aria-atomic="true"\n ${t?`data-category="${t}"`:""}\n ${void 0!==d?`data-duration="${d}"`:""}\n >\n <div class="toast-content">\n ${c}\n <section>\n ${l}\n ${u}\n </section>\n ${h||v?`<footer>${h}${v}</footer>`:""}\n </div>\n </div>\n </div>\n `,b=document.createElement("template");return b.innerHTML=p.trim(),b.content.firstChild}(t.detail?.config||{});e.appendChild(a)}));new MutationObserver((t=>{t.forEach((t=>{t.addedNodes.forEach((t=>{t.nodeType===Node.ELEMENT_NODE&&(t.matches("#toaster")&&r(t),e&&t.matches(".toast:not([data-toast-initialized])")&&i(t))}))}))})).observe(document.body,{childList:!0,subtree:!0})})();
@@ -0,0 +1,10 @@
1
+ (() => {
2
+ document.body.addEventListener('click', (event) => {
3
+ const toggleButton = event.target.closest('[data-action="toggle-dark-mode"]');
4
+
5
+ if (!toggleButton) return;
6
+
7
+ const isDark = document.documentElement.classList.toggle('dark');
8
+ localStorage.setItem('darkMode', isDark ? 'dark' : 'light');
9
+ });
10
+ })();
@@ -0,0 +1 @@
1
+ document.body.addEventListener("click",(t=>{if(!t.target.closest('[data-action="toggle-dark-mode"]'))return;const e=document.documentElement.classList.toggle("dark");localStorage.setItem("darkMode",e?"dark":"light")}));
@@ -1,91 +1,136 @@
1
- window.basecoat = window.basecoat || {};
2
- window.basecoat.registerDropdownMenu = function(Alpine) {
3
- if (Alpine.components && Alpine.components.dropdownMenu) return;
4
-
5
- Alpine.data('dropdownMenu', () => ({
6
- open: false,
7
- focusedIndex: null,
8
- menuItems: [],
9
-
10
- init() {
11
- this.$nextTick(() => {
12
- this.menuItems = Array.from(this.$el.querySelectorAll('[role=menuitem]:not([disabled]),[role=menuitemcheckbox]:not([disabled]),[role=menuitemradio]:not([disabled])'));
13
- });
14
- },
15
- focusMenuitem() {
16
- if (this.menuItems.length === 0) return;
17
-
18
- if (this.focusedIndex >= this.menuItems.length) {
19
- this.focusedIndex = this.menuItems.length - 1;
20
- } else if (this.focusedIndex < 0 || this.focusedIndex === null) {
21
- this.focusedIndex = 0;
1
+ (() => {
2
+ const initDropdownMenu = (dropdownMenuComponent) => {
3
+ const trigger = dropdownMenuComponent.querySelector(':scope > button');
4
+ const popover = dropdownMenuComponent.querySelector(':scope > [data-popover]');
5
+ const menu = popover.querySelector('[role="menu"]');
6
+
7
+ if (!trigger || !menu || !popover) {
8
+ console.error('Dropdown menu component is missing a trigger, a menu, or a popover content element.', dropdownMenuComponent);
9
+ return;
10
+ }
11
+
12
+ let menuItems = [];
13
+ let activeIndex = -1;
14
+
15
+ const closeMenu = () => {
16
+ if (trigger.getAttribute('aria-expanded') === 'false') return;
17
+ trigger.setAttribute('aria-expanded', 'false');
18
+ trigger.removeAttribute('aria-activedescendant');
19
+ popover.setAttribute('aria-hidden', 'true');
20
+ trigger.focus();
21
+ activeIndex = -1;
22
+ };
23
+
24
+ const openMenu = () => {
25
+ trigger.setAttribute('aria-expanded', 'true');
26
+ popover.setAttribute('aria-hidden', 'false');
27
+ menuItems = Array.from(menu.querySelectorAll('[role^="menuitem"]:not([disabled])'));
28
+ if (menuItems.length > 0) {
29
+ setActiveItem(0);
30
+ }
31
+ };
32
+
33
+ const setActiveItem = (index) => {
34
+ if (activeIndex > -1 && menuItems[activeIndex]) {
35
+ menuItems[activeIndex].classList.remove('active');
22
36
  }
23
- this.menuItems.forEach(item => item.blur());
24
- this.menuItems[this.focusedIndex].focus();
25
- },
26
- moveMenuitemFocus(delta) {
27
- if (this.menuItems.length === 0) return;
28
-
29
- let wasOpen = this.open;
30
- if (!this.open) {
31
- this.open = true;
32
- this.focusedIndex = 0;
37
+ activeIndex = index;
38
+ if (activeIndex > -1 && menuItems[activeIndex]) {
39
+ const activeItem = menuItems[activeIndex];
40
+ activeItem.classList.add('active');
41
+ trigger.setAttribute('aria-activedescendant', activeItem.id);
33
42
  } else {
34
- this.focusedIndex = this.focusedIndex === null
35
- ? 0
36
- : this.focusedIndex + delta;
43
+ trigger.removeAttribute('aria-activedescendant');
37
44
  }
45
+ };
38
46
 
39
- if (wasOpen) {
40
- this.focusMenuitem();
47
+ trigger.addEventListener('click', () => {
48
+ const isExpanded = trigger.getAttribute('aria-expanded') === 'true';
49
+ if (isExpanded) {
50
+ closeMenu();
41
51
  } else {
42
- setTimeout(() => this.focusMenuitem(), 50);
52
+ openMenu();
53
+ }
54
+ });
55
+
56
+ dropdownMenuComponent.addEventListener('keydown', (e) => {
57
+ const isExpanded = trigger.getAttribute('aria-expanded') === 'true';
58
+
59
+ if (e.key === 'Escape') {
60
+ if (isExpanded) closeMenu();
61
+ return;
62
+ }
63
+
64
+ if (!isExpanded) {
65
+ if (['ArrowDown', 'ArrowUp', 'Enter', ' '].includes(e.key)) {
66
+ e.preventDefault();
67
+ openMenu();
68
+ }
69
+ return;
70
+ }
71
+
72
+ if (menuItems.length === 0) return;
73
+
74
+ let nextIndex = activeIndex;
75
+
76
+ switch (e.key) {
77
+ case 'ArrowDown':
78
+ e.preventDefault();
79
+ nextIndex = activeIndex < menuItems.length - 1 ? activeIndex + 1 : 0;
80
+ break;
81
+ case 'ArrowUp':
82
+ e.preventDefault();
83
+ nextIndex = activeIndex > 0 ? activeIndex - 1 : menuItems.length - 1;
84
+ break;
85
+ case 'Home':
86
+ e.preventDefault();
87
+ nextIndex = 0;
88
+ break;
89
+ case 'End':
90
+ e.preventDefault();
91
+ nextIndex = menuItems.length - 1;
92
+ break;
93
+ case 'Enter':
94
+ case ' ':
95
+ e.preventDefault();
96
+ menuItems[activeIndex]?.click();
97
+ closeMenu();
98
+ return;
43
99
  }
44
- },
45
- handleMenuitemClick(event) {
46
- const menuitem = event.target.closest('[role=menuitem],[role=menuitemcheckbox],[role=menuitemradio]');
47
- if (menuitem && menuitem.getAttribute('aria-disabled') !== 'true' && !menuitem.disabled) {
48
- this.$nextTick(() => {
49
- this.$refs.trigger.focus();
50
- this.open = false;
51
- });
100
+
101
+ if (nextIndex !== activeIndex) {
102
+ setActiveItem(nextIndex);
52
103
  }
53
- },
54
- handleMenuitemMousemove(event) {
55
- const menuitem = event.target.closest('[role=menuitem],[role=menuitemcheckbox],[role=menuitemradio]');
56
- if (menuitem && menuitem.getAttribute('aria-disabled') !== 'true' && !menuitem.disabled) {
57
- this.focusedIndex = this.menuItems.indexOf(menuitem);
58
- this.focusMenuitem();
104
+ });
105
+
106
+ menu.addEventListener('click', (e) => {
107
+ if (e.target.closest('[role^="menuitem"]')) {
108
+ closeMenu();
59
109
  }
60
- },
61
-
62
- $trigger: {
63
- '@click'() { this.open = !this.open },
64
- '@keydown.escape.prevent'() {
65
- this.open = false;
66
- this.$refs.trigger.focus();
67
- },
68
- '@keydown.down.prevent'() { this.moveMenuitemFocus(+1); },
69
- '@keydown.up.prevent'() { this.moveMenuitemFocus(-1); },
70
- '@keydown.home.prevent'() { this.focusMenuitem(0) },
71
- '@keydown.end.prevent'() { this.focusMenuitem(this.menuItems.length - 1) },
72
- '@keydown.enter.prevent'() { this.open = !this.open },
73
- ':aria-expanded'() { return this.open },
74
- 'x-ref': 'trigger'
75
- },
76
- $content: {
77
- '@click'(e) { this.handleMenuitemClick(e) },
78
- '@keydown.escape.prevent'() {
79
- this.open = false;
80
- this.$refs.trigger.focus();
81
- },
82
- '@keydown.down.prevent'() { this.moveMenuitemFocus(+1); },
83
- '@keydown.up.prevent'() { this.moveMenuitemFocus(-1); },
84
- '@keydown.home.prevent'() { this.focusMenuitem(0) },
85
- '@keydown.end.prevent'() { this.focusMenuitem(this.menuItems.length - 1) },
86
- '@mouseover'(e) { this.handleMenuitemMousemove(e) },
87
- ':aria-hidden'() { return !this.open },
88
- 'x-cloak': ''
89
- },
90
- }));
91
- };
110
+ });
111
+
112
+ document.addEventListener('click', (e) => {
113
+ if (!dropdownMenuComponent.contains(e.target)) {
114
+ closeMenu();
115
+ }
116
+ });
117
+
118
+ dropdownMenuComponent.dataset.dropdownMenuInitialized = true;
119
+ };
120
+
121
+ document.querySelectorAll('.dropdown-menu:not([data-dropdown-menu-initialized])').forEach(initDropdownMenu);
122
+
123
+ const observer = new MutationObserver((mutations) => {
124
+ mutations.forEach((mutation) => {
125
+ mutation.addedNodes.forEach((node) => {
126
+ if (node.nodeType !== Node.ELEMENT_NODE) return;
127
+ if (node.matches('.dropdown-menu:not([data-dropdown-menu-initialized])')) {
128
+ initDropdownMenu(node);
129
+ }
130
+ node.querySelectorAll('.dropdown-menu:not([data-dropdown-menu-initialized])').forEach(initDropdownMenu);
131
+ });
132
+ });
133
+ });
134
+
135
+ observer.observe(document.body, { childList: true, subtree: true });
136
+ })();
@@ -0,0 +1 @@
1
+ (()=>{const e=e=>{const t=e.querySelector(":scope > button"),r=e.querySelector(":scope > [data-popover]"),n=r.querySelector('[role="menu"]');if(!t||!n||!r)return void console.error("Dropdown menu component is missing a trigger, a menu, or a popover content element.",e);let a=[],o=-1;const d=()=>{"false"!==t.getAttribute("aria-expanded")&&(t.setAttribute("aria-expanded","false"),t.removeAttribute("aria-activedescendant"),r.setAttribute("aria-hidden","true"),t.focus(),o=-1)},i=()=>{t.setAttribute("aria-expanded","true"),r.setAttribute("aria-hidden","false"),a=Array.from(n.querySelectorAll('[role^="menuitem"]:not([disabled])')),a.length>0&&c(0)},c=e=>{if(o>-1&&a[o]&&a[o].classList.remove("active"),o=e,o>-1&&a[o]){const e=a[o];e.classList.add("active"),t.setAttribute("aria-activedescendant",e.id)}else t.removeAttribute("aria-activedescendant")};t.addEventListener("click",(()=>{"true"===t.getAttribute("aria-expanded")?d():i()})),e.addEventListener("keydown",(e=>{const r="true"===t.getAttribute("aria-expanded");if("Escape"===e.key)return void(r&&d());if(!r)return void(["ArrowDown","ArrowUp","Enter"," "].includes(e.key)&&(e.preventDefault(),i()));if(0===a.length)return;let n=o;switch(e.key){case"ArrowDown":e.preventDefault(),n=o<a.length-1?o+1:0;break;case"ArrowUp":e.preventDefault(),n=o>0?o-1:a.length-1;break;case"Home":e.preventDefault(),n=0;break;case"End":e.preventDefault(),n=a.length-1;break;case"Enter":case" ":return e.preventDefault(),a[o]?.click(),void d()}n!==o&&c(n)})),n.addEventListener("click",(e=>{e.target.closest('[role^="menuitem"]')&&d()})),document.addEventListener("click",(t=>{e.contains(t.target)||d()})),e.dataset.dropdownMenuInitialized=!0};document.querySelectorAll(".dropdown-menu:not([data-dropdown-menu-initialized])").forEach(e);new MutationObserver((t=>{t.forEach((t=>{t.addedNodes.forEach((t=>{t.nodeType===Node.ELEMENT_NODE&&(t.matches(".dropdown-menu:not([data-dropdown-menu-initialized])")&&e(t),t.querySelectorAll(".dropdown-menu:not([data-dropdown-menu-initialized])").forEach(e))}))}))})).observe(document.body,{childList:!0,subtree:!0})})();
@@ -1,26 +1,69 @@
1
- window.basecoat = window.basecoat || {};
2
- window.basecoat.registerPopover = function(Alpine) {
3
- if (Alpine.components && Alpine.components.popover) return;
4
-
5
- Alpine.data('popover', () => ({
6
- open: false,
7
-
8
- $trigger: {
9
- '@click'() { this.open = !this.open },
10
- '@keydown.escape.prevent'() {
11
- this.open = false;
12
- this.$refs.trigger.focus();
13
- },
14
- ':aria-expanded'() { return this.open },
15
- 'x-ref': 'trigger'
16
- },
17
- $content: {
18
- '@keydown.escape.prevent'() {
19
- this.open = false;
20
- this.$refs.trigger.focus();
21
- },
22
- ':aria-hidden'() { return !this.open },
23
- 'x-cloak': ''
24
- },
25
- }));
26
- };
1
+ (() => {
2
+ const initPopover = (popoverComponent) => {
3
+ const trigger = popoverComponent.querySelector(':scope > button');
4
+ const content = popoverComponent.querySelector(':scope > [data-popover]');
5
+
6
+ if (!trigger || !content) {
7
+ console.error('Popover component is missing a trigger button or a content element.', popoverComponent);
8
+ return;
9
+ }
10
+
11
+ const closePopover = () => {
12
+ if (trigger.getAttribute('aria-expanded') === 'false') return;
13
+ trigger.setAttribute('aria-expanded', 'false');
14
+ content.setAttribute('aria-hidden', 'true');
15
+ trigger.focus();
16
+ };
17
+
18
+ const openPopover = () => {
19
+ const elementToFocus = content.querySelector('[autofocus]');
20
+ if (elementToFocus) {
21
+ content.addEventListener('transitionend', () => {
22
+ elementToFocus.focus();
23
+ }, { once: true });
24
+ }
25
+
26
+ trigger.setAttribute('aria-expanded', 'true');
27
+ content.setAttribute('aria-hidden', 'false');
28
+ };
29
+
30
+ trigger.addEventListener('click', () => {
31
+ const isExpanded = trigger.getAttribute('aria-expanded') === 'true';
32
+ if (isExpanded) {
33
+ closePopover();
34
+ } else {
35
+ openPopover();
36
+ }
37
+ });
38
+
39
+ popoverComponent.addEventListener('keydown', (e) => {
40
+ if (e.key === 'Escape') {
41
+ closePopover();
42
+ }
43
+ });
44
+
45
+ document.addEventListener('click', (e) => {
46
+ if (!popoverComponent.contains(e.target)) {
47
+ closePopover();
48
+ }
49
+ });
50
+
51
+ popoverComponent.dataset.popoverInitialized = true;
52
+ };
53
+
54
+ document.querySelectorAll('.popover:not([data-popover-initialized])').forEach(initPopover);
55
+
56
+ const observer = new MutationObserver((mutations) => {
57
+ mutations.forEach((mutation) => {
58
+ mutation.addedNodes.forEach((node) => {
59
+ if (node.nodeType !== Node.ELEMENT_NODE) return;
60
+ if (node.matches('.popover:not([data-popover-initialized])')) {
61
+ initPopover(node);
62
+ }
63
+ node.querySelectorAll('.popover:not([data-popover-initialized])').forEach(initPopover);
64
+ });
65
+ });
66
+ });
67
+
68
+ observer.observe(document.body, { childList: true, subtree: true });
69
+ })();
@@ -0,0 +1 @@
1
+ (()=>{const e=e=>{const t=e.querySelector(":scope > button"),o=e.querySelector(":scope > [data-popover]");if(!t||!o)return void console.error("Popover component is missing a trigger button or a content element.",e);const r=()=>{"false"!==t.getAttribute("aria-expanded")&&(t.setAttribute("aria-expanded","false"),o.setAttribute("aria-hidden","true"),t.focus())};t.addEventListener("click",(()=>{"true"===t.getAttribute("aria-expanded")?r():(()=>{const e=o.querySelector("[autofocus]");e&&o.addEventListener("transitionend",(()=>{e.focus()}),{once:!0}),t.setAttribute("aria-expanded","true"),o.setAttribute("aria-hidden","false")})()})),e.addEventListener("keydown",(e=>{"Escape"===e.key&&r()})),document.addEventListener("click",(t=>{e.contains(t.target)||r()})),e.dataset.popoverInitialized=!0};document.querySelectorAll(".popover:not([data-popover-initialized])").forEach(e);new MutationObserver((t=>{t.forEach((t=>{t.addedNodes.forEach((t=>{t.nodeType===Node.ELEMENT_NODE&&(t.matches(".popover:not([data-popover-initialized])")&&e(t),t.querySelectorAll(".popover:not([data-popover-initialized])").forEach(e))}))}))})).observe(document.body,{childList:!0,subtree:!0})})();