athena-ui-js 2.5.0 → 2.6.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.
@@ -1,161 +1,171 @@
1
- //feedbackGenerator for Athena UI
2
- //Author: Csaba Tóth https://github.com/csabatibortoth/
3
- //LICENCE: MIT
4
- //Repository: https://github.com/csabatibortoth/athena-ui
5
-
6
- import { asHandle, cx, el, mount, setContent } from "../../core/dom.js";
7
- import { removeAfterTransition } from "../../core/motion.js";
8
- import { translate } from "../../core/i18n.js";
9
-
10
- const TOAST_CONTAINER_ID = "athena-toasts";
11
- const URGENT_TYPES = new Set(["error", "warning"]);
12
-
13
- function closeButton(onclick) {
14
- return el("button", {
15
- class: "alert-close",
16
- text: "×",
17
- attrs: { type: "button", "aria-label": translate("close") },
18
- on: { click: onclick }
19
- });
20
- }
21
-
22
- /**
23
- * Shows a toast notification in the corner of the page.
24
- *
25
- * @param {object|string} options a message, or an options object
26
- * @param {string} options.message
27
- * @param {string} [options.title]
28
- * @param {"info"|"success"|"warning"|"error"} [options.type="info"]
29
- * @param {number} [options.duration=4000] ms before it disappears, 0 keeps it until it is closed
30
- * @returns {{close: Function}}
31
- */
32
- export function showToast(options) {
33
- const { message, title, type = "info", duration = 4000 } = typeof options === "string" ? { message: options } : options;
34
- let container = document.getElementById(TOAST_CONTAINER_ID);
35
- if (!container) {
36
- container = el("div", { id: TOAST_CONTAINER_ID, class: "toast-container" });
37
- document.body.append(container);
38
- }
39
- let timer;
40
- const close = () => {
41
- clearTimeout(timer);
42
- toast.classList.add("is-leaving");
43
- removeAfterTransition(toast);
44
- };
45
- const toast = el("div", {
46
- class: cx("toast", `alert-${type}`),
47
- attrs: { role: URGENT_TYPES.has(type) ? "alert" : "status" }
48
- }, el("div", { class: "alert-body" }, title ? el("strong", { text: title }) : "", el("div", { text: message })), closeButton(close));
49
- container.append(toast);
50
- if (duration > 0) {
51
- timer = setTimeout(close, duration);
52
- // Do not hide a toast the user is reading.
53
- toast.addEventListener("mouseenter", () => clearTimeout(timer));
54
- toast.addEventListener("mouseleave", () => { timer = setTimeout(close, duration); });
55
- }
56
- return { close };
57
- }
58
-
59
- /**
60
- * Generates an inline alert box.
61
- *
62
- * @param {object} options
63
- * @param {string|Element} [options.parent]
64
- * @param {string} [options.id]
65
- * @param {string|Node} options.message
66
- * @param {string} [options.title]
67
- * @param {"info"|"success"|"warning"|"error"} [options.type="info"]
68
- * @param {boolean} [options.dismissible]
69
- * @returns {HTMLElement}
70
- */
71
- export function generateAlert(options = {}) {
72
- const { type = "info", title, message, dismissible } = options;
73
- const body = el("div", { class: "alert-body" });
74
- if (title) body.append(el("strong", { text: title }));
75
- body.append(setContent(el("div"), message));
76
- const alert = el("div", {
77
- id: options.id,
78
- class: cx("alert", `alert-${type}`, options.class),
79
- attrs: { role: URGENT_TYPES.has(type) ? "alert" : "status" }
80
- }, body);
81
- if (dismissible) alert.append(closeButton(() => alert.remove()));
82
- return mount(options.parent, alert);
83
- }
84
-
85
- /**
86
- * Copies text to the clipboard and says so in a toast. Resolves with true when it worked (it needs a secure page and a click or key
87
- * press of the person), false otherwise (a toast with the error is shown).
88
- *
89
- * @param {string} text
90
- * @param {object} [options]
91
- * @param {string} [options.message] text of the toast (default: the translated "Copied")
92
- * @param {boolean} [options.toast=true] false copies silently
93
- * @returns {Promise<boolean>}
94
- */
95
- export async function copyToClipboard(text, { message = translate("copied"), toast = true } = {}) {
96
- try {
97
- await navigator.clipboard.writeText(String(text));
98
- if (toast) showToast({ message, type: "success" });
99
- return true;
100
- } catch {
101
- if (toast) showToast({ message: translate("copyFailed"), type: "error" });
102
- return false;
103
- }
104
- }
105
-
106
- /**
107
- * Generates a loading spinner.
108
- *
109
- * @param {object} [options]
110
- * @param {string|Element} [options.parent]
111
- * @param {string} [options.id]
112
- * @param {"small"|"large"} [options.size]
113
- * @param {string} [options.label] accessible text, defaults to the translated "Loading..."
114
- * @returns {HTMLElement}
115
- */
116
- export function generateSpinner(options = {}) {
117
- return mount(options.parent, el("span", {
118
- id: options.id,
119
- class: cx("spinner", options.size && `spinner-${options.size}`, options.class),
120
- attrs: { role: "status", "aria-label": options.label ?? translate("loading") }
121
- }));
122
- }
123
-
124
- /**
125
- * Generates a progress bar. Without a value it is indeterminate.
126
- *
127
- * @param {object} [options]
128
- * @param {string|Element} [options.parent]
129
- * @param {string} [options.id]
130
- * @param {number} [options.value] 0..max
131
- * @param {number} [options.max=100]
132
- * @param {string} [options.label] text shown above the bar
133
- * @param {"info"|"success"|"warning"|"error"} [options.type]
134
- * @returns {{element: HTMLElement, setValue: (value: number) => void}}
135
- */
136
- export function generateProgress(options = {}) {
137
- const max = options.max ?? 100;
138
- const fill = el("div", { class: "progress-fill" });
139
- const bar = el("div", {
140
- class: "progress-bar",
141
- attrs: { role: "progressbar", "aria-valuemin": 0, "aria-valuemax": max, "aria-label": options.label ?? null }
142
- }, fill);
143
- const element = el("div", { id: options.id, class: cx("progress", options.type && `progress-${options.type}`, options.class) },
144
- options.label ? el("div", { class: "progress-label", text: options.label }) : "", bar);
145
-
146
- const setValue = value => {
147
- const indeterminate = value == null;
148
- element.classList.toggle("progress-indeterminate", indeterminate);
149
- if (indeterminate) {
150
- bar.removeAttribute("aria-valuenow");
151
- fill.style.width = "";
152
- } else {
153
- const clamped = Math.min(Math.max(value, 0), max);
154
- bar.setAttribute("aria-valuenow", clamped);
155
- fill.style.width = `${(clamped / max) * 100}%`;
156
- }
157
- };
158
- setValue(options.value);
159
- mount(options.parent, element);
160
- return asHandle({ element, setValue, update: ({ value } = {}) => setValue(value) });
161
- }
1
+ //feedbackGenerator for Athena UI
2
+ //Author: Csaba Tóth https://github.com/csabatibortoth/
3
+ //LICENCE: MIT
4
+ //Repository: https://github.com/csabatibortoth/athena-ui
5
+
6
+ import { asHandle, cx, el, mount, setContent } from "../../core/dom.js";
7
+ import { removeAfterTransition } from "../../core/motion.js";
8
+ import { translate } from "../../core/i18n.js";
9
+
10
+ const TOAST_CONTAINER_ID = "athena-toasts";
11
+ const URGENT_TYPES = new Set(["error", "warning"]);
12
+ const TOAST_POSITIONS = ["top-right", "top-left", "top-center", "bottom-right", "bottom-left", "bottom-center"];
13
+
14
+ function closeButton(onclick) {
15
+ return el("button", {
16
+ class: "alert-close",
17
+ text: "×",
18
+ attrs: { type: "button", "aria-label": translate("close") },
19
+ on: { click: onclick }
20
+ });
21
+ }
22
+
23
+ /**
24
+ * Shows a toast notification in the corner of the page.
25
+ *
26
+ * @param {object|string} options a message, or an options object
27
+ * @param {string} options.message
28
+ * @param {string} [options.title]
29
+ * @param {"info"|"success"|"warning"|"error"} [options.type="info"]
30
+ * @param {number} [options.duration=4000] ms before it disappears, 0 keeps it until it is closed
31
+ * @param {"top-right"|"top-left"|"top-center"|"bottom-right"|"bottom-left"|"bottom-center"} [options.position="top-right"] the corner (or the middle of an edge) of the window.
32
+ * Every position has its own stack. An application that keeps its main buttons in the top right corner shows its toasts at the bottom.
33
+ * @param {{x?: number, y?: number}} [options.offset] the distance from the edge in px (default 16); also the variables --athena-toast-x and --athena-toast-y
34
+ * @returns {{close: Function}}
35
+ */
36
+ export function showToast(options) {
37
+ const { message, title, type = "info", duration = 4000, offset } = typeof options === "string" ? { message: options } : options;
38
+ const position = TOAST_POSITIONS.includes(options?.position) ? options.position : "top-right";
39
+ const containerId = position === "top-right" ? TOAST_CONTAINER_ID : `${TOAST_CONTAINER_ID}-${position}`;
40
+ let container = document.getElementById(containerId);
41
+ if (!container) {
42
+ container = el("div", { id: containerId, class: cx("toast-container", `toast-container-${position}`) });
43
+ document.body.append(container);
44
+ }
45
+ if (offset) {
46
+ if (Number.isFinite(offset.x)) container.style.setProperty("--athena-toast-x", `${offset.x}px`);
47
+ if (Number.isFinite(offset.y)) container.style.setProperty("--athena-toast-y", `${offset.y}px`);
48
+ }
49
+ let timer;
50
+ const close = () => {
51
+ clearTimeout(timer);
52
+ toast.classList.add("is-leaving");
53
+ removeAfterTransition(toast);
54
+ };
55
+ const toast = el("div", {
56
+ class: cx("toast", `alert-${type}`),
57
+ attrs: { role: URGENT_TYPES.has(type) ? "alert" : "status" }
58
+ }, el("div", { class: "alert-body" }, title ? el("strong", { text: title }) : "", el("div", { text: message })), closeButton(close));
59
+ container.append(toast);
60
+ if (duration > 0) {
61
+ timer = setTimeout(close, duration);
62
+ // Do not hide a toast the user is reading.
63
+ toast.addEventListener("mouseenter", () => clearTimeout(timer));
64
+ toast.addEventListener("mouseleave", () => { timer = setTimeout(close, duration); });
65
+ }
66
+ return { close };
67
+ }
68
+
69
+ /**
70
+ * Generates an inline alert box.
71
+ *
72
+ * @param {object} options
73
+ * @param {string|Element} [options.parent]
74
+ * @param {string} [options.id]
75
+ * @param {string|Node} options.message
76
+ * @param {string} [options.title]
77
+ * @param {"info"|"success"|"warning"|"error"} [options.type="info"]
78
+ * @param {boolean} [options.dismissible]
79
+ * @returns {HTMLElement}
80
+ */
81
+ export function generateAlert(options = {}) {
82
+ const { type = "info", title, message, dismissible } = options;
83
+ const body = el("div", { class: "alert-body" });
84
+ if (title) body.append(el("strong", { text: title }));
85
+ body.append(setContent(el("div"), message));
86
+ const alert = el("div", {
87
+ id: options.id,
88
+ class: cx("alert", `alert-${type}`, options.class),
89
+ attrs: { role: URGENT_TYPES.has(type) ? "alert" : "status" }
90
+ }, body);
91
+ if (dismissible) alert.append(closeButton(() => alert.remove()));
92
+ return mount(options.parent, alert);
93
+ }
94
+
95
+ /**
96
+ * Copies text to the clipboard and says so in a toast. Resolves with true when it worked (it needs a secure page and a click or key
97
+ * press of the person), false otherwise (a toast with the error is shown).
98
+ *
99
+ * @param {string} text
100
+ * @param {object} [options]
101
+ * @param {string} [options.message] text of the toast (default: the translated "Copied")
102
+ * @param {boolean} [options.toast=true] false copies silently
103
+ * @returns {Promise<boolean>}
104
+ */
105
+ export async function copyToClipboard(text, { message = translate("copied"), toast = true } = {}) {
106
+ try {
107
+ await navigator.clipboard.writeText(String(text));
108
+ if (toast) showToast({ message, type: "success" });
109
+ return true;
110
+ } catch {
111
+ if (toast) showToast({ message: translate("copyFailed"), type: "error" });
112
+ return false;
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Generates a loading spinner.
118
+ *
119
+ * @param {object} [options]
120
+ * @param {string|Element} [options.parent]
121
+ * @param {string} [options.id]
122
+ * @param {"small"|"large"} [options.size]
123
+ * @param {string} [options.label] accessible text, defaults to the translated "Loading..."
124
+ * @returns {HTMLElement}
125
+ */
126
+ export function generateSpinner(options = {}) {
127
+ return mount(options.parent, el("span", {
128
+ id: options.id,
129
+ class: cx("spinner", options.size && `spinner-${options.size}`, options.class),
130
+ attrs: { role: "status", "aria-label": options.label ?? translate("loading") }
131
+ }));
132
+ }
133
+
134
+ /**
135
+ * Generates a progress bar. Without a value it is indeterminate.
136
+ *
137
+ * @param {object} [options]
138
+ * @param {string|Element} [options.parent]
139
+ * @param {string} [options.id]
140
+ * @param {number} [options.value] 0..max
141
+ * @param {number} [options.max=100]
142
+ * @param {string} [options.label] text shown above the bar
143
+ * @param {"info"|"success"|"warning"|"error"} [options.type]
144
+ * @returns {{element: HTMLElement, setValue: (value: number) => void}}
145
+ */
146
+ export function generateProgress(options = {}) {
147
+ const max = options.max ?? 100;
148
+ const fill = el("div", { class: "progress-fill" });
149
+ const bar = el("div", {
150
+ class: "progress-bar",
151
+ attrs: { role: "progressbar", "aria-valuemin": 0, "aria-valuemax": max, "aria-label": options.label ?? null }
152
+ }, fill);
153
+ const element = el("div", { id: options.id, class: cx("progress", options.type && `progress-${options.type}`, options.class) },
154
+ options.label ? el("div", { class: "progress-label", text: options.label }) : "", bar);
155
+
156
+ const setValue = value => {
157
+ const indeterminate = value == null;
158
+ element.classList.toggle("progress-indeterminate", indeterminate);
159
+ if (indeterminate) {
160
+ bar.removeAttribute("aria-valuenow");
161
+ fill.style.width = "";
162
+ } else {
163
+ const clamped = Math.min(Math.max(value, 0), max);
164
+ bar.setAttribute("aria-valuenow", clamped);
165
+ fill.style.width = `${(clamped / max) * 100}%`;
166
+ }
167
+ };
168
+ setValue(options.value);
169
+ mount(options.parent, element);
170
+ return asHandle({ element, setValue, update: ({ value } = {}) => setValue(value) });
171
+ }