flotilla/src/app/toast.ts

33 lines
693 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
2024-08-09 23:22:09 +00:00
export type ToastParams = {
2024-08-06 20:10:10 +00:00
message: string
2024-08-09 23:22:09 +00:00
timeout?: number
theme?: "error"
2024-08-06 20:10:10 +00:00
}
2024-08-09 23:22:09 +00:00
export type Toast = ToastParams & {
id: string
2024-08-06 20:10:10 +00:00
}
export const toast = writable<Toast | null>(null)
2024-08-09 23:22:09 +00:00
export const pushToast = (params: ToastParams) => {
const id = randomId()
toast.set({id, ...params})
2024-08-06 20:10:10 +00:00
2024-08-09 23:22:09 +00:00
setTimeout(() => popToast(id), params.timeout || 5000)
2024-08-06 20:10:10 +00:00
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!"})
}