flotilla/src/app/toast.ts

34 lines
732 B
TypeScript
Raw Normal View History

2024-08-06 20:10:10 +00:00
import {writable} from "svelte/store"
import {randomId} from "@welshman/lib"
2024-08-08 23:32:46 +00:00
import {copyToClipboard} from '@lib/html'
2024-08-06 20:10:10 +00:00
export type Toast = {
id: string
message: string
options: ToastOptions
}
export type ToastOptions = {
timeout?: number
}
export const toast = writable<Toast | null>(null)
export const pushToast = (
{message = "", id = randomId()}: Partial<Toast>,
2024-08-08 23:32:46 +00:00
options: ToastOptions = {},
2024-08-06 20:10:10 +00:00
) => {
toast.set({id, message, options})
setTimeout(() => popToast(id), options.timeout || 5000)
return id
}
export const popToast = (id: string) => toast.update($t => ($t?.id === id ? null : $t))
2024-08-08 23:32:46 +00:00
export const clip = (value: string) => {
copyToClipboard(value)
pushToast({message: "Copied to clipboard!"})
}