@cedx/base 0.16.0 → 0.18.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.
@@ -7,7 +7,7 @@ export class Toast extends HTMLElement {
7
7
  /**
8
8
  * The list of observed attributes.
9
9
  */
10
- static observedAttributes = ["animation", "caption", "context", "culture", "icon", "open"];
10
+ static observedAttributes = ["animation", "autohide", "caption", "context", "culture", "delay", "icon"];
11
11
  /**
12
12
  * The time units.
13
13
  */
@@ -41,10 +41,7 @@ export class Toast extends HTMLElement {
41
41
  return this.hasAttribute("animation");
42
42
  }
43
43
  set animation(value) {
44
- if (value)
45
- this.setAttribute("animation", "");
46
- else
47
- this.removeAttribute("animation");
44
+ this.toggleAttribute("animation", value);
48
45
  }
49
46
  /**
50
47
  * Value indicating whether to automatically hide this toast.
@@ -53,10 +50,13 @@ export class Toast extends HTMLElement {
53
50
  return this.hasAttribute("autohide");
54
51
  }
55
52
  set autoHide(value) {
56
- if (value)
57
- this.setAttribute("autohide", "");
58
- else
59
- this.removeAttribute("autohide");
53
+ this.toggleAttribute("autohide", value);
54
+ }
55
+ /**
56
+ * The child content displayed in the body.
57
+ */
58
+ set body(value) {
59
+ this.querySelector(".toast-body").replaceChildren(...value.childNodes);
60
60
  }
61
61
  /**
62
62
  * The title displayed in the header.
@@ -67,12 +67,6 @@ export class Toast extends HTMLElement {
67
67
  set caption(value) {
68
68
  this.setAttribute("caption", value);
69
69
  }
70
- /**
71
- * The child content displayed in the body.
72
- */
73
- set childContent(value) {
74
- this.querySelector(".toast-body").replaceChildren(...value.childNodes);
75
- }
76
70
  /**
77
71
  * A contextual modifier.
78
72
  */
@@ -129,10 +123,7 @@ export class Toast extends HTMLElement {
129
123
  return this.hasAttribute("open");
130
124
  }
131
125
  set open(value) {
132
- if (value)
133
- this.setAttribute("open", "");
134
- else
135
- this.removeAttribute("open");
126
+ this.toggleAttribute("open", value);
136
127
  }
137
128
  /**
138
129
  * Method invoked when an attribute has been changed.
@@ -146,6 +137,9 @@ export class Toast extends HTMLElement {
146
137
  case "animation":
147
138
  this.#updateAnimation(newValue != null);
148
139
  break;
140
+ case "autohide":
141
+ this.#updateAutoHide(newValue != null);
142
+ break;
149
143
  case "caption":
150
144
  this.#updateCaption(newValue ?? "");
151
145
  break;
@@ -155,24 +149,31 @@ export class Toast extends HTMLElement {
155
149
  case "culture":
156
150
  this.#formatter = new Intl.RelativeTimeFormat((newValue ?? "").trim() || navigator.language, { style: "long" });
157
151
  break;
152
+ case "delay":
153
+ this.#updateDelay(Number(newValue));
154
+ break;
158
155
  case "icon":
159
156
  this.#updateIcon(newValue);
160
157
  break;
161
- case "open":
162
- this.#updateVisibility(newValue != null);
163
- break;
164
158
  // No default
165
159
  }
166
160
  }
161
+ /**
162
+ * Closes this toast.
163
+ */
164
+ close() {
165
+ this.#toast.hide();
166
+ }
167
167
  /**
168
168
  * Method invoked when this component is connected.
169
169
  */
170
170
  connectedCallback() {
171
- const toast = this.querySelector(".toast");
171
+ const toast = this.firstElementChild;
172
172
  toast.addEventListener("hidden.bs.toast", () => clearInterval(this.#timer));
173
173
  toast.addEventListener("show.bs.toast", () => this.#timer = window.setInterval(this.#updateElapsedTime, 1_000));
174
- const { animation, autoHide: autohide, delay } = this;
175
- this.#toast = new BootstrapToast(toast, { animation, autohide, delay });
174
+ this.#toast = new BootstrapToast(toast);
175
+ if (this.open)
176
+ this.show();
176
177
  }
177
178
  /**
178
179
  * Method invoked when this component is disconnected.
@@ -181,12 +182,6 @@ export class Toast extends HTMLElement {
181
182
  clearInterval(this.#timer);
182
183
  this.#toast.dispose();
183
184
  }
184
- /**
185
- * Hides this toast.
186
- */
187
- hide() {
188
- this.#toast.hide();
189
- }
190
185
  /**
191
186
  * Shows this toast.
192
187
  */
@@ -211,11 +206,18 @@ export class Toast extends HTMLElement {
211
206
  return this.#formatter.format(Math.ceil(-elapsed), Toast.#timeUnits[index]);
212
207
  }
213
208
  /**
214
- * Updates the toast animation.
209
+ * Updates the value indicating whether to apply a fade transition.
215
210
  * @param value The new value.
216
211
  */
217
212
  #updateAnimation(value) {
218
- this.firstElementChild.classList.toggle("fade", value);
213
+ this.firstElementChild.dataset.bsAnimation = value ? "true" : "false";
214
+ }
215
+ /**
216
+ * Updates the value indicating whether to automatically hide this toast.
217
+ * @param value The new value.
218
+ */
219
+ #updateAutoHide(value) {
220
+ this.firstElementChild.dataset.bsAutohide = value ? "true" : "false";
219
221
  }
220
222
  /**
221
223
  * Updates the title displayed in the header.
@@ -237,6 +239,14 @@ export class Toast extends HTMLElement {
237
239
  classList.remove(...contexts.map(context => `text-${toCss(context)}`));
238
240
  classList.add(`text-${toCss(value)}`);
239
241
  }
242
+ /**
243
+ * Updates the delay to hide the toast.
244
+ * @param value The new value.
245
+ */
246
+ #updateDelay(value) {
247
+ const delay = Math.max(0, Number.isNaN(value) ? 5_000 : value);
248
+ this.firstElementChild.dataset.bsDelay = delay.toString();
249
+ }
240
250
  /**
241
251
  * Updates the label corresponding to the elapsed time.
242
252
  */
@@ -251,11 +261,4 @@ export class Toast extends HTMLElement {
251
261
  #updateIcon(value) {
252
262
  this.querySelector(".toast-header .icon").textContent = (value ?? "").trim() || getIcon(this.context);
253
263
  }
254
- /**
255
- * Updates the toast visibility.
256
- * @param value The new value.
257
- */
258
- #updateVisibility(value) {
259
- this.firstElementChild.classList.toggle("show", value);
260
- }
261
264
  }
