🍞 global toast system replaces inline flash banners
Browse files at this commit6cf99b1f583eafe92bd9229604692fca110cfd3eParent: 3f5523c- feat
- 🍞 global toast system replaces inline flash banners
3 changed files +57 -13
- src/components/layout/app-shell.tsx +2 -0
- src/components/ui/toast.tsx +49 -0
- src/lib/flash.ts +6 -13
src/components/layout/app-shell.tsxmodified+2 -0
| @@ -7,6 +7,7 @@ import { SideNav } from "@/components/layout/side-nav"; | |||
| 7 | import { SidebarAccount } from "@/components/layout/sidebar-account"; | 7 | import { SidebarAccount } from "@/components/layout/sidebar-account"; |
| 8 | import { Button } from "@/components/ui/button"; | 8 | import { Button } from "@/components/ui/button"; |
| 9 | import { IconPanelLeft, IconX } from "@/components/ui/icons"; | 9 | import { IconPanelLeft, IconX } from "@/components/ui/icons"; |
| 10 | +import { Toaster } from "@/components/ui/toast"; | ||
| 10 | import { useAuth } from "@/stores/auth-context"; | 11 | import { useAuth } from "@/stores/auth-context"; |
| 11 | import { usePreferences, useT } from "@/stores/preferences-context"; | 12 | import { usePreferences, useT } from "@/stores/preferences-context"; |
| 12 | import { cn } from "@/lib/cn"; | 13 | import { cn } from "@/lib/cn"; |
| @@ -128,6 +129,7 @@ export function AppShell(props: ParentProps) { | |||
| 128 | <Show when={auth.user() && !wide()}> | 129 | <Show when={auth.user() && !wide()}> |
| 129 | <MobileTabBar onMenu={() => setMobileOpen(true)} /> | 130 | <MobileTabBar onMenu={() => setMobileOpen(true)} /> |
| 130 | </Show> | 131 | </Show> |
| 132 | + <Toaster /> | ||
| 131 | </div> | 133 | </div> |
| 132 | ); | 134 | ); |
| 133 | } | 135 | } |
src/components/ui/toast.tsxadded+49 -0
| @@ -0,0 +1,49 @@ | |||
| 1 | +import { For, createSignal } from "solid-js"; | ||
| 2 | +import { Portal } from "solid-js/web"; | ||
| 3 | +import { IconCheck, IconX } from "@/components/ui/icons"; | ||
| 4 | +import { cn } from "@/lib/cn"; | ||
| 5 | + | ||
| 6 | +type Toast = { | ||
| 7 | + id: number; | ||
| 8 | + title: string; | ||
| 9 | +}; | ||
| 10 | + | ||
| 11 | +const [toasts, setToasts] = createSignal<Toast[]>([]); | ||
| 12 | +let nextId = 1; | ||
| 13 | + | ||
| 14 | +export function showToast(props: { title: string }) { | ||
| 15 | + const id = nextId++; | ||
| 16 | + setToasts((items) => [...items, { id, title: props.title }].slice(-4)); | ||
| 17 | + window.setTimeout(() => dismissToast(id), 3000); | ||
| 18 | +} | ||
| 19 | + | ||
| 20 | +function dismissToast(id: number) { | ||
| 21 | + setToasts((items) => items.filter((item) => item.id !== id)); | ||
| 22 | +} | ||
| 23 | + | ||
| 24 | +export function Toaster() { | ||
| 25 | + return ( | ||
| 26 | + <Portal> | ||
| 27 | + <div class="pointer-events-none fixed inset-x-3 bottom-4 z-[80] flex flex-col items-end gap-2 sm:inset-x-auto sm:right-5 sm:w-96"> | ||
| 28 | + <For each={toasts()}> | ||
| 29 | + {(toast) => ( | ||
| 30 | + <div class="pointer-events-auto flex w-full items-start gap-3 rounded-xl border border-border bg-card px-3 py-3 text-card-foreground shadow-soft"> | ||
| 31 | + <span class="mt-0.5 flex h-5 w-5 shrink-0 items-center justify-center rounded-full bg-emerald-500/10 text-emerald-700 dark:text-emerald-300"> | ||
| 32 | + <IconCheck class="h-3.5 w-3.5" /> | ||
| 33 | + </span> | ||
| 34 | + <p class="min-w-0 flex-1 text-sm font-medium leading-5">{toast.title}</p> | ||
| 35 | + <button | ||
| 36 | + type="button" | ||
| 37 | + class={cn("rounded-md p-1 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground")} | ||
| 38 | + aria-label="Dismiss" | ||
| 39 | + onClick={() => dismissToast(toast.id)} | ||
| 40 | + > | ||
| 41 | + <IconX class="h-3.5 w-3.5" /> | ||
| 42 | + </button> | ||
| 43 | + </div> | ||
| 44 | + )} | ||
| 45 | + </For> | ||
| 46 | + </div> | ||
| 47 | + </Portal> | ||
| 48 | + ); | ||
| 49 | +} | ||
src/lib/flash.tsmodified+6 -13
| @@ -1,18 +1,11 @@ | |||
| 1 | -import { createEffect, createSignal, onCleanup, type Accessor } from "solid-js"; | 1 | +import { createSignal, type Accessor } from "solid-js"; |
| 2 | +import { showToast } from "@/components/ui/toast"; | ||
| 2 | 3 | ||
| 3 | -/** Short-lived success banner; auto-clears after `ms` (default 3s). */ | 4 | +/** Short-lived success feedback; page banners stay empty, global toast renders it. */ |
| 4 | -export function createFlash(ms = 3000): [Accessor<string>, (text: string) => void] { | 5 | +export function createFlash(_ms = 3000): [Accessor<string>, (text: string) => void] { |
| 5 | const [message, setMessage] = createSignal(""); | 6 | const [message, setMessage] = createSignal(""); |
| 6 | - const [tick, setTick] = createSignal(0); | ||
| 7 | - createEffect(() => { | ||
| 8 | - tick(); | ||
| 9 | - const text = message(); | ||
| 10 | - if (!text) return; | ||
| 11 | - const id = window.setTimeout(() => setMessage(""), ms); | ||
| 12 | - onCleanup(() => window.clearTimeout(id)); | ||
| 13 | - }); | ||
| 14 | return [message, (text) => { | 7 | return [message, (text) => { |
| 15 | - setMessage(text); | 8 | + setMessage(""); |
| 16 | - setTick((value) => value + 1); | 9 | + showToast({ title: text }); |
| 17 | }]; | 10 | }]; |
| 18 | } | 11 | } |