ga-toasts 1.0.0 → 2.0.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.
@@ -0,0 +1,174 @@
1
+ /**
2
+ * GA Toasts — modern, accessible, dependency-free toast notifications.
3
+ *
4
+ * Design goals of the 2.x rewrite:
5
+ * - Every toast owns its state (element, timer, listeners) in a registry, so
6
+ * closing/updating never leaves dangling timers or crashes (v1 bug: closing
7
+ * a toast before its auto-close timer fired threw on `parentNode`).
8
+ * - Listeners are attached through a per-toast AbortController and torn down in
9
+ * one call — no `cloneNode` tricks.
10
+ * - Messages are rendered as text by default (`html: true` to opt into markup),
11
+ * closing the v1 XSS hole.
12
+ * - Screen readers are notified through a persistent `aria-live` region.
13
+ * - The stacked layout is computed in JS (a single transform per toast) so the
14
+ * enter/leave, swipe, stack-offset and expand-on-hover transforms never fight.
15
+ */
16
+ type ToastType = 'success' | 'error' | 'warning' | 'info' | 'primary' | 'secondary' | 'loading';
17
+ type ToastPosition = 'top-start' | 'top-center' | 'top-end' | 'middle-start' | 'middle-center' | 'middle-end' | 'bottom-start' | 'bottom-center' | 'bottom-end';
18
+ type ToastAnimation = 'fade' | 'slide' | 'bounce' | 'scale';
19
+ type ToastVariant = '' | 'filled' | 'light';
20
+ type ToastSize = '' | 'xs' | 'sm' | 'md' | 'lg' | 'xl';
21
+ type ToastProgressPosition = 'top' | 'bottom' | 'none';
22
+ interface ToastAction {
23
+ text: string;
24
+ /** Extra class(es) for the button. */
25
+ className?: string;
26
+ /** @deprecated use `className` */
27
+ class?: string;
28
+ /** Click handler; receives the event and the toast handle. */
29
+ onClick?: (event: MouseEvent, handle: ToastHandle) => void;
30
+ /** @deprecated use `onClick` */
31
+ click?: (event: MouseEvent, handle: ToastHandle) => void;
32
+ /** Close the toast automatically after the handler runs (default true). */
33
+ closeOnClick?: boolean;
34
+ }
35
+ interface ToastOptions {
36
+ /** Explicit id. If a toast with this id exists it is updated instead of duplicated. */
37
+ id?: string;
38
+ title?: string;
39
+ message?: string;
40
+ /** Small muted line above the message (e.g. a timestamp). */
41
+ meta?: string;
42
+ type?: ToastType;
43
+ /** Auto-close delay in ms. `0` (or negative) means the toast is sticky. */
44
+ duration?: number;
45
+ closable?: boolean;
46
+ position?: ToastPosition;
47
+ /** Custom icon markup, or `false`/`null` to hide the icon entirely. */
48
+ icon?: string | null | false;
49
+ actions?: ToastAction[];
50
+ size?: ToastSize;
51
+ variant?: ToastVariant;
52
+ animation?: ToastAnimation;
53
+ /** Treat the whole toast as a button that closes on click. */
54
+ clickToClose?: boolean;
55
+ /** Allow dismissing by dragging horizontally (pointer/touch). Default true. */
56
+ swipeToClose?: boolean;
57
+ /** Render `message` as HTML instead of escaped text. Default false. */
58
+ html?: boolean;
59
+ /** Avatar image URL shown before the title. */
60
+ avatar?: string | null;
61
+ avatarAlt?: string;
62
+ /** Show an unread dot. */
63
+ unread?: boolean;
64
+ truncateTitle?: boolean;
65
+ /** Show the countdown progress bar. Default true. */
66
+ progress?: boolean;
67
+ progressPosition?: ToastProgressPosition;
68
+ /** Pause the countdown while hovered/focused. Default true. */
69
+ pauseOnHover?: boolean;
70
+ /** Pause the countdown while the tab is backgrounded. Default true. */
71
+ pauseOnPageHidden?: boolean;
72
+ /** Dismiss the newest toast when Escape is pressed. Default true. */
73
+ closeOnEscape?: boolean;
74
+ /** Denser layout with reduced padding. */
75
+ compact?: boolean;
76
+ /** Show a small status chip (defaults to the capitalized type). */
77
+ showStatus?: boolean;
78
+ statusText?: string;
79
+ /** Automatically pick an icon based on `type`. Default true. */
80
+ autoIcon?: boolean;
81
+ /** Frosted-glass background. Default true. */
82
+ glassmorphism?: boolean;
83
+ /** Segmented progress: total steps. */
84
+ steps?: number;
85
+ /** Segmented progress: current (1-based) step. */
86
+ currentStep?: number;
87
+ /** Extra class name(s) added to the toast root. */
88
+ className?: string;
89
+ /** ARIA role. Defaults to `alert` for error/warning, `status` otherwise. */
90
+ role?: string;
91
+ /**
92
+ * Arbitrary content instead of the default title/message layout. A string is
93
+ * inserted as HTML (dev-authored, so trusted); an element/function is used as-is.
94
+ */
95
+ content?: string | HTMLElement | (() => HTMLElement | string);
96
+ /**
97
+ * Move keyboard focus into the toast when shown and restore it on close.
98
+ * Used by `confirm()`; opt in for other actionable toasts.
99
+ */
100
+ moveFocus?: boolean;
101
+ /**
102
+ * Render as a modal: a backdrop blocks the rest of the page and focus is
103
+ * trapped until an action is taken. Only one modal toast can be open at a
104
+ * time — further modal calls are ignored until it closes. Used by `confirm()`.
105
+ */
106
+ modal?: boolean;
107
+ onShow?: (handle: ToastHandle) => void;
108
+ onClose?: (handle: ToastHandle) => void;
109
+ }
110
+ interface ConfirmOptions extends ToastOptions {
111
+ confirmText?: string;
112
+ cancelText?: string;
113
+ onConfirm?: () => void;
114
+ onCancel?: () => void;
115
+ }
116
+ interface PromiseMessages<T> {
117
+ loading: string;
118
+ success: string | ((value: T) => string);
119
+ error: string | ((error: unknown) => string);
120
+ }
121
+ /** Handle returned from every toast; lets you update or close it later. */
122
+ interface ToastHandle {
123
+ readonly id: string;
124
+ readonly el: HTMLElement;
125
+ update(options: Partial<ToastOptions>): ToastHandle;
126
+ close(): void;
127
+ }
128
+ declare function injectStyles(): void;
129
+ declare function show(options?: ToastOptions): ToastHandle;
130
+ declare function close(target: string | HTMLElement | null | undefined): void;
131
+ declare function closeAll(): void;
132
+ declare function clear(type?: ToastType): void;
133
+ declare function getCount(type?: ToastType): number;
134
+ declare function exists(id: string): boolean;
135
+ declare function get(id: string): HTMLElement | null;
136
+ declare const success: (message: string, options?: ToastOptions) => ToastHandle;
137
+ declare const error: (message: string, options?: ToastOptions) => ToastHandle;
138
+ declare const warning: (message: string, options?: ToastOptions) => ToastHandle;
139
+ declare const info: (message: string, options?: ToastOptions) => ToastHandle;
140
+ declare function loading(message?: string, options?: ToastOptions): ToastHandle;
141
+ declare function confirm(message: string, options?: ConfirmOptions): ToastHandle;
142
+ declare function promise<T>(input: Promise<T> | (() => Promise<T>), messages: PromiseMessages<T>, options?: ToastOptions): Promise<T>;
143
+ declare function setDefaults(defaults: ToastOptions): void;
144
+ declare function setLogger(fn: ((event: string, payload: unknown) => void) | null): void;
145
+ /** Render arbitrary content (HTML string, element, or a factory) as a toast. */
146
+ declare function custom(content: NonNullable<ToastOptions['content']>, options?: ToastOptions): ToastHandle;
147
+ interface ToastFn {
148
+ (message: string, options?: ToastOptions): ToastHandle;
149
+ show: typeof show;
150
+ success: typeof success;
151
+ error: typeof error;
152
+ warning: typeof warning;
153
+ info: typeof info;
154
+ loading: typeof loading;
155
+ confirm: typeof confirm;
156
+ promise: typeof promise;
157
+ custom: typeof custom;
158
+ close: typeof close;
159
+ closeAll: typeof closeAll;
160
+ clear: typeof clear;
161
+ update: (id: string, options: Partial<ToastOptions>) => ToastHandle | null;
162
+ get: typeof get;
163
+ exists: typeof exists;
164
+ getCount: typeof getCount;
165
+ setDefaults: typeof setDefaults;
166
+ setLogger: typeof setLogger;
167
+ injectStyles: typeof injectStyles;
168
+ }
169
+ /** Callable shorthand: `toast('Saved')`, plus `toast.success(...)` etc. */
170
+ declare const toast: ToastFn;
171
+ /** Named object API, backwards-compatible with GA Toasts 1.x. */
172
+ declare const GaToasts: ToastFn;
173
+
174
+ export { type ConfirmOptions, GaToasts, type PromiseMessages, type ToastAction, type ToastAnimation, type ToastFn, type ToastHandle, type ToastOptions, type ToastPosition, type ToastProgressPosition, type ToastSize, type ToastType, type ToastVariant, toast as default, toast };
@@ -0,0 +1,174 @@
1
+ /**
2
+ * GA Toasts — modern, accessible, dependency-free toast notifications.
3
+ *
4
+ * Design goals of the 2.x rewrite:
5
+ * - Every toast owns its state (element, timer, listeners) in a registry, so
6
+ * closing/updating never leaves dangling timers or crashes (v1 bug: closing
7
+ * a toast before its auto-close timer fired threw on `parentNode`).
8
+ * - Listeners are attached through a per-toast AbortController and torn down in
9
+ * one call — no `cloneNode` tricks.
10
+ * - Messages are rendered as text by default (`html: true` to opt into markup),
11
+ * closing the v1 XSS hole.
12
+ * - Screen readers are notified through a persistent `aria-live` region.
13
+ * - The stacked layout is computed in JS (a single transform per toast) so the
14
+ * enter/leave, swipe, stack-offset and expand-on-hover transforms never fight.
15
+ */
16
+ type ToastType = 'success' | 'error' | 'warning' | 'info' | 'primary' | 'secondary' | 'loading';
17
+ type ToastPosition = 'top-start' | 'top-center' | 'top-end' | 'middle-start' | 'middle-center' | 'middle-end' | 'bottom-start' | 'bottom-center' | 'bottom-end';
18
+ type ToastAnimation = 'fade' | 'slide' | 'bounce' | 'scale';
19
+ type ToastVariant = '' | 'filled' | 'light';
20
+ type ToastSize = '' | 'xs' | 'sm' | 'md' | 'lg' | 'xl';
21
+ type ToastProgressPosition = 'top' | 'bottom' | 'none';
22
+ interface ToastAction {
23
+ text: string;
24
+ /** Extra class(es) for the button. */
25
+ className?: string;
26
+ /** @deprecated use `className` */
27
+ class?: string;
28
+ /** Click handler; receives the event and the toast handle. */
29
+ onClick?: (event: MouseEvent, handle: ToastHandle) => void;
30
+ /** @deprecated use `onClick` */
31
+ click?: (event: MouseEvent, handle: ToastHandle) => void;
32
+ /** Close the toast automatically after the handler runs (default true). */
33
+ closeOnClick?: boolean;
34
+ }
35
+ interface ToastOptions {
36
+ /** Explicit id. If a toast with this id exists it is updated instead of duplicated. */
37
+ id?: string;
38
+ title?: string;
39
+ message?: string;
40
+ /** Small muted line above the message (e.g. a timestamp). */
41
+ meta?: string;
42
+ type?: ToastType;
43
+ /** Auto-close delay in ms. `0` (or negative) means the toast is sticky. */
44
+ duration?: number;
45
+ closable?: boolean;
46
+ position?: ToastPosition;
47
+ /** Custom icon markup, or `false`/`null` to hide the icon entirely. */
48
+ icon?: string | null | false;
49
+ actions?: ToastAction[];
50
+ size?: ToastSize;
51
+ variant?: ToastVariant;
52
+ animation?: ToastAnimation;
53
+ /** Treat the whole toast as a button that closes on click. */
54
+ clickToClose?: boolean;
55
+ /** Allow dismissing by dragging horizontally (pointer/touch). Default true. */
56
+ swipeToClose?: boolean;
57
+ /** Render `message` as HTML instead of escaped text. Default false. */
58
+ html?: boolean;
59
+ /** Avatar image URL shown before the title. */
60
+ avatar?: string | null;
61
+ avatarAlt?: string;
62
+ /** Show an unread dot. */
63
+ unread?: boolean;
64
+ truncateTitle?: boolean;
65
+ /** Show the countdown progress bar. Default true. */
66
+ progress?: boolean;
67
+ progressPosition?: ToastProgressPosition;
68
+ /** Pause the countdown while hovered/focused. Default true. */
69
+ pauseOnHover?: boolean;
70
+ /** Pause the countdown while the tab is backgrounded. Default true. */
71
+ pauseOnPageHidden?: boolean;
72
+ /** Dismiss the newest toast when Escape is pressed. Default true. */
73
+ closeOnEscape?: boolean;
74
+ /** Denser layout with reduced padding. */
75
+ compact?: boolean;
76
+ /** Show a small status chip (defaults to the capitalized type). */
77
+ showStatus?: boolean;
78
+ statusText?: string;
79
+ /** Automatically pick an icon based on `type`. Default true. */
80
+ autoIcon?: boolean;
81
+ /** Frosted-glass background. Default true. */
82
+ glassmorphism?: boolean;
83
+ /** Segmented progress: total steps. */
84
+ steps?: number;
85
+ /** Segmented progress: current (1-based) step. */
86
+ currentStep?: number;
87
+ /** Extra class name(s) added to the toast root. */
88
+ className?: string;
89
+ /** ARIA role. Defaults to `alert` for error/warning, `status` otherwise. */
90
+ role?: string;
91
+ /**
92
+ * Arbitrary content instead of the default title/message layout. A string is
93
+ * inserted as HTML (dev-authored, so trusted); an element/function is used as-is.
94
+ */
95
+ content?: string | HTMLElement | (() => HTMLElement | string);
96
+ /**
97
+ * Move keyboard focus into the toast when shown and restore it on close.
98
+ * Used by `confirm()`; opt in for other actionable toasts.
99
+ */
100
+ moveFocus?: boolean;
101
+ /**
102
+ * Render as a modal: a backdrop blocks the rest of the page and focus is
103
+ * trapped until an action is taken. Only one modal toast can be open at a
104
+ * time — further modal calls are ignored until it closes. Used by `confirm()`.
105
+ */
106
+ modal?: boolean;
107
+ onShow?: (handle: ToastHandle) => void;
108
+ onClose?: (handle: ToastHandle) => void;
109
+ }
110
+ interface ConfirmOptions extends ToastOptions {
111
+ confirmText?: string;
112
+ cancelText?: string;
113
+ onConfirm?: () => void;
114
+ onCancel?: () => void;
115
+ }
116
+ interface PromiseMessages<T> {
117
+ loading: string;
118
+ success: string | ((value: T) => string);
119
+ error: string | ((error: unknown) => string);
120
+ }
121
+ /** Handle returned from every toast; lets you update or close it later. */
122
+ interface ToastHandle {
123
+ readonly id: string;
124
+ readonly el: HTMLElement;
125
+ update(options: Partial<ToastOptions>): ToastHandle;
126
+ close(): void;
127
+ }
128
+ declare function injectStyles(): void;
129
+ declare function show(options?: ToastOptions): ToastHandle;
130
+ declare function close(target: string | HTMLElement | null | undefined): void;
131
+ declare function closeAll(): void;
132
+ declare function clear(type?: ToastType): void;
133
+ declare function getCount(type?: ToastType): number;
134
+ declare function exists(id: string): boolean;
135
+ declare function get(id: string): HTMLElement | null;
136
+ declare const success: (message: string, options?: ToastOptions) => ToastHandle;
137
+ declare const error: (message: string, options?: ToastOptions) => ToastHandle;
138
+ declare const warning: (message: string, options?: ToastOptions) => ToastHandle;
139
+ declare const info: (message: string, options?: ToastOptions) => ToastHandle;
140
+ declare function loading(message?: string, options?: ToastOptions): ToastHandle;
141
+ declare function confirm(message: string, options?: ConfirmOptions): ToastHandle;
142
+ declare function promise<T>(input: Promise<T> | (() => Promise<T>), messages: PromiseMessages<T>, options?: ToastOptions): Promise<T>;
143
+ declare function setDefaults(defaults: ToastOptions): void;
144
+ declare function setLogger(fn: ((event: string, payload: unknown) => void) | null): void;
145
+ /** Render arbitrary content (HTML string, element, or a factory) as a toast. */
146
+ declare function custom(content: NonNullable<ToastOptions['content']>, options?: ToastOptions): ToastHandle;
147
+ interface ToastFn {
148
+ (message: string, options?: ToastOptions): ToastHandle;
149
+ show: typeof show;
150
+ success: typeof success;
151
+ error: typeof error;
152
+ warning: typeof warning;
153
+ info: typeof info;
154
+ loading: typeof loading;
155
+ confirm: typeof confirm;
156
+ promise: typeof promise;
157
+ custom: typeof custom;
158
+ close: typeof close;
159
+ closeAll: typeof closeAll;
160
+ clear: typeof clear;
161
+ update: (id: string, options: Partial<ToastOptions>) => ToastHandle | null;
162
+ get: typeof get;
163
+ exists: typeof exists;
164
+ getCount: typeof getCount;
165
+ setDefaults: typeof setDefaults;
166
+ setLogger: typeof setLogger;
167
+ injectStyles: typeof injectStyles;
168
+ }
169
+ /** Callable shorthand: `toast('Saved')`, plus `toast.success(...)` etc. */
170
+ declare const toast: ToastFn;
171
+ /** Named object API, backwards-compatible with GA Toasts 1.x. */
172
+ declare const GaToasts: ToastFn;
173
+
174
+ export { type ConfirmOptions, GaToasts, type PromiseMessages, type ToastAction, type ToastAnimation, type ToastFn, type ToastHandle, type ToastOptions, type ToastPosition, type ToastProgressPosition, type ToastSize, type ToastType, type ToastVariant, toast as default, toast };