@@ -1,42 +1,6 @@
1
1
  import { Context } from "../Context.js";
2
2
  import { Position } from "../Position.js";
3
- /**
4
- * Represents a notification.
5
- */
6
- export interface IToast {
7
- /**
8
- * Value indicating whether to apply a fade transition.
9
- */
10
- animation?: boolean;
11
- /**
12
- * Value indicating whether to automatically hide the toast.
13
- */
14
- autoHide?: boolean;
15
- /**
16
- * The title displayed in the header.
17
- */
18
- caption: string;
19
- /**
20
- * The child content displayed in the body.
21
- */
22
- childContent: DocumentFragment | string;
23
- /**
24
- * The default contextual modifier.
25
- */
26
- context?: Context;
27
- /**
28
- * The culture used to format the relative time.
29
- */
30
- culture?: Intl.Locale;
31
- /**
32
- * The delay, in milliseconds, to hide the toast.
33
- */
34
- delay?: number;
35
- /**
36
- * The icon displayed next to the caption.
37
- */
38
- icon?: string;
39
- }
3
+ import type { IToast } from "./Toast.js";
40
4
  /**
41
5
  * Manages the notifications.
42
6
  */
@@ -46,6 +10,10 @@ export declare class Toaster extends HTMLElement {
46
10
  * The list of observed attributes.
47
11
  */
48
12
  static readonly observedAttributes: string[];
13
+ /**
14
+ * Creates a new toaster.
15
+ */
16
+ constructor();
49
17
  /**
50
18
  * Value indicating whether to apply a fade transition.
51
19
  */
@@ -92,11 +60,11 @@ export declare class Toaster extends HTMLElement {
92
60
  * Shows a toast.
93
61
  * @param context The contextual modifier.
94
62
  * @param caption The title displayed in the toast header.
95
- * @param childContent The child content displayed in the toast body.
63
+ * @param body The child content displayed in the toast body.
96
64
  */
97
- notify(context: Context, caption: string, childContent: DocumentFragment | string): void;
65
+ show(context: Context, caption: string, body: DocumentFragment | string): void;
98
66
  /**
99
- * Shows the specified toast.
67
+ * Shows a toast.
100
68
  * @param toast The toast to show.
101
69
  */
102
70
  show(toast: IToast): void;
@@ -1 +1 @@
1
- {"version":3,"file":"Toaster.d.ts","sourceRoot":"","sources":["../../../src/Client/UI/Components/Toaster.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,EAAC,MAAM,eAAe,CAAC;AAEtC,OAAO,EAAC,QAAQ,EAAQ,MAAM,gBAAgB,CAAC;AAE/C;;GAEG;AACH,MAAM,WAAW,MAAM;IAEtB;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB;;OAEG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,YAAY,EAAE,gBAAgB,GAAC,MAAM,CAAC;IAEtC;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAElB;;OAEG;IACH,OAAO,CAAC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Q,CAGvB;IACD,IAAI,QAAQ,CAAC,KAAK,EAAE,QAAQ,EAE3B;IAED;;;;;OAKG;IACH,wBAAwB,CAAC,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,GAAC,IAAI,GAAG,IAAI;IAO/F;;;;;OAKG;IACH,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,gBAAgB,GAAC,MAAM,GAAG,IAAI;IAE5E;;;OAGG;IACH,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;CAsCzB;AAED;;GAEG;AACH,OAAO,CAAC,MAAM,CAAC;IAEd;;OAEG;IACH,UAAU,qBAAqB;QAC9B,mBAAmB,EAAE,OAAO,CAAC;KAC7B;CACD"}
@@ -13,6 +13,14 @@ export class Toaster extends HTMLElement {
13
13
  * The template for a toast.
14
14
  */
15
15
  #toastTemplate = this.querySelector("template").content;
16
+ /**
17
+ * Creates a new toaster.
18
+ */
19
+ constructor() {
20
+ super();
21
+ for (const toast of this.querySelectorAll("toaster-item"))
22
+ toast.addEventListener("hidden.bs.toast", () => toast.remove());
23
+ }
16
24
  /**
17
25
  * Registers the component.
18
26
  */
@@ -26,10 +34,7 @@ export class Toaster extends HTMLElement {
26
34
  return this.hasAttribute("animation");
27
35
  }
28
36
  set animation(value) {
29
- if (value)
30
- this.setAttribute("animation", "");
31
- else
32
- this.removeAttribute("animation");
37
+ this.toggleAttribute("animation", value);
33
38
  }
34
39
  /**
35
40
  * Value indicating whether to automatically hide the toasts.
@@ -38,10 +43,7 @@ export class Toaster extends HTMLElement {
38
43
  return this.hasAttribute("autohide");
39
44
  }
40
45
  set autoHide(value) {
41
- if (value)
42
- this.setAttribute("autohide", "");
43
- else
44
- this.removeAttribute("autohide");
46
+ this.toggleAttribute("autohide", value);
45
47
  }
46
48
  /**
47
49
  * The default contextual modifier.
@@ -113,25 +115,21 @@ export class Toaster extends HTMLElement {
113
115
  }
114
116
  /**
115
117
  * Shows a toast.
116
- * @param context The contextual modifier.
118
+ * @param toast The toast to show, or the contextual modifier.
117
119
  * @param caption The title displayed in the toast header.
118
- * @param childContent The child content displayed in the toast body.
119
- */
120
- notify(context, caption, childContent) {
121
- this.show({ context, caption, childContent });
122
- }
123
- /**
124
- * Shows the specified toast.
125
- * @param toast The toast to show.
120
+ * @param body The child content displayed in the toast body.
126
121
  */
127
- show(toast) {
122
+ show(toast, caption = "", body = "") {
123
+ if (typeof toast == "string")
124
+ toast = { context: toast, caption, body };
128
125
  const item = document.createElement("toaster-item");
129
- item.addEventListener("hidden.bs.toast", () => item.remove());
130
- item.appendChild(this.#toastTemplate.cloneNode(true).querySelector(".toast"));
126
+ const childContent = this.#toastTemplate.cloneNode(true).querySelector(".toast");
127
+ childContent.addEventListener("hidden.bs.toast", () => item.remove());
128
+ item.appendChild(childContent);
131
129
  item.animation = toast.animation ?? this.animation;
132
130
  item.autoHide = toast.autoHide ?? this.autoHide;
131
+ item.body = typeof toast.body == "string" ? createDocumentFragment(toast.body) : toast.body;
133
132
  item.caption = toast.caption;
134
- item.childContent = typeof toast.childContent == "string" ? createDocumentFragment(toast.childContent) : toast.childContent;
135
133
  item.context = toast.context ?? this.context;
136
134
  item.culture = toast.culture ?? this.culture;
137
135
  item.delay = toast.delay ?? this.delay;
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "name": "@cedx/base",
8
8
  "repository": "cedx/base",
9
9
  "type": "module",
10
- "version": "0.16.0",
10
+ "version": "0.18.0",
11
11
  "devDependencies": {
12
12
  "@playwright/browser-chromium": "^1.55.0",
13
13
  "@types/bootstrap": "^5.2.10",
@@ -21,7 +21,7 @@
21
21
  "mocha": "^11.7.1",
22
22
  "playwright": "^1.55.0",
23
23
  "serve-handler": "^6.1.6",
24
- "typedoc": "^0.28.10",
24
+ "typedoc": "^0.28.11",
25
25
  "typescript": "^5.9.2",
26
26
  "typescript-eslint": "^8.40.0"
27
27
  },
@@ -2,6 +2,37 @@ import {Context, toCss as contextCss} from "../Context.js";
2
2
  import {DialogResult} from "../DialogResult.js";
3
3
  import {Variant, toCss as variantCss} from "../Variant.js";
4
4
 
5
+ /**
6
+ * Represents a dialog box button.
7
+ */
8
+ export interface IDialogButton {
9
+
10
+ /**
11
+ * A contextual modifier.
12
+ */
13
+ context?: Context|null;
14
+
15
+ /**
16
+ * The button icon.
17
+ */
18
+ icon?: string|null;
19
+
20
+ /**
21
+ * The button label.
22
+ */
23
+ label?: string;
24
+
25
+ /**
26
+ * The button value.
27
+ */
28
+ value?: DialogResult;
29
+
30
+ /**
31
+ * A tone variant.
32
+ */
33
+ variant?: Variant|null;
34
+ }
35
+
5
36
  /**
6
37
  * Represents a dialog box button.
7
38
  */