{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"toast-stack","type":"registry:block","title":"Toast stack","description":"A notification stack where new cards slide in on top, older ones tuck behind with a spring, and the oldest leaves on a timer that pauses on hover.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/prequel/toast-stack/toast-stack.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useEffect, useRef, useState } from \"react\";\nimport {\n  AnimatePresence,\n  MotionConfig,\n  motion,\n  useInView,\n  useReducedMotion,\n} from \"motion/react\";\nimport { Bell, CircleCheck, CircleX, Info, Plus, TriangleAlert, X } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nconst EASE: [number, number, number, number] = [0.22, 1, 0.36, 1];\n\n/* ------------------------------------------------------------------ */\n/* types                                                               */\n/* ------------------------------------------------------------------ */\n\nexport type ToastStackTone = \"success\" | \"info\" | \"warning\" | \"error\";\n\nexport type ToastStackItem = {\n  id: string;\n  title: string;\n  description?: string;\n  tone?: ToastStackTone;\n  /** Static timestamp, e.g. \"2 min ago\". */\n  time?: string;\n};\n\nexport type ToastStackProps = {\n  className?: string;\n  title?: string;\n  description?: string;\n  initialToasts?: ToastStackItem[];\n  /** Notifications used by the \"New\" button, in order. */\n  queue?: Omit<ToastStackItem, \"id\">[];\n  /** How long a toast stays before the oldest one leaves. Defaults to 9000 ms. */\n  autoDismissMs?: number;\n  /** Most cards kept in the stack. Defaults to 4. */\n  max?: number;\n};\n\n/* ------------------------------------------------------------------ */\n/* deterministic demo data                                             */\n/* ------------------------------------------------------------------ */\n\nconst DEFAULT_TOASTS: ToastStackItem[] = [\n  {\n    id: \"deploy\",\n    title: \"Deploy finished\",\n    description: \"api@2026.11.15 is live in production.\",\n    tone: \"success\",\n    time: \"just now\",\n  },\n  {\n    id: \"comment\",\n    title: \"New comment\",\n    description: \"Mei Tanaka replied on the billing thread.\",\n    tone: \"info\",\n    time: \"2 min ago\",\n  },\n  {\n    id: \"retry\",\n    title: \"Retry queued\",\n    description: \"Webhook to Kite & Co failed twice.\",\n    tone: \"warning\",\n    time: \"6 min ago\",\n  },\n];\n\nconst DEFAULT_QUEUE: Omit<ToastStackItem, \"id\">[] = [\n  {\n    title: \"Invoice #4021 paid\",\n    description: \"$1,284.00 from Bluebird Studio.\",\n    tone: \"success\",\n    time: \"just now\",\n  },\n  {\n    title: \"Member invited\",\n    description: \"priya@halocloud.io can review billing.\",\n    tone: \"info\",\n    time: \"just now\",\n  },\n  {\n    title: \"Build #184 failed\",\n    description: \"Type error in the pricing page.\",\n    tone: \"error\",\n    time: \"just now\",\n  },\n  {\n    title: \"Usage at 86%\",\n    description: \"Sable Systems is close to its seat cap.\",\n    tone: \"warning\",\n    time: \"just now\",\n  },\n];\n\n/* ------------------------------------------------------------------ */\n/* tone styling                                                        */\n/* ------------------------------------------------------------------ */\n\nconst TONES: Record<\n  ToastStackTone,\n  { icon: typeof CircleCheck; iconClass: string; borderClass: string }\n> = {\n  success: {\n    icon: CircleCheck,\n    iconClass: \"bg-success/10 text-success\",\n    borderClass: \"border-success/25\",\n  },\n  info: {\n    icon: Info,\n    iconClass: \"bg-muted text-foreground\",\n    borderClass: \"border-border\",\n  },\n  warning: {\n    icon: TriangleAlert,\n    iconClass: \"bg-muted text-foreground\",\n    borderClass: \"border-border\",\n  },\n  error: {\n    icon: CircleX,\n    iconClass: \"bg-destructive/10 text-destructive\",\n    borderClass: \"border-destructive/25\",\n  },\n};\n\n/* ------------------------------------------------------------------ */\n/* one toast card                                                      */\n/* ------------------------------------------------------------------ */\n\nfunction ToastCard({\n  toast,\n  front,\n  onDismiss,\n}: {\n  toast: ToastStackItem;\n  front: boolean;\n  onDismiss: (id: string) => void;\n}) {\n  const tone = TONES[toast.tone ?? \"info\"];\n  const Icon = tone.icon;\n\n  return (\n    <div\n      className={cn(\n        \"flex h-full min-w-0 items-start gap-3 rounded-lg border bg-card p-3 shadow-sm\",\n        tone.borderClass\n      )}\n    >\n      <span\n        aria-hidden=\"true\"\n        className={cn(\n          \"grid h-7 w-7 shrink-0 place-items-center rounded-full\",\n          tone.iconClass\n        )}\n      >\n        <Icon className=\"h-3.5 w-3.5\" />\n      </span>\n\n      <div className=\"min-w-0 flex-1\">\n        <div className=\"flex items-baseline justify-between gap-2\">\n          <p className=\"truncate text-[12.5px] font-medium text-foreground\">\n            {toast.title}\n          </p>\n          <span className=\"shrink-0 text-[10.5px] tabular-nums text-muted-foreground\">\n            {toast.time ?? \"just now\"}\n          </span>\n        </div>\n        {toast.description ? (\n          <p className=\"mt-0.5 truncate text-[11.5px] text-muted-foreground\">\n            {toast.description}\n          </p>\n        ) : null}\n      </div>\n\n      {front ? (\n        <button\n          type=\"button\"\n          onClick={() => onDismiss(toast.id)}\n          aria-label={`Dismiss ${toast.title}`}\n          className=\"grid h-6 w-6 shrink-0 place-items-center rounded-md text-muted-foreground transition-colors hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring motion-reduce:transition-none\"\n        >\n          <X aria-hidden=\"true\" className=\"h-3.5 w-3.5\" />\n        </button>\n      ) : (\n        <span aria-hidden=\"true\" className=\"h-6 w-6 shrink-0\" />\n      )}\n    </div>\n  );\n}\n\n/* ------------------------------------------------------------------ */\n/* block                                                               */\n/* ------------------------------------------------------------------ */\n\nexport function ToastStack({\n  className,\n  title = \"Notifications\",\n  description = \"New events stack on top and leave after nine seconds.\",\n  initialToasts = DEFAULT_TOASTS,\n  queue = DEFAULT_QUEUE,\n  autoDismissMs = 9000,\n  max = 4,\n}: ToastStackProps) {\n  const ref = useRef<HTMLDivElement>(null);\n  const inView = useInView(ref, { once: true, margin: \"-10% 0px\" });\n  const reduce = Boolean(useReducedMotion());\n\n  const safeMax = Math.max(1, max);\n  const [toasts, setToasts] = useState<ToastStackItem[]>(() =>\n    initialToasts.slice(0, safeMax)\n  );\n  const added = useRef(0);\n  const [paused, setPaused] = useState(false);\n\n  const safeQueue = queue.length > 0 ? queue : DEFAULT_QUEUE;\n\n  useEffect(() => {\n    if (paused || autoDismissMs <= 0 || toasts.length === 0) return;\n    const timer = window.setTimeout(() => {\n      setToasts((current) => current.slice(0, -1));\n    }, Math.max(1200, autoDismissMs));\n    return () => window.clearTimeout(timer);\n  }, [paused, autoDismissMs, toasts.length]);\n\n  const addToast = () => {\n    const index = added.current;\n    added.current = index + 1;\n    const next = safeQueue[index % safeQueue.length];\n    if (!next) return;\n    setToasts((current) =>\n      [{ ...next, id: `queued-${index}` }, ...current].slice(0, safeMax)\n    );\n  };\n\n  const dismiss = (id: string) => {\n    setToasts((current) => current.filter((toast) => toast.id !== id));\n  };\n\n  const visible = toasts.slice(0, safeMax);\n  const stackHeight =\n    visible.length === 0 ? 92 : 76 + (Math.min(visible.length, safeMax) - 1) * 10;\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <motion.div\n        ref={ref}\n        initial={reduce ? false : { opacity: 0, y: 16 }}\n        animate={inView || reduce ? { opacity: 1, y: 0 } : { opacity: 0, y: 16 }}\n        transition={{ duration: reduce ? 0 : 0.6, ease: EASE }}\n        className={cn(\n          \"w-full rounded-xl border border-border bg-card p-5 text-card-foreground sm:p-6\",\n          className\n        )}\n      >\n        <div className=\"flex flex-wrap items-start justify-between gap-3\">\n          <div className=\"min-w-0\">\n            <h2 className=\"text-[15px] font-semibold tracking-tight\">{title}</h2>\n            <p className=\"mt-0.5 text-[13px] text-muted-foreground\">{description}</p>\n          </div>\n\n          <div className=\"flex shrink-0 items-center gap-1.5\">\n            <button\n              type=\"button\"\n              onClick={addToast}\n              className=\"inline-flex h-8 items-center gap-1.5 rounded-md border border-border bg-card px-2.5 text-[11.5px] font-medium text-foreground transition-colors hover:bg-muted focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-card motion-reduce:transition-none\"\n            >\n              <Plus aria-hidden=\"true\" className=\"h-3.5 w-3.5\" />\n              New\n            </button>\n            <button\n              type=\"button\"\n              onClick={() => setToasts([])}\n              disabled={toasts.length === 0}\n              className=\"inline-flex h-8 items-center gap-1.5 rounded-md px-2 text-[11.5px] font-medium text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-40 motion-reduce:transition-none\"\n            >\n              Clear\n            </button>\n          </div>\n        </div>\n\n        <div\n          className=\"mt-5\"\n          onMouseEnter={() => setPaused(true)}\n          onMouseLeave={() => setPaused(false)}\n          onFocusCapture={() => setPaused(true)}\n          onBlurCapture={(event) => {\n            if (!event.currentTarget.contains(event.relatedTarget as Node | null)) {\n              setPaused(false);\n            }\n          }}\n        >\n          <div\n            role=\"region\"\n            aria-live=\"polite\"\n            aria-label=\"Notifications\"\n            className=\"relative transition-[height] duration-300 ease-out motion-reduce:transition-none\"\n            style={{ height: reduce ? undefined : stackHeight }}\n          >\n            <AnimatePresence initial={false}>\n              {visible.length === 0 ? (\n                <motion.div\n                  key=\"empty\"\n                  initial={reduce ? false : { opacity: 0 }}\n                  animate={{ opacity: 1 }}\n                  exit={{ opacity: 0 }}\n                  transition={{ duration: reduce ? 0 : 0.2, ease: EASE }}\n                  className=\"flex h-[92px] flex-col items-center justify-center gap-1 rounded-lg border border-dashed border-border text-center\"\n                >\n                  <Bell aria-hidden=\"true\" className=\"h-4 w-4 text-muted-foreground\" />\n                  <p className=\"text-[12px] text-muted-foreground\">\n                    All caught up. New events land here.\n                  </p>\n                </motion.div>\n              ) : reduce ? (\n                <ul key=\"flow\" className=\"flex flex-col gap-2\">\n                  {visible.map((toast) => (\n                    <li key={toast.id} className=\"h-[76px]\">\n                      <ToastCard toast={toast} front onDismiss={dismiss} />\n                    </li>\n                  ))}\n                </ul>\n              ) : (\n                visible.map((toast, index) => (\n                  <motion.div\n                    key={toast.id}\n                    className=\"absolute inset-x-0 top-0 h-[76px]\"\n                    style={{ zIndex: visible.length - index }}\n                    aria-hidden={index > 0 ? \"true\" : undefined}\n                    initial={{ opacity: 0, y: -16, scale: 0.97 }}\n                    animate={{\n                      opacity: 1,\n                      y: index * 10,\n                      scale: 1 - Math.min(index, 3) * 0.018,\n                    }}\n                    exit={{\n                      opacity: 0,\n                      x: 24,\n                      transition: { duration: 0.2, ease: EASE },\n                    }}\n                    transition={{\n                      type: \"spring\",\n                      stiffness: 340,\n                      damping: 32,\n                      mass: 0.8,\n                      opacity: { duration: 0.22, ease: EASE },\n                    }}\n                  >\n                    <div\n                      className={cn(\n                        \"h-full\",\n                        index === 0 ? \"\" : \"pointer-events-none\"\n                      )}\n                    >\n                      <ToastCard\n                        toast={toast}\n                        front={index === 0}\n                        onDismiss={dismiss}\n                      />\n                    </div>\n                  </motion.div>\n                ))\n              )}\n            </AnimatePresence>\n          </div>\n        </div>\n\n        <div className=\"mt-4 flex flex-wrap items-center justify-between gap-2 border-t border-border pt-3.5\">\n          <span className=\"inline-flex items-center gap-1.5 text-[11.5px] text-muted-foreground\">\n            <span\n              aria-hidden=\"true\"\n              className={cn(\n                \"h-1.5 w-1.5 rounded-full\",\n                paused ? \"bg-muted-foreground\" : \"bg-success\"\n              )}\n            />\n            {paused ? \"Paused while you are here\" : `${visible.length} of ${safeMax} in the stack`}\n          </span>\n          <span className=\"tabular-nums text-[11px] text-muted-foreground\">\n            {autoDismissMs > 0\n              ? `auto-dismiss ${Math.round(autoDismissMs / 1000)} s`\n              : \"kept until dismissed\"}\n          </span>\n        </div>\n      </motion.div>\n    </MotionConfig>\n  );\n}\n\nexport default ToastStack;\n"}]}