{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "calendar-toaster",
  "title": "Calendar Toaster",
  "author": "vmnog <https://vmnog.com>",
  "description": "Notion Calendar's toasts: \"Event deleted\" and \"Event cut\" with Undo, and \"Event copied to clipboard\", at the bottom right of the calendar.",
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "@calendarcn/calendar-core"
  ],
  "files": [
    {
      "path": "src/components/calendar-toaster.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { Info, X } from \"lucide-react\";\n\nimport { cn } from \"@/lib/utils\";\nimport { useCalendarNotice } from \"@/components/calendar-provider\";\nimport type { CalendarNotice } from \"@/components/calendar-types\";\nimport type { CalendarToasterProps } from \"@/components/week-view-types\";\n\n/**\n * How long a toast stays when `duration` is not set, in milliseconds.\n * Notion Calendar's are gone 5 to 6 seconds after the action\n */\nconst DEFAULT_TOAST_DURATION_MS = 5000;\n\n/** How long a toast fades out at the end of its time, in milliseconds */\nconst TOAST_FADE_MS = 150;\n\n/** Accessible name of the toaster, a region screen readers can jump to */\nconst TOASTER_LABEL = \"Notifications\";\n\n/**\n * A toast's look: dark in both themes, like the event menus and the\n * details' tooltip. The border shows in dark mode only, where #252525 sits\n * close to the #191919 page\n */\nconst TOAST_CLASS =\n  \"pointer-events-auto relative flex w-[220px] max-w-full gap-2 rounded-md border border-transparent bg-[#252525] py-2.5 pr-8 pl-3 text-xs leading-[18px] text-[#F0F0F0] shadow-lg dark:border-[#303030] dark:shadow-black/40\";\n\n/** Slides in, and fades over its last `TOAST_FADE_MS`; neither with reduced motion */\nconst TOAST_MOTION_CLASS =\n  \"animate-in fade-in-0 slide-in-from-bottom-2 transition-opacity duration-150 motion-reduce:animate-none motion-reduce:transition-none\";\n\n/** The toast's Undo and Close: a faint fill on hover, a ring on keyboard focus */\nconst TOAST_BUTTON_CLASS =\n  \"rounded-sm outline-none hover:bg-white/10 focus-visible:ring-1 focus-visible:ring-white/50\";\n\n/** Props of one toast (only used in this file) */\ninterface CalendarToastProps {\n  notice: CalendarNotice;\n  /** Milliseconds before it closes by itself */\n  duration: number;\n  /** Closes the notice with this id */\n  onDismiss: (id: number) => void;\n}\n\n/**\n * Notion Calendar's toasts: \"Event deleted\" and \"Event cut\" with an Undo\n * button, \"Event copied to clipboard\", and any notice your app shows with\n * `useCalendarNotice()`, one at a time. Render it inside the provider, in a\n * positioned element around the view (`relative`): it sits at that\n * element's bottom right. A toast closes after `duration`, held while the\n * pointer or focus is on it, or with its close button. Screen readers hear\n * the provider's live region instead, so the toast isn't announced.\n */\nexport function CalendarToaster({\n  duration = DEFAULT_TOAST_DURATION_MS,\n  className,\n}: CalendarToasterProps) {\n  const { notice, dismissNotice } = useCalendarNotice();\n  if (!notice) return null;\n\n  return (\n    <section\n      aria-label={TOASTER_LABEL}\n      className={cn(\n        \"pointer-events-none absolute right-4 bottom-4 z-40 flex max-w-[calc(100%-2rem)] justify-end\",\n        className,\n      )}\n    >\n      {/* A new notice is a new toast, so its time starts over */}\n      <CalendarToast\n        key={notice.id}\n        notice={notice}\n        duration={duration}\n        onDismiss={dismissNotice}\n      />\n    </section>\n  );\n}\n\n/** One toast, with its own timer */\nfunction CalendarToast({ notice, duration, onDismiss }: CalendarToastProps) {\n  const { id, message, title, onUndo } = notice;\n  /** The pending fade or close */\n  const timerRef = React.useRef<ReturnType<typeof setTimeout> | null>(null);\n  /** Whether the pointer is on the toast, which holds it */\n  const isHoveredRef = React.useRef(false);\n  /** Whether focus is inside the toast, which holds it */\n  const hasFocusRef = React.useRef(false);\n  /** Whether it is fading out at the end of its time */\n  const [isLeaving, setIsLeaving] = React.useState(false);\n\n  const clearTimer = React.useCallback(() => {\n    if (timerRef.current === null) return;\n    clearTimeout(timerRef.current);\n    timerRef.current = null;\n  }, []);\n\n  /** Starts its time over: it fades, then closes, `duration` from now */\n  const startTimer = React.useCallback(() => {\n    clearTimer();\n    timerRef.current = setTimeout(\n      () => {\n        setIsLeaving(true);\n        timerRef.current = setTimeout(() => onDismiss(id), TOAST_FADE_MS);\n      },\n      Math.max(0, duration - TOAST_FADE_MS),\n    );\n  }, [clearTimer, duration, id, onDismiss]);\n\n  // Its time runs from the moment it shows, unless something holds it\n  React.useEffect(() => {\n    if (!isHoveredRef.current && !hasFocusRef.current) startTimer();\n    return clearTimer;\n  }, [startTimer, clearTimer]);\n\n  /**\n   * The pointer or focus came onto it: stop its time and undo a fade. The\n   * handlers start and stop the timer themselves, not an effect, so the\n   * hold applies before the event returns\n   */\n  const hold = () => {\n    clearTimer();\n    setIsLeaving(false);\n  };\n\n  /** Once neither the pointer nor focus is on it, its full time starts over */\n  const releaseIfFree = () => {\n    if (isHoveredRef.current || hasFocusRef.current) return;\n    startTimer();\n  };\n\n  const handleBlur = (e: React.FocusEvent<HTMLDivElement>) => {\n    // Focus moving between its own buttons keeps it held\n    if (\n      e.relatedTarget instanceof Node &&\n      e.currentTarget.contains(e.relatedTarget)\n    ) {\n      return;\n    }\n    hasFocusRef.current = false;\n    releaseIfFree();\n  };\n\n  const handleUndo = () => {\n    onUndo?.();\n    onDismiss(id);\n  };\n\n  return (\n    <div\n      data-slot=\"calendar-toast\"\n      className={cn(TOAST_CLASS, TOAST_MOTION_CLASS, isLeaving && \"opacity-0\")}\n      onPointerEnter={() => {\n        isHoveredRef.current = true;\n        hold();\n      }}\n      onPointerLeave={() => {\n        isHoveredRef.current = false;\n        releaseIfFree();\n      }}\n      onFocus={() => {\n        hasFocusRef.current = true;\n        hold();\n      }}\n      onBlur={handleBlur}\n    >\n      <Info aria-hidden className=\"mt-0.5 size-3.5 shrink-0 text-white/60\" />\n      <div className=\"min-w-0 flex-1\">\n        <p className=\"font-medium\">{message}</p>\n        {title ? (\n          <p className=\"truncate text-white/60\">{`“${title}”`}</p>\n        ) : null}\n        {onUndo ? (\n          <div className=\"mt-1.5 flex justify-end\">\n            <button\n              type=\"button\"\n              className={cn(\n                TOAST_BUTTON_CLASS,\n                \"bg-white/10 px-2 py-0.5 font-medium text-white hover:bg-white/15\",\n              )}\n              onClick={handleUndo}\n            >\n              Undo\n            </button>\n          </div>\n        ) : null}\n      </div>\n      <button\n        type=\"button\"\n        aria-label=\"Close\"\n        className={cn(\n          TOAST_BUTTON_CLASS,\n          \"absolute top-2 right-2 p-0.5 text-white/40 hover:text-white\",\n        )}\n        onClick={() => onDismiss(id)}\n      >\n        <X aria-hidden className=\"size-3.5\" />\n      </button>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "categories": [
    "calendar",
    "events"
  ],
  "type": "registry:component"
}