15 | 🔬 Simple Example
16 | Here's a simple example of how to use our package:
17 |
18 |
19 |
Basic Usage
20 |
21 | toast('Hello, World!')}>Show Toast
22 |
23 |
24 | {`import toast, { toastConfig } from 'react-simple-toasts';
25 | import 'react-simple-toasts/dist/style.css';
26 | import 'react-simple-toasts/dist/theme/dark.css';
27 |
28 | toastConfig({
29 | theme: 'dark',
30 | });
31 |
32 | export default function App() {
33 | return (
34 | toast('Hello, World!')}>
35 | Show Toast
36 |
37 | );
38 | }`}
39 |
40 |
41 |
42 |
43 |
Duration
44 |
45 |
46 | default: 3000
47 |
48 |
49 | The duration option, in milliseconds, allows you to control how long the
50 | toast message is displayed. There are two different ways to set it as shown in the
51 | example.
52 |
53 |
54 | toast('Hello, World!', 1000)}>Show Toast
55 |
56 |
57 | {`import toast from 'react-simple-toasts';
58 | import 'react-simple-toasts/dist/style.css';
59 | toast('Hello, World!', 1000);
60 |
61 | // OR
62 |
63 | toast('Hello, World!', { duration: 1000 })`}
64 |
65 |
66 |
67 |
68 |
Theme
69 |
70 |
71 | default: undefined
72 |
73 |
74 | The theme option in the toast configuration allows you to apply different
75 | styles to your toasts. If no theme is specified, no default styles will be applied. You
76 | need to import the CSS file for the desired theme as shown in the code example below.
77 | For the available themes, please refer to the{' '}
78 | Built in Themes page.
79 |
80 |
81 | toast('Hello, World!', { theme: null })}>No theme
82 |
83 | toast('Hello, World!', { theme: 'dark' })}>Dark Toast
84 | toast('Hello, World!', { theme: 'light' })}>Light Toast
85 |
86 |
87 | {`import toast from 'react-simple-toasts';
88 | import 'react-simple-toasts/dist/style.css';
89 | import 'react-simple-toasts/dist/theme/dark.css';
90 | import 'react-simple-toasts/dist/theme/light.css';
91 |
92 | export default function App() {
93 | return (
94 | <>
95 | toast('Hello, World!')}>
96 | No theme
97 |
98 |
99 | toast('Hello, World!', { theme: 'dark' })}>
100 | Dark Toast
101 |
102 |
103 | toast('Hello, World!', { theme: 'light' })}>
104 | Light Toast
105 |
106 | >
107 | );
108 | }`}
109 |
110 |
111 |
112 |
113 |
className
114 |
115 |
116 | default: undefined
117 |
118 |
119 | The className option allows you to customize the style of the toast
120 | message. You can provide your own CSS class name and define the styles in your CSS file
121 | as shown in the example.
122 |
123 |
124 |
126 | toast('Hello, World!', {
127 | className: 'my-toast',
128 | theme: null,
129 | })
130 | }
131 | >
132 | Show Toast
133 |
134 |
135 |
136 | {`/* my-style.css */
137 |
138 | .my-toast {
139 | background-color: rgba(255, 107, 129, 0.9);
140 | padding: 10px 20px;
141 | color: #fff;
142 | border-radius: 3px;
143 | }`}
144 |
145 |
146 | {`import toast from 'react-simple-toasts';
147 | import 'react-simple-toasts/dist/style.css';
148 | import 'my-style.css';
149 |
150 | export default function App() {
151 | return (
152 | toast('Hello, World!', { className: 'my-toast' })}>
153 | Show Toast
154 |
155 | );
156 | }`}
157 |
158 |
159 |
160 |
161 |
clickable
162 |
163 |
164 | default: false
165 |
166 |
167 |
168 | The clickable option allows you to make the toast message interactive,
169 | meaning it can be clicked. Once clickable is set to true, you can provide
170 | an onClick handler to execute an action when the toast is clicked, as
171 | demonstrated in the example.
172 |
173 |
174 |
176 | toast('Hello, World!', {
177 | clickable: true,
178 | onClick: () => alert('Clicked!'),
179 | })
180 | }
181 | >
182 | Show Toast
183 |
184 |
185 |
186 | {`toast('Hello, World!', {
187 | clickable: true,
188 | onClick: () => alert('Clicked!'),
189 | });`}
190 |
191 |
192 |
193 |
194 |
clickClosable
195 |
196 |
197 | default: false
198 |
199 |
200 |
201 | The clickClosable prop allows users to dismiss the toast by clicking on it.
202 | When set to true, a click anywhere on the toast message will close the
203 | toast. This provides an additional, user-friendly way to dismiss toasts, beyond waiting
204 | for them to automatically disappear.
205 |
206 |
207 |
208 |
210 | toast('Hello, World!', {
211 | clickClosable: true,
212 | })
213 | }
214 | >
215 | Show Toast
216 |
217 |
218 |
219 | {`toast('Hello, World!', { clickClosable: true })`}
220 |
221 |
222 |
223 |
224 |
position
225 |
226 |
227 | default: bottom-center
228 |
229 |
230 |
231 | The position prop determines the location on the screen where the toast
232 | will appear. Available positions include 'top-left',
233 | 'top-center', 'top-right', 'bottom-left',{' '}
234 | 'bottom-center','bottom-right', and 'center'.
235 | This gives you the flexibility to ensure that the toast doesn't interfere with other
236 | important UI elements.
237 |
238 |
239 |
240 | setPosition(e.target.value as ToastPosition)}
244 | >
245 | Top Left
246 | Top Center
247 | Top Right
248 | Bottom Left
249 | Bottom Center
250 | Bottom Right
251 | Center
252 |
253 |
255 | toast('Hello, World!', {
256 | position,
257 | })
258 | }
259 | >
260 | Show Toast
261 |
262 |
263 |
264 | {`toast('Hello, World!', { position: 'top-left' });
265 | toast('Hello, World!', { position: 'top-center' });
266 | toast('Hello, World!', { position: 'top-right' });
267 | toast('Hello, World!', { position: 'bottom-left' });
268 | toast('Hello, World!', { position: 'bottom-center' });
269 | toast('Hello, World!', { position: 'bottom-right' });
270 | toast('Hello, World!', { position: 'center' });`}
271 |
272 |
273 |
274 |
275 |
maxVisibleToasts
276 |
277 |
278 | default: undefined
279 |
280 |
281 |
282 | The maxVisibleToasts prop sets a limit to the number of toasts that can be
283 | displayed on the screen at the same time. If more toasts are triggered while the limit
284 | is reached, they will be queued and displayed as older toasts disappear. This helps
285 | prevent a scenario where a large number of toasts are displayed simultaneously,
286 | potentially disrupting the user experience.
287 |
288 |
289 |
290 |
292 | toast('Hello, World!', {
293 | maxVisibleToasts: 3,
294 | })
295 | }
296 | >
297 | Show Toast
298 |
299 |
300 |
301 | {`toast('Hello, World!', { maxVisibleToasts: 3 })`}
302 |
303 |
304 |
305 |
306 |
loading
307 |
308 | default: false
309 |
310 |
311 | The loading option provides the functionality to display a loading indicator within a
312 | toast message. This option is useful for visually indicating to the user that a lengthy
313 | operation is in progress. The color of the loading indicator is determined by the color
314 | of the message in the toast. The loading option can be set as true or as a Promise
315 | object, and there are two ways to use it
316 |
317 |
318 | toast('Synchronizing...', { loading: true })}>Show Toast
319 |
320 |
321 | {`toast('Synchronizing...', { loading: true });`}
322 |
323 |
324 | Or, to simulate a lengthy operation, you can use a Promise that resolves after a delay.
325 | The following example demonstrates a dummy Promise that resolves after 3 seconds, but in a
326 | real-world scenario, this could represent an actual asynchronous task:
327 |
328 | {`const taskPromise = new Promise((resolve) => setTimeout(resolve, 3000));
329 |
330 | toast('Synchronizing...', { loading: taskPromise });`}
331 |
332 |
333 |
336 | 🚀 Advanced Example
337 | This is a more advanced example with additional options:
338 |
339 |
340 |
341 |
Promise Based Loading
342 |
343 | This example demonstrates how to use a Promise to control a loading indicator in a toast
344 | message. A loading indicator is shown while the Promise is pending. Once resolved, the
345 | toast updates to a success message with a different theme and duration.
346 |
347 |
348 | {
350 | const taskPromise = new Promise((resolve) => setTimeout(resolve, 2000));
351 |
352 | const taskToast = toast('Task started...', {
353 | theme: 'info',
354 | loading: taskPromise,
355 | duration: Infinity,
356 | });
357 |
358 | taskPromise.then(() =>
359 | taskToast.update({
360 | message: 'Task completed!',
361 | duration: 2000,
362 | theme: 'success',
363 | }),
364 | );
365 | }}
366 | >
367 | Show Toast
368 |
369 |
370 |
371 | {`import toast from 'react-simple-toasts';
372 | import 'react-simple-toasts/dist/style.css';
373 | import 'react-simple-toasts/dist/theme/info.css';
374 | import 'react-simple-toasts/dist/theme/success.css';
375 |
376 | // ...
377 |
378 | const taskPromise = new Promise((resolve) => setTimeout(resolve, 2000));
379 |
380 | const taskToast = toast('Task started...', {
381 | theme: 'info',
382 | loading: taskPromise,
383 | duration: Infinity,
384 | });
385 |
386 | taskPromise.then(() =>
387 | taskToast.update({
388 | message: 'Task completed!',
389 | duration: 2000,
390 | theme: 'success',
391 | }),
392 | );`}
393 |
394 |
395 |
396 |
397 |
Infinity Duration
398 |
399 |
400 | If you want to create a toast notification that stays on the screen indefinitely until
401 | manually closed, you can pass 0, null, or{' '}
402 | Infinity as the duration. This will create an "infinite toast". The example
403 | below shows how to create an infinite toast and provide a button for manually closing
404 | it. This can be useful in scenarios where you want to make sure a critical message is
405 | not missed by the user.
406 |
407 |
408 |
409 |
410 | With clickClosable
411 |
412 |
413 | toast('Hello, World!', { duration: Infinity, clickClosable: true })}
415 | >
416 | Show Toast
417 |
418 |
419 |
420 | {`import toast, { Toast } from 'react-simple-toasts';
421 | import 'react-simple-toasts/dist/style.css';
422 |
423 | export default function App() {
424 | const handleShowClick = () => {
425 | toast('Hello, World!', {
426 | duration: null,
427 | clickClosable: true,
428 | });
429 | };
430 |
431 | return (
432 |
433 | Show Toast
434 |
435 | );
436 | }`}
437 |
438 |
439 |
440 |
Manual Control
441 |
442 |
443 | setInfiniteToast(toast('Hello, World!', Infinity))}
446 | >
447 | Show Toast
448 |
449 | {
452 | infiniteToast?.close();
453 | setInfiniteToast(null);
454 | }}
455 | >
456 | Close Toast
457 |
458 |
459 |
460 | {`import toast, { Toast } from 'react-simple-toasts';
461 | import 'react-simple-toasts/dist/style.css';
462 |
463 | export default function App() {
464 | const [infiniteToast, setInfiniteToast] = useState(null);
465 |
466 | const handleShowClick = () => {
467 | const infiniteToast = toast('Hello, World!', { duration: null });
468 | setInfiniteToast(infiniteToast);
469 | };
470 |
471 | const handleCloseClick = () => {
472 | infiniteToast?.close();
473 | setInfiniteToast(null);
474 | };
475 |
476 | return (
477 | <>
478 |
482 | Show Toast
483 |
484 |
488 | Close Toast
489 |
490 | >
491 | );
492 | }`}
493 |
494 |
495 |
496 |
497 |
Updating Toasts in Real-time
498 |
499 |
500 | The example below demonstrates how to dynamically update the content of a toast message
501 | in real-time. Here, a countdown timer is implemented to show the remaining lifetime of
502 | the toast. It's updated every 100 milliseconds until the toast is automatically closed
503 | after 5 seconds. The update function of the toast instance is used to
504 | accomplish this.
505 |
506 |
507 |
508 | {
510 | const toastCreatedAt = Date.now();
511 | const updatableToast = toast('Toast will close in 5s', {
512 | duration: 5000,
513 | onClose: () => clearInterval(interval),
514 | });
515 | const interval = setInterval(() => {
516 | const remainingTime = Math.max(0, 5000 - (Date.now() - toastCreatedAt));
517 | updatableToast?.update(
518 | `Toast will close in ${(remainingTime / 1000).toFixed(1)}s`,
519 | );
520 | }, 100);
521 | }}
522 | >
523 | Show Toast
524 |
525 |
526 |
527 | {`import toast from 'react-simple-toasts';
528 | import 'react-simple-toasts/dist/style.css';
529 |
530 | export default function App() {
531 | const handleClick = () => {
532 | const DURATION = 5000;
533 | const toastCreatedAt = Date.now();
534 |
535 | const myToast = toast('Toast will close in 5s', {
536 | duration: DURATION,
537 | onClose: () => clearInterval(interval),
538 | });
539 |
540 | const interval = setInterval(() => {
541 | const remainingTime = Math.max(0, DURATION - (Date.now() - toastCreatedAt));
542 | myToast.update(\`Toast will close in \${(remainingTime / 1000).toFixed(1)}s\`);
543 | }, 100);
544 | };
545 |
546 | return (
547 |
548 | Show Toast
549 |
550 | );
551 | }`}
552 |
553 |
554 |