asta-ui 0.8.0 → 0.9.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.
@@ -0,0 +1,120 @@
1
+ /* Hand-written CSS for the parts Tailwind classes can't express: native dialog, toasts, menus, keyframes.
2
+ * Motion is transform and opacity only, with the kit's one easing. */
3
+
4
+ @keyframes asta-field-in {
5
+ from {
6
+ opacity: 0;
7
+ transform: translateY(-3px);
8
+ }
9
+ }
10
+
11
+ @keyframes asta-badge-ping { 0% { transform: scale(1); opacity: 0.7 } 100% { transform: scale(3); opacity: 0 } }
12
+ @keyframes asta-pop { 0% { transform: scale(0.4) } 60% { transform: scale(1.15) } 100% { transform: scale(1) } }
13
+ @keyframes asta-fade { from { opacity: 0 } }
14
+ @keyframes asta-rise { from { opacity: 0; transform: translateY(6px) } to { opacity: 1; transform: none } }
15
+ @keyframes asta-scale-in { from { opacity: 0; transform: scale(0.85) } }
16
+ @keyframes asta-spinner-dot { 0%, 80%, 100% { transform: scale(0.55); opacity: 0.35 } 40% { transform: scale(1); opacity: 1 } }
17
+ @keyframes asta-spinner-bar { 0%, 100% { transform: scaleY(0.35) } 50% { transform: scaleY(1) } }
18
+ @keyframes asta-progress-sweep { from { transform: translateX(-100%) } to { transform: translateX(260%) } }
19
+ @keyframes asta-skeleton-shimmer { from { transform: translateX(-100%) } to { transform: translateX(100%) } }
20
+
21
+ /* Dialog: enters from a little below, scaled down. @starting-style gives the entry transition. */
22
+ .asta-dialog {
23
+ opacity: 0;
24
+ transform: translateY(12px) scale(0.96);
25
+ transition:
26
+ opacity 0.26s ease-in,
27
+ transform 0.32s cubic-bezier(0.7, 0, 0.84, 0),
28
+ overlay 0.32s allow-discrete,
29
+ display 0.32s allow-discrete;
30
+ }
31
+ .asta-dialog[open]:not([data-closing]) {
32
+ opacity: 1;
33
+ transform: none;
34
+ transition:
35
+ opacity 0.3s ease-out,
36
+ transform 0.52s cubic-bezier(0.16, 1, 0.3, 1),
37
+ overlay 0.52s allow-discrete,
38
+ display 0.52s allow-discrete;
39
+ }
40
+ @starting-style {
41
+ .asta-dialog[open] {
42
+ opacity: 0;
43
+ transform: translateY(12px) scale(0.96);
44
+ }
45
+ }
46
+ .asta-dialog::backdrop {
47
+ background: rgb(10 10 10 / 0.38);
48
+ opacity: 0;
49
+ transition:
50
+ opacity 0.26s ease-in,
51
+ overlay 0.32s allow-discrete,
52
+ display 0.32s allow-discrete;
53
+ }
54
+ .asta-dialog[open]:not([data-closing])::backdrop {
55
+ opacity: 1;
56
+ transition:
57
+ opacity 0.3s ease-out,
58
+ overlay 0.3s allow-discrete,
59
+ display 0.3s allow-discrete;
60
+ }
61
+ @starting-style {
62
+ .asta-dialog[open]::backdrop {
63
+ opacity: 0;
64
+ }
65
+ }
66
+
67
+ /* Toasts: asta.js sets transform, opacity and height inline; this is the timing. */
68
+ .asta-toast {
69
+ transition:
70
+ transform 0.45s cubic-bezier(0.16, 1, 0.3, 1),
71
+ opacity 0.3s ease,
72
+ height 0.35s cubic-bezier(0.16, 1, 0.3, 1); /* perf-ok: the tucked stack clamps to the front toast */
73
+ }
74
+
75
+ /* Menus drop in from the trigger. data-up flips them above it. */
76
+ [data-asta-menu] > [role="menu"] {
77
+ opacity: 0;
78
+ transform: translateY(-4px) scale(0.97);
79
+ transform-origin: top;
80
+ transition:
81
+ opacity 0.14s ease-in,
82
+ transform 0.16s ease-in;
83
+ }
84
+ [data-asta-menu][data-open] > [role="menu"] {
85
+ opacity: 1;
86
+ transform: none;
87
+ transition:
88
+ opacity 0.2s ease-out,
89
+ transform 0.28s cubic-bezier(0.16, 1, 0.3, 1);
90
+ }
91
+ @starting-style {
92
+ [data-asta-menu][data-open] > [role="menu"] {
93
+ opacity: 0;
94
+ transform: translateY(-4px) scale(0.97);
95
+ }
96
+ }
97
+ [data-asta-menu][data-up] > [role="menu"] {
98
+ top: auto;
99
+ bottom: 100%;
100
+ margin-top: 0;
101
+ margin-bottom: 6px;
102
+ transform-origin: bottom;
103
+ }
104
+ /* data-align="end" pins the menu to the trigger's right edge, so it grows from that corner. */
105
+ [data-asta-menu][data-align="end"] > [role="menu"] {
106
+ transform-origin: top right;
107
+ }
108
+ [data-asta-menu][data-align="end"][data-up] > [role="menu"] {
109
+ transform-origin: bottom right;
110
+ }
111
+
112
+ @media (prefers-reduced-motion: reduce) {
113
+ .asta-dialog,
114
+ .asta-dialog::backdrop,
115
+ .asta-toast,
116
+ [data-asta-menu] > [role="menu"] {
117
+ transition: none !important;
118
+ animation: none !important;
119
+ }
120
+ }
@@ -0,0 +1,441 @@
1
+ /* Astabyte UI runtime for the HTML + CSS layer. One small script, no dependencies.
2
+ * Everything is wired with event delegation on document, so markup swapped in by htmx works without re-init.
3
+ * Dialog <dialog class="asta-dialog">, opened by [data-asta-open="id"], closed by [data-asta-close]
4
+ * Toast asta.toast({ title, description, tone, duration, action: { label, href | event, detail } }),
5
+ * HX-Trigger "asta:toast", <template data-asta-toast>
6
+ * Tabs [data-asta-tabs] with [role=tab][aria-controls] and [role=tabpanel]
7
+ * Menu [data-asta-menu] with [data-asta-menu-trigger] and [role=menu] holding [role=menuitem]
8
+ */
9
+ (() => {
10
+ if (window.asta) return;
11
+ const reduce = () => matchMedia("(prefers-reduced-motion: reduce)").matches;
12
+ const byId = (id) => (id ? document.getElementById(id) : null);
13
+
14
+ /* ---------- Dialog ---------- */
15
+
16
+ function open(d) {
17
+ if (d && !d.open) d.showModal();
18
+ }
19
+ function close(d) {
20
+ if (!d || !d.open || d.dataset.closing) return;
21
+ // Let the CSS exit transition run before the dialog leaves the top layer.
22
+ if (reduce()) return d.close();
23
+ d.dataset.closing = "";
24
+ const done = () => {
25
+ delete d.dataset.closing;
26
+ d.close();
27
+ };
28
+ const t = setTimeout(done, 320);
29
+ d.addEventListener("transitionend", function end(e) {
30
+ if (e.target !== d || e.propertyName !== "opacity") return;
31
+ d.removeEventListener("transitionend", end);
32
+ clearTimeout(t);
33
+ done();
34
+ });
35
+ }
36
+
37
+ document.addEventListener("click", (e) => {
38
+ const t = e.target instanceof Element ? e.target : null;
39
+ if (!t) return;
40
+ const opener = t.closest("[data-asta-open]");
41
+ if (opener) {
42
+ e.preventDefault();
43
+ open(byId(opener.getAttribute("data-asta-open")));
44
+ return;
45
+ }
46
+ const closer = t.closest("[data-asta-close]");
47
+ if (closer) {
48
+ close(closer.closest("dialog"));
49
+ return;
50
+ }
51
+ // A click on the backdrop lands on the <dialog> itself, outside the panel's box.
52
+ if (t.matches("dialog.asta-dialog[open]")) {
53
+ const r = t.getBoundingClientRect();
54
+ if (e.clientX < r.left || e.clientX > r.right || e.clientY < r.top || e.clientY > r.bottom) close(t);
55
+ }
56
+ });
57
+ document.addEventListener(
58
+ "cancel",
59
+ (e) => {
60
+ if (e.target.matches?.("dialog.asta-dialog")) {
61
+ e.preventDefault();
62
+ close(e.target);
63
+ }
64
+ },
65
+ true,
66
+ );
67
+ document.addEventListener("asta:open", (e) => open(byId(e.detail?.id)));
68
+ document.addEventListener("asta:close", (e) => close(byId(e.detail?.id)));
69
+
70
+ /* ---------- Toast ---------- */
71
+
72
+ const TONE = {
73
+ success: { color: "#30a46c", path: "M4.5 8.5l2.2 2.2L11.5 5.5" },
74
+ error: { color: "#e5484d", path: "M8 4.5v4M8 11h.01" },
75
+ };
76
+ const esc = (s) => String(s ?? "").replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
77
+
78
+ // The stack sits bottom right, toasts tucked behind each other. Hover or focus fans it out and pauses every
79
+ // timer. Swipe a toast sideways to dismiss it. At most three show; the rest wait behind.
80
+ const MAX = 3;
81
+ const GAP = 8;
82
+ let fanned = false;
83
+ let list = null;
84
+
85
+ function stack() {
86
+ if (list?.isConnected) return list;
87
+ const section = document.createElement("section");
88
+ section.id = "asta-toasts";
89
+ section.setAttribute("aria-label", "Notifications");
90
+ section.className = "fixed bottom-4 left-4 right-4 z-40 sm:left-auto sm:w-[340px]";
91
+ list = document.createElement("ol");
92
+ list.setAttribute("aria-live", "polite");
93
+ list.className = "relative";
94
+ section.append(list);
95
+ section.addEventListener("pointerenter", () => fan(true));
96
+ section.addEventListener("pointerleave", () => fan(false));
97
+ section.addEventListener("focusin", () => fan(true));
98
+ section.addEventListener("focusout", (e) => section.contains(e.relatedTarget) || fan(false));
99
+ document.body.append(section);
100
+ return list;
101
+ }
102
+
103
+ const live = () => (list ? [...list.children].filter((li) => !li.dataset.leaving).reverse() : []); // newest first
104
+
105
+ function fan(on) {
106
+ if (fanned === on) return;
107
+ fanned = on;
108
+ for (const li of live()) on ? pause(li) : run(li);
109
+ layout();
110
+ }
111
+
112
+ function place(li, dx = 0) {
113
+ const ty = !li._shown ? 24 : li.dataset.leaving === "down" ? 16 : li._y;
114
+ li.style.transform = `translate3d(${dx}px, ${ty}px, 0) scale(${li._scale})`;
115
+ }
116
+
117
+ function layout() {
118
+ const items = live();
119
+ const front = items[0]?.firstElementChild.offsetHeight;
120
+ let above = 0;
121
+ items.forEach((li, i) => {
122
+ const tucked = !fanned && i > 0;
123
+ li._y = i === 0 ? 0 : fanned ? -above : -i * 12;
124
+ li._scale = tucked ? 1 - i * 0.05 : 1;
125
+ above += li.firstElementChild.offsetHeight + GAP;
126
+ li.style.zIndex = String(100 - i);
127
+ li.style.height = tucked ? `${front}px` : "";
128
+ li.firstElementChild.style.opacity = tucked ? "0" : "";
129
+ const off = !li._shown || i >= MAX;
130
+ li.style.opacity = off ? "0" : "";
131
+ li.style.pointerEvents = off ? "none" : "";
132
+ place(li);
133
+ });
134
+ }
135
+
136
+ function run(li) {
137
+ if (fanned || li.dataset.leaving) return;
138
+ li._start = Date.now();
139
+ li._timer = setTimeout(() => leave(li, "down"), li._left);
140
+ }
141
+ function pause(li) {
142
+ clearTimeout(li._timer);
143
+ li._left = Math.max(800, li._left - (Date.now() - li._start));
144
+ }
145
+
146
+ function toast(t) {
147
+ if (!t || !t.title) return;
148
+ const tone = TONE[t.tone];
149
+ const li = document.createElement("li");
150
+ li.className =
151
+ "asta-toast absolute bottom-0 left-0 right-0 origin-bottom touch-pan-y select-none overflow-hidden rounded-xl bg-paper shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--ink)_10%,transparent),0_12px_30px_-14px_rgb(0_0_0/0.35)]";
152
+ li.setAttribute("role", t.tone === "error" ? "alert" : "status");
153
+ const a = t.action;
154
+ const safe = (u) => /^(https?:\/\/|\/(?!\/)|\.{0,2}\/|\?|#)/i.test(String(u ?? ""));
155
+ const action = a?.label
156
+ ? a.href && safe(a.href)
157
+ ? `<a href="${esc(a.href)}" data-asta-toast-action class="flex h-7 shrink-0 items-center rounded-lg bg-ink px-2.5 text-[12px] font-medium text-paper outline-none transition-opacity hover:opacity-85 focus-visible:ring-2 focus-visible:ring-brand/50">${esc(a.label)}</a>`
158
+ : `<button type="button" data-asta-toast-action class="h-7 shrink-0 rounded-lg bg-ink px-2.5 text-[12px] font-medium text-paper outline-none transition-opacity hover:opacity-85 focus-visible:ring-2 focus-visible:ring-brand/50">${esc(a.label)}</button>`
159
+ : `<button type="button" data-asta-toast-close aria-label="Dismiss" class="flex h-6 w-6 shrink-0 items-center justify-center rounded-full text-mute outline-none transition-colors hover:bg-paper-2 hover:text-ink focus-visible:ring-2 focus-visible:ring-brand/50"><svg viewBox="0 0 16 16" class="h-3 w-3" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" aria-hidden="true"><path d="m4.5 4.5 7 7M11.5 4.5l-7 7"/></svg></button>`;
160
+ li.innerHTML = `<div class="flex items-start gap-3 p-3.5 pr-3 transition-opacity">${
161
+ tone
162
+ ? `<span class="mt-px flex h-5 w-5 shrink-0 items-center justify-center rounded-full text-white" style="background:${tone.color}"><svg viewBox="0 0 16 16" class="h-3 w-3" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="${tone.path}"/></svg></span>`
163
+ : ""
164
+ }<div class="min-w-0 flex-1"><p class="text-[13.5px] font-medium leading-5 tracking-[-0.01em] text-ink">${esc(t.title)}</p>${
165
+ t.description ? `<p class="mt-0.5 text-[12.5px] leading-snug text-mute">${esc(t.description)}</p>` : ""
166
+ }</div>${action}</div>`;
167
+
168
+ li._left = t.duration || 4000;
169
+ li._scale = 1;
170
+ li._y = 0;
171
+ stack().append(li);
172
+ layout();
173
+ requestAnimationFrame(() =>
174
+ requestAnimationFrame(() => {
175
+ li._shown = true;
176
+ layout();
177
+ }),
178
+ );
179
+ run(li);
180
+
181
+ li.querySelector("[data-asta-toast-close]")?.addEventListener("click", () => leave(li, "down"));
182
+ li.querySelector("[data-asta-toast-action]")?.addEventListener("click", () => {
183
+ // An action names an event (dispatched on document, with the toast's data) or a link.
184
+ if (a.event) document.dispatchEvent(new CustomEvent(a.event, { detail: a.detail ?? null, bubbles: true }));
185
+ leave(li, "down");
186
+ });
187
+
188
+ // Swipe sideways to dismiss.
189
+ let drag = null;
190
+ li.addEventListener("pointerdown", (e) => {
191
+ if (e.target.closest("button, a")) return;
192
+ li.setPointerCapture(e.pointerId);
193
+ drag = { x: e.clientX, t: Date.now() };
194
+ li.style.transition = "none";
195
+ });
196
+ li.addEventListener("pointermove", (e) => {
197
+ if (!drag) return;
198
+ const dx = e.clientX - drag.x;
199
+ place(li, dx);
200
+ li.style.opacity = String(1 - Math.min(0.7, Math.abs(dx) / 260));
201
+ });
202
+ const drop = (e, cancel) => {
203
+ if (!drag) return;
204
+ const dx = cancel ? 0 : e.clientX - drag.x;
205
+ const v = Math.abs(dx) / Math.max(1, Date.now() - drag.t);
206
+ drag = null;
207
+ li.style.transition = "";
208
+ if (Math.abs(dx) > 90 || v > 0.6) leave(li, dx < 0 ? "left" : "right");
209
+ else layout();
210
+ };
211
+ li.addEventListener("pointerup", (e) => drop(e, false));
212
+ li.addEventListener("pointercancel", (e) => drop(e, true));
213
+ }
214
+
215
+ function leave(li, dir = "down") {
216
+ if (!li || li.dataset.leaving) return;
217
+ clearTimeout(li._timer);
218
+ li.dataset.leaving = dir;
219
+ li.style.opacity = "0";
220
+ li.style.pointerEvents = "none";
221
+ place(li, dir === "left" ? -380 : dir === "right" ? 380 : 0);
222
+ layout();
223
+ setTimeout(() => li.remove(), reduce() ? 0 : 260);
224
+ }
225
+
226
+ // htmx hands a non-object HX-Trigger value over as { value }, so a list of toasts arrives wrapped.
227
+ const toasts = (d) => {
228
+ if (d && !d.title && d.value !== undefined) d = d.value;
229
+ (Array.isArray(d) ? d : [d]).forEach(toast);
230
+ };
231
+ document.addEventListener("asta:toast", (e) => toasts(e.detail));
232
+
233
+ /* ---------- Tabs ---------- */
234
+
235
+ // A shown panel gets "asta:show", so hx-trigger="asta:show once" loads it lazily.
236
+ function select(tab, focus) {
237
+ const root = tab.closest("[data-asta-tabs]");
238
+ if (!root) return;
239
+ for (const t of root.querySelectorAll('[role="tab"]')) {
240
+ if (t.closest("[data-asta-tabs]") !== root) continue;
241
+ const on = t === tab;
242
+ t.setAttribute("aria-selected", String(on));
243
+ t.tabIndex = on ? 0 : -1;
244
+ const panel = byId(t.getAttribute("aria-controls"));
245
+ if (panel) {
246
+ panel.hidden = !on;
247
+ if (on) panel.dispatchEvent(new CustomEvent("asta:show", { bubbles: true }));
248
+ }
249
+ }
250
+ if (focus) tab.focus();
251
+ slide(tab.closest('[role="tablist"]'), true);
252
+ root.dispatchEvent(new CustomEvent("asta:tab", { bubbles: true, detail: { tab } }));
253
+ }
254
+
255
+ // The indicator moves with transform only. Its width is set once per change, and a scaleX from the old width
256
+ // to the new one (FLIP) hides the jump.
257
+ function slide(list, animate) {
258
+ const bar = list?.querySelector(":scope > [data-asta-tab-indicator]");
259
+ const tab = list?.querySelector('[role="tab"][aria-selected="true"]');
260
+ if (!bar || !tab) return;
261
+ const x = tab.offsetLeft;
262
+ const w = tab.offsetWidth;
263
+ const px = parseFloat(bar.dataset.x ?? "");
264
+ const pw = parseFloat(bar.dataset.w ?? "");
265
+ bar.dataset.x = x;
266
+ bar.dataset.w = w;
267
+ bar.style.width = `${w}px`;
268
+ if (!animate || reduce() || !pw || (px === x && pw === w)) {
269
+ bar.style.transition = "none";
270
+ bar.style.transform = `translateX(${x}px)`;
271
+ bar.style.opacity = "1";
272
+ void bar.offsetWidth;
273
+ bar.style.transition = "";
274
+ return;
275
+ }
276
+ bar.style.transition = "none";
277
+ bar.style.transform = `translateX(${px}px) scaleX(${pw / w})`;
278
+ void bar.offsetWidth;
279
+ bar.style.transition = "";
280
+ bar.style.transform = `translateX(${x}px)`;
281
+ }
282
+
283
+ // Re-measure when a tab list or a tab changes size (layout, fonts), never per frame.
284
+ const sized = new ResizeObserver((entries) => {
285
+ const lists = new Set(entries.map((e) => e.target.closest('[role="tablist"]')));
286
+ for (const list of lists) slide(list, false);
287
+ });
288
+ function watchTabs(el) {
289
+ for (const bar of el.querySelectorAll("[data-asta-tab-indicator]")) {
290
+ const list = bar.parentElement;
291
+ if (list.dataset.astaWatched !== undefined) continue;
292
+ list.dataset.astaWatched = "";
293
+ sized.observe(list);
294
+ for (const t of list.querySelectorAll('[role="tab"]')) sized.observe(t);
295
+ }
296
+ }
297
+ if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", () => watchTabs(document));
298
+ else watchTabs(document);
299
+ document.addEventListener("htmx:afterSettle", (e) => watchTabs(e.detail?.elt?.parentElement || document));
300
+
301
+ document.addEventListener("click", (e) => {
302
+ const tab = e.target instanceof Element && e.target.closest('[data-asta-tabs] [role="tab"]');
303
+ if (tab && !tab.disabled) select(tab, false);
304
+ });
305
+ document.addEventListener("keydown", (e) => {
306
+ const tab = e.target instanceof Element && e.target.closest('[data-asta-tabs] [role="tab"]');
307
+ if (!tab) return;
308
+ const list = [...tab.closest('[role="tablist"]').querySelectorAll('[role="tab"]:not([disabled])')];
309
+ const i = list.indexOf(tab);
310
+ const next = { ArrowRight: i + 1, ArrowDown: i + 1, ArrowLeft: i - 1, ArrowUp: i - 1, Home: 0, End: list.length - 1 }[e.key];
311
+ if (next === undefined) return;
312
+ e.preventDefault();
313
+ // A click, so hx-get on a tab fires from the keyboard too. The click handler selects it.
314
+ const to = list[(next + list.length) % list.length];
315
+ to.focus();
316
+ to.click();
317
+ });
318
+
319
+ /* ---------- Menu ---------- */
320
+
321
+ // The nearest ancestor that cuts off overflow, if any.
322
+ function clipper(el) {
323
+ for (let p = el.parentElement; p && p !== document.body; p = p.parentElement) {
324
+ const o = getComputedStyle(p);
325
+ if (/auto|scroll|hidden|clip/.test(o.overflowX + o.overflowY)) return p;
326
+ }
327
+ return null;
328
+ }
329
+ // A pinned menu doesn't follow its trigger, so close it when anything scrolls.
330
+ addEventListener(
331
+ "scroll",
332
+ () => {
333
+ for (const m of document.querySelectorAll("[data-asta-menu][data-pinned][data-open]")) setMenu(m, false);
334
+ },
335
+ { capture: true, passive: true },
336
+ );
337
+ const items = (m) => [...m.querySelectorAll('[role="menuitem"]:not([aria-disabled="true"]):not([disabled])')];
338
+ function setMenu(root, on, focusFirst) {
339
+ const trigger = root.querySelector("[data-asta-menu-trigger]");
340
+ const menu = root.querySelector('[role="menu"]');
341
+ if (!trigger || !menu) return;
342
+ if (on) {
343
+ for (const other of document.querySelectorAll("[data-asta-menu][data-open]")) if (other !== root) setMenu(other, false);
344
+ root.setAttribute("data-open", "");
345
+ menu.hidden = false;
346
+ trigger.setAttribute("aria-expanded", "true");
347
+ menu.style.cssText = "";
348
+ const r = menu.getBoundingClientRect();
349
+ const box = clipper(root)?.getBoundingClientRect();
350
+ if (box && (r.bottom > box.bottom || r.right > box.right || r.left < box.left)) {
351
+ // Inside a scroll box (a table, a card) the menu would be cut off, so pin it to the viewport instead.
352
+ const t = trigger.getBoundingClientRect();
353
+ const up = t.bottom + r.height + 6 > innerHeight - 8 && t.top - r.height - 6 > 8;
354
+ const left = root.getAttribute("data-align") === "end" ? t.right - r.width : t.left;
355
+ Object.assign(menu.style, {
356
+ position: "fixed",
357
+ margin: "0",
358
+ right: "auto",
359
+ bottom: "auto",
360
+ top: `${up ? t.top - r.height - 6 : t.bottom + 6}px`,
361
+ left: `${Math.max(8, Math.min(left, innerWidth - r.width - 8))}px`,
362
+ });
363
+ root.toggleAttribute("data-up", up);
364
+ root.setAttribute("data-pinned", "");
365
+ } else {
366
+ // Flip up when there's no room below.
367
+ root.toggleAttribute("data-up", r.bottom > innerHeight - 8 && r.top - r.height > 8);
368
+ root.removeAttribute("data-pinned");
369
+ }
370
+ if (focusFirst) items(menu)[0]?.focus({ preventScroll: true });
371
+ } else if (root.hasAttribute("data-open")) {
372
+ root.removeAttribute("data-open");
373
+ trigger.setAttribute("aria-expanded", "false");
374
+ setTimeout(() => {
375
+ if (!root.hasAttribute("data-open")) menu.hidden = true;
376
+ }, reduce() ? 0 : 160);
377
+ }
378
+ }
379
+ document.addEventListener("click", (e) => {
380
+ const t = e.target instanceof Element ? e.target : null;
381
+ const trigger = t?.closest("[data-asta-menu-trigger]");
382
+ if (trigger) {
383
+ const root = trigger.closest("[data-asta-menu]");
384
+ setMenu(root, !root.hasAttribute("data-open"), e.detail === 0);
385
+ return;
386
+ }
387
+ const item = t?.closest('[data-asta-menu] [role="menuitem"]');
388
+ for (const root of document.querySelectorAll("[data-asta-menu][data-open]")) {
389
+ if (item && root.contains(item)) {
390
+ if (!item.hasAttribute("data-keep-open")) setMenu(root, false);
391
+ } else if (!root.contains(t)) setMenu(root, false);
392
+ }
393
+ });
394
+ document.addEventListener("keydown", (e) => {
395
+ const t = e.target instanceof Element ? e.target : null;
396
+ const root = t?.closest("[data-asta-menu]");
397
+ if (!root) return;
398
+ const menu = root.querySelector('[role="menu"]');
399
+ const trigger = root.querySelector("[data-asta-menu-trigger]");
400
+ if (t === trigger && (e.key === "ArrowDown" || e.key === "ArrowUp")) {
401
+ e.preventDefault();
402
+ setMenu(root, true, true);
403
+ return;
404
+ }
405
+ if (!root.hasAttribute("data-open")) return;
406
+ const list = items(menu);
407
+ const i = list.indexOf(document.activeElement);
408
+ if (e.key === "Escape") {
409
+ e.preventDefault();
410
+ setMenu(root, false);
411
+ trigger.focus();
412
+ } else if (e.key === "ArrowDown" || e.key === "ArrowUp") {
413
+ e.preventDefault();
414
+ list[(i + (e.key === "ArrowDown" ? 1 : -1) + list.length) % list.length]?.focus();
415
+ } else if (e.key === "Home" || e.key === "End") {
416
+ e.preventDefault();
417
+ list[e.key === "Home" ? 0 : list.length - 1]?.focus();
418
+ } else if (e.key === "Tab") setMenu(root, false);
419
+ });
420
+
421
+ /* ---------- Boot and htmx ---------- */
422
+
423
+ function scan(el) {
424
+ for (const d of el.querySelectorAll("dialog.asta-dialog[data-asta-autoopen]")) {
425
+ d.removeAttribute("data-asta-autoopen");
426
+ open(d);
427
+ }
428
+ for (const tpl of el.querySelectorAll("template[data-asta-toast]")) {
429
+ try {
430
+ toasts(JSON.parse(tpl.getAttribute("data-asta-toast")));
431
+ } catch {}
432
+ tpl.remove();
433
+ }
434
+ }
435
+ const boot = () => scan(document);
436
+ if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", boot);
437
+ else boot();
438
+ document.addEventListener("htmx:afterSettle", (e) => scan(e.detail?.elt?.parentElement || document));
439
+
440
+ window.asta = { toast, open: (id) => open(byId(id)), close: (id) => close(byId(id)), version: "0.7.0" };
441
+ })();