@orbitkit/components 0.3.0 → 0.4.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/dist/astro/ambient-image/AmbientFigcaption.astro +15 -0
- package/dist/astro/ambient-image/AmbientImage.astro +70 -0
- package/dist/astro/ambient-image/index.ts +4 -0
- package/dist/astro/button-group/ButtonGroup.astro +21 -0
- package/dist/astro/button-group/buttonGroupVariants.ts +34 -0
- package/dist/astro/button-group/index.ts +3 -0
- package/dist/astro/diff/Diff.astro +55 -0
- package/dist/astro/diff/DiffItem.astro +21 -0
- package/dist/astro/diff/index.ts +4 -0
- package/dist/astro/loader/Loader.astro +16 -0
- package/dist/astro/loader/index.ts +6 -0
- package/dist/astro/loader/loaders/12-dots-scale-rotate.svg +1 -0
- package/dist/astro/loader/loaders/180-ring-with-bg.svg +1 -0
- package/dist/astro/loader/loaders/180-ring.svg +1 -0
- package/dist/astro/loader/loaders/270-ring-with-bg.svg +1 -0
- package/dist/astro/loader/loaders/270-ring.svg +1 -0
- package/dist/astro/loader/loaders/3-dots-bounce.svg +1 -0
- package/dist/astro/loader/loaders/3-dots-fade.svg +1 -0
- package/dist/astro/loader/loaders/3-dots-move.svg +1 -0
- package/dist/astro/loader/loaders/3-dots-rotate.svg +1 -0
- package/dist/astro/loader/loaders/3-dots-scale-middle.svg +1 -0
- package/dist/astro/loader/loaders/3-dots-scale.svg +1 -0
- package/dist/astro/loader/loaders/6-dots-rotate.svg +1 -0
- package/dist/astro/loader/loaders/6-dots-scale-middle.svg +1 -0
- package/dist/astro/loader/loaders/6-dots-scale.svg +1 -0
- package/dist/astro/loader/loaders/8-dots-rotate.svg +1 -0
- package/dist/astro/loader/loaders/90-ring-with-bg.svg +1 -0
- package/dist/astro/loader/loaders/90-ring.svg +1 -0
- package/dist/astro/loader/loaders/bars-fade.svg +1 -0
- package/dist/astro/loader/loaders/bars-rotate-fade.svg +1 -0
- package/dist/astro/loader/loaders/bars-scale-fade.svg +1 -0
- package/dist/astro/loader/loaders/bars-scale-middle.svg +1 -0
- package/dist/astro/loader/loaders/bars-scale.svg +1 -0
- package/dist/astro/loader/loaders/blocks-scale.svg +1 -0
- package/dist/astro/loader/loaders/blocks-shuffle-2.svg +1 -0
- package/dist/astro/loader/loaders/blocks-shuffle-3.svg +1 -0
- package/dist/astro/loader/loaders/blocks-wave.svg +1 -0
- package/dist/astro/loader/loaders/bouncing-ball.svg +1 -0
- package/dist/astro/loader/loaders/clock.svg +1 -0
- package/dist/astro/loader/loaders/dot-revolve.svg +1 -0
- package/dist/astro/loader/loaders/eclipse-half.svg +1 -0
- package/dist/astro/loader/loaders/eclipse.svg +1 -0
- package/dist/astro/loader/loaders/gooey-balls-1.svg +1 -0
- package/dist/astro/loader/loaders/gooey-balls-2.svg +1 -0
- package/dist/astro/loader/loaders/index.ts +99 -0
- package/dist/astro/loader/loaders/pulse-2.svg +1 -0
- package/dist/astro/loader/loaders/pulse-3.svg +1 -0
- package/dist/astro/loader/loaders/pulse-multiple.svg +1 -0
- package/dist/astro/loader/loaders/pulse-ring.svg +1 -0
- package/dist/astro/loader/loaders/pulse-rings-2.svg +1 -0
- package/dist/astro/loader/loaders/pulse-rings-3.svg +1 -0
- package/dist/astro/loader/loaders/pulse-rings-multiple.svg +1 -0
- package/dist/astro/loader/loaders/pulse.svg +1 -0
- package/dist/astro/loader/loaders/ring-resize.svg +1 -0
- package/dist/astro/loader/loaders/tadpole.svg +1 -0
- package/dist/astro/loader/loaders/wifi-fade.svg +1 -0
- package/dist/astro/loader/loaders/wifi.svg +1 -0
- package/dist/astro/loader/loaders/wind-toy.svg +1 -0
- package/dist/astro/marquee/Marquee.astro +53 -53
- package/dist/astro/marquee/index.ts +3 -3
- package/dist/astro/scroll-progress/ScrollProgress.astro +41 -41
- package/dist/astro/scroll-progress/ScrollProgressBar.astro +19 -19
- package/dist/astro/scroll-progress/index.ts +4 -4
- package/dist/astro/toast/Toast.astro +36 -36
- package/dist/astro/toast/ToastDescription.astro +10 -10
- package/dist/astro/toast/ToastTitle.astro +18 -18
- package/dist/astro/toast/Toaster.astro +78 -78
- package/dist/astro/toast/assets.ts +6 -6
- package/dist/astro/toast/index.ts +30 -30
- package/dist/astro/toast/toast.ts +277 -277
- package/dist/index.js +18 -2
- package/dist/index.js.map +1 -1
- package/package.json +55 -54
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
import { toastIcons } from "./assets";
|
|
2
|
-
import type {
|
|
3
|
-
ToastDomElements,
|
|
4
|
-
ToastOptions,
|
|
5
|
-
ToastPosition,
|
|
6
|
-
ToastType,
|
|
7
|
-
} from "./toast";
|
|
8
|
-
import { Toast as ToastHandler } from "./toast";
|
|
9
|
-
import Toast from "./Toast.astro";
|
|
10
|
-
import ToastDescription from "./ToastDescription.astro";
|
|
11
|
-
import ToastTitle from "./ToastTitle.astro";
|
|
12
|
-
|
|
13
|
-
export { Toast, ToastDescription, ToastHandler, toastIcons, ToastTitle };
|
|
14
|
-
export type { ToastDomElements, ToastOptions, ToastPosition, ToastType };
|
|
15
|
-
|
|
16
|
-
export const toast = Object.assign(
|
|
17
|
-
(opts: ToastOptions) => new ToastHandler(opts),
|
|
18
|
-
{
|
|
19
|
-
success: (opts: Omit<ToastOptions, "type">) =>
|
|
20
|
-
new ToastHandler({ ...opts, type: "success" }),
|
|
21
|
-
error: (opts: Omit<ToastOptions, "type">) =>
|
|
22
|
-
new ToastHandler({ ...opts, type: "error" }),
|
|
23
|
-
warning: (opts: Omit<ToastOptions, "type">) =>
|
|
24
|
-
new ToastHandler({ ...opts, type: "warning" }),
|
|
25
|
-
info: (opts: Omit<ToastOptions, "type">) =>
|
|
26
|
-
new ToastHandler({ ...opts, type: "info" }),
|
|
27
|
-
default: (opts: Omit<ToastOptions, "type">) =>
|
|
28
|
-
new ToastHandler({ ...opts, type: "default" }),
|
|
29
|
-
},
|
|
30
|
-
);
|
|
1
|
+
import { toastIcons } from "./assets";
|
|
2
|
+
import type {
|
|
3
|
+
ToastDomElements,
|
|
4
|
+
ToastOptions,
|
|
5
|
+
ToastPosition,
|
|
6
|
+
ToastType,
|
|
7
|
+
} from "./toast";
|
|
8
|
+
import { Toast as ToastHandler } from "./toast";
|
|
9
|
+
import Toast from "./Toast.astro";
|
|
10
|
+
import ToastDescription from "./ToastDescription.astro";
|
|
11
|
+
import ToastTitle from "./ToastTitle.astro";
|
|
12
|
+
|
|
13
|
+
export { Toast, ToastDescription, ToastHandler, toastIcons, ToastTitle };
|
|
14
|
+
export type { ToastDomElements, ToastOptions, ToastPosition, ToastType };
|
|
15
|
+
|
|
16
|
+
export const toast = Object.assign(
|
|
17
|
+
(opts: ToastOptions) => new ToastHandler(opts),
|
|
18
|
+
{
|
|
19
|
+
success: (opts: Omit<ToastOptions, "type">) =>
|
|
20
|
+
new ToastHandler({ ...opts, type: "success" }),
|
|
21
|
+
error: (opts: Omit<ToastOptions, "type">) =>
|
|
22
|
+
new ToastHandler({ ...opts, type: "error" }),
|
|
23
|
+
warning: (opts: Omit<ToastOptions, "type">) =>
|
|
24
|
+
new ToastHandler({ ...opts, type: "warning" }),
|
|
25
|
+
info: (opts: Omit<ToastOptions, "type">) =>
|
|
26
|
+
new ToastHandler({ ...opts, type: "info" }),
|
|
27
|
+
default: (opts: Omit<ToastOptions, "type">) =>
|
|
28
|
+
new ToastHandler({ ...opts, type: "default" }),
|
|
29
|
+
},
|
|
30
|
+
);
|
|
@@ -1,277 +1,277 @@
|
|
|
1
|
-
import { toastIcons } from "./assets";
|
|
2
|
-
|
|
3
|
-
export interface ToastDomElements {
|
|
4
|
-
title: HTMLElement | null;
|
|
5
|
-
description: HTMLElement | null;
|
|
6
|
-
dismissibleButton: HTMLElement | null;
|
|
7
|
-
iconContainer: HTMLElement | null;
|
|
8
|
-
closeButtons: NodeListOf<HTMLElement>;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export type ToastPosition =
|
|
12
|
-
| "top-right"
|
|
13
|
-
| "top-center"
|
|
14
|
-
| "top-left"
|
|
15
|
-
| "bottom-right"
|
|
16
|
-
| "bottom-center"
|
|
17
|
-
| "bottom-left";
|
|
18
|
-
|
|
19
|
-
export type ToastType = "default" | "success" | "error" | "warning" | "info";
|
|
20
|
-
|
|
21
|
-
export interface ToastOptions {
|
|
22
|
-
title: string;
|
|
23
|
-
description?: string;
|
|
24
|
-
duration?: number;
|
|
25
|
-
dismissible?: boolean;
|
|
26
|
-
autoClose?: boolean;
|
|
27
|
-
type?: ToastType;
|
|
28
|
-
onClose?: () => void;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const DEFAULT_DURATION = 3000;
|
|
32
|
-
const DEFAULT_MAX_VISIBLE_TOASTS = 3;
|
|
33
|
-
const DEFAULT_OFFSET = 24;
|
|
34
|
-
const DEFAULT_GAP = 16;
|
|
35
|
-
const DEFAULT_POSITION: ToastPosition = "bottom-right";
|
|
36
|
-
const DEFAULT_TOAST_TYPE: ToastType = "default";
|
|
37
|
-
|
|
38
|
-
export class Toast {
|
|
39
|
-
private static instances: HTMLElement[] = [];
|
|
40
|
-
|
|
41
|
-
private opts: ToastOptions;
|
|
42
|
-
private toaster: HTMLElement | null;
|
|
43
|
-
private toast: HTMLElement | null;
|
|
44
|
-
private elements: ToastDomElements;
|
|
45
|
-
|
|
46
|
-
private duration: number;
|
|
47
|
-
private startTime: number = 0;
|
|
48
|
-
private remainingTime: number = 0;
|
|
49
|
-
private timeoutId: number | null = null;
|
|
50
|
-
private position: ToastPosition;
|
|
51
|
-
private dismissible: boolean;
|
|
52
|
-
private autoClose: boolean;
|
|
53
|
-
|
|
54
|
-
private maxVisibleToasts: number;
|
|
55
|
-
private offset: number;
|
|
56
|
-
private gap: number;
|
|
57
|
-
|
|
58
|
-
constructor(opts: ToastOptions) {
|
|
59
|
-
this.opts = opts;
|
|
60
|
-
|
|
61
|
-
this.toaster = document.querySelector("[data-toaster]");
|
|
62
|
-
if (!this.toaster) {
|
|
63
|
-
throw new Error(
|
|
64
|
-
"Toaster container with 'data-toaster' attribute not found in the document.",
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
this.toast = this.getTemplate().querySelector(
|
|
69
|
-
"[data-toast]",
|
|
70
|
-
) as HTMLElement;
|
|
71
|
-
if (!this.toast) {
|
|
72
|
-
throw new Error("Toast element with 'data-toast' not found in template.");
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
this.elements = {
|
|
76
|
-
title: this.toast.querySelector("[data-toast-title]"),
|
|
77
|
-
description: this.toast.querySelector("[data-toast-description]"),
|
|
78
|
-
iconContainer: this.toast.querySelector("[data-toast-icon]"),
|
|
79
|
-
closeButtons: this.toast.querySelectorAll("[data-close]") ?? [],
|
|
80
|
-
dismissibleButton: this.toast.querySelector("[data-dismissible-button]"),
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
this.gap = parseInt(this.toaster.dataset.gap ?? `${DEFAULT_GAP}`, 10);
|
|
84
|
-
this.maxVisibleToasts = parseInt(
|
|
85
|
-
this.toaster.dataset.visibleToasts ?? `${DEFAULT_MAX_VISIBLE_TOASTS}`,
|
|
86
|
-
10,
|
|
87
|
-
);
|
|
88
|
-
this.offset = parseInt(
|
|
89
|
-
this.toaster.dataset.offset ?? `${DEFAULT_OFFSET}`,
|
|
90
|
-
10,
|
|
91
|
-
);
|
|
92
|
-
this.position =
|
|
93
|
-
(this.toaster?.dataset.position as typeof this.position) ??
|
|
94
|
-
DEFAULT_POSITION;
|
|
95
|
-
this.duration =
|
|
96
|
-
this.opts.duration ??
|
|
97
|
-
parseInt(this.toaster.dataset.duration ?? `${DEFAULT_DURATION}`);
|
|
98
|
-
this.dismissible =
|
|
99
|
-
this.opts.dismissible ?? this.toaster.dataset.dismissible === "true";
|
|
100
|
-
this.autoClose =
|
|
101
|
-
this.opts.autoClose ?? this.toaster.dataset.autoClose === "true";
|
|
102
|
-
|
|
103
|
-
this.init();
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
private init() {
|
|
107
|
-
this.initializeToastContent();
|
|
108
|
-
this.calculateInitialPosition();
|
|
109
|
-
this.setupAccessibility();
|
|
110
|
-
this.setupEventListeners();
|
|
111
|
-
this.showToast();
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
private setupAccessibility() {
|
|
115
|
-
if (this.toast) {
|
|
116
|
-
this.toast.setAttribute("role", "alert");
|
|
117
|
-
this.toast.setAttribute("aria-live", "assertive");
|
|
118
|
-
this.toast.setAttribute("aria-atomic", "true");
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
private initializeToastContent() {
|
|
123
|
-
this.toast!.setAttribute(
|
|
124
|
-
"data-toast-type",
|
|
125
|
-
this.opts.type ?? DEFAULT_TOAST_TYPE,
|
|
126
|
-
);
|
|
127
|
-
|
|
128
|
-
if (this.elements.iconContainer) {
|
|
129
|
-
const iconType = toastIcons[this.opts.type ?? DEFAULT_TOAST_TYPE];
|
|
130
|
-
if (iconType) {
|
|
131
|
-
this.elements.iconContainer.innerHTML = iconType;
|
|
132
|
-
} else {
|
|
133
|
-
this.elements.iconContainer.remove();
|
|
134
|
-
this.elements.iconContainer = null;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
if (this.elements.title) {
|
|
139
|
-
this.elements.title.textContent = this.opts.title;
|
|
140
|
-
} else {
|
|
141
|
-
console.warn(
|
|
142
|
-
"Toast title element not found. Title might not be displayed.",
|
|
143
|
-
);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (this.opts.description && this.elements.description) {
|
|
147
|
-
this.elements.description.textContent = this.opts.description;
|
|
148
|
-
} else if (this.elements.description) {
|
|
149
|
-
this.elements.description.remove();
|
|
150
|
-
this.elements.description = null;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
if (!this.dismissible && this.elements.dismissibleButton) {
|
|
154
|
-
this.elements.dismissibleButton.remove();
|
|
155
|
-
this.elements.dismissibleButton = null;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
private setupEventListeners() {
|
|
160
|
-
this.elements.closeButtons.forEach((trigger) => {
|
|
161
|
-
trigger.addEventListener("click", () => this.removeToast());
|
|
162
|
-
});
|
|
163
|
-
this.toaster!.addEventListener("mouseenter", () => this.handleMouseEnter());
|
|
164
|
-
this.toaster!.addEventListener("mouseleave", () => this.handleMouseLeave());
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
private showToast() {
|
|
168
|
-
this.toaster?.insertAdjacentElement("afterbegin", this.toast!);
|
|
169
|
-
Toast.instances.unshift(this.toast!);
|
|
170
|
-
this.adjustToastPositions();
|
|
171
|
-
this.setState(this.toast!, "showing");
|
|
172
|
-
|
|
173
|
-
if (this.autoClose) {
|
|
174
|
-
this.startTime = Date.now();
|
|
175
|
-
this.remainingTime = this.duration;
|
|
176
|
-
this.timeoutId = window.setInterval(() => {
|
|
177
|
-
this.removeToast();
|
|
178
|
-
this.opts.onClose?.();
|
|
179
|
-
}, this.duration);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
private removeToast() {
|
|
184
|
-
this.setState(this.toast!, "hide");
|
|
185
|
-
const index = Toast.instances.indexOf(this.toast!);
|
|
186
|
-
if (index !== -1) {
|
|
187
|
-
Toast.instances.splice(index, 1);
|
|
188
|
-
}
|
|
189
|
-
this.adjustToastPositions();
|
|
190
|
-
|
|
191
|
-
setTimeout(() => {
|
|
192
|
-
this.toast?.remove();
|
|
193
|
-
this.opts.onClose?.();
|
|
194
|
-
}, 500);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
private adjustToastPositions() {
|
|
198
|
-
const isButtom = this.position.startsWith("bottom");
|
|
199
|
-
let accumulatedHeight = this.offset;
|
|
200
|
-
|
|
201
|
-
Toast.instances.forEach((currentToast, index) => {
|
|
202
|
-
accumulatedHeight += currentToast.offsetHeight;
|
|
203
|
-
const verticalOffset = accumulatedHeight + index * this.gap;
|
|
204
|
-
|
|
205
|
-
const sign = isButtom ? -1 : 1;
|
|
206
|
-
const translateYValue =
|
|
207
|
-
sign * (verticalOffset - currentToast.offsetHeight);
|
|
208
|
-
|
|
209
|
-
let translateXValue = "";
|
|
210
|
-
if (this.position.endsWith("center")) {
|
|
211
|
-
translateXValue = "translateX(-50%)";
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
currentToast.style.transform = `${translateXValue} translateY(${translateYValue}px)`;
|
|
215
|
-
|
|
216
|
-
this.setState(
|
|
217
|
-
currentToast,
|
|
218
|
-
index < this.maxVisibleToasts ? "showing" : "hide",
|
|
219
|
-
);
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
private handleMouseEnter() {
|
|
224
|
-
if (!this.autoClose) return;
|
|
225
|
-
if (this.timeoutId) {
|
|
226
|
-
clearTimeout(this.timeoutId);
|
|
227
|
-
const elapsedTime = Date.now() - this.startTime;
|
|
228
|
-
this.remainingTime = Math.max(0, this.remainingTime - elapsedTime);
|
|
229
|
-
this.timeoutId = null;
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
private handleMouseLeave() {
|
|
234
|
-
if (!this.autoClose) return;
|
|
235
|
-
if (this.remainingTime > 0) {
|
|
236
|
-
this.startTime = Date.now();
|
|
237
|
-
this.timeoutId = window.setTimeout(
|
|
238
|
-
() => this.removeToast(),
|
|
239
|
-
this.remainingTime,
|
|
240
|
-
);
|
|
241
|
-
} else {
|
|
242
|
-
this.removeToast();
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
private calculateInitialPosition() {
|
|
247
|
-
const [y, x] = this.position.split("-");
|
|
248
|
-
|
|
249
|
-
if (y === "top") {
|
|
250
|
-
this.toast!.style.top = `0px`;
|
|
251
|
-
} else {
|
|
252
|
-
this.toast!.style.bottom = `0px`;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
if (x === "left") {
|
|
256
|
-
this.toast!.style.left = `${this.offset}px`;
|
|
257
|
-
} else if (x === "center") {
|
|
258
|
-
this.toast!.style.left = "50%";
|
|
259
|
-
this.toast!.style.transform = "translateX(-50%)";
|
|
260
|
-
} else {
|
|
261
|
-
this.toast!.style.right = `${this.offset}px`;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
private setState(toast: HTMLElement, state: "showing" | "hide") {
|
|
266
|
-
toast.setAttribute("data-state", state);
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
private getTemplate() {
|
|
270
|
-
const toastTemplate =
|
|
271
|
-
this.toaster?.querySelector<HTMLTemplateElement>("#toast-template");
|
|
272
|
-
if (!toastTemplate) {
|
|
273
|
-
throw new Error("Toast template not found in the document.");
|
|
274
|
-
}
|
|
275
|
-
return toastTemplate.content.cloneNode(true) as DocumentFragment;
|
|
276
|
-
}
|
|
277
|
-
}
|
|
1
|
+
import { toastIcons } from "./assets";
|
|
2
|
+
|
|
3
|
+
export interface ToastDomElements {
|
|
4
|
+
title: HTMLElement | null;
|
|
5
|
+
description: HTMLElement | null;
|
|
6
|
+
dismissibleButton: HTMLElement | null;
|
|
7
|
+
iconContainer: HTMLElement | null;
|
|
8
|
+
closeButtons: NodeListOf<HTMLElement>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type ToastPosition =
|
|
12
|
+
| "top-right"
|
|
13
|
+
| "top-center"
|
|
14
|
+
| "top-left"
|
|
15
|
+
| "bottom-right"
|
|
16
|
+
| "bottom-center"
|
|
17
|
+
| "bottom-left";
|
|
18
|
+
|
|
19
|
+
export type ToastType = "default" | "success" | "error" | "warning" | "info";
|
|
20
|
+
|
|
21
|
+
export interface ToastOptions {
|
|
22
|
+
title: string;
|
|
23
|
+
description?: string;
|
|
24
|
+
duration?: number;
|
|
25
|
+
dismissible?: boolean;
|
|
26
|
+
autoClose?: boolean;
|
|
27
|
+
type?: ToastType;
|
|
28
|
+
onClose?: () => void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const DEFAULT_DURATION = 3000;
|
|
32
|
+
const DEFAULT_MAX_VISIBLE_TOASTS = 3;
|
|
33
|
+
const DEFAULT_OFFSET = 24;
|
|
34
|
+
const DEFAULT_GAP = 16;
|
|
35
|
+
const DEFAULT_POSITION: ToastPosition = "bottom-right";
|
|
36
|
+
const DEFAULT_TOAST_TYPE: ToastType = "default";
|
|
37
|
+
|
|
38
|
+
export class Toast {
|
|
39
|
+
private static instances: HTMLElement[] = [];
|
|
40
|
+
|
|
41
|
+
private opts: ToastOptions;
|
|
42
|
+
private toaster: HTMLElement | null;
|
|
43
|
+
private toast: HTMLElement | null;
|
|
44
|
+
private elements: ToastDomElements;
|
|
45
|
+
|
|
46
|
+
private duration: number;
|
|
47
|
+
private startTime: number = 0;
|
|
48
|
+
private remainingTime: number = 0;
|
|
49
|
+
private timeoutId: number | null = null;
|
|
50
|
+
private position: ToastPosition;
|
|
51
|
+
private dismissible: boolean;
|
|
52
|
+
private autoClose: boolean;
|
|
53
|
+
|
|
54
|
+
private maxVisibleToasts: number;
|
|
55
|
+
private offset: number;
|
|
56
|
+
private gap: number;
|
|
57
|
+
|
|
58
|
+
constructor(opts: ToastOptions) {
|
|
59
|
+
this.opts = opts;
|
|
60
|
+
|
|
61
|
+
this.toaster = document.querySelector("[data-toaster]");
|
|
62
|
+
if (!this.toaster) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
"Toaster container with 'data-toaster' attribute not found in the document.",
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
this.toast = this.getTemplate().querySelector(
|
|
69
|
+
"[data-toast]",
|
|
70
|
+
) as HTMLElement;
|
|
71
|
+
if (!this.toast) {
|
|
72
|
+
throw new Error("Toast element with 'data-toast' not found in template.");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
this.elements = {
|
|
76
|
+
title: this.toast.querySelector("[data-toast-title]"),
|
|
77
|
+
description: this.toast.querySelector("[data-toast-description]"),
|
|
78
|
+
iconContainer: this.toast.querySelector("[data-toast-icon]"),
|
|
79
|
+
closeButtons: this.toast.querySelectorAll("[data-close]") ?? [],
|
|
80
|
+
dismissibleButton: this.toast.querySelector("[data-dismissible-button]"),
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
this.gap = parseInt(this.toaster.dataset.gap ?? `${DEFAULT_GAP}`, 10);
|
|
84
|
+
this.maxVisibleToasts = parseInt(
|
|
85
|
+
this.toaster.dataset.visibleToasts ?? `${DEFAULT_MAX_VISIBLE_TOASTS}`,
|
|
86
|
+
10,
|
|
87
|
+
);
|
|
88
|
+
this.offset = parseInt(
|
|
89
|
+
this.toaster.dataset.offset ?? `${DEFAULT_OFFSET}`,
|
|
90
|
+
10,
|
|
91
|
+
);
|
|
92
|
+
this.position =
|
|
93
|
+
(this.toaster?.dataset.position as typeof this.position) ??
|
|
94
|
+
DEFAULT_POSITION;
|
|
95
|
+
this.duration =
|
|
96
|
+
this.opts.duration ??
|
|
97
|
+
parseInt(this.toaster.dataset.duration ?? `${DEFAULT_DURATION}`);
|
|
98
|
+
this.dismissible =
|
|
99
|
+
this.opts.dismissible ?? this.toaster.dataset.dismissible === "true";
|
|
100
|
+
this.autoClose =
|
|
101
|
+
this.opts.autoClose ?? this.toaster.dataset.autoClose === "true";
|
|
102
|
+
|
|
103
|
+
this.init();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private init() {
|
|
107
|
+
this.initializeToastContent();
|
|
108
|
+
this.calculateInitialPosition();
|
|
109
|
+
this.setupAccessibility();
|
|
110
|
+
this.setupEventListeners();
|
|
111
|
+
this.showToast();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private setupAccessibility() {
|
|
115
|
+
if (this.toast) {
|
|
116
|
+
this.toast.setAttribute("role", "alert");
|
|
117
|
+
this.toast.setAttribute("aria-live", "assertive");
|
|
118
|
+
this.toast.setAttribute("aria-atomic", "true");
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private initializeToastContent() {
|
|
123
|
+
this.toast!.setAttribute(
|
|
124
|
+
"data-toast-type",
|
|
125
|
+
this.opts.type ?? DEFAULT_TOAST_TYPE,
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
if (this.elements.iconContainer) {
|
|
129
|
+
const iconType = toastIcons[this.opts.type ?? DEFAULT_TOAST_TYPE];
|
|
130
|
+
if (iconType) {
|
|
131
|
+
this.elements.iconContainer.innerHTML = iconType;
|
|
132
|
+
} else {
|
|
133
|
+
this.elements.iconContainer.remove();
|
|
134
|
+
this.elements.iconContainer = null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (this.elements.title) {
|
|
139
|
+
this.elements.title.textContent = this.opts.title;
|
|
140
|
+
} else {
|
|
141
|
+
console.warn(
|
|
142
|
+
"Toast title element not found. Title might not be displayed.",
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (this.opts.description && this.elements.description) {
|
|
147
|
+
this.elements.description.textContent = this.opts.description;
|
|
148
|
+
} else if (this.elements.description) {
|
|
149
|
+
this.elements.description.remove();
|
|
150
|
+
this.elements.description = null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (!this.dismissible && this.elements.dismissibleButton) {
|
|
154
|
+
this.elements.dismissibleButton.remove();
|
|
155
|
+
this.elements.dismissibleButton = null;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private setupEventListeners() {
|
|
160
|
+
this.elements.closeButtons.forEach((trigger) => {
|
|
161
|
+
trigger.addEventListener("click", () => this.removeToast());
|
|
162
|
+
});
|
|
163
|
+
this.toaster!.addEventListener("mouseenter", () => this.handleMouseEnter());
|
|
164
|
+
this.toaster!.addEventListener("mouseleave", () => this.handleMouseLeave());
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private showToast() {
|
|
168
|
+
this.toaster?.insertAdjacentElement("afterbegin", this.toast!);
|
|
169
|
+
Toast.instances.unshift(this.toast!);
|
|
170
|
+
this.adjustToastPositions();
|
|
171
|
+
this.setState(this.toast!, "showing");
|
|
172
|
+
|
|
173
|
+
if (this.autoClose) {
|
|
174
|
+
this.startTime = Date.now();
|
|
175
|
+
this.remainingTime = this.duration;
|
|
176
|
+
this.timeoutId = window.setInterval(() => {
|
|
177
|
+
this.removeToast();
|
|
178
|
+
this.opts.onClose?.();
|
|
179
|
+
}, this.duration);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private removeToast() {
|
|
184
|
+
this.setState(this.toast!, "hide");
|
|
185
|
+
const index = Toast.instances.indexOf(this.toast!);
|
|
186
|
+
if (index !== -1) {
|
|
187
|
+
Toast.instances.splice(index, 1);
|
|
188
|
+
}
|
|
189
|
+
this.adjustToastPositions();
|
|
190
|
+
|
|
191
|
+
setTimeout(() => {
|
|
192
|
+
this.toast?.remove();
|
|
193
|
+
this.opts.onClose?.();
|
|
194
|
+
}, 500);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private adjustToastPositions() {
|
|
198
|
+
const isButtom = this.position.startsWith("bottom");
|
|
199
|
+
let accumulatedHeight = this.offset;
|
|
200
|
+
|
|
201
|
+
Toast.instances.forEach((currentToast, index) => {
|
|
202
|
+
accumulatedHeight += currentToast.offsetHeight;
|
|
203
|
+
const verticalOffset = accumulatedHeight + index * this.gap;
|
|
204
|
+
|
|
205
|
+
const sign = isButtom ? -1 : 1;
|
|
206
|
+
const translateYValue =
|
|
207
|
+
sign * (verticalOffset - currentToast.offsetHeight);
|
|
208
|
+
|
|
209
|
+
let translateXValue = "";
|
|
210
|
+
if (this.position.endsWith("center")) {
|
|
211
|
+
translateXValue = "translateX(-50%)";
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
currentToast.style.transform = `${translateXValue} translateY(${translateYValue}px)`;
|
|
215
|
+
|
|
216
|
+
this.setState(
|
|
217
|
+
currentToast,
|
|
218
|
+
index < this.maxVisibleToasts ? "showing" : "hide",
|
|
219
|
+
);
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private handleMouseEnter() {
|
|
224
|
+
if (!this.autoClose) return;
|
|
225
|
+
if (this.timeoutId) {
|
|
226
|
+
clearTimeout(this.timeoutId);
|
|
227
|
+
const elapsedTime = Date.now() - this.startTime;
|
|
228
|
+
this.remainingTime = Math.max(0, this.remainingTime - elapsedTime);
|
|
229
|
+
this.timeoutId = null;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
private handleMouseLeave() {
|
|
234
|
+
if (!this.autoClose) return;
|
|
235
|
+
if (this.remainingTime > 0) {
|
|
236
|
+
this.startTime = Date.now();
|
|
237
|
+
this.timeoutId = window.setTimeout(
|
|
238
|
+
() => this.removeToast(),
|
|
239
|
+
this.remainingTime,
|
|
240
|
+
);
|
|
241
|
+
} else {
|
|
242
|
+
this.removeToast();
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
private calculateInitialPosition() {
|
|
247
|
+
const [y, x] = this.position.split("-");
|
|
248
|
+
|
|
249
|
+
if (y === "top") {
|
|
250
|
+
this.toast!.style.top = `0px`;
|
|
251
|
+
} else {
|
|
252
|
+
this.toast!.style.bottom = `0px`;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (x === "left") {
|
|
256
|
+
this.toast!.style.left = `${this.offset}px`;
|
|
257
|
+
} else if (x === "center") {
|
|
258
|
+
this.toast!.style.left = "50%";
|
|
259
|
+
this.toast!.style.transform = "translateX(-50%)";
|
|
260
|
+
} else {
|
|
261
|
+
this.toast!.style.right = `${this.offset}px`;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
private setState(toast: HTMLElement, state: "showing" | "hide") {
|
|
266
|
+
toast.setAttribute("data-state", state);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
private getTemplate() {
|
|
270
|
+
const toastTemplate =
|
|
271
|
+
this.toaster?.querySelector<HTMLTemplateElement>("#toast-template");
|
|
272
|
+
if (!toastTemplate) {
|
|
273
|
+
throw new Error("Toast template not found in the document.");
|
|
274
|
+
}
|
|
275
|
+
return toastTemplate.content.cloneNode(true) as DocumentFragment;
|
|
276
|
+
}
|
|
277
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -124,13 +124,29 @@ var registry_default = {
|
|
|
124
124
|
{
|
|
125
125
|
name: "marquee",
|
|
126
126
|
version: "0.0.1"
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
name: "ambient-image",
|
|
130
|
+
version: "0.0.1"
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
name: "button-group",
|
|
134
|
+
version: "0.0.1"
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
name: "diff",
|
|
138
|
+
version: "0.0.1"
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
name: "loader",
|
|
142
|
+
version: "0.0.1"
|
|
127
143
|
}
|
|
128
144
|
]
|
|
129
145
|
};
|
|
130
146
|
|
|
131
147
|
// src/index.ts
|
|
132
|
-
import { fileURLToPath } from "
|
|
133
|
-
import path from "
|
|
148
|
+
import { fileURLToPath } from "url";
|
|
149
|
+
import path from "path";
|
|
134
150
|
var __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
135
151
|
function getAllComponents() {
|
|
136
152
|
const availableComponents = registry_default.components;
|