@rubenlangeweg/mero-ui 0.7.0 → 0.9.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.
- package/CHANGELOG.md +25 -0
- package/README.md +25 -4
- package/agent-docs/components/accordion.md +1 -1
- package/agent-docs/components/action-bar.md +1 -1
- package/agent-docs/components/action-button.md +32 -1
- package/agent-docs/components/agenda.md +1 -1
- package/agent-docs/components/alert-dialog.md +1 -1
- package/agent-docs/components/alert.md +1 -1
- package/agent-docs/components/app-layout.md +1 -1
- package/agent-docs/components/area-chart.md +1 -1
- package/agent-docs/components/aura.md +1 -1
- package/agent-docs/components/autocomplete.md +1 -1
- package/agent-docs/components/avatar-group.md +1 -1
- package/agent-docs/components/avatar.md +1 -1
- package/agent-docs/components/badge.md +1 -1
- package/agent-docs/components/bar-chart.md +1 -1
- package/agent-docs/components/breadcrumbs.md +1 -1
- package/agent-docs/components/browser-mockup.md +1 -1
- package/agent-docs/components/button-group.md +1 -1
- package/agent-docs/components/button.md +32 -1
- package/agent-docs/components/calendar.md +3 -3
- package/agent-docs/components/card.md +56 -1
- package/agent-docs/components/carousel.md +1 -1
- package/agent-docs/components/cell-color-picker.md +1 -1
- package/agent-docs/components/cell-select.md +1 -1
- package/agent-docs/components/cell-slider.md +1 -1
- package/agent-docs/components/cell-switch.md +1 -1
- package/agent-docs/components/chain-of-thought.md +1 -1
- package/agent-docs/components/chart-tooltip.md +1 -1
- package/agent-docs/components/chat-attachment.md +1 -1
- package/agent-docs/components/chat-conversation.md +1 -1
- package/agent-docs/components/chat-list-view.md +1 -1
- package/agent-docs/components/chat-loader.md +1 -1
- package/agent-docs/components/chat-message-actions.md +1 -1
- package/agent-docs/components/chat-message.md +1 -1
- package/agent-docs/components/chat-source.md +1 -1
- package/agent-docs/components/chat-tool.md +1 -1
- package/agent-docs/components/checkbox-button-group.md +1 -1
- package/agent-docs/components/checkbox-group.md +1 -1
- package/agent-docs/components/checkbox.md +1 -1
- package/agent-docs/components/chip.md +1 -1
- package/agent-docs/components/close-button.md +1 -1
- package/agent-docs/components/code-block.md +1 -1
- package/agent-docs/components/code-mockup.md +1 -1
- package/agent-docs/components/color-area.md +1 -1
- package/agent-docs/components/color-field.md +1 -1
- package/agent-docs/components/color-picker.md +1 -1
- package/agent-docs/components/color-slider.md +1 -1
- package/agent-docs/components/color-swatch.md +1 -1
- package/agent-docs/components/combo-box.md +1 -1
- package/agent-docs/components/command-menu.md +1 -1
- package/agent-docs/components/command.md +1 -1
- package/agent-docs/components/composed-chart.md +1 -1
- package/agent-docs/components/context-menu.md +1 -1
- package/agent-docs/components/countdown.md +1 -1
- package/agent-docs/components/data-grid.md +1 -1
- package/agent-docs/components/date-field.md +1 -1
- package/agent-docs/components/date-picker.md +1 -1
- package/agent-docs/components/date-range-picker.md +1 -1
- package/agent-docs/components/description-list.md +30 -0
- package/agent-docs/components/description.md +1 -1
- package/agent-docs/components/diff.md +1 -1
- package/agent-docs/components/disclosure-group.md +1 -1
- package/agent-docs/components/disclosure.md +1 -1
- package/agent-docs/components/dock.md +1 -1
- package/agent-docs/components/drawer.md +83 -1
- package/agent-docs/components/drop-zone.md +1 -1
- package/agent-docs/components/dropdown.md +1 -1
- package/agent-docs/components/emoji-picker.md +1 -1
- package/agent-docs/components/emoji-reaction-button.md +1 -1
- package/agent-docs/components/empty-state.md +36 -4
- package/agent-docs/components/error-message.md +1 -1
- package/agent-docs/components/fab.md +1 -1
- package/agent-docs/components/field-error.md +1 -1
- package/agent-docs/components/fieldset.md +45 -1
- package/agent-docs/components/file-input.md +1 -1
- package/agent-docs/components/file-tree.md +1 -1
- package/agent-docs/components/filter-chips.md +1 -1
- package/agent-docs/components/floating-toc.md +1 -1
- package/agent-docs/components/footer.md +1 -1
- package/agent-docs/components/form.md +155 -1
- package/agent-docs/components/hero.md +1 -1
- package/agent-docs/components/holo-card.md +1 -1
- package/agent-docs/components/hover-card.md +1 -1
- package/agent-docs/components/hover-gallery.md +1 -1
- package/agent-docs/components/indicator.md +1 -1
- package/agent-docs/components/inline-select.md +1 -1
- package/agent-docs/components/input-group.md +1 -1
- package/agent-docs/components/input-otp.md +1 -1
- package/agent-docs/components/input.md +1 -1
- package/agent-docs/components/item-card-group.md +1 -1
- package/agent-docs/components/item-card.md +1 -1
- package/agent-docs/components/join.md +1 -1
- package/agent-docs/components/joined-filter.md +1 -1
- package/agent-docs/components/kanban.md +1 -1
- package/agent-docs/components/kbd.md +32 -1
- package/agent-docs/components/kpi-group.md +1 -1
- package/agent-docs/components/kpi.md +1 -1
- package/agent-docs/components/label.md +1 -1
- package/agent-docs/components/line-chart.md +1 -1
- package/agent-docs/components/link.md +1 -1
- package/agent-docs/components/list-box.md +1 -1
- package/agent-docs/components/list-view.md +1 -1
- package/agent-docs/components/live-progress.md +34 -1
- package/agent-docs/components/map.md +1 -1
- package/agent-docs/components/markdown.md +1 -1
- package/agent-docs/components/mask.md +1 -1
- package/agent-docs/components/megamenu.md +1 -1
- package/agent-docs/components/menu-button.md +35 -1
- package/agent-docs/components/menu.md +1 -1
- package/agent-docs/components/meter.md +1 -1
- package/agent-docs/components/modal.md +1 -1
- package/agent-docs/components/native-select.md +1 -1
- package/agent-docs/components/navbar.md +1 -1
- package/agent-docs/components/number-field.md +1 -1
- package/agent-docs/components/number-stepper.md +1 -1
- package/agent-docs/components/number-value.md +1 -1
- package/agent-docs/components/page-header.md +69 -0
- package/agent-docs/components/pagination.md +1 -1
- package/agent-docs/components/phone-mockup.md +1 -1
- package/agent-docs/components/pie-chart.md +1 -1
- package/agent-docs/components/popover.md +1 -1
- package/agent-docs/components/pressable-feedback.md +1 -1
- package/agent-docs/components/progress-bar.md +1 -1
- package/agent-docs/components/progress-circle.md +1 -1
- package/agent-docs/components/prompt-input.md +1 -1
- package/agent-docs/components/prompt-suggestion.md +1 -1
- package/agent-docs/components/radar-chart.md +1 -1
- package/agent-docs/components/radial-chart.md +1 -1
- package/agent-docs/components/radio-button-group.md +1 -1
- package/agent-docs/components/radio-group.md +1 -1
- package/agent-docs/components/range-calendar.md +1 -1
- package/agent-docs/components/rating.md +1 -1
- package/agent-docs/components/resizable.md +1 -1
- package/agent-docs/components/rich-text-editor.md +1 -1
- package/agent-docs/components/scroll-shadow.md +1 -1
- package/agent-docs/components/search-field.md +43 -1
- package/agent-docs/components/segment.md +1 -1
- package/agent-docs/components/select.md +1 -1
- package/agent-docs/components/separator.md +1 -1
- package/agent-docs/components/sheet.md +1 -1
- package/agent-docs/components/sidebar.md +1 -1
- package/agent-docs/components/skeleton.md +1 -1
- package/agent-docs/components/slider.md +1 -1
- package/agent-docs/components/sortable-table.md +1 -1
- package/agent-docs/components/spinner.md +1 -1
- package/agent-docs/components/stack.md +1 -1
- package/agent-docs/components/status.md +1 -1
- package/agent-docs/components/stepper.md +1 -1
- package/agent-docs/components/surface.md +1 -1
- package/agent-docs/components/swap.md +1 -1
- package/agent-docs/components/switch.md +1 -1
- package/agent-docs/components/table.md +58 -1
- package/agent-docs/components/tabs.md +24 -1
- package/agent-docs/components/tag-group.md +1 -1
- package/agent-docs/components/text-area.md +1 -1
- package/agent-docs/components/text-field.md +1 -1
- package/agent-docs/components/text-rotate.md +1 -1
- package/agent-docs/components/text-shimmer.md +1 -1
- package/agent-docs/components/theme-controller.md +1 -1
- package/agent-docs/components/time-field.md +1 -1
- package/agent-docs/components/timeline.md +1 -1
- package/agent-docs/components/toast.md +1 -1
- package/agent-docs/components/toggle-button-group.md +1 -1
- package/agent-docs/components/toggle-button.md +1 -1
- package/agent-docs/components/toolbar.md +1 -1
- package/agent-docs/components/tooltip.md +1 -1
- package/agent-docs/components/trend-chip.md +1 -1
- package/agent-docs/components/typed-confirmation.md +1 -1
- package/agent-docs/components/typography.md +24 -1
- package/agent-docs/components/validator.md +1 -1
- package/agent-docs/components/visually-hidden.md +27 -0
- package/agent-docs/components/widget.md +1 -1
- package/agent-docs/components/window-mockup.md +1 -1
- package/agent-docs/llms-components.txt +989 -194
- package/agent-docs/llms-full.txt +1079 -206
- package/agent-docs/llms-patterns.txt +91 -13
- package/agent-docs/llms.txt +5 -2
- package/package.json +4 -1
- package/ui/insight-charts.mjs +1 -1
- package/ui/insight-markdown.mjs +1 -1
- package/ui/mero-collections.css +1 -7
- package/ui/mero-collections.js +1 -1
- package/ui/mero-composition.css +1 -1
- package/ui/mero-composition.js +1 -1
- package/ui/mero-forms.js +415 -27
- package/ui/mero-gallery.css +1 -1
- package/ui/mero-gallery.js +1 -1
- package/ui/mero-insights.css +1 -1
- package/ui/mero-insights.js +1 -1
- package/ui/mero-inventory.css +1 -1
- package/ui/mero-inventory.js +1 -1
- package/ui/mero-layouts.css +1 -4
- package/ui/mero-live.js +25 -4
- package/ui/mero-workspace.css +1 -1
- package/ui/mero-workspace.js +1 -1
- package/ui/mero.css +104 -5
- package/ui/mero.js +220 -8
package/ui/mero-forms.js
CHANGED
|
@@ -1,20 +1,57 @@
|
|
|
1
|
-
/*! @rubenlangeweg/mero-ui 0.
|
|
1
|
+
/*! @rubenlangeweg/mero-ui 0.9.0 | MIT */
|
|
2
2
|
/**
|
|
3
3
|
* Optional HTMX form behavior. Server responses:
|
|
4
4
|
* 422 + replacement form for validation, or HX-Trigger:
|
|
5
5
|
* {"mero:form-saved":{"message":"Saved"}}, which htmx 4 fires after the swap settles.
|
|
6
|
-
* Standalone [data-mero-action] buttons
|
|
7
|
-
* pending|succeeded|rejected|unknown: 2xx succeeded,
|
|
8
|
-
* anything else unknown
|
|
6
|
+
* Standalone [data-mero-action] buttons, and the submitter of a data-mero-form,
|
|
7
|
+
* get data-mero-action-state pending|succeeded|rejected|unknown: 2xx succeeded,
|
|
8
|
+
* 4xx (not 408) rejected, anything else unknown, including a request aborted
|
|
9
|
+
* after it was sent. Mero-Action-Outcome and Mero-Action-Message headers
|
|
9
10
|
* override the state and text; a bubbling mero:action event reports each step.
|
|
11
|
+
* data-mero-action-expect turns a success without the expected content into unknown.
|
|
10
12
|
*/
|
|
11
13
|
(() => {
|
|
12
14
|
const formSelector = "form[data-mero-form]";
|
|
13
15
|
const confirmSelector = "form[data-mero-form][data-mero-confirm]";
|
|
14
16
|
const requesting = new WeakMap();
|
|
15
17
|
const formRequests = new WeakMap();
|
|
18
|
+
// A swap can detach the button before finally:request, so the request remembers its button and status.
|
|
19
|
+
const actionRequests = new WeakMap();
|
|
16
20
|
// htmx 4 also reports requests a before:request listener cancelled: no response, still "issuing".
|
|
21
|
+
// Once fetch has started, an abort (hx-sync, htmx:abort, timeout) lands in htmx's catch as "error: AbortError…",
|
|
22
|
+
// so a request that may have reached the server never counts as cancelled.
|
|
17
23
|
const cancelled = ctx => !ctx.response && !String(ctx.status).startsWith("error");
|
|
24
|
+
const outcomes = ["succeeded", "rejected", "unknown"];
|
|
25
|
+
// A knob on the submitter wins over the same knob on its form.
|
|
26
|
+
const knob = (owners, name) => owners.find(owner => owner?.hasAttribute(name))?.getAttribute(name) ?? null;
|
|
27
|
+
|
|
28
|
+
// Outcome of a request that was sent. The client-side expect check can only lower success to unknown,
|
|
29
|
+
// and then the server's success message is not shown.
|
|
30
|
+
function serverOutcome(ctx) {
|
|
31
|
+
const status = ctx.response?.status ?? 0;
|
|
32
|
+
const header = ctx.response?.headers.get("Mero-Action-Outcome");
|
|
33
|
+
return outcomes.includes(header) ? header
|
|
34
|
+
: status >= 200 && status < 300 ? "succeeded"
|
|
35
|
+
: status >= 400 && status < 500 && status !== 408 ? "rejected" : "unknown";
|
|
36
|
+
}
|
|
37
|
+
function outcomeOf(ctx, owners) {
|
|
38
|
+
const state = serverOutcome(ctx);
|
|
39
|
+
const expect = knob(owners, "data-mero-action-expect");
|
|
40
|
+
if (state !== "succeeded" || !expect) return state;
|
|
41
|
+
const template = document.createElement("template");
|
|
42
|
+
template.innerHTML = ctx.text ?? "";
|
|
43
|
+
try { return template.content.querySelector(expect) ? state : "unknown"; } catch { return "unknown"; }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// data-mero-action-unknown is either the text to show or "#id" of a hidden recovery panel to reveal.
|
|
47
|
+
function unknownPanel(owners) {
|
|
48
|
+
const value = knob(owners, "data-mero-action-unknown");
|
|
49
|
+
return value?.startsWith("#") ? document.getElementById(value.slice(1)) : null;
|
|
50
|
+
}
|
|
51
|
+
function unknownText(owners) {
|
|
52
|
+
const value = knob(owners, "data-mero-action-unknown");
|
|
53
|
+
return value && !value.startsWith("#") ? value : null;
|
|
54
|
+
}
|
|
18
55
|
|
|
19
56
|
function requestForm(event) {
|
|
20
57
|
const source = event.detail.ctx?.sourceElement;
|
|
@@ -74,17 +111,47 @@
|
|
|
74
111
|
event.preventDefault();
|
|
75
112
|
return;
|
|
76
113
|
}
|
|
77
|
-
|
|
114
|
+
const { ctx } = event.detail;
|
|
115
|
+
formRequests.set(ctx, form);
|
|
78
116
|
const buttons = [...form.elements].filter(control => !control.disabled && (control.type === "submit" || control.type === "image"));
|
|
79
|
-
|
|
117
|
+
const submitter = ctx.request.submitter?.form === form ? ctx.request.submitter : null;
|
|
118
|
+
requesting.set(form, { buttons, ctx, owners: [submitter, form] });
|
|
80
119
|
form.setAttribute("aria-busy", "true");
|
|
81
120
|
buttons.forEach((button) => { button.disabled = true; });
|
|
82
121
|
const error = form.querySelector("[data-mero-form-error]");
|
|
83
122
|
if (error) error.hidden = true;
|
|
123
|
+
const panel = unknownPanel([submitter, form]);
|
|
124
|
+
if (panel) panel.hidden = true;
|
|
125
|
+
(submitter ?? form).dataset.meroActionState = "pending";
|
|
126
|
+
form.dispatchEvent(new CustomEvent("mero:action", { bubbles: true, detail: { state: "pending" } }));
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// Decide before htmx applies HX-Trigger: a response that is not a confirmed
|
|
130
|
+
// success must never close the dialog or announce "saved".
|
|
131
|
+
document.addEventListener("htmx:after:request", (event) => {
|
|
132
|
+
const { ctx } = event.detail;
|
|
133
|
+
const form = formRequests.get(ctx);
|
|
134
|
+
if (!form || !ctx.hx?.trigger) return;
|
|
135
|
+
if (outcomeOf(ctx, requesting.get(form)?.owners ?? [form]) === "succeeded") return;
|
|
136
|
+
try {
|
|
137
|
+
const triggers = JSON.parse(ctx.hx.trigger);
|
|
138
|
+
delete triggers["mero:form-saved"];
|
|
139
|
+
ctx.hx.trigger = JSON.stringify(triggers);
|
|
140
|
+
} catch {
|
|
141
|
+
ctx.hx.trigger = ctx.hx.trigger.split(",").map(name => name.trim()).filter(name => name !== "mero:form-saved").join(", ");
|
|
142
|
+
}
|
|
84
143
|
});
|
|
85
144
|
|
|
86
145
|
document.addEventListener("htmx:before:swap", (event) => {
|
|
87
|
-
const
|
|
146
|
+
const { ctx } = event.detail;
|
|
147
|
+
const form = formRequests.get(ctx);
|
|
148
|
+
const owners = form ? requesting.get(form)?.owners : actionRequests.get(ctx) && [actionRequests.get(ctx).action];
|
|
149
|
+
// A response that fails its expect check is not the page the server meant to send.
|
|
150
|
+
if (owners && knob(owners, "data-mero-action-expect") && serverOutcome(ctx) === "succeeded" && outcomeOf(ctx, owners) === "unknown") {
|
|
151
|
+
event.preventDefault();
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const status = ctx.response?.status;
|
|
88
155
|
if (!(status >= 400) || !requestForm(event)) return;
|
|
89
156
|
// htmx 4 swaps every error response. Only a 422 carries a replacement form.
|
|
90
157
|
if (status !== 422) event.preventDefault();
|
|
@@ -112,27 +179,52 @@
|
|
|
112
179
|
const form = formRequests.get(ctx);
|
|
113
180
|
// A cancelled duplicate has no entry, so it never releases the request still in flight.
|
|
114
181
|
if (!form) return;
|
|
182
|
+
const owners = requesting.get(form)?.owners ?? [form];
|
|
183
|
+
const holder = owners.find(Boolean);
|
|
115
184
|
form.removeAttribute("aria-busy");
|
|
116
185
|
requesting.get(form)?.buttons.forEach((button) => { button.disabled = false; });
|
|
117
186
|
requesting.delete(form);
|
|
118
187
|
const typed = form.matches(confirmSelector);
|
|
119
188
|
if (typed) checkConfirmation(form);
|
|
120
|
-
if (cancelled(ctx))
|
|
121
|
-
|
|
122
|
-
if (typed) {
|
|
123
|
-
if (!status || status >= 500 || String(ctx.status).startsWith("error")) {
|
|
124
|
-
showError(form, "Outcome unknown. Check the current state before retrying.");
|
|
125
|
-
} else if (status >= 400 && status !== 422) {
|
|
126
|
-
showError(form, ctx.response.headers.get("Mero-Form-Error") || "The server rejected this action.");
|
|
127
|
-
}
|
|
189
|
+
if (cancelled(ctx)) {
|
|
190
|
+
delete holder.dataset.meroActionState;
|
|
128
191
|
return;
|
|
129
192
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
193
|
+
// After a 422 swap htmx points the source at the replacement form.
|
|
194
|
+
const live = form.isConnected ? form : ctx.sourceElement?.matches?.(formSelector) && ctx.sourceElement.isConnected ? ctx.sourceElement : null;
|
|
195
|
+
if (form.hasAttribute("data-mero-clear-passwords")) {
|
|
196
|
+
for (const node of new Set([form, live])) {
|
|
197
|
+
node?.querySelectorAll('input[type="password"]').forEach(input => { input.value = ""; });
|
|
198
|
+
}
|
|
135
199
|
}
|
|
200
|
+
const state = outcomeOf(ctx, owners);
|
|
201
|
+
const status = ctx.response?.status ?? 0;
|
|
202
|
+
const headerMessage = state === serverOutcome(ctx) && ctx.response?.headers.get("Mero-Action-Message") || null;
|
|
203
|
+
if (holder.isConnected) holder.dataset.meroActionState = state;
|
|
204
|
+
const explicit = outcomes.includes(ctx.response?.headers.get("Mero-Action-Outcome"));
|
|
205
|
+
let message = headerMessage;
|
|
206
|
+
if (state === "succeeded") {
|
|
207
|
+
if (message) window.MeroUI?.toast(message, { tone: "success", trigger: live ?? undefined });
|
|
208
|
+
} else if (state === "rejected" && status === 422 && !explicit) {
|
|
209
|
+
// The replacement form shows its own field errors.
|
|
210
|
+
} else {
|
|
211
|
+
if (state === "unknown") {
|
|
212
|
+
message ||= unknownText(owners);
|
|
213
|
+
const panel = unknownPanel(owners);
|
|
214
|
+
if (panel) panel.hidden = false;
|
|
215
|
+
}
|
|
216
|
+
message ||= ctx.response?.headers.get("Mero-Form-Error") || null;
|
|
217
|
+
if (!message) {
|
|
218
|
+
const signal = ctx.request.signal;
|
|
219
|
+
message = typed
|
|
220
|
+
? (state === "unknown" ? "Outcome unknown. Check the current state before retrying." : "The server rejected this action.")
|
|
221
|
+
: !ctx.response && (signal?.reason === "timeout" || !signal?.aborted)
|
|
222
|
+
? "The server could not be reached. Check whether the change was saved before retrying."
|
|
223
|
+
: "The request could not be completed. Check the current state and try again.";
|
|
224
|
+
}
|
|
225
|
+
if (live) showError(live, message);
|
|
226
|
+
}
|
|
227
|
+
(live ?? document).dispatchEvent(new CustomEvent("mero:action", { bubbles: true, detail: { state, status, message } }));
|
|
136
228
|
});
|
|
137
229
|
|
|
138
230
|
document.addEventListener("mero:form-saved", (event) => {
|
|
@@ -171,8 +263,6 @@
|
|
|
171
263
|
if (actionOf(event)?.dataset.meroActionState === "pending") event.preventDefault();
|
|
172
264
|
});
|
|
173
265
|
|
|
174
|
-
// A swap can detach the button before finally:request, so the request remembers its button and status.
|
|
175
|
-
const actionRequests = new WeakMap();
|
|
176
266
|
|
|
177
267
|
document.addEventListener("htmx:before:request", (event) => {
|
|
178
268
|
const action = actionOf(event);
|
|
@@ -183,6 +273,8 @@
|
|
|
183
273
|
action.setAttribute("aria-busy", "true");
|
|
184
274
|
action.setAttribute("aria-disabled", "true");
|
|
185
275
|
if (output) { output.textContent = ""; delete output.dataset.tone; }
|
|
276
|
+
const panel = unknownPanel([action]);
|
|
277
|
+
if (panel) panel.hidden = true;
|
|
186
278
|
action.dispatchEvent(new CustomEvent("mero:action", { bubbles: true, detail: { state: "pending" } }));
|
|
187
279
|
});
|
|
188
280
|
|
|
@@ -201,12 +293,14 @@
|
|
|
201
293
|
return;
|
|
202
294
|
}
|
|
203
295
|
const status = ctx.response?.status ?? 0;
|
|
204
|
-
const
|
|
205
|
-
const state = Object.hasOwn(actionMessages, header) ? header
|
|
206
|
-
: status >= 200 && status < 300 ? "succeeded"
|
|
207
|
-
: status >= 400 && status < 500 && status !== 408 ? "rejected" : "unknown";
|
|
296
|
+
const state = outcomeOf(ctx, [action]);
|
|
208
297
|
const [fallback, tone] = actionMessages[state];
|
|
209
|
-
const
|
|
298
|
+
const headerMessage = state === serverOutcome(ctx) && ctx.response?.headers.get("Mero-Action-Message");
|
|
299
|
+
const message = headerMessage || (state === "unknown" && unknownText([action])) || fallback;
|
|
300
|
+
if (state === "unknown") {
|
|
301
|
+
const panel = unknownPanel([action]);
|
|
302
|
+
if (panel) panel.hidden = false;
|
|
303
|
+
}
|
|
210
304
|
action.removeAttribute("aria-busy");
|
|
211
305
|
action.removeAttribute("aria-disabled");
|
|
212
306
|
action.dataset.meroActionState = state;
|
|
@@ -220,4 +314,298 @@
|
|
|
220
314
|
}
|
|
221
315
|
(action.isConnected ? action : document).dispatchEvent(new CustomEvent("mero:action", { bubbles: true, detail: { state, status, message } }));
|
|
222
316
|
});
|
|
317
|
+
// Form state helpers. [data-mero-show-when="field=value other"] shows a branch only while the form's
|
|
318
|
+
// field has one of the values, and disables its controls while hidden so they neither validate nor submit.
|
|
319
|
+
// form[data-mero-dirty] keeps its submit buttons disabled until a control differs from the rendered value.
|
|
320
|
+
const disabledByBranch = new WeakSet();
|
|
321
|
+
function fieldValues(form, name) {
|
|
322
|
+
const values = [];
|
|
323
|
+
for (const control of form.elements) {
|
|
324
|
+
if (control.name !== name || control.disabled) continue;
|
|
325
|
+
if (control.matches("input[type=checkbox], input[type=radio]")) { if (control.checked) values.push(control.value); }
|
|
326
|
+
else if (control.matches("select")) values.push(...[...control.selectedOptions].map(option => option.value));
|
|
327
|
+
else values.push(control.value);
|
|
328
|
+
}
|
|
329
|
+
return values;
|
|
330
|
+
}
|
|
331
|
+
function changed(form) {
|
|
332
|
+
return [...form.elements].some(control => {
|
|
333
|
+
if (control.matches("input[type=checkbox], input[type=radio]")) return control.checked !== control.defaultChecked;
|
|
334
|
+
if (control.matches("select")) return [...control.options].some(option => option.selected !== option.defaultSelected);
|
|
335
|
+
return control.matches("input, textarea") && !control.matches("[type=submit], [type=button], [type=reset], [type=image]") && control.value !== control.defaultValue;
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
function updateForm(form) {
|
|
339
|
+
// Outer branches first, so a nested branch reads the state its parent left.
|
|
340
|
+
for (const branch of form.querySelectorAll("[data-mero-show-when]")) {
|
|
341
|
+
const [name, wanted = ""] = branch.getAttribute("data-mero-show-when").split("=");
|
|
342
|
+
const values = fieldValues(form, name.trim());
|
|
343
|
+
const show = wanted.trim().split(/\s+/).some(value => values.includes(value)) && !branch.parentElement.closest("[data-mero-show-when][hidden]");
|
|
344
|
+
branch.hidden = !show;
|
|
345
|
+
if (branch.matches("fieldset")) branch.disabled = !show;
|
|
346
|
+
else for (const control of branch.querySelectorAll("input, select, textarea, button")) {
|
|
347
|
+
if (!show && !control.disabled) { control.disabled = true; disabledByBranch.add(control); }
|
|
348
|
+
else if (show && disabledByBranch.delete(control)) control.disabled = false;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (form.matches("[data-mero-dirty]") && !requesting.has(form)) {
|
|
352
|
+
const clean = !changed(form);
|
|
353
|
+
for (const button of form.querySelectorAll("button:not([type]), [type=submit], [type=image]")) {
|
|
354
|
+
if (!button.closest("[data-mero-show-when][hidden]")) button.disabled = clean;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
const stateForms = root => [...(root.matches?.("form") ? [root] : []), ...(root.querySelectorAll?.("form") ?? [])]
|
|
359
|
+
.filter(form => form.matches("[data-mero-dirty]") || form.querySelector("[data-mero-show-when]"));
|
|
360
|
+
for (const type of ["input", "change"]) document.addEventListener(type, event => {
|
|
361
|
+
const form = event.target.form ?? event.target.closest?.("form");
|
|
362
|
+
if (form && stateForms(form).length) updateForm(form);
|
|
363
|
+
});
|
|
364
|
+
document.addEventListener("reset", event => {
|
|
365
|
+
if (stateForms(event.target).length) setTimeout(() => updateForm(event.target));
|
|
366
|
+
});
|
|
367
|
+
// Swapped-in forms, and forms released after a request, which re-enables their submit buttons.
|
|
368
|
+
document.addEventListener("htmx:after:settle", event => stateForms(event.target).forEach(updateForm));
|
|
369
|
+
document.addEventListener("htmx:finally:request", event => {
|
|
370
|
+
const form = event.detail.ctx.sourceElement?.closest?.("form");
|
|
371
|
+
if (form && stateForms(form).length) queueMicrotask(() => updateForm(form));
|
|
372
|
+
});
|
|
373
|
+
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", () => stateForms(document).forEach(updateForm), { once: true });
|
|
374
|
+
else stateForms(document).forEach(updateForm);
|
|
375
|
+
// data-mero-action-reread="#region": after an unknown outcome, read the current page again with a GET
|
|
376
|
+
// and morph that region, so the person sees what actually happened. Mero never re-sends the action.
|
|
377
|
+
// The answer is dropped when the page moved on, the region is gone or a newer read started.
|
|
378
|
+
let rereads = 0;
|
|
379
|
+
document.addEventListener("mero:action", event => {
|
|
380
|
+
const { state, message } = event.detail;
|
|
381
|
+
const owner = event.target.closest?.("[data-mero-action-reread]");
|
|
382
|
+
if (state !== "unknown" || !owner || !window.htmx) return;
|
|
383
|
+
const selector = owner.getAttribute("data-mero-action-reread");
|
|
384
|
+
let region;
|
|
385
|
+
try { region = document.querySelector(selector); } catch { return; }
|
|
386
|
+
if (!region) return;
|
|
387
|
+
const action = event.target;
|
|
388
|
+
const id = ++rereads;
|
|
389
|
+
const url = location.href;
|
|
390
|
+
region.setAttribute("aria-busy", "true");
|
|
391
|
+
window.htmx.ajax("GET", url, {
|
|
392
|
+
target: region, select: selector, swap: "outerMorph",
|
|
393
|
+
headers: { "Mero-Reread": String(id) },
|
|
394
|
+
}).catch(() => {}).finally(() => {
|
|
395
|
+
region.removeAttribute("aria-busy");
|
|
396
|
+
if (id !== rereads) return;
|
|
397
|
+
// The morph can reset the button and its status to the server's markup: show the outcome again.
|
|
398
|
+
const element = action.isConnected ? action : action.id ? document.getElementById(action.id) : null;
|
|
399
|
+
const button = element?.matches("[data-mero-action]");
|
|
400
|
+
const output = button ? element.parentElement?.querySelector("[data-mero-action-status]") : element?.querySelector("[data-mero-form-error]");
|
|
401
|
+
if (button) element.dataset.meroActionState = "unknown";
|
|
402
|
+
if (output && message) {
|
|
403
|
+
output.hidden = false;
|
|
404
|
+
output.textContent = "";
|
|
405
|
+
output.textContent = message;
|
|
406
|
+
if (button) output.dataset.tone = "danger";
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
// Mero already announced it once; repeat it only when the read removed where it was shown.
|
|
410
|
+
if (message && !element) window.MeroUI?.toast(message, { tone: "danger" });
|
|
411
|
+
});
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
document.addEventListener("htmx:before:swap", event => {
|
|
415
|
+
const { ctx, tasks } = event.detail;
|
|
416
|
+
const id = ctx.request?.headers?.["Mero-Reread"];
|
|
417
|
+
if (!id) return;
|
|
418
|
+
const target = typeof ctx.target === "string" ? document.querySelector(ctx.target) : ctx.target;
|
|
419
|
+
const url = new URL(ctx.request.action, location.href);
|
|
420
|
+
// Only a successful read of the page still on screen may replace what the person sees.
|
|
421
|
+
if (Number(id) !== rereads || !target?.isConnected || url.href !== location.href.split("#")[0] || !(ctx.response?.status >= 200 && ctx.response.status < 300) ||
|
|
422
|
+
// A page without the region would otherwise morph it away.
|
|
423
|
+
!tasks.find(task => task.type === "main")?.fragment?.firstElementChild) {
|
|
424
|
+
event.preventDefault();
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
// form[data-mero-search]: a search or filter form that GETs results into a list. A failed read never
|
|
429
|
+
// replaces the list: Mero skips the swap of a non-2xx response or one without the hx-select region,
|
|
430
|
+
// explains it in [data-mero-search-error], and once nothing is pending puts selects, checkboxes and
|
|
431
|
+
// radios back to the values of the list on screen. A redirect to a page without the region loads fully.
|
|
432
|
+
const searchSelector = "form[data-mero-search]";
|
|
433
|
+
const searchPending = new WeakMap();
|
|
434
|
+
const searchRequests = new WeakMap();
|
|
435
|
+
const searchFailed = new WeakSet();
|
|
436
|
+
const searchFollowed = new WeakSet();
|
|
437
|
+
const searchOf = ctx => {
|
|
438
|
+
const source = ctx.sourceElement;
|
|
439
|
+
return source?.matches?.(searchSelector) ? source : source?.form?.matches(searchSelector) ? source.form : null;
|
|
440
|
+
};
|
|
441
|
+
const choices = form => [...form.elements].filter(control => control.matches("select, input[type=checkbox], input[type=radio]"));
|
|
442
|
+
|
|
443
|
+
document.addEventListener("htmx:before:request", event => {
|
|
444
|
+
const form = searchOf(event.detail.ctx);
|
|
445
|
+
if (!form || event.defaultPrevented) return;
|
|
446
|
+
if (!searchPending.has(form)) searchPending.set(form, new Set());
|
|
447
|
+
searchPending.get(form).add(event.detail.ctx);
|
|
448
|
+
searchRequests.set(event.detail.ctx, form);
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
document.addEventListener("htmx:before:swap", event => {
|
|
452
|
+
const { ctx, tasks } = event.detail;
|
|
453
|
+
if (!searchRequests.has(ctx) || event.defaultPrevented) return;
|
|
454
|
+
const status = ctx.response?.status;
|
|
455
|
+
const main = tasks.find(task => task.type === "main");
|
|
456
|
+
const missing = Boolean(ctx.select) && !main?.fragment?.firstElementChild;
|
|
457
|
+
if (status >= 200 && status < 300 && !missing) return;
|
|
458
|
+
event.preventDefault();
|
|
459
|
+
if (missing && ctx.response.raw.redirected) {
|
|
460
|
+
// Typically an expired session answered with a login page.
|
|
461
|
+
searchFollowed.add(ctx);
|
|
462
|
+
location.assign(ctx.response.raw.url);
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
searchFailed.add(ctx);
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
document.addEventListener("htmx:finally:request", event => {
|
|
469
|
+
const { ctx } = event.detail;
|
|
470
|
+
const form = searchRequests.get(ctx);
|
|
471
|
+
if (!form) return;
|
|
472
|
+
const pending = searchPending.get(form);
|
|
473
|
+
pending.delete(ctx);
|
|
474
|
+
if (searchFollowed.has(ctx) || cancelled(ctx)) return;
|
|
475
|
+
const signal = ctx.request.signal;
|
|
476
|
+
// A newer search replaced this one (hx-sync): that request decides what the list shows.
|
|
477
|
+
const superseded = !ctx.response && signal?.aborted && signal.reason !== "timeout";
|
|
478
|
+
const failed = searchFailed.has(ctx) || (!ctx.response && !superseded);
|
|
479
|
+
const error = form.querySelector("[data-mero-search-error]");
|
|
480
|
+
if (!failed && !superseded) {
|
|
481
|
+
// The list now matches these controls: remember them as the values to return to.
|
|
482
|
+
for (const control of choices(form)) {
|
|
483
|
+
if (control.matches("select")) for (const option of control.options) option.defaultSelected = option.selected;
|
|
484
|
+
else control.defaultChecked = control.checked;
|
|
485
|
+
}
|
|
486
|
+
if (error) { error.textContent = ""; error.hidden = true; }
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
if (failed && error) {
|
|
490
|
+
error.textContent = "";
|
|
491
|
+
error.textContent = error.getAttribute("data-mero-search-error") || ctx.response?.headers.get("Mero-Form-Error") ||
|
|
492
|
+
"Couldn't update the results. The list still shows the previous results.";
|
|
493
|
+
error.hidden = false;
|
|
494
|
+
}
|
|
495
|
+
if (pending.size) return;
|
|
496
|
+
for (const control of choices(form)) {
|
|
497
|
+
if (control.matches("select")) {
|
|
498
|
+
const fallback = [...control.options].find(option => option.defaultSelected) ?? control.options[0];
|
|
499
|
+
if (fallback) fallback.selected = true;
|
|
500
|
+
} else control.checked = control.defaultChecked;
|
|
501
|
+
}
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
// One-time credentials. [data-mero-ephemeral] content is cleared when its dialog closes, on a
|
|
505
|
+
// [data-mero-ephemeral-dismiss] click and when the page is hidden. data-mero-hold keeps the page
|
|
506
|
+
// while a held request is pending, or while a held ephemeral element shows content: requests that
|
|
507
|
+
// would replace the page or the held content are cancelled, Back is undone and unload asks first.
|
|
508
|
+
const pendingHolds = new Map();
|
|
509
|
+
let steady = { url: location.href, key: window.navigation?.currentEntry?.key };
|
|
510
|
+
const remember = () => { steady = { url: location.href, key: window.navigation?.currentEntry?.key }; };
|
|
511
|
+
const holdFallback = "Copy what is shown before you leave this page. It cannot be shown again.";
|
|
512
|
+
const shown = node => node.isConnected && (node.childElementCount > 0 || node.textContent.trim() !== "");
|
|
513
|
+
const heldEphemeral = () => [...document.querySelectorAll("[data-mero-ephemeral][data-mero-hold]")].filter(shown);
|
|
514
|
+
|
|
515
|
+
function hold() {
|
|
516
|
+
for (const [ctx, owner] of pendingHolds) {
|
|
517
|
+
return { elements: [owner, ...heldEphemeral()], message: owner.getAttribute("data-mero-hold") || holdFallback, ctx };
|
|
518
|
+
}
|
|
519
|
+
const elements = heldEphemeral();
|
|
520
|
+
return elements.length ? { elements, message: elements[0].getAttribute("data-mero-hold") || holdFallback } : null;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
function warnHeld(message) {
|
|
524
|
+
window.MeroUI?.toast(message, { tone: "warning" });
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
function clearEphemeral(root) {
|
|
528
|
+
const nodes = root.matches?.("[data-mero-ephemeral]") ? [root] : [...root.querySelectorAll("[data-mero-ephemeral]")];
|
|
529
|
+
for (const node of nodes) node.replaceChildren();
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
document.addEventListener("htmx:before:request", event => {
|
|
533
|
+
const { ctx } = event.detail;
|
|
534
|
+
if (event.defaultPrevented) return;
|
|
535
|
+
const owner = ctx.sourceElement?.closest?.("form[data-mero-hold], [data-mero-action][data-mero-hold]");
|
|
536
|
+
if (!owner) return;
|
|
537
|
+
// The entry a held page returns to after Back, even if the app pushed it itself.
|
|
538
|
+
if (!hold()) remember();
|
|
539
|
+
pendingHolds.set(ctx, owner);
|
|
540
|
+
});
|
|
541
|
+
document.addEventListener("htmx:finally:request", event => {
|
|
542
|
+
pendingHolds.delete(event.detail.ctx);
|
|
543
|
+
});
|
|
544
|
+
|
|
545
|
+
// config:request runs before htmx queues or sends anything, so a cancel here sends nothing.
|
|
546
|
+
document.addEventListener("htmx:config:request", event => {
|
|
547
|
+
const held = hold();
|
|
548
|
+
const { ctx } = event.detail;
|
|
549
|
+
if (!held || event.defaultPrevented) return;
|
|
550
|
+
const source = ctx.sourceElement;
|
|
551
|
+
// Controls inside the held content may act on it, for example to acknowledge it.
|
|
552
|
+
if (held.elements.some(node => node !== source && node.contains?.(source) && node.matches("[data-mero-ephemeral]"))) return;
|
|
553
|
+
const target = typeof ctx.target === "string" ? document.querySelector(ctx.target) : ctx.target;
|
|
554
|
+
const replacesPage = ctx.request.headers["HX-Boosted"] || ctx.request.headers["HX-History-Restore-Request"] ||
|
|
555
|
+
target === document.body || target === document.documentElement;
|
|
556
|
+
if (replacesPage || (target && held.elements.some(node => target.contains(node)))) {
|
|
557
|
+
event.preventDefault();
|
|
558
|
+
warnHeld(held.message);
|
|
559
|
+
}
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
// htmx restores history after the URL has changed, so a held page puts its own entry back.
|
|
563
|
+
// Cancelling the navigate event instead would break htmx's own intercept() in WebKit.
|
|
564
|
+
document.addEventListener("htmx:after:history:push", remember);
|
|
565
|
+
document.addEventListener("htmx:after:history:replace", remember);
|
|
566
|
+
window.navigation?.addEventListener("currententrychange", event => {
|
|
567
|
+
if (event.navigationType !== "traverse") remember();
|
|
568
|
+
});
|
|
569
|
+
// popstate covers every same-document traversal, including entries htmx did not stamp.
|
|
570
|
+
addEventListener("popstate", () => {
|
|
571
|
+
const held = hold();
|
|
572
|
+
if (!held) return remember();
|
|
573
|
+
if (location.href === steady.url) return;
|
|
574
|
+
warnHeld(held.message);
|
|
575
|
+
const entry = steady.key && window.navigation?.entries().find(item => item.key === steady.key);
|
|
576
|
+
// Let the interrupted traversal settle before going back to the held entry.
|
|
577
|
+
setTimeout(() => entry ? window.navigation.traverseTo(entry.key) : history.pushState({ htmx: true }, "", steady.url));
|
|
578
|
+
});
|
|
579
|
+
document.addEventListener("htmx:before:history:restore", event => {
|
|
580
|
+
if (hold()) event.preventDefault();
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
addEventListener("beforeunload", event => {
|
|
584
|
+
if (!hold()) return;
|
|
585
|
+
event.preventDefault();
|
|
586
|
+
event.returnValue = "";
|
|
587
|
+
});
|
|
588
|
+
// A page left anyway, or kept in the back-forward cache, must not show the secret again.
|
|
589
|
+
addEventListener("pagehide", () => clearEphemeral(document));
|
|
590
|
+
|
|
591
|
+
document.addEventListener("close", event => {
|
|
592
|
+
if (event.target instanceof HTMLDialogElement) clearEphemeral(event.target);
|
|
593
|
+
}, true);
|
|
594
|
+
|
|
595
|
+
document.addEventListener("click", event => {
|
|
596
|
+
const dismiss = event.target.closest?.("[data-mero-ephemeral-dismiss]");
|
|
597
|
+
const container = dismiss?.closest("[data-mero-ephemeral]");
|
|
598
|
+
if (!container) return;
|
|
599
|
+
const focused = container.contains(document.activeElement);
|
|
600
|
+
clearEphemeral(container);
|
|
601
|
+
if (!focused && document.activeElement !== document.body) return;
|
|
602
|
+
const next = dismiss.getAttribute("data-mero-ephemeral-dismiss");
|
|
603
|
+
const focus = next ? document.querySelector(next) : null;
|
|
604
|
+
if (focus) {
|
|
605
|
+
focus.focus({ preventScroll: true });
|
|
606
|
+
} else {
|
|
607
|
+
if (!container.hasAttribute("tabindex")) container.tabIndex = -1;
|
|
608
|
+
container.focus({ preventScroll: true });
|
|
609
|
+
}
|
|
610
|
+
});
|
|
223
611
|
})();
|
package/ui/mero-gallery.css
CHANGED
package/ui/mero-gallery.js
CHANGED
package/ui/mero-insights.css
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! @rubenlangeweg/mero-ui 0.
|
|
1
|
+
/*! @rubenlangeweg/mero-ui 0.9.0 | MIT */
|
|
2
2
|
/* Optional charts, metrics and conversation styles. Uses Mero's semantic theme. */
|
|
3
3
|
.mero-plot, .mero-chat, .mero-chat-inbox, .mero-code-block, .mero-insight-metric, .mero-chat-disclosure, .mero-markdown-editor {
|
|
4
4
|
box-sizing: border-box;
|
package/ui/mero-insights.js
CHANGED
package/ui/mero-inventory.css
CHANGED
package/ui/mero-inventory.js
CHANGED
package/ui/mero-layouts.css
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! @rubenlangeweg/mero-ui 0.
|
|
1
|
+
/*! @rubenlangeweg/mero-ui 0.9.0 | MIT */
|
|
2
2
|
/* Optional layouts built from the core theme tokens. */
|
|
3
3
|
.mero-app {
|
|
4
4
|
display: grid;
|
|
@@ -34,9 +34,6 @@
|
|
|
34
34
|
.mero-app-brand { display: flex; align-items: center; gap: .65rem; font-weight: 700; padding: .25rem .7rem 1.35rem; letter-spacing: -.025em; }
|
|
35
35
|
.mero-app-monogram { display: grid; place-items: center; inline-size: 1.9rem; block-size: 1.9rem; border-radius: .6rem; background: var(--mero-primary); color: var(--mero-primary-ink); font: 660 1rem/1 var(--mero-font); }
|
|
36
36
|
.mero-app-main { min-inline-size: 0; padding: clamp(1rem, 3.5vw, 2.25rem); }
|
|
37
|
-
.mero-page-head { display: flex; align-items: start; justify-content: space-between; flex-wrap: wrap; gap: .75rem; margin-block-end: 1.5rem; }
|
|
38
|
-
.mero-page-head :where(h1, h2) { margin: 0; font-size: clamp(1.55rem, 3vw, 2.15rem); line-height: 1.12; letter-spacing: -.05em; }
|
|
39
|
-
.mero-page-head p { margin: .35rem 0 0; color: var(--mero-muted); font-size: .9rem; }
|
|
40
37
|
.mero-stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(9rem, 100%), 1fr)); gap: .65rem; margin-block-end: 1.25rem; }
|
|
41
38
|
.mero-stat { display: grid; gap: .4rem; padding: .9rem 1rem; border: 1px solid var(--mero-line); border-radius: .75rem; background: var(--mero-surface); }
|
|
42
39
|
.mero-stat dt { color: var(--mero-muted); font-size: .78rem; }
|
package/ui/mero-live.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
/*! @rubenlangeweg/mero-ui 0.
|
|
1
|
+
/*! @rubenlangeweg/mero-ui 0.9.0 | MIT */
|
|
2
2
|
// Optional polling for a read-only status fragment. htmx owns each GET and swap.
|
|
3
|
+
// With data-mero-live-follow, an error response carrying HX-Redirect, HX-Location or HX-Refresh
|
|
4
|
+
// (such as a 401 after the session expired) is handed to htmx instead of retried.
|
|
3
5
|
(() => {
|
|
4
6
|
const selector = "[data-mero-live]";
|
|
5
7
|
const states = new WeakMap();
|
|
@@ -19,7 +21,8 @@
|
|
|
19
21
|
|
|
20
22
|
function init(region) {
|
|
21
23
|
if (states.has(region)) return;
|
|
22
|
-
|
|
24
|
+
// The server rendered the region, so the page load counts as the first good read.
|
|
25
|
+
const state = { timer: null, request: null, goodAt: Date.now(), followed: false };
|
|
23
26
|
states.set(region, state);
|
|
24
27
|
schedule(region, state);
|
|
25
28
|
}
|
|
@@ -78,8 +81,17 @@
|
|
|
78
81
|
document.addEventListener("htmx:before:response", event => {
|
|
79
82
|
const { ctx } = event.detail;
|
|
80
83
|
if (!ctx.sourceElement?.matches(selector)) return;
|
|
84
|
+
const state = states.get(ctx.sourceElement);
|
|
85
|
+
if (state?.request !== ctx) return event.preventDefault();
|
|
86
|
+
if (ctx.response.raw.ok) return;
|
|
87
|
+
// htmx acts on these headers before any swap, so the error body is never shown.
|
|
88
|
+
if (ctx.sourceElement.hasAttribute("data-mero-live-follow") && (ctx.hx?.redirect || ctx.hx?.location || ctx.hx?.refresh === "true")) {
|
|
89
|
+
state.followed = true;
|
|
90
|
+
ctx.hx = { redirect: ctx.hx.redirect, location: ctx.hx.location, refresh: ctx.hx.refresh };
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
81
93
|
// Stop before htmx interprets swap or redirect headers from an error response.
|
|
82
|
-
|
|
94
|
+
event.preventDefault();
|
|
83
95
|
});
|
|
84
96
|
|
|
85
97
|
document.addEventListener("htmx:before:swap", event => {
|
|
@@ -99,11 +111,20 @@
|
|
|
99
111
|
if (!state || state.request !== ctx) return;
|
|
100
112
|
state.request = null;
|
|
101
113
|
region.removeAttribute("aria-busy");
|
|
114
|
+
// The page is leaving or htmx is loading the new location: polling ends here.
|
|
115
|
+
if (state.followed) {
|
|
116
|
+
region.setAttribute("data-mero-live-done", "");
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
102
119
|
// A cancelled before:request also fires finally in htmx 4.
|
|
103
120
|
if (ctx.response || String(ctx.status).startsWith("error")) {
|
|
104
121
|
const failed = !ctx.response?.raw.ok || String(ctx.status).startsWith("error");
|
|
105
122
|
const error = region.querySelector("[data-mero-live-error]");
|
|
106
|
-
|
|
123
|
+
if (!failed) state.goodAt = Date.now();
|
|
124
|
+
// The author's text may name the last good read: "Couldn't refresh. Showing the status from {time}."
|
|
125
|
+
const template = error?.getAttribute("data-mero-live-error");
|
|
126
|
+
const time = new Date(state.goodAt).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
|
|
127
|
+
const message = !failed ? "" : template ? template.replaceAll("{time}", time) : "Couldn't refresh. Retrying…";
|
|
107
128
|
if (error && error.textContent !== message) error.textContent = message;
|
|
108
129
|
const retry = region.querySelector("[data-mero-live-retry]");
|
|
109
130
|
if (retry) {
|
package/ui/mero-workspace.css
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! @rubenlangeweg/mero-ui 0.
|
|
1
|
+
/*! @rubenlangeweg/mero-ui 0.9.0 | MIT */
|
|
2
2
|
/* Workspace forms, navigation and editing tools. Uses the host's Mero tokens. */
|
|
3
3
|
:where(.mero-ws-cell,.mero-ws-labeled,.mero-ws-cardform,.mero-ws-inline,.mero-ws-upload,.mero-ws-editor,.mero-ws-shell,.mero-ws-navbar,.mero-ws-command,.mero-ws-context,.mero-ws-segment,.mero-ws-wizard,.mero-ws-split,.mero-ws-emoji,.mero-ws-filter,.mero-ws-dock,.mero-ws-mega,.mero-ws-fab) {
|
|
4
4
|
box-sizing:border-box; max-inline-size:100%; color:var(--mero-text); font:400 .9rem/1.45 var(--mero-font);
|
package/ui/mero-workspace.js
CHANGED