@r01al/simple-toast 1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Simple Toast
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,248 @@
1
+ # Simple Toast
2
+
3
+ A lightweight, framework-agnostic toast notification library with zero dependencies. Perfect for displaying non-blocking notifications that give users quick feedback without interrupting their flow.
4
+
5
+ ## Features
6
+
7
+ - ๐Ÿš€ **Zero dependencies** - Pure vanilla JavaScript
8
+ - ๐Ÿ’Ž **No CSS files** - Styles injected automatically via JavaScript
9
+ - ๐ŸŽจ **Multiple notification types** - success, error, info, warning
10
+ - โฑ๏ธ **Auto-dismiss** - Configurable timers
11
+ - ๐Ÿ“ **Flexible positioning** - 9 position options
12
+ - ๐Ÿ“š **Stacking behavior** - Multiple toasts stack nicely
13
+ - ๐ŸŒ“ **Theming** - Light, dark themes built-in
14
+ - โœจ **Smooth animations** - Opacity and margin transitions
15
+ - โ™ฟ **Accessibility** - ARIA labels and keyboard support
16
+ - ๐Ÿงน **Clean DOM** - Container and styles removed when no toasts visible
17
+ - ๐Ÿ“ฆ **Tiny footprint** - ~13KB unminified (includes all styles)
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ npm install simple-toast
23
+ ```
24
+
25
+ Or use via CDN:
26
+
27
+ ```html
28
+ <script src="https://unpkg.com/simple-toast/dist/simple-toast.umd.js"></script>
29
+ <!-- That's it! No CSS file needed - styles are injected automatically! -->
30
+ ```
31
+
32
+ ## Usage
33
+
34
+ ### Basic Example
35
+
36
+ **ES Modules:**
37
+ ```javascript
38
+ import { toast } from 'simple-toast';
39
+ // No CSS import needed - styles are auto-injected on first use!
40
+
41
+ // Show different toast types
42
+ toast.success('Operation completed successfully!');
43
+ toast.error('Something went wrong!');
44
+ toast.info('Here is some information.');
45
+ toast.warning('Please be careful!');
46
+ ```
47
+
48
+ **Browser (UMD):**
49
+ ```html
50
+ <!DOCTYPE html>
51
+ <html>
52
+ <head>
53
+ <title>Toast Demo</title>
54
+ </head>
55
+ <body>
56
+ <button onclick="showToast()">Show Toast</button>
57
+
58
+ <script src="simple-toast.umd.js"></script>
59
+ <script>
60
+ function showToast() {
61
+ SimpleToast.toast.success('Hello World!');
62
+ }
63
+ </script>
64
+ </body>
65
+ </html>
66
+ ```
67
+
68
+ **CommonJS:**
69
+ ```javascript
70
+ const { toast } = require('simple-toast');
71
+ toast.info('This works in Node too!');
72
+ ```
73
+
74
+ ### Advanced Configuration
75
+
76
+ ```javascript
77
+ import { toast, configure } from 'simple-toast';
78
+
79
+ // Global configuration
80
+ configure({
81
+ position: 'top-right',
82
+ duration: 3000,
83
+ theme: 'dark',
84
+ maxToasts: 5
85
+ });
86
+
87
+ // Per-toast configuration
88
+ toast.success('Custom toast', {
89
+ duration: 5000,
90
+ position: 'bottom-center',
91
+ dismissible: true
92
+ });
93
+ ```
94
+
95
+ ## API
96
+
97
+ ### `toast(message, options)`
98
+
99
+ Display a toast notification.
100
+
101
+ #### Options
102
+
103
+ | Option | Type | Default | Description |
104
+ |--------|------|---------|-------------|
105
+ | `type` | string | `'info'` | Type of toast: `'success'`, `'error'`, `'info'`, `'warning'` |
106
+ | `duration` | number | `3000` | Auto-dismiss duration in ms (0 = no auto-dismiss) |
107
+ | `position` | string | `'top-right'` | Position: `'top-left'`, `'top-center'`, `'top-right'`, `'bottom-left'`, `'bottom-center'`, `'bottom-right'`, `'middle-left'`, `'middle-center'`, `'middle-right'` |
108
+ | `dismissible` | boolean | `true` | Show close button |
109
+ | `theme` | string | `'light'` | Theme: `'light'`, `'dark'`, `'custom'` |
110
+ | `className` | string | `''` | Custom CSS class |
111
+
112
+ ### Shorthand Methods
113
+
114
+ ```javascript
115
+ toast.success(message, options)
116
+ toast.error(message, options)
117
+ toast.info(message, options)
118
+ toast.warning(message, options)
119
+ ```
120
+
121
+ ### Configuration
122
+
123
+ ```javascript
124
+ configure(options) // Set global defaults
125
+ ```
126
+
127
+ ### Manual Dismissal
128
+
129
+ ```javascript
130
+ const toastId = toast.info('This is a toast');
131
+ toast.dismiss(toastId); // Dismiss specific toast
132
+ toast.dismissAll(); // Dismiss all toasts
133
+ ```
134
+
135
+ ## Theming
136
+
137
+ ### Using Built-in Themes
138
+
139
+ ```javascript
140
+ // Light theme (default)
141
+ configure({ theme: 'light' });
142
+
143
+ // Dark theme
144
+ configure({ theme: 'dark' });
145
+ ```
146
+
147
+ ### Theme Colors
148
+
149
+ **Light Theme:**
150
+ - Success: `#10b981` (green)
151
+ - Error: `#ef4444` (red)
152
+ - Info: `#3b82f6` (blue)
153
+ - Warning: `#f59e0b` (orange)
154
+
155
+ **Dark Theme:**
156
+ - Success: `#059669` (darker green)
157
+ - Error: `#dc2626` (darker red)
158
+ - Info: `#2563eb` (darker blue)
159
+ - Warning: `#d97706` (darker orange)
160
+
161
+ ### Custom Styling
162
+
163
+ Since styles are injected via JavaScript, you can override them with your own CSS:
164
+
165
+ ```css
166
+ /* Add after library loads */
167
+ .simple-toast-success {
168
+ background-color: #your-color !important;
169
+ }
170
+
171
+ .simple-toast {
172
+ border-radius: 16px !important;
173
+ box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3) !important;
174
+ }
175
+ ```
176
+
177
+ Or modify the injected styles programmatically before first use.
178
+
179
+ ## Architecture
180
+
181
+ Simple Toast uses a modular architecture with separate concerns:
182
+
183
+ - **`index.js`** - Main API exports
184
+ - **`config.js`** - Configuration management
185
+ - **`container.js`** - DOM container lifecycle
186
+ - **`toast-manager.js`** - Toast state and lifecycle management
187
+ - **`toast-creator.js`** - DOM element creation
188
+ - **`animations.js`** - Show/hide animations
189
+ - **`icons.js`** - SVG icon definitions
190
+ - **`styles.js`** - CSS injection (auto-injected on first use)
191
+
192
+ Total: ~539 lines of JavaScript (includes all CSS as string)
193
+
194
+ ## How It Works
195
+
196
+ 1. Import the library: `import { toast } from 'simple-toast'`
197
+ 2. Call `toast.success('Message')`
198
+ 3. On first toast, styles are injected into `<head>` via `<style>` tag
199
+ 4. Container div is added to `<body>`
200
+ 5. Toast element is created and animated in
201
+ 6. After duration, toast animates out and is removed
202
+ 7. When no toasts remain, container is removed from DOM
203
+ 8. **Zero DOM footprint when inactive!**
204
+
205
+ ## Browser Support
206
+
207
+ Works in all browsers with ES6+ support:
208
+ - Chrome 49+
209
+ - Firefox 31+
210
+ - Safari 10+
211
+ - Edge 15+
212
+ - IE 9+ (with transpilation)
213
+
214
+ No CSS custom properties required - styles use simple selectors and values.
215
+
216
+ ## Build Formats
217
+
218
+ - **CommonJS** (`simple-toast.cjs.js`) - For Node.js and bundlers
219
+ - **ES Module** (`simple-toast.esm.js`) - For modern bundlers and browsers
220
+ - **UMD** (`simple-toast.umd.js`) - For direct browser usage via `<script>` tag
221
+
222
+ All formats include styles bundled - no separate CSS file needed!
223
+
224
+ ## TypeScript Support
225
+
226
+ Full TypeScript definitions included:
227
+
228
+ ```typescript
229
+ import { toast, configure, ToastOptions } from 'simple-toast';
230
+
231
+ const options: ToastOptions = {
232
+ type: 'success',
233
+ duration: 5000,
234
+ position: 'top-center',
235
+ dismissible: true,
236
+ theme: 'dark'
237
+ };
238
+
239
+ toast('Hello TypeScript!', options);
240
+ ```
241
+
242
+ ## Contributing
243
+
244
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup and guidelines.
245
+
246
+ ## License
247
+
248
+ MIT
@@ -0,0 +1,400 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const defaultConfig = {
6
+ position: 'tr',
7
+ duration: 3000,
8
+ theme: 'l',
9
+ dismissible: true,
10
+ maxToasts: 10
11
+ };
12
+
13
+ let globalConfig = { ...defaultConfig };
14
+
15
+ function configure(config) {
16
+ globalConfig = { ...globalConfig, ...config };
17
+ }
18
+
19
+ function getConfig() {
20
+ return globalConfig;
21
+ }
22
+
23
+ let stylesInjected = false;
24
+
25
+ function injectStyles() {
26
+ if (stylesInjected) return;
27
+
28
+ const style = document.createElement('style');
29
+ style.setAttribute('data-r01st', '');
30
+ style.textContent = `
31
+ .r01st-container {
32
+ position: fixed;
33
+ z-index: 9999;
34
+ pointer-events: none;
35
+ padding: 16px;
36
+ }
37
+ .r01st-container[data-position="tl"] {
38
+ top: 0;
39
+ left: 0;
40
+ }
41
+
42
+ .r01st-container[data-position="tc"] {
43
+ top: 0;
44
+ left: 50%;
45
+ margin-left: -210px;
46
+ }
47
+
48
+ .r01st-container[data-position="tr"] {
49
+ top: 0;
50
+ right: 0;
51
+ }
52
+ .r01st-container[data-position="ml"] {
53
+ top: 50%;
54
+ left: 0;
55
+ margin-top: -50px;
56
+ }
57
+
58
+ .r01st-container[data-position="mc"] {
59
+ top: 50%;
60
+ left: 50%;
61
+ margin-left: -210px;
62
+ margin-top: -50px;
63
+ }
64
+
65
+ .r01st-container[data-position="mr"] {
66
+ top: 50%;
67
+ right: 0;
68
+ margin-top: -50px;
69
+ }
70
+ .r01st-container[data-position="bl"] {
71
+ bottom: 0;
72
+ left: 0;
73
+ }
74
+
75
+ .r01st-container[data-position="bc"] {
76
+ bottom: 0;
77
+ left: 50%;
78
+ margin-left: -210px;
79
+ }
80
+
81
+ .r01st-container[data-position="br"] {
82
+ bottom: 0;
83
+ right: 0;
84
+ }
85
+ .r01st {
86
+ pointer-events: auto;
87
+ position: relative;
88
+ min-width: 280px;
89
+ max-width: 420px;
90
+ padding: 16px;
91
+ margin-bottom: 16px;
92
+ border-radius: 8px;
93
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
94
+ font-size: 14px;
95
+ line-height: 1.5;
96
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
97
+ color: #ffffff;
98
+ opacity: 0;
99
+ transition: opacity 0.3s ease, margin-top 0.3s ease;
100
+ margin-top: -20px;
101
+ }
102
+ .r01st-show {
103
+ opacity: 1;
104
+ margin-top: 0;
105
+ }
106
+ .r01st-container[data-theme="l"] .r01st-success {
107
+ background-color: #10b981;
108
+ }
109
+
110
+ .r01st-container[data-theme="l"] .r01st-error {
111
+ background-color: #ef4444;
112
+ }
113
+
114
+ .r01st-container[data-theme="l"] .r01st-info {
115
+ background-color: #3b82f6;
116
+ }
117
+
118
+ .r01st-container[data-theme="l"] .r01st-warning {
119
+ background-color: #f59e0b;
120
+ }
121
+ .r01st-container[data-theme="d"] .r01st-success {
122
+ background-color: #059669;
123
+ }
124
+
125
+ .r01st-container[data-theme="d"] .r01st-error {
126
+ background-color: #dc2626;
127
+ }
128
+
129
+ .r01st-container[data-theme="d"] .r01st-info {
130
+ background-color: #2563eb;
131
+ }
132
+
133
+ .r01st-container[data-theme="d"] .r01st-warning {
134
+ background-color: #d97706;
135
+ }
136
+ .r01st-content {
137
+ display: inline-block;
138
+ width: 100%;
139
+ vertical-align: top;
140
+ padding-right: 30px;
141
+ }
142
+ .r01st-icon {
143
+ display: inline-block;
144
+ width: 20px;
145
+ height: 20px;
146
+ vertical-align: middle;
147
+ margin-right: 12px;
148
+ }
149
+
150
+ .r01st-icon svg {
151
+ width: 100%;
152
+ height: 100%;
153
+ display: block;
154
+ }
155
+ .r01st-message {
156
+ display: inline-block;
157
+ vertical-align: middle;
158
+ word-wrap: break-word;
159
+ max-width: calc(100% - 32px);
160
+ }
161
+ .r01st-close {
162
+ position: absolute;
163
+ top: 16px;
164
+ right: 16px;
165
+ background: transparent;
166
+ border: none;
167
+ color: #ffffff;
168
+ font-size: 24px;
169
+ line-height: 1;
170
+ cursor: pointer;
171
+ padding: 0;
172
+ width: 24px;
173
+ height: 24px;
174
+ text-align: center;
175
+ border-radius: 4px;
176
+ opacity: 0.8;
177
+ }
178
+
179
+ .r01st-close:hover {
180
+ background-color: rgba(255, 255, 255, 0.2);
181
+ opacity: 1;
182
+ }
183
+
184
+ .r01st-close:focus {
185
+ outline: 2px solid #ffffff;
186
+ outline-offset: 2px;
187
+ }
188
+ .r01st-container[data-position^="b"] .r01st {
189
+ margin-top: 0;
190
+ margin-bottom: -20px;
191
+ }
192
+
193
+ .r01st-container[data-position^="b"] .r01st-show {
194
+ margin-bottom: 16px;
195
+ }
196
+ @media (max-width: 480px) {
197
+ .r01st-container {
198
+ left: 0 !important;
199
+ right: 0 !important;
200
+ margin-left: 0 !important;
201
+ padding: 8px;
202
+ }
203
+
204
+ .r01st {
205
+ min-width: auto;
206
+ max-width: none;
207
+ }
208
+ }
209
+ @media (prefers-reduced-motion: reduce) {
210
+ .r01st {
211
+ transition: opacity 0.2s;
212
+ margin-top: 0;
213
+ }
214
+ }
215
+ @media print {
216
+ .r01st-container {
217
+ display: none;
218
+ }
219
+ }
220
+ `;
221
+
222
+ document.head.appendChild(style);
223
+ stylesInjected = true;
224
+ }
225
+
226
+ let container = null;
227
+
228
+ function getContainer(position, theme) {
229
+ injectStyles();
230
+
231
+ if (container && (container.getAttribute('data-position') !== position || container.getAttribute('data-theme') !== theme)) {
232
+ container.parentNode.removeChild(container);
233
+ container = null;
234
+ }
235
+
236
+ if (!container) {
237
+ container = document.createElement('div');
238
+ container.className = 'r01st-container';
239
+ container.setAttribute('role', 'region');
240
+ container.setAttribute('aria-label', 'Notifications');
241
+ container.setAttribute('aria-live', 'polite');
242
+ container.setAttribute('data-position', position);
243
+ container.setAttribute('data-theme', theme);
244
+ document.body.appendChild(container);
245
+ }
246
+
247
+ return container;
248
+ }
249
+
250
+ function cleanupContainer(activeCount) {
251
+ if (container && activeCount === 0) {
252
+ container.parentNode.removeChild(container);
253
+ container = null;
254
+ }
255
+ }
256
+
257
+ function getIcon(type) {
258
+ const icons = {
259
+ success: '<svg viewBox="0 0 20 20" fill="currentColor"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"/></svg>',
260
+ error: '<svg viewBox="0 0 20 20" fill="currentColor"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd"/></svg>',
261
+ info: '<svg viewBox="0 0 20 20" fill="currentColor"><path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd"/></svg>',
262
+ warning: '<svg viewBox="0 0 20 20" fill="currentColor"><path fill-rule="evenodd" d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z" clip-rule="evenodd"/></svg>'
263
+ };
264
+ return icons[type] || icons.info;
265
+ }
266
+
267
+ function createToastElement(message, type, dismissible, className, id, onClose) {
268
+ const toastEl = document.createElement('div');
269
+ toastEl.className = 'r01st r01st-' + type + (className ? ' ' + className : '');
270
+ toastEl.setAttribute('role', 'alert');
271
+ toastEl.setAttribute('data-toast-id', id);
272
+
273
+ const content = document.createElement('div');
274
+ content.className = 'r01st-content';
275
+
276
+ const icon = document.createElement('span');
277
+ icon.className = 'r01st-icon';
278
+ icon.innerHTML = getIcon(type);
279
+ icon.setAttribute('aria-hidden', 'true');
280
+ content.appendChild(icon);
281
+
282
+ const messageEl = document.createElement('span');
283
+ messageEl.className = 'r01st-message';
284
+ messageEl.textContent = message;
285
+ content.appendChild(messageEl);
286
+
287
+ toastEl.appendChild(content);
288
+
289
+ if (dismissible) {
290
+ const closeBtn = document.createElement('button');
291
+ closeBtn.className = 'r01st-close';
292
+ closeBtn.innerHTML = '&times;';
293
+ closeBtn.setAttribute('aria-label', 'Close notification');
294
+ closeBtn.onclick = onClose;
295
+ toastEl.appendChild(closeBtn);
296
+ }
297
+
298
+ return toastEl;
299
+ }
300
+
301
+ function showToast(element) {
302
+ requestAnimationFrame(() => {
303
+ element.className = element.className + ' r01st-show';
304
+ });
305
+ }
306
+
307
+ function hideToast(element, callback) {
308
+ element.className = element.className.replace(' r01st-show', '');
309
+
310
+ setTimeout(() => {
311
+ if (element.parentNode) {
312
+ element.parentNode.removeChild(element);
313
+ }
314
+ if (callback) callback();
315
+ }, 300);
316
+ }
317
+
318
+ let toastCounter = 0;
319
+ const activeToasts = new Map();
320
+
321
+ function createToast(message, options = {}) {
322
+ const config = { ...getConfig(), ...options };
323
+ const { position, duration, dismissible, theme, className, type = 'info' } = config;
324
+
325
+ const id = ++toastCounter;
326
+
327
+ if (activeToasts.size >= getConfig().maxToasts) {
328
+ const firstToast = activeToasts.keys().next().value;
329
+ dismissToast(firstToast);
330
+ }
331
+
332
+ const container = getContainer(position, theme);
333
+
334
+ const handleClose = () => dismissToast(id);
335
+
336
+ const toastEl = createToastElement(message, type, dismissible, className, id, handleClose);
337
+
338
+ container.appendChild(toastEl);
339
+
340
+ showToast(toastEl);
341
+
342
+ const toastData = {
343
+ element: toastEl,
344
+ timeoutId: null
345
+ };
346
+
347
+ if (duration > 0) {
348
+ toastData.timeoutId = setTimeout(() => dismissToast(id), duration);
349
+ }
350
+
351
+ activeToasts.set(id, toastData);
352
+
353
+ return id;
354
+ }
355
+
356
+ function dismissToast(id) {
357
+ const toastData = activeToasts.get(id);
358
+ if (!toastData) return;
359
+
360
+ const { element, timeoutId } = toastData;
361
+
362
+ if (timeoutId) {
363
+ clearTimeout(timeoutId);
364
+ }
365
+
366
+ activeToasts.delete(id);
367
+
368
+ hideToast(element, () => {
369
+ cleanupContainer(activeToasts.size);
370
+ });
371
+ }
372
+
373
+ function dismissAllToasts() {
374
+ const ids = Array.from(activeToasts.keys());
375
+ ids.forEach(id => dismissToast(id));
376
+ }
377
+
378
+ /**
379
+ * Display a toast notification
380
+ * @param {string} message - The message to display
381
+ * @param {Object} options - Toast options
382
+ * @returns {number} Toast ID
383
+ */
384
+ function toast(message, options = {}) {
385
+ return createToast(message, options);
386
+ }
387
+
388
+ // Shorthand methods
389
+ toast.success = (message, options = {}) => toast(message, { ...options, type: 'success' });
390
+ toast.error = (message, options = {}) => toast(message, { ...options, type: 'error' });
391
+ toast.info = (message, options = {}) => toast(message, { ...options, type: 'info' });
392
+ toast.warning = (message, options = {}) => toast(message, { ...options, type: 'warning' });
393
+
394
+ // Export dismiss methods
395
+ toast.dismiss = dismissToast;
396
+ toast.dismissAll = dismissAllToasts;
397
+
398
+ exports.configure = configure;
399
+ exports.default = toast;
400
+ exports.toast = toast;