UNPKG

tldraw

Version:

A tiny little drawing editor.

40 lines (39 loc) 1.18 kB
import { jsx } from "react/jsx-runtime"; import { ToastProvider } from "@radix-ui/react-toast"; import { uniqueId, useAtom } from "@tldraw/editor"; import { createContext, useContext, useMemo } from "react"; const ToastsContext = createContext(null); function TldrawUiToastsProvider({ children }) { const toasts = useAtom("toasts", []); const current = useMemo(() => { return { toasts, addToast(toast) { const id = toast.id ?? uniqueId(); toasts.update((d) => [...d.filter((m) => m.id !== toast.id), { ...toast, id }]); return id; }, removeToast(id) { toasts.update((d) => d.filter((m) => m.id !== id)); return id; }, clearToasts() { toasts.set([]); } }; }, [toasts]); return /* @__PURE__ */ jsx(ToastProvider, { children: /* @__PURE__ */ jsx(ToastsContext.Provider, { value: current, children }) }); } function useToasts() { const ctx = useContext(ToastsContext); if (!ctx) { throw new Error("useToasts must be used within a ToastsProvider"); } return ctx; } export { TldrawUiToastsProvider, ToastsContext, useToasts }; //# sourceMappingURL=toasts.mjs.map