@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.
@@ -1,52 +1,7 @@
1
1
  import {Context} from "../Context.js";
2
2
  import {createDocumentFragment} from "../ElementExtensions.js";
3
3
  import {Position, toCss} from "../Position.js";
4
-
5
- /**
6
- * Represents a notification.
7
- */
8
- export interface IToast {
9
-
10
- /**
11
- * Value indicating whether to apply a fade transition.
12
- */
13
- animation?: boolean;
14
-
15
- /**
16
- * Value indicating whether to automatically hide the toast.
17
- */
18
- autoHide?: boolean;
19
-
20
- /**
21
- * The title displayed in the header.
22
- */
23
- caption: string;
24
-
25
- /**
26
- * The child content displayed in the body.
27
- */
28
- childContent: DocumentFragment|string;
29
-
30
- /**
31
- * The default contextual modifier.
32
- */
33
- context?: Context;
34
-
35
- /**
36
- * The culture used to format the relative time.
37
- */
38
- culture?: Intl.Locale;
39
-
40
- /**
41
- * The delay, in milliseconds, to hide the toast.
42
- */
43
- delay?: number;
44
-
45
- /**
46
- * The icon displayed next to the caption.
47
- */
48
- icon?: string;
49
- }
4
+ import type {IToast} from "./Toast.js";
50
5
 
51
6
  /**
52
7
  * Manages the notifications.
@@ -63,6 +18,14 @@ export class Toaster extends HTMLElement {
63
18
  */
64
19
  readonly #toastTemplate: DocumentFragment = this.querySelector("template")!.content;
65
20
 
21
+ /**
22
+ * Creates a new toaster.
23
+ */
24
+ constructor() {
25
+ super();
26
+ for (const toast of this.querySelectorAll("toaster-item")) toast.addEventListener("hidden.bs.toast", () => toast.remove());
27
+ }
28
+
66
29
  /**
67
30
  * Registers the component.
68
31
  */
@@ -77,8 +40,7 @@ export class Toaster extends HTMLElement {
77
40
  return this.hasAttribute("animation");
78
41
  }
79
42
  set animation(value: boolean) {
80
- if (value) this.setAttribute("animation", "");
81
- else this.removeAttribute("animation");
43
+ this.toggleAttribute("animation", value);
82
44
  }
83
45
 
84
46
  /**
@@ -88,8 +50,7 @@ export class Toaster extends HTMLElement {
88
50
  return this.hasAttribute("autohide");
89
51
  }
90
52
  set autoHide(value: boolean) {
91
- if (value) this.setAttribute("autohide", "");
92
- else this.removeAttribute("autohide");
53
+ this.toggleAttribute("autohide", value);
93
54
  }
94
55
 
95
56
  /**
@@ -156,9 +117,7 @@ export class Toaster extends HTMLElement {
156
117
  */
157
118
  attributeChangedCallback(attribute: string, oldValue: string|null, newValue: string|null): void {
158
119
  if (newValue != oldValue) switch (attribute) {
159
- case "position":
160
- this.#updatePosition(Object.values(Position).includes(newValue as Position) ? newValue as Position : Position.BottomEnd);
161
- break;
120
+ case "position": this.#updatePosition(Object.values(Position).includes(newValue as Position) ? newValue as Position : Position.BottomEnd); break;
162
121
  // No default
163
122
  }
164
123
  }
@@ -167,25 +126,34 @@ export class Toaster extends HTMLElement {
167
126
  * Shows a toast.
168
127
  * @param context The contextual modifier.
169
128
  * @param caption The title displayed in the toast header.
170
- * @param childContent The child content displayed in the toast body.
129
+ * @param body The child content displayed in the toast body.
171
130
  */
172
- notify(context: Context, caption: string, childContent: DocumentFragment|string): void {
173
- this.show({context, caption, childContent});
174
- }
131
+ show(context: Context, caption: string, body: DocumentFragment|string): void;
175
132
 
176
133
  /**
177
- * Shows the specified toast.
134
+ * Shows a toast.
178
135
  * @param toast The toast to show.
179
136
  */
180
- show(toast: IToast): void {
137
+ show(toast: IToast): void;
138
+
139
+ /**
140
+ * Shows a toast.
141
+ * @param toast The toast to show, or the contextual modifier.
142
+ * @param caption The title displayed in the toast header.
143
+ * @param body The child content displayed in the toast body.
144
+ */
145
+ show(toast: IToast|Context, caption = "", body: DocumentFragment|string = ""): void {
146
+ if (typeof toast == "string") toast = {context: toast, caption, body};
147
+
181
148
  const item = document.createElement("toaster-item");
182
- item.addEventListener("hidden.bs.toast", () => item.remove());
183
- item.appendChild((this.#toastTemplate.cloneNode(true) as DocumentFragment).querySelector(".toast")!);
149
+ const childContent = (this.#toastTemplate.cloneNode(true) as DocumentFragment).querySelector(".toast")!;
150
+ childContent.addEventListener("hidden.bs.toast", () => item.remove());
151
+ item.appendChild(childContent);
184
152
 
185
153
  item.animation = toast.animation ?? this.animation;
186
154
  item.autoHide = toast.autoHide ?? this.autoHide;
155
+ item.body = typeof toast.body == "string" ? createDocumentFragment(toast.body) : toast.body;
187
156
  item.caption = toast.caption;
188
- item.childContent = typeof toast.childContent == "string" ? createDocumentFragment(toast.childContent) : toast.childContent;
189
157
  item.context = toast.context ?? this.context;
190
158
  item.culture = toast.culture ?? this.culture;
191
159
  item.delay = toast.delay ?? this.delay;