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