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