minisnackbar 1.0.2 → 3.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.
@@ -1,389 +1,434 @@
1
- (function (factory) {
2
- typeof define === 'function' && define.amd ? define(factory) :
3
- factory();
4
- })((function () { 'use strict';
1
+ var Snackbar = (function () {
2
+ 'use strict';
5
3
 
6
- /**
7
- * MiniSnackbar - A simple vanilla JavaScript snackbar/toast library
8
- *
9
- * @version 1.0.0
10
- * @author Shanto Islam <shantoislamdev@gmail.com>
11
- * @license MIT
12
- * @description A lightweight, zero-dependency snackbar library with Material Design integration
13
- * @repository https://github.com/shantoislamdev/minisnackbar
14
- * @homepage https://github.com/shantoislamdev/minisnackbar#readme
15
- */
16
-
17
- class Snackbar {
18
- static _queue = []
19
- static _isShowing = false
20
- static _currentTimeout = null
21
- static _state = 'idle' // idle, showing, transitioning
22
- static _currentActionHandler = null
23
- static _transitionDuration = 250
24
- static _initialized = false
25
-
26
- static init(options = {}) {
27
- if (this._initialized) return
28
-
29
- if (typeof document === 'undefined' || !document.body) {
30
- console.error('Snackbar: DOM is not available');
31
- return
32
- }
33
-
34
- if (options.transitionDuration && typeof options.transitionDuration === 'number') {
35
- this._transitionDuration = options.transitionDuration;
36
- }
37
-
38
- if (document.getElementById('mini-snackbar')) {
39
- this._initialized = true;
40
- return
41
- }
42
-
43
- if (!document.getElementById('mini-snackbar-styles')) {
44
- const style = document.createElement('style');
45
- style.id = 'mini-snackbar-styles';
46
- style.textContent = `
47
- .mini-snackbar {
48
- /* Positioning */
49
- position: fixed;
50
- z-index: 1000;
51
- left: 50%;
52
- bottom: 30px;
53
- transform: translateX(-50%) translateY(100%);
54
-
55
- /* Sizing */
56
- min-width: 250px;
57
- max-width: 90%;
58
-
59
- /* Layout */
60
- display: flex;
61
- align-items: center;
62
- justify-content: space-between;
63
- gap: 8px;
64
- padding: 0.875rem 1rem;
65
-
66
- /* Visibility */
67
- visibility: hidden;
68
-
69
- /* Theming */
70
- background-color: var(--mini-snackbar-bg, var(--md-sys-color-inverse-surface, rgba(255, 255, 255, 1)));
71
- color: var(--mini-snackbar-text, var(--md-sys-color-inverse-on-surface, rgba(27, 27, 27, 1)));
72
- border: var(--mini-snackbar-border, none);
73
- font-family: var(--mini-snackbar-font-family, inherit);
74
- font-size: 0.875rem;
75
- text-align: left;
76
- border-radius: var(--mini-snackbar-radius, 1rem);
77
- box-shadow: var(--mini-snackbar-shadow, 0 3px 5px -1px rgba(0,0,0,.2), 0 6px 10px 0 rgba(0,0,0,.14), 0 1px 18px 0 rgba(0,0,0,.12));
78
-
79
- /* Animation */
80
- transition: var(--mini-snackbar-transition, transform ${this._transitionDuration}ms ease-in-out);
81
- }
82
-
83
- .mini-snackbar.show {
84
- visibility: visible;
85
- transform: translateX(-50%) translateY(0);
86
- }
87
-
88
- /* Material Component Action Button */
89
- .mini-snackbar .mini-snackbar-action {
90
- flex-shrink: 0;
91
- padding: 0.5rem 1rem;
92
- margin: -0.5rem -0.5rem -0.5rem 0;
93
- }
94
-
95
- /* Fallback Action Button (when Material Components unavailable) */
96
- .mini-snackbar md-text-button[data-fallback] {
97
- display: inline-block;
98
- flex-shrink: 0;
99
- padding: 0.5rem 1rem;
100
- margin: -0.5rem -0.5rem -0.5rem 0;
101
- border: none;
102
- background: var(--mini-snackbar-btn-bg, transparent);
103
- font-size: inherit;
104
- font-family: inherit;
105
- font-weight: 500;
106
- letter-spacing: 0.0892857143em;
107
- text-transform: uppercase;
108
- color: var(--mini-snackbar-btn-text, inherit);
109
- cursor: pointer;
110
- user-select: none;
111
- border-radius: var(--mini-snackbar-btn-radius, 1rem);
112
- transition: opacity 0.2s ease;
113
- }
114
-
115
- .mini-snackbar md-text-button[data-fallback]:hover {
116
- opacity: var(--mini-snackbar-btn-hover-opacity, 0.8);
117
- outline: var(--mini-snackbar-btn-hover-outline, 2px solid var(--mini-snackbar-btn-text, inherit));
118
- outline-offset: var(--mini-snackbar-btn-outline-offset, 2px);
119
- background-color: var(--mini-snackbar-btn-hover-bg, transparent);
120
- }
121
-
122
- .mini-snackbar md-text-button[data-fallback]:focus {
123
- outline: var(--mini-snackbar-btn-focus-outline, 2px solid var(--mini-snackbar-btn-text, inherit));
124
- outline-offset: var(--mini-snackbar-btn-outline-offset, 2px);
125
- }
126
-
127
- /* Mobile responsive */
128
- @media (max-width: 600px) {
129
- .mini-snackbar {
130
- bottom: 90px;
131
- }
132
- }
133
-
134
- /* Accessibility: Reduced motion */
135
- @media (prefers-reduced-motion: reduce) {
136
- .mini-snackbar {
137
- transition: opacity 0.15s ease;
138
- }
139
- }
140
- `;
141
- document.head.appendChild(style);
142
- }
143
-
144
- const snackbar = document.createElement('div');
145
- snackbar.id = 'mini-snackbar';
146
- snackbar.className = 'mini-snackbar';
147
- snackbar.setAttribute('role', 'alert');
148
- snackbar.setAttribute('aria-live', 'assertive');
149
- snackbar.setAttribute('aria-atomic', 'true');
150
-
151
- const snackbarText = document.createElement('span');
152
- snackbarText.className = 'mini-snackbar-text';
153
- snackbar.appendChild(snackbarText);
154
-
155
- document.body.appendChild(snackbar);
156
- this._initialized = true;
157
- }
158
-
159
- static destroy() {
160
- this.hideCurrent();
161
- this.clearQueue();
162
-
163
- const snackbar = document.getElementById('mini-snackbar');
164
- if (snackbar) snackbar.remove();
165
-
166
- const styles = document.getElementById('mini-snackbar-styles');
167
- if (styles) styles.remove();
168
-
169
- this._state = 'idle';
170
- this._isShowing = false;
171
- this._currentActionHandler = null;
172
- this._currentTimeout = null;
173
- this._initialized = false;
174
- }
175
-
176
- static getTransitionDuration() {
177
- const snackbar = document.getElementById('mini-snackbar');
178
- if (!snackbar) return this._transitionDuration
179
-
180
- try {
181
- const computedStyle = window.getComputedStyle(snackbar);
182
- const duration = computedStyle.transitionDuration;
183
- if (duration && duration !== '0s') {
184
- const value = parseFloat(duration);
185
- return duration.includes('ms') ? value : value * 1000
186
- }
187
- } catch (e) {
188
- console.warn('Snackbar: Could not read transition duration from CSS', e);
189
- }
190
-
191
- return this._transitionDuration
192
- }
193
-
194
- static add(message, action = null, duration = 3000) {
195
- if (!this._initialized) {
196
- console.warn('Snackbar: Not initialized. Call Snackbar.init() first.');
197
- return
198
- }
199
-
200
- if (typeof message !== 'string' || message.trim() === '') {
201
- console.warn('Snackbar: Message must be a non-empty string');
202
- return
203
- }
204
- if (action !== null && (typeof action !== 'object' || typeof action.text !== 'string' || typeof action.handler !== 'function')) {
205
- console.warn('Snackbar: Action must be an object with "text" (string) and "handler" (function) properties');
206
- return
207
- }
208
- if (typeof duration !== 'number' || duration <= 0) {
209
- console.warn('Snackbar: Duration must be a positive number');
210
- return
211
- }
212
-
213
- this._queue.push({ message, action, duration });
214
- if (this._state === 'idle') this.showNext();
215
- }
216
-
217
- static _cleanupAction() {
218
- const snackbar = document.getElementById('mini-snackbar');
219
- if (!snackbar) return
220
-
221
- const actionButton = snackbar.querySelector('.mini-snackbar-action');
222
- if (actionButton && this._currentActionHandler) {
223
- actionButton.removeEventListener('click', this._currentActionHandler);
224
- this._currentActionHandler = null;
225
- actionButton.remove();
226
- }
227
- }
228
-
229
- static _showSnackbar(message, action, duration, onHide = null) {
230
- const snackbar = document.getElementById('mini-snackbar');
231
- if (!snackbar) {
232
- console.error('Snackbar: Snackbar element not found. Ensure init() has been called.');
233
- return
234
- }
235
-
236
- this._state = 'showing';
237
- this._isShowing = true;
238
- const snackbarText = snackbar.querySelector('.mini-snackbar-text');
239
- snackbarText.textContent = message;
240
-
241
- if (action) {
242
- const actionButton = document.createElement('md-text-button');
243
- actionButton.classList.add('mini-snackbar-action');
244
-
245
- // Fallback for when Material Components are not available
246
- if (customElements.get('md-text-button') === undefined) {
247
- actionButton.setAttribute('data-fallback', '');
248
- }
249
-
250
- actionButton.textContent = action.text;
251
-
252
- this._currentActionHandler = () => {
253
- action.handler();
254
- this._hideSnackbar(onHide);
255
- };
256
-
257
- actionButton.addEventListener('click', this._currentActionHandler);
258
- snackbar.appendChild(actionButton);
259
- }
260
-
261
- snackbar.classList.add('show');
262
-
263
- this._currentTimeout = setTimeout(() => {
264
- this._hideSnackbar(onHide);
265
- }, duration);
266
- }
267
-
268
- static _hideSnackbar(onHide = null) {
269
- if (this._currentTimeout) {
270
- clearTimeout(this._currentTimeout);
271
- this._currentTimeout = null;
272
- }
273
-
274
- this._state = 'transitioning';
275
- const snackbar = document.getElementById('mini-snackbar');
276
- if (snackbar) {
277
- snackbar.classList.remove('show');
278
- }
279
-
280
- this._cleanupAction();
281
-
282
- // Wait for CSS transition to complete
283
- const transitionDuration = this.getTransitionDuration();
284
- setTimeout(() => {
285
- this._isShowing = false;
286
- this._state = 'idle';
287
- if (onHide) onHide();
288
- }, transitionDuration);
289
- }
290
-
291
- static show(message, action = null, duration = 3000) {
292
- if (!this._initialized) {
293
- console.warn('Snackbar: Not initialized. Call Snackbar.init() first.');
294
- return
295
- }
296
-
297
- if (typeof message !== 'string' || message.trim() === '') {
298
- console.warn('Snackbar: Message must be a non-empty string');
299
- return
300
- }
301
- if (action !== null && (typeof action !== 'object' || typeof action.text !== 'string' || typeof action.handler !== 'function')) {
302
- console.warn('Snackbar: Action must be an object with "text" (string) and "handler" (function) properties');
303
- return
304
- }
305
- if (typeof duration !== 'number' || duration <= 0) {
306
- console.warn('Snackbar: Duration must be a positive number');
307
- return
308
- }
309
-
310
- // Queue message if currently transitioning
311
- if (this._state === 'transitioning') {
312
- this.add(message, action, duration);
313
- return
314
- }
315
-
316
- // Interrupt current snackbar if showing
317
- if (this._isShowing) {
318
- this._state = 'transitioning';
319
- if (this._currentTimeout) {
320
- clearTimeout(this._currentTimeout);
321
- this._currentTimeout = null;
322
- }
323
- const snackbar = document.getElementById('mini-snackbar');
324
- if (snackbar) {
325
- snackbar.classList.remove('show');
326
- }
327
- this._cleanupAction();
328
-
329
- const transitionDuration = this.getTransitionDuration();
330
- setTimeout(() => {
331
- this._isShowing = false;
332
- this._state = 'idle';
333
- this._showSnackbar(message, action, duration);
334
- }, transitionDuration);
335
- } else {
336
- this._showSnackbar(message, action, duration);
337
- }
338
- }
339
-
340
- static showNext() {
341
- if (this._queue.length === 0) {
342
- this._isShowing = false;
343
- this._state = 'idle';
344
- return
345
- }
346
-
347
- const { message, action, duration } = this._queue.shift();
348
- this._showSnackbar(message, action, duration, () => {
349
- setTimeout(() => this.showNext(), 200);
350
- });
351
- }
352
-
353
- static clearQueue() {
354
- this._queue = [];
355
- }
356
-
357
- static hideCurrent() {
358
- if (this._isShowing && this._state !== 'transitioning') {
359
- this._hideSnackbar();
360
- }
361
- }
362
-
363
- static isInitialized() {
364
- return this._initialized
365
- }
366
-
367
- // Getters/setters for testing
368
- static get queue() { return this._queue }
369
- static set queue(value) { this._queue = value; }
370
- static get isShowing() { return this._isShowing }
371
- static set isShowing(value) { this._isShowing = value; }
372
- static get currentTimeout() { return this._currentTimeout }
373
- static set currentTimeout(value) { this._currentTimeout = value; }
374
- static get state() { return this._state }
375
- static set state(value) { this._state = value; }
376
- }
377
-
378
- // Module exports
379
- if (typeof module !== 'undefined' && module.exports) {
380
- module.exports = Snackbar;
381
- }
382
-
383
- // Make available globally in browser
384
- if (typeof window !== 'undefined') {
385
- window.Snackbar = Snackbar;
4
+ const SNACKBAR_ID = 'mini-snackbar';
5
+ const STYLES_ID = 'mini-snackbar-styles';
6
+ const SNACKBAR_CLASS = 'mini-snackbar';
7
+ const SNACKBAR_VISIBLE_CLASS = 'show';
8
+ const SNACKBAR_TEXT_CLASS = 'mini-snackbar-text';
9
+ const SNACKBAR_ACTION_CLASS = 'mini-snackbar-action';
10
+ const DEFAULT_DURATION = 3000;
11
+ const DEFAULT_TRANSITION_DURATION = 250;
12
+ const QUEUE_GAP_DURATION = 200;
13
+
14
+ function createSnackbarStyles(transitionDuration) {
15
+ return `
16
+ .mini-snackbar {
17
+ position: fixed;
18
+ z-index: 1000;
19
+ left: 50%;
20
+ bottom: 30px;
21
+ transform: translateX(-50%) translateY(100%);
22
+ min-width: 250px;
23
+ max-width: 90%;
24
+ display: flex;
25
+ align-items: center;
26
+ justify-content: space-between;
27
+ gap: 8px;
28
+ padding: 0.875rem 1rem;
29
+ visibility: hidden;
30
+ background-color: var(--mini-snackbar-bg, var(--md-sys-color-inverse-surface, rgba(255, 255, 255, 1)));
31
+ color: var(--mini-snackbar-text, var(--md-sys-color-inverse-on-surface, rgba(27, 27, 27, 1)));
32
+ border: var(--mini-snackbar-border, none);
33
+ font-family: var(--mini-snackbar-font-family, inherit);
34
+ font-size: 0.875rem;
35
+ text-align: left;
36
+ border-radius: var(--mini-snackbar-radius, 1rem);
37
+ box-shadow: var(--mini-snackbar-shadow, 0 3px 5px -1px rgba(0,0,0,.2), 0 6px 10px 0 rgba(0,0,0,.14), 0 1px 18px 0 rgba(0,0,0,.12));
38
+ transition: var(--mini-snackbar-transition, transform ${transitionDuration}ms ease-in-out);
39
+ }
40
+
41
+ .mini-snackbar.show {
42
+ visibility: visible;
43
+ transform: translateX(-50%) translateY(0);
44
+ }
45
+
46
+ .mini-snackbar .mini-snackbar-action {
47
+ flex-shrink: 0;
48
+ padding: 0.5rem 1rem;
49
+ margin: -0.5rem -0.5rem -0.5rem 0;
50
+ }
51
+
52
+ .mini-snackbar button.mini-snackbar-action {
53
+ border: none;
54
+ background: var(--mini-snackbar-btn-bg, transparent);
55
+ font-size: inherit;
56
+ font-family: inherit;
57
+ font-weight: 500;
58
+ letter-spacing: 0.0892857143em;
59
+ text-transform: uppercase;
60
+ color: var(--mini-snackbar-btn-text, inherit);
61
+ cursor: pointer;
62
+ user-select: none;
63
+ border-radius: var(--mini-snackbar-btn-radius, 1rem);
64
+ transition: opacity 0.2s ease;
65
+ }
66
+
67
+ .mini-snackbar button.mini-snackbar-action:hover {
68
+ opacity: var(--mini-snackbar-btn-hover-opacity, 0.8);
69
+ outline: var(--mini-snackbar-btn-hover-outline, 2px solid var(--mini-snackbar-btn-text, inherit));
70
+ outline-offset: var(--mini-snackbar-btn-outline-offset, 2px);
71
+ background-color: var(--mini-snackbar-btn-hover-bg, transparent);
72
+ }
73
+
74
+ .mini-snackbar button.mini-snackbar-action:focus {
75
+ outline: var(--mini-snackbar-btn-focus-outline, 2px solid var(--mini-snackbar-btn-text, inherit));
76
+ outline-offset: var(--mini-snackbar-btn-outline-offset, 2px);
77
+ }
78
+
79
+ @media (max-width: 600px) {
80
+ .mini-snackbar {
81
+ bottom: 90px;
82
+ }
83
+ }
84
+
85
+ @media (prefers-reduced-motion: reduce) {
86
+ .mini-snackbar {
87
+ transition: opacity 0.15s ease;
88
+ }
89
+ }
90
+ `;
91
+ }
92
+ function installSnackbarStyles(document, transitionDuration) {
93
+ const existingStyles = document.getElementById(STYLES_ID);
94
+ if (existingStyles)
95
+ return;
96
+ const styles = document.createElement('style');
97
+ styles.id = STYLES_ID;
98
+ styles.textContent = createSnackbarStyles(transitionDuration);
99
+ document.head.appendChild(styles);
100
+ }
101
+ function removeSnackbarStyles(document) {
102
+ document.getElementById(STYLES_ID)?.remove();
103
+ }
104
+
105
+ class SnackbarRenderer {
106
+ constructor(document) {
107
+ this.actionButton = null;
108
+ this.actionHandler = null;
109
+ this.document = document;
110
+ }
111
+ ensureRoot(transitionDuration) {
112
+ if (!this.document.body)
113
+ return false;
114
+ installSnackbarStyles(this.document, transitionDuration);
115
+ if (this.getRoot())
116
+ return true;
117
+ const snackbar = this.document.createElement('div');
118
+ snackbar.id = SNACKBAR_ID;
119
+ snackbar.className = SNACKBAR_CLASS;
120
+ snackbar.setAttribute('role', 'alert');
121
+ snackbar.setAttribute('aria-live', 'assertive');
122
+ snackbar.setAttribute('aria-atomic', 'true');
123
+ const text = this.document.createElement('span');
124
+ text.className = SNACKBAR_TEXT_CLASS;
125
+ snackbar.appendChild(text);
126
+ this.document.body.appendChild(snackbar);
127
+ return true;
128
+ }
129
+ destroy() {
130
+ this.cleanupAction();
131
+ this.getRoot()?.remove();
132
+ removeSnackbarStyles(this.document);
133
+ }
134
+ setMessage(message) {
135
+ const text = this.getRoot()?.querySelector(`.${SNACKBAR_TEXT_CLASS}`);
136
+ if (text)
137
+ text.textContent = message;
138
+ }
139
+ setAction(action, onClick) {
140
+ this.cleanupAction();
141
+ if (!action)
142
+ return;
143
+ const actionButton = this.createActionButton();
144
+ actionButton.classList.add(SNACKBAR_ACTION_CLASS);
145
+ actionButton.textContent = action.text;
146
+ this.actionHandler = onClick;
147
+ actionButton.addEventListener('click', this.actionHandler);
148
+ this.actionButton = actionButton;
149
+ this.getRoot()?.appendChild(actionButton);
150
+ }
151
+ show() {
152
+ this.getRoot()?.classList.add(SNACKBAR_VISIBLE_CLASS);
153
+ }
154
+ hide() {
155
+ this.getRoot()?.classList.remove(SNACKBAR_VISIBLE_CLASS);
156
+ }
157
+ cleanupAction() {
158
+ if (this.actionButton && this.actionHandler) {
159
+ this.actionButton.removeEventListener('click', this.actionHandler);
160
+ }
161
+ this.actionButton?.remove();
162
+ this.actionButton = null;
163
+ this.actionHandler = null;
164
+ }
165
+ getTransitionDuration(fallback) {
166
+ const snackbar = this.getRoot();
167
+ const view = this.document.defaultView;
168
+ if (!snackbar || !view)
169
+ return fallback;
170
+ try {
171
+ const duration = view.getComputedStyle(snackbar).transitionDuration;
172
+ return parseTransitionDuration(duration, fallback);
173
+ }
174
+ catch (error) {
175
+ console.warn('Snackbar: Could not read transition duration from CSS', error);
176
+ return fallback;
177
+ }
178
+ }
179
+ getRoot() {
180
+ return this.document.getElementById(SNACKBAR_ID);
181
+ }
182
+ createActionButton() {
183
+ const registry = this.document.defaultView?.customElements;
184
+ if (registry?.get('md-text-button')) {
185
+ return this.document.createElement('md-text-button');
186
+ }
187
+ const button = this.document.createElement('button');
188
+ button.type = 'button';
189
+ return button;
190
+ }
386
191
  }
192
+ function parseTransitionDuration(duration, fallback) {
193
+ if (!duration || duration === '0s')
194
+ return fallback;
195
+ const firstDuration = duration.split(',')[0]?.trim();
196
+ if (!firstDuration)
197
+ return fallback;
198
+ const value = Number.parseFloat(firstDuration);
199
+ if (!Number.isFinite(value))
200
+ return fallback;
201
+ return firstDuration.endsWith('ms') ? value : value * 1000;
202
+ }
203
+
204
+ const isValidDuration = (duration) => typeof duration === 'number' && Number.isFinite(duration) && duration > 0;
205
+ function normalizeItem(message, action = null, duration = DEFAULT_DURATION, warn = console.warn) {
206
+ if (typeof message !== 'string' || message.trim() === '') {
207
+ warn('Snackbar: Message must be a non-empty string');
208
+ return null;
209
+ }
210
+ if (action !== null &&
211
+ (typeof action !== 'object' ||
212
+ typeof action.text !== 'string' ||
213
+ typeof action.handler !== 'function')) {
214
+ warn('Snackbar: Action must be an object with "text" (string) and "handler" (function) properties');
215
+ return null;
216
+ }
217
+ if (!isValidDuration(duration)) {
218
+ warn('Snackbar: Duration must be a positive number');
219
+ return null;
220
+ }
221
+ return {
222
+ message,
223
+ action: action,
224
+ duration
225
+ };
226
+ }
227
+ function normalizeTransitionDuration(duration, warn = console.warn) {
228
+ if (duration === undefined)
229
+ return null;
230
+ if (typeof duration !== 'number' || !Number.isFinite(duration) || duration < 0) {
231
+ warn('Snackbar: transitionDuration must be a non-negative number');
232
+ return null;
233
+ }
234
+ return duration;
235
+ }
236
+
237
+ class SnackbarController {
238
+ constructor() {
239
+ this.queue = [];
240
+ this.state = 'idle';
241
+ this.renderer = null;
242
+ this.displayTimer = null;
243
+ this.transitionTimer = null;
244
+ this.queueGapTimer = null;
245
+ this.transitionDuration = DEFAULT_TRANSITION_DURATION;
246
+ this.initialized = false;
247
+ }
248
+ init(options = {}) {
249
+ if (this.initialized)
250
+ return;
251
+ if (typeof document === 'undefined') {
252
+ console.error('Snackbar: DOM is not available');
253
+ return;
254
+ }
255
+ const transitionDuration = normalizeTransitionDuration(options.transitionDuration);
256
+ if (transitionDuration !== null)
257
+ this.transitionDuration = transitionDuration;
258
+ this.renderer = new SnackbarRenderer(document);
259
+ if (!this.renderer.ensureRoot(this.transitionDuration)) {
260
+ console.error('Snackbar: DOM is not available');
261
+ this.renderer = null;
262
+ return;
263
+ }
264
+ this.initialized = true;
265
+ }
266
+ destroy() {
267
+ this.clearTimers();
268
+ this.clearQueue();
269
+ this.renderer?.destroy();
270
+ this.renderer = null;
271
+ this.state = 'idle';
272
+ this.transitionDuration = DEFAULT_TRANSITION_DURATION;
273
+ this.initialized = false;
274
+ }
275
+ add(message, action = null, duration) {
276
+ if (!this.ensureInitialized())
277
+ return;
278
+ const item = normalizeItem(message, action, duration);
279
+ if (!item)
280
+ return;
281
+ this.queue.push(item);
282
+ if (this.state === 'idle')
283
+ this.showNext();
284
+ }
285
+ show(message, action = null, duration) {
286
+ if (!this.ensureInitialized())
287
+ return;
288
+ const item = normalizeItem(message, action, duration);
289
+ if (!item)
290
+ return;
291
+ this.clearQueue();
292
+ if (this.state === 'idle') {
293
+ this.showItem(item);
294
+ return;
295
+ }
296
+ if (this.state === 'transitioning') {
297
+ this.clearTimer('transitionTimer');
298
+ this.state = 'idle';
299
+ this.showItem(item);
300
+ return;
301
+ }
302
+ this.hideActiveItem(() => this.showItem(item));
303
+ }
304
+ clearQueue() {
305
+ this.queue = [];
306
+ this.clearTimer('queueGapTimer');
307
+ }
308
+ hideCurrent() {
309
+ if (this.state === 'showing') {
310
+ this.hideActiveItem(() => this.finishCurrentItem());
311
+ }
312
+ }
313
+ isInitialized() {
314
+ return this.initialized;
315
+ }
316
+ getTransitionDuration() {
317
+ return this.renderer?.getTransitionDuration(this.transitionDuration) ?? this.transitionDuration;
318
+ }
319
+ showNext() {
320
+ if (this.state !== 'idle')
321
+ return;
322
+ const item = this.queue.shift();
323
+ if (!item)
324
+ return;
325
+ this.showItem(item);
326
+ }
327
+ showItem(item) {
328
+ if (!this.renderer)
329
+ return;
330
+ this.clearTimers();
331
+ this.state = 'showing';
332
+ this.renderer.setMessage(item.message);
333
+ this.renderer.setAction(item.action, () => {
334
+ try {
335
+ item.action?.handler();
336
+ }
337
+ finally {
338
+ this.hideActiveItem(() => this.finishCurrentItem());
339
+ }
340
+ });
341
+ this.renderer.show();
342
+ this.displayTimer = setTimeout(() => {
343
+ this.hideActiveItem(() => this.finishCurrentItem());
344
+ }, item.duration);
345
+ }
346
+ hideActiveItem(afterHidden) {
347
+ if (!this.renderer || this.state === 'transitioning')
348
+ return;
349
+ this.clearTimer('displayTimer');
350
+ this.state = 'transitioning';
351
+ this.renderer.hide();
352
+ this.renderer.cleanupAction();
353
+ this.transitionTimer = setTimeout(() => {
354
+ this.transitionTimer = null;
355
+ this.state = 'idle';
356
+ afterHidden?.();
357
+ }, this.getTransitionDuration());
358
+ }
359
+ finishCurrentItem() {
360
+ if (this.state !== 'idle')
361
+ return;
362
+ if (this.queue.length === 0)
363
+ return;
364
+ this.clearTimer('queueGapTimer');
365
+ this.queueGapTimer = setTimeout(() => {
366
+ this.queueGapTimer = null;
367
+ this.showNext();
368
+ }, QUEUE_GAP_DURATION);
369
+ }
370
+ ensureInitialized() {
371
+ if (this.initialized)
372
+ return true;
373
+ console.warn('Snackbar: Not initialized. Call Snackbar.init() first.');
374
+ return false;
375
+ }
376
+ clearTimers() {
377
+ this.clearTimer('displayTimer');
378
+ this.clearTimer('transitionTimer');
379
+ this.clearTimer('queueGapTimer');
380
+ }
381
+ clearTimer(timer) {
382
+ const timeout = this[timer];
383
+ if (timeout)
384
+ clearTimeout(timeout);
385
+ this[timer] = null;
386
+ }
387
+ }
388
+
389
+ /**
390
+ * MiniSnackbar - A simple vanilla JavaScript snackbar/toast library
391
+ *
392
+ * @version 3.0.0
393
+ * @author Shanto Islam <shantoislamdev@gmail.com>
394
+ * @license MIT
395
+ * @description A lightweight, zero-dependency snackbar library with Material Design integration
396
+ * @repository https://github.com/shantoislamdev/minisnackbar
397
+ * @homepage https://github.com/shantoislamdev/minisnackbar#readme
398
+ */
399
+ const controller = new SnackbarController();
400
+ class Snackbar {
401
+ static init(options = {}) {
402
+ controller.init(options);
403
+ }
404
+ static destroy() {
405
+ controller.destroy();
406
+ }
407
+ static getTransitionDuration() {
408
+ return controller.getTransitionDuration();
409
+ }
410
+ static add(message, action = null, duration) {
411
+ controller.add(message, action, duration);
412
+ }
413
+ static show(message, action = null, duration) {
414
+ controller.show(message, action, duration);
415
+ }
416
+ static clearQueue() {
417
+ controller.clearQueue();
418
+ }
419
+ static hideCurrent() {
420
+ controller.hideCurrent();
421
+ }
422
+ static isInitialized() {
423
+ return controller.isInitialized();
424
+ }
425
+ }
426
+
427
+ if (typeof window !== 'undefined') {
428
+ window.Snackbar = Snackbar;
429
+ }
430
+
431
+ return Snackbar;
387
432
 
388
- }));
433
+ })();
389
434
  //# sourceMappingURL=minisnackbar.js.map