{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"fling-toast","type":"registry:ui","title":"Fling Toast","description":"Stacked toasts that fan out on hover, pause their progress timers while you read, and fling away when swiped sideways.","dependencies":["motion","lucide-react"],"registryDependencies":[],"files":[{"path":"registry/ui/fling-toast.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useCallback, useLayoutEffect, useRef, useState, type KeyboardEvent, type ReactNode } from \"react\";\nimport { AnimatePresence, animate, motion, useMotionValue, useReducedMotion, useTransform, type PanInfo } from \"motion/react\";\nimport { Bell, CircleAlert, CircleCheck, Info, X } from \"lucide-react\";\n\nexport type ToastTone = \"default\" | \"success\" | \"error\" | \"info\";\nexport type ToastId = string | number;\n\nexport interface ToastItem {\n  id: ToastId;\n  title: ReactNode;\n  description?: ReactNode;\n  tone?: ToastTone;\n}\n\n/** Tiny local queue. Pass `toasts` and `dismiss` to <FlingToast />. */\nexport function useSwipeToast(initial: ToastItem[] = []) {\n  const [toasts, setToasts] = useState(initial);\n  const seq = useRef(0);\n  const push = useCallback((t: Omit<ToastItem, \"id\"> & { id?: ToastId }) => {\n    const id = t.id ?? `t${++seq.current}-${Date.now()}`;\n    setToasts((v) => [...v, { ...t, id }]);\n    return id;\n  }, []);\n  const dismiss = useCallback((id: ToastId) => setToasts((v) => v.filter((t) => t.id !== id)), []);\n  return { toasts, push, dismiss };\n}\n\ntype Position = \"top-left\" | \"top-center\" | \"top-right\" | \"bottom-left\" | \"bottom-center\" | \"bottom-right\";\n\nexport interface FlingToastProps {\n  toasts: ToastItem[];\n  onDismiss: (id: ToastId) => void;\n  /** Corner of the container (or viewport with strategy=\"fixed\"). */\n  position?: Position;\n  /** \"absolute\" keeps the stack inside the nearest positioned parent. */\n  strategy?: \"absolute\" | \"fixed\";\n  /** Ms before a toast auto-dismisses. 0 keeps them until swiped. */\n  duration?: number;\n  /** How many toasts peek out of the collapsed stack. */\n  visible?: number;\n  /** Px between toasts when the stack is expanded. */\n  gap?: number;\n  className?: string;\n}\n\nconst css = `@keyframes swipe-toast-timer{from{transform:scaleX(1)}to{transform:scaleX(0)}}`;\nconst PEEK = 12;\nconst place: Record<Position, string> = {\n  \"top-left\": \"top-0 left-0\",\n  \"top-center\": \"top-0 left-1/2 -translate-x-1/2\",\n  \"top-right\": \"top-0 right-0\",\n  \"bottom-left\": \"bottom-0 left-0\",\n  \"bottom-center\": \"bottom-0 left-1/2 -translate-x-1/2\",\n  \"bottom-right\": \"bottom-0 right-0\",\n};\nconst tones = {\n  default: { Icon: Bell, color: \"var(--foreground)\" },\n  success: { Icon: CircleCheck, color: \"#22c55e\" },\n  error: { Icon: CircleAlert, color: \"#ef4444\" },\n  info: { Icon: Info, color: \"#38bdf8\" },\n};\n\nexport function FlingToast({\n  toasts,\n  onDismiss,\n  position = \"bottom-right\",\n  strategy = \"absolute\",\n  duration = 5000,\n  visible = 3,\n  gap = 10,\n  className,\n}: FlingToastProps) {\n  const [expanded, setExpanded] = useState(false);\n  const [heights, setHeights] = useState<Record<string, number>>({});\n  const top = position.startsWith(\"top\");\n  const dir = top ? 1 : -1;\n\n  const front = [...toasts].reverse(); // newest first\n  const h = (t: ToastItem) => heights[String(t.id)] ?? 64;\n  const offsets: number[] = [];\n  front.reduce((acc, t, i) => ((offsets[i] = acc), acc + h(t) + gap), 0);\n  const shown = Math.min(front.length, visible);\n  const stackHeight = !front.length\n    ? 0\n    : expanded\n      ? offsets[front.length - 1] + h(front[front.length - 1])\n      : h(front[0]) + (shown - 1) * PEEK;\n\n  const onHeight = useCallback((id: ToastId, px: number) => {\n    setHeights((v) => (v[String(id)] === px ? v : { ...v, [String(id)]: px }));\n  }, []);\n\n  return (\n    <section\n      aria-label=\"Notifications\"\n      className={`${strategy === \"fixed\" ? \"fixed\" : \"absolute\"} ${place[position]} z-50 w-[min(22rem,100%)] p-4 ${className ?? \"\"}`}\n    >\n      <style href=\"fling-toast\" precedence=\"default\">\n        {css}\n      </style>\n      <ol\n        aria-live=\"polite\"\n        className=\"relative transition-[height] duration-300\"\n        style={{ height: stackHeight }}\n        onPointerEnter={() => setExpanded(true)}\n        onPointerLeave={() => setExpanded(false)}\n        onFocus={() => setExpanded(true)}\n        onBlur={(e) => !e.currentTarget.contains(e.relatedTarget) && setExpanded(false)}\n      >\n        <AnimatePresence initial={false}>\n          {front.map((t, i) => (\n            <Toast\n              key={t.id}\n              toast={t}\n              index={i}\n              total={front.length}\n              visible={visible}\n              expanded={expanded}\n              y={dir * (expanded ? offsets[i] : i * PEEK)}\n              top={top}\n              duration={duration}\n              onHeight={onHeight}\n              onDismiss={onDismiss}\n            />\n          ))}\n        </AnimatePresence>\n      </ol>\n    </section>\n  );\n}\n\ninterface ToastProps {\n  toast: ToastItem;\n  index: number;\n  total: number;\n  visible: number;\n  expanded: boolean;\n  y: number;\n  top: boolean;\n  duration: number;\n  onHeight: (id: ToastId, px: number) => void;\n  onDismiss: (id: ToastId) => void;\n}\n\nfunction Toast({ toast, index, total, visible, expanded, y, top, duration, onHeight, onDismiss }: ToastProps) {\n  const reduce = useReducedMotion();\n  const ref = useRef<HTMLLIElement>(null);\n  const [dragging, setDragging] = useState(false);\n  const leaving = useRef(false);\n  const x = useMotionValue(0);\n  const fade = useTransform(x, [-220, 0, 220], [0, 1, 0]);\n  const { Icon, color } = tones[toast.tone ?? \"default\"];\n  const hidden = index >= visible && !expanded;\n  const tucked = index > 0 && !expanded;\n\n  useLayoutEffect(() => {\n    if (ref.current) onHeight(toast.id, ref.current.offsetHeight);\n  }, [toast.id, onHeight]);\n\n  const leave = (sign: number, velocity = 0) => {\n    if (leaving.current) return;\n    leaving.current = true;\n    if (reduce || !sign) return onDismiss(toast.id);\n    animate(x, sign * 440, { type: \"spring\", stiffness: 260, damping: 32, velocity }).then(() => onDismiss(toast.id));\n  };\n\n  const onDragEnd = (_: unknown, info: PanInfo) => {\n    setDragging(false);\n    const { offset, velocity } = info;\n    if (Math.abs(offset.x) > 90 || Math.abs(velocity.x) > 600) leave(Math.sign(offset.x || velocity.x), velocity.x);\n  };\n\n  const onKey = (e: KeyboardEvent<HTMLLIElement>) => {\n    if (e.key === \"Escape\" || e.key === \"Delete\" || e.key === \"Backspace\") {\n      e.preventDefault();\n      leave(1);\n    }\n  };\n\n  return (\n    <motion.li\n      ref={ref}\n      tabIndex={hidden ? -1 : 0}\n      aria-hidden={hidden}\n      onKeyDown={onKey}\n      className={`absolute inset-x-0 ${top ? \"top-0\" : \"bottom-0\"} rounded-xl outline-none focus-visible:ring-2 focus-visible:ring-ring ${hidden ? \"pointer-events-none\" : \"\"}`}\n      style={{ zIndex: total - index, transformOrigin: top ? \"top center\" : \"bottom center\" }}\n      initial={{ y: top ? -36 : 36, opacity: 0, scale: 0.9 }}\n      animate={{ y, opacity: hidden ? 0 : 1, scale: expanded ? 1 : 1 - index * 0.06 }}\n      exit={{ opacity: 0, scale: 0.9, transition: { duration: 0.18 } }}\n      transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 420, damping: 30, mass: 0.8 }}\n    >\n      <motion.div\n        drag={hidden ? false : \"x\"}\n        dragConstraints={{ left: 0, right: 0 }}\n        dragElastic={0.9}\n        onDragStart={() => setDragging(true)}\n        onDragEnd={onDragEnd}\n        style={{ x, opacity: fade, touchAction: \"pan-y\" }}\n        className=\"relative flex cursor-grab items-start gap-3 overflow-hidden rounded-xl border border-border bg-card p-3 pr-9 shadow-lg shadow-black/20 select-none active:cursor-grabbing\"\n      >\n        <motion.div\n          className=\"flex min-w-0 flex-1 items-start gap-3\"\n          animate={{ opacity: tucked ? 0 : 1 }}\n          transition={{ duration: 0.15 }}\n        >\n          <Icon aria-hidden className=\"mt-0.5 size-4 shrink-0\" style={{ color }} />\n          <div className=\"min-w-0\">\n            <p className=\"text-sm font-medium text-foreground\">{toast.title}</p>\n            {toast.description && <p className=\"mt-0.5 text-xs text-muted-foreground\">{toast.description}</p>}\n          </div>\n        </motion.div>\n        <button\n          type=\"button\"\n          aria-label=\"Dismiss notification\"\n          tabIndex={hidden ? -1 : 0}\n          onClick={() => leave(0)}\n          className=\"absolute top-2 right-2 grid size-6 place-items-center rounded-md text-muted-foreground hover:bg-muted hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring\"\n        >\n          <X className=\"size-3.5\" />\n        </button>\n        {duration > 0 && (\n          <span aria-hidden className=\"absolute inset-x-3 bottom-1 h-0.5 overflow-hidden rounded-full bg-muted\">\n            <span\n              className=\"block size-full origin-left rounded-full\"\n              style={{\n                background: color,\n                animation: `swipe-toast-timer ${duration}ms linear forwards`,\n                animationPlayState: expanded || dragging ? \"paused\" : \"running\",\n              }}\n              onAnimationEnd={() => leave(0)}\n            />\n          </span>\n        )}\n      </motion.div>\n    </motion.li>\n  );\n}\n"}]}