{"version":3,"file":"toast-9m9KhHCR.js","sources":["../../src/js/components/toast.ts"],"sourcesContent":["interface ToastColors {\n background: string;\n border: string;\n textStrong: string;\n textMuted: string;\n hoverStrong: string;\n}\n\n// Function to parse the variant and return corresponding colors\nfunction parseVariant(variant: string | null): ToastColors {\n switch (variant) {\n case 'destructive':\n return {\n background: 'bg-destructive-200',\n border: 'border-destructive-300',\n textStrong: 'text-destructive-950',\n textMuted: 'text-destructive-900',\n hoverStrong: 'hover:text-destructive-950',\n };\n default:\n return {\n background: 'bg-white',\n border: 'border-border',\n textStrong: 'text-foreground',\n textMuted: 'text-muted-foreground',\n hoverStrong: 'hover:text-foreground',\n };\n }\n}\n\n// Initialize toasts by adding event listeners to elements with [data-toast]\nexport function initToasts() {\n const elements = document.querySelectorAll('[data-toast]');\n elements.forEach((element) => {\n const timeoutString = element.getAttribute('data-toast-timeout') ?? '';\n const timeout = isNaN(parseInt(timeoutString, 10))\n ? undefined\n : parseInt(timeoutString, 10);\n const title =\n element.getAttribute('data-toast-title') || 'Default toast title';\n const message =\n element.getAttribute('data-toast-message') || 'Default toast message';\n const variant = element.getAttribute('data-toast-variant') || 'default';\n element.addEventListener('click', () =>\n createToast(title, message, timeout, variant),\n );\n });\n}\n\n// Function to create a toast\nexport function createToast(\n title: string,\n message: string,\n timeout: number = 0,\n variant: string = 'default',\n) {\n const toast = document.createElement('div');\n const colors = parseVariant(variant);\n\n toast.classList.add(\n 'toast-item',\n 'fixed',\n 'left-0',\n '-translate-x-1/2',\n 'flex',\n 'items-center',\n 'p-4',\n 'rounded-lg',\n 'border',\n 'shadow-xl',\n 'w-11/12',\n 'max-w-md',\n 'md:left-1/2',\n colors.background,\n colors.border,\n colors.textStrong,\n );\n\n toast.innerHTML = `\n \n
\n
\n

${title}

\n

${message}

\n
\n
\n `;\n\n const toastContainer = document.getElementById('toast-container');\n if (toastContainer) {\n toastContainer.prepend(toast);\n updateToastPositions();\n handleClose(toast, timeout);\n }\n}\n\nfunction updateToastPositions() {\n const toasts = document.querySelectorAll('.toast-item');\n toasts.forEach((toast, index) => {\n const finalBottom = index * (toast.offsetHeight + 16) + 16;\n toast.style.bottom = `-${toast.offsetHeight}px`;\n void toast.offsetWidth;\n toast.style.bottom = `${finalBottom}px`;\n });\n}\n\nfunction handleClose(toast: HTMLElement, timeoutDuration: number) {\n const close = toast.querySelector('[data-toast-close]');\n let timerId: number;\n\n close?.addEventListener('click', () => {\n hideToast(toast);\n clearTimeout(timerId);\n });\n\n if (timeoutDuration === 0) {\n return;\n }\n\n timerId = window.setTimeout(() => hideToast(toast), timeoutDuration);\n\n toast.addEventListener('mouseover', () => {\n clearTimeout(timerId);\n });\n\n toast.addEventListener('mouseout', () => {\n timerId = window.setTimeout(() => hideToast(toast), timeoutDuration);\n });\n}\n\nfunction hideToast(toast: HTMLElement) {\n toast.style.transform = 'translateX(100%)';\n toast.addEventListener(\n 'transitionend',\n () => {\n toast.remove();\n updateToastPositions();\n },\n { once: true },\n );\n}\n\nexport function addToastListener() {\n document.addEventListener('createToast', (event) => {\n const e = event as CustomEvent;\n\n const title = e.detail.title;\n const message = e.detail.message;\n const success = e.detail.success;\n\n createToast(title, message, 5000, success ? 'default' : 'destructive');\n });\n}\n"],"names":["parseVariant","variant","initToasts","element","_a","timeoutString","timeout","title","message","createToast","toast","colors","toastContainer","updateToastPositions","handleClose","index","finalBottom","timeoutDuration","close","timerId","hideToast","addToastListener","event","e","success"],"mappings":"AASA,SAASA,EAAaC,EAAqC,CACzD,OAAQA,EAAS,CACf,IAAK,cACI,MAAA,CACL,WAAY,qBACZ,OAAQ,yBACR,WAAY,uBACZ,UAAW,uBACX,YAAa,4BACf,EACF,QACS,MAAA,CACL,WAAY,WACZ,OAAQ,gBACR,WAAY,kBACZ,UAAW,wBACX,YAAa,uBACf,CAAA,CAEN,CAGO,SAASC,GAAa,CACV,SAAS,iBAAiB,cAAc,EAChD,QAASC,GAAY,CAxBhC,IAAAC,EAyBI,MAAMC,GAAgBD,EAAAD,EAA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