From f3c75fc921c910c63f7effa65ba2ef412b7abe1e Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Sat, 21 Jun 2025 22:33:47 +0200 Subject: [PATCH] Lib: Update toast.ts to allow action toasts --- src/lib/toast.ts | 75 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 20 deletions(-) diff --git a/src/lib/toast.ts b/src/lib/toast.ts index ae8ee67..2764f5b 100644 --- a/src/lib/toast.ts +++ b/src/lib/toast.ts @@ -1,28 +1,63 @@ import type { ToastSettings } from "@skeletonlabs/skeleton"; -export const get_info_toast = (message: string, timeout: number = 2000): ToastSettings => { - return { - message, - hideDismiss: true, - timeout, - background: "variant-filled-tertiary", - }; -}; +export const get_info_toast = ( + message: string, + timeout: number | null = 2000, + action_label: string | undefined = undefined, + action_response: (() => void) | undefined = undefined, +): ToastSettings => + get_toast(message, "variant-filled-tertiary", timeout, action_label, action_response); -export const get_warning_toast = (message: string, timeout: number = 2000): ToastSettings => { - return { - message, - hideDismiss: true, - timeout, - background: "variant-filled-secondary", - }; -}; +export const get_warning_toast = ( + message: string, + timeout: number | null = 2000, + action_label: string | undefined = undefined, + action_response: (() => void) | undefined = undefined, +): ToastSettings => + get_toast(message, "variant-filled-secondary", timeout, action_label, action_response); -export const get_error_toast = (message: string, timeout: number = 2000): ToastSettings => { +export const get_error_toast = ( + message: string, + timeout: number | null = 2000, + action_label: string | undefined = undefined, + action_response: (() => void) | undefined = undefined, +): ToastSettings => + get_toast(message, "variant-filled-primary", timeout, action_label, action_response); + +/** + * Utility function to create [ToastSettings]. + * If [timeout] is defined, the toast will disappear automatically and the dismiss-button will be hidden. + * If [timeout] is undefined, the toast will stay until dismissed from the dismiss-button. + * If [action_label] and [action_response] are defined, a callback function will be executed after accepting. + * In this case, [timeout] behaves as if undefined. + */ +const get_toast = ( + message: string, + background: string, + timeout: number | null = 2000, + action_label: string | undefined = undefined, + action_response: (() => void) | undefined = undefined, +): ToastSettings => { return { message, - hideDismiss: true, - timeout, - background: "variant-filled-primary", + background, + + // If we have a timeout and no action, dismiss is hidden + hideDismiss: !!timeout && (!action_label || !action_response), + + // If we have a timeout and no action, the timeout is used + timeout: !!timeout && (!action_label || !action_response) ? timeout : undefined, + + // If we have a timeout and no action, autohide is true + autohide: !!timeout && (!action_label || !action_response), + + // If we have a label and a response, use the action + action: + !!action_label && !!action_response + ? { + label: action_label, + response: action_response, + } + : undefined, }; };