forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

toast.tsx53 lines, 2.1 KiBHistoryRawDownload
1import { For, createSignal } from "solid-js";
2import { Portal } from "solid-js/web";
3import { IconCheck, IconX } from "@/components/ui/icons";
4import { cn } from "@/lib/cn";
5import { useT } from "@/stores/preferences-context";
6
7type Toast = {
8 id: number;
9 title: string;
10};
11
12const [toasts, setToasts] = createSignal<Toast[]>([]);
13let nextId = 1;
14
15export function showToast(props: { title: string }) {
16 const id = nextId++;
17 setToasts((items) => [...items, { id, title: props.title }].slice(-4));
18 window.setTimeout(() => dismissToast(id), 3000);
19}
20
21function dismissToast(id: number) {
22 setToasts((items) => items.filter((item) => item.id !== id));
23}
24
25export function Toaster() {
26 const t = useT();
27 return (
28 <Portal>
29 {/* A polite live region: a saved/deleted confirmation is read out
30 without stealing focus from where the user is working. */}
31 <div role="status" aria-live="polite" class="pointer-events-none fixed inset-x-3 top-4 z-80 flex flex-col items-end gap-2 sm:inset-x-auto sm:right-5 sm:w-[380px]">
32 <For each={toasts()}>
33 {(toast) => (
34 <div class="pointer-events-auto flex min-h-[65px] w-full items-center gap-3 rounded-lg border border-border-line bg-surface-base px-4 py-3 text-foreground shadow-xl shadow-black/10">
35 <span class="flex h-5 w-5 shrink-0 items-center justify-center rounded-full bg-emerald-500/10 text-emerald-700 dark:text-emerald-300">
36 <IconCheck class="h-3.5 w-3.5" />
37 </span>
38 <p class="min-w-0 flex-1 text-xs font-medium leading-relaxed">{toast.title}</p>
39 <button
40 type="button"
41 class={cn("inline-flex h-6 w-6 shrink-0 items-center justify-center rounded-md text-muted-foreground transition-all hover:bg-muted hover:text-foreground active:scale-[0.96] focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring")}
42 aria-label={t("common.dismiss")}
43 onClick={() => dismissToast(toast.id)}
44 >
45 <IconX class="h-3.5 w-3.5" />
46 </button>
47 </div>
48 )}
49 </For>
50 </div>
51 </Portal>
52 );
53}