SPB Git

Repository Public

toast.tsx 2.4 KB 79 lines tsx

1// Messages éphémères (confirmation d'un favori, comparateur plein…).23import { useSyncExternalStore } from "react";4import { AnimatePresence, motion } from "motion/react";5import { ImmokaSettlingDoor } from "@/brand/House";67interface Toast {8  id: number;9  text: string;10  action?: { label: string; run: () => void };11  /** petite porte qui s'ouvre : une action a réussi */12  door?: boolean;13}1415let toasts: Toast[] = [];16let seq = 0;17const listeners = new Set<() => void>();18const emit = () => listeners.forEach((l) => l());1920export function toast(text: string, action?: Toast["action"], door = false) {21  const id = ++seq;22  toasts = [...toasts.slice(-2), { id, text, action, door }];23  emit();24  window.setTimeout(() => {25    toasts = toasts.filter((t) => t.id !== id);26    emit();27  }, 3800);28}2930export function Toaster() {31  const list = useSyncExternalStore(32    (l) => {33      listeners.add(l);34      return () => listeners.delete(l);35    },36    () => toasts,37    () => toasts,38  );39  return (40    <div41      aria-live="polite"42      className="pointer-events-none fixed inset-x-0 bottom-[calc(5.5rem+env(safe-area-inset-bottom))] z-[90] flex flex-col items-center gap-2 px-4 lg:bottom-8"43    >44      <AnimatePresence>45        {list.map((t) => (46          <motion.div47            key={t.id}48            layout49            initial={{ opacity: 0, y: 8 }}50            animate={{ opacity: 1, y: 0 }}51            exit={{ opacity: 0, y: 4 }}52            transition={{ duration: 0.16, ease: [0.2, 0.7, 0.2, 1] }}53            className="pointer-events-auto flex items-center gap-3 rounded-[10px] bg-ink py-2.5 pl-4 pr-2.5 text-[0.88rem] text-bg shadow-float"54          >55            {t.door && (56              <span className="[--house:var(--bg)]">57                <ImmokaSettlingDoor size={20} angle={62} delay={80} />58              </span>59            )}60            <span className={t.action ? "" : "pr-2.5"}>{t.text}</span>61            {t.action && (62              <button63                type="button"64                onClick={t.action.run}65                className="rounded-[7px] bg-bg/15 px-3 py-1.5 text-xs font-medium text-bg"66              >67                {t.action.label}68              </button>69            )}70          </motion.div>71        ))}72      </AnimatePresence>73    </div>74  );75}7677/** Confirmation accompagnée de la porte qui s'ouvre. */78export const toastDoor = (text: string) => toast(text, undefined, true);79
Files main
Menu

Theme