|
| 1 | +<script lang="ts"> |
| 2 | + import { Toast, ToastContainer, P, Button, Heading } from "flowbite-svelte"; |
| 3 | + import { fly } from "svelte/transition"; |
| 4 | + import { onDestroy } from "svelte"; |
| 5 | +
|
| 6 | + type ToastColor = "green" | "red" | "yellow" | "blue"; |
| 7 | +
|
| 8 | + interface ToastItem { |
| 9 | + id: number; |
| 10 | + message: string; |
| 11 | + color: ToastColor; |
| 12 | + timeoutId?: ReturnType<typeof setTimeout>; |
| 13 | + visible: boolean; |
| 14 | + } |
| 15 | +
|
| 16 | + let toasts = $state<ToastItem[]>([]); |
| 17 | + let nextId = $state(1); |
| 18 | +
|
| 19 | + const messages: Record<ToastColor, string> = { |
| 20 | + green: "Successfully saved!", |
| 21 | + blue: "New message received", |
| 22 | + yellow: "Please review your changes", |
| 23 | + red: "Operation failed" |
| 24 | + }; |
| 25 | +
|
| 26 | + function addToast(color?: ToastColor) { |
| 27 | + const selectedColor = color || (["green", "blue", "yellow", "red"][Math.floor(Math.random() * 4)] as ToastColor); |
| 28 | + const newToast: ToastItem = { |
| 29 | + id: nextId, |
| 30 | + message: messages[selectedColor], |
| 31 | + color: selectedColor, |
| 32 | + visible: true |
| 33 | + }; |
| 34 | +
|
| 35 | + // Auto-dismiss after 5 seconds |
| 36 | + const timeoutId = setTimeout(() => { |
| 37 | + dismissToast(newToast.id); |
| 38 | + }, 5000); |
| 39 | + newToast.timeoutId = timeoutId; |
| 40 | +
|
| 41 | + toasts = [...toasts, newToast]; |
| 42 | + nextId++; |
| 43 | + } |
| 44 | +
|
| 45 | + function dismissToast(id: number) { |
| 46 | + // Clear timeout if it exists |
| 47 | + const toast = toasts.find((t) => t.id === id); |
| 48 | + if (toast?.timeoutId) { |
| 49 | + clearTimeout(toast.timeoutId); |
| 50 | + } |
| 51 | + |
| 52 | + // Set visible to false to trigger outro transition |
| 53 | + toasts = toasts.map((t) => (t.id === id ? { ...t, visible: false } : t)); |
| 54 | +
|
| 55 | + setTimeout(() => { |
| 56 | + toasts = toasts.filter((t) => t.id !== id); |
| 57 | + }, 300); // Slightly longer than transition duration |
| 58 | + } |
| 59 | +
|
| 60 | + function handleClose(id: number) { |
| 61 | + return () => { |
| 62 | + dismissToast(id); |
| 63 | + }; |
| 64 | + } |
| 65 | +
|
| 66 | + onDestroy(() => { |
| 67 | + // Clear all pending timeouts on unmount |
| 68 | + toasts.forEach((toast) => { |
| 69 | + if (toast.timeoutId) { |
| 70 | + clearTimeout(toast.timeoutId); |
| 71 | + } |
| 72 | + }); |
| 73 | + }); |
| 74 | +</script> |
| 75 | + |
| 76 | +<ToastContainer position="top-right"> |
| 77 | + {#each toasts as toast (toast.id)} |
| 78 | + <Toast |
| 79 | + color={toast.color} |
| 80 | + dismissable={true} |
| 81 | + transition={fly} |
| 82 | + params={{ x: 200, duration: 800 }} |
| 83 | + class="w-64" |
| 84 | + onclose={handleClose(toast.id)} |
| 85 | + bind:toastStatus={toast.visible} |
| 86 | + > |
| 87 | + {toast.message} |
| 88 | + </Toast> |
| 89 | + {/each} |
| 90 | +</ToastContainer> |
| 91 | + |
| 92 | +<div class="relative min-h-screen p-8"> |
| 93 | + <div class="mx-auto max-w-2xl"> |
| 94 | + <div class="rounded-xl p-8 shadow-lg"> |
| 95 | + <div class="mb-6 flex items-center gap-3"> |
| 96 | + <svg class="h-8 w-8 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 97 | + <path |
| 98 | + stroke-linecap="round" |
| 99 | + stroke-linejoin="round" |
| 100 | + stroke-width="2" |
| 101 | + d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9" |
| 102 | + /> |
| 103 | + </svg> |
| 104 | + <Heading tag="h1" class="text-3xl">Toast Notifications Demo</Heading> |
| 105 | + </div> |
| 106 | + <P class="mb-8">Click the buttons below to trigger toast notifications. Each toast will appear in the top-right corner and automatically dismiss after 5 seconds.</P> |
| 107 | + <div class="space-y-4"> |
| 108 | + <div class="grid grid-cols-2 gap-3"> |
| 109 | + <Button onclick={() => addToast("green")} color="green">Success Toast</Button> |
| 110 | + <Button onclick={() => addToast("blue")} color="blue">Info Toast</Button> |
| 111 | + <Button onclick={() => addToast("yellow")} color="yellow">Warning Toast</Button> |
| 112 | + <Button onclick={() => addToast("red")} color="red">Error Toast</Button> |
| 113 | + </div> |
| 114 | + <Button onclick={() => addToast()} color="dark" class="w-full">Random Toast</Button> |
| 115 | + </div> |
| 116 | + </div> |
| 117 | + </div> |
| 118 | +</div> |
0 commit comments