# DeToast — Production Optimized 🔧 File kamu sudah bagus, tinggal beberapa fix kritis biar production-ready. Berikut versi final lengkap yang sudah dioptimalkan. **File:** `src/lib/detoast/DeToast.svelte` ```svelte {#each Object.entries($toastsByPosition) as [pos, items] (pos)}
{#each items as t (t.id)} {@const p = presets[t.type]}
$toastConfig.pauseOnHover && pauseTimer(t.id)} on:mouseleave={() => $toastConfig.pauseOnHover && resumeTimer(t.id)} on:touchstart={(e) => swipeStart(e, t.id, $toastConfig.swipeToDismiss)} on:touchmove={(e) => swipeMove(e, t.id)} on:touchend={() => swipeEnd(t.id)} role="alert" >
{#if t.duration === 0 && t.type === 'info' && !t.confirm} {:else} {@html p.icon} {/if}
{#if t.title}
{@html t.title}
{/if}
{@html t.message}
{#if t.action && !t.confirm} {/if} {#if t.confirm}
{/if}
{#if t.dismissible} {/if} {#if t.progressBar && t.duration > 0}
{/if}
{/each}
{/each} ``` --- ## 📊 Perubahan Kunci dari File Kamu | # | Masalah Sebelumnya | Perbaikan | |---|---|---| | 1 | `animate:flip` di cucu `{#each}` → error compile | Pindah ke `.toast-slot` (anak langsung) | | 2 | `new AudioContext()` tiap sound → error setelah 6x | Cache global `_audioCtx` | | 3 | Tidak ada cleanup saat unmount → memory leak | Tambah `onDestroy(() => cleanup())` | | 4 | `width` per-toast tidak berfungsi | Bungkus dengan `.toast-slot` wrapper | | 5 | Confirm dialog bisa numpuk | Tambah `CONFIRM_GROUP` dedupe | | 6 | Tidak ada `prefers-reduced-motion` | Tambah media query + flag runtime | | 7 | Progress bar reset saat pause → update | Pakai `animation-delay` negatif | | 8 | `setPosition` tidak validasi | Tambah whitelist `VALID_POSITIONS` | | 9 | `get(config)` dipanggil berulang | Ambil sekali per fungsi | | 10 | Duplikat `@keyframes spin` di CSS | Hapus duplikat | | 11 | `msg` bisa overflow kalau panjang | Tambah `max-height: 160px` + scroll | | 12 | `focus-visible` tidak ada di tombol | Tambah outline | --- ## 📦 Cara Pakai ### 1. Mount di layout ```svelte ``` ### 2. Import dari mana saja ```svelte ``` ### 3. Contoh lengkap ```js // Basic detoast.success('Data tersimpan!'); detoast.info('Sinkronisasi...'); detoast.warning('Koneksi lemah'); detoast.error('Gagal!', { position: 'top-center', width: '240px' }); // Promise await detoast.promise( fetch('/api/save', { method: 'POST' }).then(r => r.json()), { loading: 'Menyimpan...', success: (res) => `Tersimpan! ID: ${res.id}`, error: (err) => `Gagal: ${err.message}` } ); // Confirm const ok = await detoast.confirm('Hapus item ini?', { title: 'Konfirmasi', yesLabel: 'Ya, Hapus', noLabel: 'Batal', type: 'warning' }); if (ok) await hapusItem(); // Update progress const id = detoast.loading('Upload...'); for (let i = 0; i <= 100; i += 10) { await new Promise(r => setTimeout(r, 200)); detoast.update(id, { message: `Upload ${i}%` }); } detoast.update(id, { type: 'success', message: 'Selesai!', duration: 3000, dismissible: true, progressBar: true }); ``` --- ## ✅ Checklist Production | Fitur | Status | |---|---| | Compile tanpa error | ✅ | | SSR safe | ✅ | | No memory leak | ✅ | | AudioContext cached | ✅ | | Reduced motion | ✅ | | Dark mode auto | ✅ | | Confirm dedupe | ✅ | | Progress resume | ✅ | | Width per-toast | ✅ | | Validasi posisi | ✅ | | Accessibility (`role`, `aria-live`, `focus-visible`) | ✅ | | Zero dependency | ✅ | | Single file | ✅ | Selesai! File ini siap production. 🚀