@cedx/base 0.17.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.
@@ -18,6 +18,14 @@ export class Toaster extends HTMLElement {
18
18
  */
19
19
  readonly #toastTemplate: DocumentFragment = this.querySelector("template")!.content;
20
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
+
21
29
  /**
22
30
  * Registers the component.
23
31
  */
@@ -86,7 +94,8 @@ export class Toaster extends HTMLElement {
86
94
  return value.trim() || null;
87
95
  }
88
96
  set icon(value: string|null) {
89
- this.toggleAttribute("icon", Boolean(value));
97
+ if (value) this.setAttribute("icon", value);
98
+ else this.removeAttribute("icon");
90
99
  }
91
100
 
92
101
  /**
@@ -108,9 +117,7 @@ export class Toaster extends HTMLElement {
108
117
  */
109
118
  attributeChangedCallback(attribute: string, oldValue: string|null, newValue: string|null): void {
110
119
  if (newValue != oldValue) switch (attribute) {
111
- case "position":
112
- this.#updatePosition(Object.values(Position).includes(newValue as Position) ? newValue as Position : Position.BottomEnd);
113
- break;
120
+ case "position": this.#updatePosition(Object.values(Position).includes(newValue as Position) ? newValue as Position : Position.BottomEnd); break;
114
121
  // No default
115
122
  }
116
123
  }
@@ -121,18 +128,27 @@ export class Toaster extends HTMLElement {
121
128
  * @param caption The title displayed in the toast header.
122
129
  * @param body The child content displayed in the toast body.
123
130
  */
124
- notify(context: Context, caption: string, body: DocumentFragment|string): void {
125
- this.show({context, caption, body});
126
- }
131
+ show(context: Context, caption: string, body: DocumentFragment|string): void;
127
132
 
128
133
  /**
129
- * Shows the specified toast.
134
+ * Shows a toast.
130
135
  * @param toast The toast to show.
131
136
  */
132
- 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
+
133
148
  const item = document.createElement("toaster-item");
134
- item.addEventListener("hidden.bs.toast", () => item.remove());
135
- 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);
136
152
 
137
153
  item.animation = toast.animation ?? this.animation;
138
154
  item.autoHide = toast.autoHide ?? this.autoHide;