{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "toast-stack",
  "title": "Toast Stack",
  "description": "A stacked toast notification system: toasts pile up in a layered stack that fans out on hover, with six tones including a loading state that turns into a result, auto-dismiss that pauses on hover, swipe to dismiss and an action button.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/toast-stack/toast-stack.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence, useReducedMotion, type PanInfo } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type ToastTone = \"neutral\" | \"success\" | \"error\" | \"warning\" | \"info\" | \"loading\";\nexport type ToastStackPosition = \"top-left\" | \"top-center\" | \"top-right\" | \"bottom-left\" | \"bottom-center\" | \"bottom-right\";\nexport type ToastStackSize = \"sm\" | \"md\" | \"lg\";\n\nexport interface ToastItem {\n  id: string;\n  title: string;\n  description?: string;\n  tone?: ToastTone;\n  /** Milliseconds before it closes itself. 0 keeps it open; loading toasts never close on their own. */\n  duration?: number;\n  action?: { label: string; onClick: () => void };\n  /** Bumped by update() so the auto-dismiss timer restarts. */\n  version?: number;\n}\n\nexport type ToastInput = Omit<ToastItem, \"id\" | \"version\"> & { id?: string };\n\nexport interface ToastStackProps {\n  toasts: ToastItem[];\n  onDismiss: (id: string) => void;\n  position?: ToastStackPosition;\n  /** How many toasts show in the collapsed stack; older ones wait behind until the front ones close. */\n  max?: number;\n  /** \"hover\" fans the stack out while the pointer (or keyboard focus) is on it; \"always\" keeps it open. */\n  expand?: \"hover\" | \"always\";\n  /** Default auto-dismiss time for toasts that don't set their own. */\n  duration?: number;\n  showProgress?: boolean;\n  showClose?: boolean;\n  size?: ToastStackSize;\n  theme?: \"dark\" | \"light\";\n  /** Positions the stack inside its nearest positioned ancestor instead of the viewport. */\n  contained?: boolean;\n  /** Distance from the edges, in px. */\n  offset?: number;\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.56)\", faint: \"rgba(245,244,241,0.14)\", bg: \"#141414\", hover: \"rgba(255,255,255,0.08)\", border: \"rgba(255,255,255,0.12)\", borderHover: \"rgba(255,255,255,0.22)\", focus: \"rgba(245,244,241,0.85)\", shadow: \"0 10px 30px rgba(0,0,0,0.45), 0 2px 6px rgba(0,0,0,0.3)\", success: \"#5BD68A\", error: \"#FF7A6B\", warning: \"#F2A841\", info: \"#8AB4FF\", actionBg: \"#F5F4F1\", actionFg: \"#0A0A0A\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.56)\", faint: \"rgba(10,10,10,0.1)\", bg: \"#FFFFFF\", hover: \"rgba(10,10,10,0.06)\", border: \"rgba(10,10,10,0.12)\", borderHover: \"rgba(10,10,10,0.24)\", focus: \"rgba(10,10,10,0.85)\", shadow: \"0 10px 30px rgba(10,10,10,0.12), 0 2px 6px rgba(10,10,10,0.06)\", success: \"#16A34A\", error: \"#E5484D\", warning: \"#B7791F\", info: \"#2F6FEB\", actionBg: \"#0A0A0A\", actionFg: \"#FFFFFF\" },\n};\n\nconst SIZES: Record<ToastStackSize, { width: number; font: number; small: number; radius: number; pad: number; icon: number; gap: number; peek: number }> = {\n  sm: { width: 320, font: 13, small: 12, radius: 12, pad: 12, icon: 16, gap: 8, peek: 10 },\n  md: { width: 356, font: 14, small: 13, radius: 14, pad: 14, icon: 18, gap: 10, peek: 12 },\n  lg: { width: 400, font: 15.5, small: 14, radius: 16, pad: 16, icon: 20, gap: 12, peek: 14 },\n};\n\nconst KEYFRAMES = \"@keyframes rf-toast-timer{from{transform:scaleX(1)}to{transform:scaleX(0)}}\";\n\nlet counter = 0;\nconst nextId = () => `toast-${Date.now().toString(36)}-${(counter++).toString(36)}`;\n\n/**\n * State for a ToastStack: push() adds a toast and returns its id, update() changes one in place\n * (e.g. a loading toast becoming a success), dismiss() removes one, clear() removes them all.\n */\nexport function useToastStack(initial: ToastInput[] = []) {\n  const [toasts, setToasts] = React.useState<ToastItem[]>(() => initial.map((t) => ({ ...t, id: t.id ?? nextId(), version: 0 })));\n\n  const push = React.useCallback((input: ToastInput) => {\n    const id = input.id ?? nextId();\n    setToasts((list) => [{ ...input, id, version: 0 }, ...list.filter((t) => t.id !== id)]);\n    return id;\n  }, []);\n  const update = React.useCallback((id: string, patch: Partial<Omit<ToastItem, \"id\">>) => {\n    setToasts((list) => list.map((t) => (t.id === id ? { ...t, ...patch, version: (t.version ?? 0) + 1 } : t)));\n  }, []);\n  const dismiss = React.useCallback((id: string) => setToasts((list) => list.filter((t) => t.id !== id)), []);\n  const clear = React.useCallback(() => setToasts([]), []);\n\n  return { toasts, push, update, dismiss, clear };\n}\n\nfunction ToneIcon({ tone, size, color }: { tone: ToastTone; size: number; color: string }) {\n  if (tone === \"neutral\") return null;\n  const common = { width: size, height: size, viewBox: \"0 0 20 20\", fill: \"none\", stroke: color, strokeWidth: 1.7, strokeLinecap: \"round\" as const, strokeLinejoin: \"round\" as const, \"aria-hidden\": true };\n  if (tone === \"loading\") {\n    return (\n      <motion.svg {...common} animate={{ rotate: 360 }} transition={{ duration: 0.9, repeat: Infinity, ease: \"linear\" }}>\n        <circle cx=\"10\" cy=\"10\" r=\"7.25\" strokeOpacity=\"0.22\" />\n        <path d=\"M10 2.75a7.25 7.25 0 0 1 7.25 7.25\" />\n      </motion.svg>\n    );\n  }\n  return (\n    <svg {...common}>\n      {tone === \"warning\" ? <path d=\"M10 3.2 17.6 16.4H2.4L10 3.2Z\" /> : <circle cx=\"10\" cy=\"10\" r=\"7.25\" />}\n      {tone === \"success\" && <motion.path d=\"M6.6 10.2 8.9 12.5 13.4 7.8\" initial={{ pathLength: 0 }} animate={{ pathLength: 1 }} transition={{ duration: 0.3, delay: 0.08, ease: \"easeOut\" }} />}\n      {tone === \"error\" && <path d=\"M7.6 7.6 12.4 12.4M12.4 7.6 7.6 12.4\" />}\n      {tone === \"warning\" && <path d=\"M10 8.2v3.4M10 14.1h.01\" />}\n      {tone === \"info\" && <path d=\"M10 9.2v4.4M10 6.6h.01\" />}\n    </svg>\n  );\n}\n\ninterface ToastCardProps {\n  toast: ToastItem;\n  index: number;\n  front: boolean;\n  expanded: boolean;\n  hiddenBehind: boolean;\n  y: number;\n  height: number | undefined;\n  frontHeight: number;\n  fromBottom: boolean;\n  swipeSign: number;\n  paused: boolean;\n  total: number;\n  defaultDuration: number;\n  showProgress: boolean;\n  showClose: boolean;\n  p: (typeof PALETTES)[\"dark\"];\n  s: (typeof SIZES)[\"md\"];\n  reduce: boolean;\n  onDismiss: (id: string) => void;\n  onHeight: (id: string, h: number) => void;\n}\n\nfunction ToastCard({ toast, index, front, expanded, hiddenBehind, y, height, frontHeight, fromBottom, swipeSign, paused, total, defaultDuration, showProgress, showClose, p, s, reduce, onDismiss, onHeight }: ToastCardProps) {\n  const innerRef = React.useRef<HTMLDivElement>(null);\n  const tone = toast.tone ?? \"neutral\";\n  const duration = tone === \"loading\" ? 0 : (toast.duration ?? defaultDuration);\n  const collapsedBehind = !expanded && !front;\n\n  // Report the natural height so the stack can lay itself out; re-measure when the content changes.\n  React.useLayoutEffect(() => {\n    const el = innerRef.current;\n    if (!el) return;\n    const report = () => onHeight(toast.id, el.offsetHeight);\n    report();\n    const ro = new ResizeObserver(report);\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, [toast.id, onHeight]);\n\n  const toneColor = tone === \"success\" ? p.success : tone === \"error\" ? p.error : tone === \"warning\" ? p.warning : tone === \"info\" ? p.info : p.text;\n  const scale = collapsedBehind ? 1 - index * 0.055 : 1;\n\n  const onDragEnd = (_: unknown, info: PanInfo) => {\n    if (Math.abs(info.offset.x) > 90 || Math.abs(info.velocity.x) > 600) onDismiss(toast.id);\n  };\n\n  return (\n    <motion.li\n      layout={false}\n      role={tone === \"error\" ? \"alert\" : \"status\"}\n      aria-atomic=\"true\"\n      initial={reduce ? { opacity: 0 } : { opacity: 0, y: fromBottom ? 24 : -24, scale: 0.96 }}\n      animate={{ opacity: hiddenBehind ? 0 : 1, y, scale, transition: { type: \"spring\", stiffness: 420, damping: 36, mass: 0.9 } }}\n      exit={reduce ? { opacity: 0, transition: { duration: 0.12 } } : { opacity: 0, scale: 0.94, x: 0, transition: { duration: 0.18, ease: [0.4, 0, 1, 1] } }}\n      drag={reduce ? false : \"x\"}\n      dragConstraints={{ left: 0, right: 0 }}\n      dragElastic={{ left: swipeSign < 0 ? 0.9 : 0.12, right: swipeSign > 0 ? 0.9 : 0.12 }}\n      dragSnapToOrigin\n      onDragEnd={onDragEnd}\n      className=\"absolute inset-x-0 list-none\"\n      style={{\n        [fromBottom ? \"bottom\" : \"top\"]: 0,\n        zIndex: total - index,\n        transformOrigin: fromBottom ? \"50% 100%\" : \"50% 0%\",\n        pointerEvents: hiddenBehind ? \"none\" : \"auto\",\n        touchAction: \"pan-y\",\n      }}\n    >\n      <div\n        className=\"relative overflow-hidden\"\n        style={{\n          height: collapsedBehind ? frontHeight : height,\n          borderRadius: s.radius,\n          background: p.bg,\n          border: `1px solid ${p.border}`,\n          boxShadow: p.shadow,\n          transition: \"height 0.25s cubic-bezier(0.22,1,0.36,1)\",\n        }}\n      >\n        <div\n          ref={innerRef}\n          className=\"flex items-start\"\n          style={{ gap: 10, padding: s.pad, paddingRight: showClose ? s.pad + 22 : s.pad, opacity: collapsedBehind ? 0 : 1, transition: \"opacity 0.2s ease\" }}\n        >\n          {tone !== \"neutral\" && (\n            <span className=\"flex shrink-0 items-center\" style={{ height: s.font * 1.4 }}>\n              <ToneIcon tone={tone} size={s.icon} color={toneColor} />\n            </span>\n          )}\n          <div className=\"min-w-0 flex-1\">\n            <div className=\"font-semibold tracking-[-0.01em]\" style={{ color: p.text, fontSize: s.font, lineHeight: 1.4 }}>\n              {toast.title}\n            </div>\n            {toast.description && (\n              <div style={{ color: p.muted, fontSize: s.small, lineHeight: 1.45, marginTop: 2 }}>{toast.description}</div>\n            )}\n          </div>\n          {toast.action && (\n            <button\n              type=\"button\"\n              onClick={() => {\n                toast.action?.onClick();\n                onDismiss(toast.id);\n              }}\n              className=\"shrink-0 cursor-pointer border-none font-semibold outline-none focus-visible:ring-2\"\n              style={{ alignSelf: \"center\", background: p.actionBg, color: p.actionFg, fontSize: s.small, padding: \"5px 10px\", borderRadius: s.radius - 5, [\"--tw-ring-color\" as string]: p.focus, [\"--tw-ring-offset-color\" as string]: p.bg }}\n            >\n              {toast.action.label}\n            </button>\n          )}\n        </div>\n\n        {showClose && (\n          <button\n            type=\"button\"\n            aria-label={`Dismiss: ${toast.title}`}\n            onClick={() => onDismiss(toast.id)}\n            className=\"absolute flex cursor-pointer items-center justify-center border-none bg-transparent outline-none focus-visible:ring-2\"\n            style={{ top: s.pad - 3, right: s.pad - 6, width: 24, height: 24, borderRadius: 7, color: p.muted, opacity: collapsedBehind ? 0 : 1, transition: \"background 0.14s ease, color 0.14s ease, opacity 0.2s ease\", [\"--tw-ring-color\" as string]: p.focus }}\n            onMouseEnter={(e) => {\n              e.currentTarget.style.background = p.hover;\n              e.currentTarget.style.color = p.text;\n            }}\n            onMouseLeave={(e) => {\n              e.currentTarget.style.background = \"transparent\";\n              e.currentTarget.style.color = p.muted;\n            }}\n            tabIndex={collapsedBehind ? -1 : 0}\n          >\n            <svg width=\"12\" height=\"12\" viewBox=\"0 0 12 12\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" aria-hidden=\"true\">\n              <path d=\"M3 3l6 6M9 3L3 9\" />\n            </svg>\n          </button>\n        )}\n\n        {/* The timer is a CSS animation: pausing it (hover, focus, hidden tab) freezes the countdown\n            exactly where it is, and its end is what closes the toast. */}\n        {duration > 0 && (\n          <span\n            key={`${toast.id}-${toast.version ?? 0}`}\n            aria-hidden=\"true\"\n            onAnimationEnd={() => onDismiss(toast.id)}\n            className=\"absolute bottom-0 left-0 h-[2px] w-full\"\n            style={{\n              background: toneColor,\n              opacity: showProgress && !collapsedBehind ? 0.55 : 0,\n              transformOrigin: \"0 50%\",\n              animation: `rf-toast-timer ${duration}ms linear forwards`,\n              animationPlayState: paused ? \"paused\" : \"running\",\n            }}\n          />\n        )}\n      </div>\n    </motion.li>\n  );\n}\n\nexport function ToastStack({\n  toasts,\n  onDismiss,\n  position = \"bottom-right\",\n  max = 3,\n  expand = \"hover\",\n  duration = 4000,\n  showProgress = true,\n  showClose = true,\n  size = \"md\",\n  theme = \"dark\",\n  contained = false,\n  offset = 24,\n  className,\n}: ToastStackProps) {\n  const p = PALETTES[theme];\n  const s = SIZES[size];\n  const reduce = Boolean(useReducedMotion());\n  const [hovered, setHovered] = React.useState(false);\n  const [focusedIn, setFocusedIn] = React.useState(false);\n  const [tabHidden, setTabHidden] = React.useState(false);\n  const [heights, setHeights] = React.useState<Record<string, number>>({});\n\n  const fromBottom = position.startsWith(\"bottom\");\n  const swipeSign = position.endsWith(\"left\") ? -1 : 1;\n  const expanded = expand === \"always\" || hovered || focusedIn;\n  const paused = hovered || focusedIn || tabHidden;\n\n  React.useEffect(() => {\n    const sync = () => setTabHidden(document.visibilityState === \"hidden\");\n    document.addEventListener(\"visibilitychange\", sync);\n    return () => document.removeEventListener(\"visibilitychange\", sync);\n  }, []);\n\n  // Once the stack empties, the pointer can't leave it any more, so drop the hover state with it.\n  const [prevCount, setPrevCount] = React.useState(toasts.length);\n  if (toasts.length !== prevCount) {\n    setPrevCount(toasts.length);\n    if (toasts.length === 0) {\n      setHovered(false);\n      setFocusedIn(false);\n    }\n  }\n\n  const onHeight = React.useCallback((id: string, h: number) => {\n    setHeights((prev) => (prev[id] === h ? prev : { ...prev, [id]: h }));\n  }, []);\n\n  const shown = toasts.slice(0, Math.max(1, max));\n  const frontHeight = heights[toasts[0]?.id] ?? 64;\n  const sign = fromBottom ? -1 : 1;\n\n  const offsets: number[] = [];\n  let acc = 0;\n  toasts.forEach((t, i) => {\n    offsets.push(expanded ? acc : i * s.peek);\n    acc += (heights[t.id] ?? frontHeight) + s.gap;\n  });\n  const listHeight = toasts.length === 0 ? 0 : expanded ? acc - s.gap : frontHeight + (shown.length - 1) * s.peek;\n\n  const horizontal = position.endsWith(\"left\") ? { left: offset } : position.endsWith(\"right\") ? { right: offset } : { left: \"50%\", transform: \"translateX(-50%)\" };\n  const vertical = fromBottom ? { bottom: offset } : { top: offset };\n\n  return (\n    <section\n      aria-label=\"Notifications\"\n      aria-live=\"polite\"\n      className={cn(contained ? \"absolute\" : \"fixed\", \"z-[1000]\", className)}\n      style={{ ...horizontal, ...vertical, width: s.width, maxWidth: `calc(100% - ${offset * 2}px)`, fontFamily: \"Inter, sans-serif\" }}\n    >\n      <style>{KEYFRAMES}</style>\n      <ol\n        className=\"relative m-0 p-0\"\n        style={{ height: listHeight, transition: reduce ? undefined : \"height 0.3s cubic-bezier(0.22,1,0.36,1)\" }}\n        onMouseEnter={() => setHovered(true)}\n        onMouseLeave={() => setHovered(false)}\n        onFocus={() => setFocusedIn(true)}\n        onBlur={(e) => {\n          if (!e.currentTarget.contains(e.relatedTarget as Node | null)) setFocusedIn(false);\n        }}\n      >\n        <AnimatePresence initial={false}>\n          {toasts.map((t, i) => (\n            <ToastCard\n              key={t.id}\n              toast={t}\n              index={i}\n              front={i === 0}\n              expanded={expanded}\n              hiddenBehind={!expanded && i >= shown.length}\n              y={sign * offsets[i]}\n              height={heights[t.id]}\n              frontHeight={frontHeight}\n              fromBottom={fromBottom}\n              swipeSign={swipeSign}\n              paused={paused}\n              total={toasts.length}\n              defaultDuration={duration}\n              showProgress={showProgress}\n              showClose={showClose}\n              p={p}\n              s={s}\n              reduce={reduce}\n              onDismiss={onDismiss}\n              onHeight={onHeight}\n            />\n          ))}\n        </AnimatePresence>\n      </ol>\n    </section>\n  );\n}\n\nexport default ToastStack;\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}