UNPKG

@yamada-ui/react

Version:

React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion

70 lines (66 loc) 2.13 kB
"use client"; import { utils_exports } from "../../utils/index.js"; import { useSystem } from "../../core/system/system-provider.js"; import { NoticeItem } from "./notice.js"; import { useNoticeContext } from "./notice-provider.js"; import { useCallback, useMemo } from "react"; import { jsx } from "react/jsx-runtime"; import { toast } from "sonner"; //#region src/components/notice/use-notice.tsx /** * `useNotice` is a custom hook that controls the notifications of the application using Sonner. * * @see https://yamada-ui.com/docs/hooks/use-notice */ const useNotice = (options = {}) => { const { getId, getLimit, updateLimit } = useNoticeContext(); const { config } = useSystem(); const systemOptions = useMemo(() => (0, utils_exports.omitObject)(config.notice ?? {}, ["expand"]), [config]); const defaultOptions = useMemo(() => ({ ...systemOptions, ...options }), [options, systemOptions]); const getOptions = useCallback((options$1) => ({ ...defaultOptions, ...options$1 }), [defaultOptions]); return useMemo(() => { const notice = (options$1 = {}) => { options$1 = getOptions(options$1); const { closable = true, closeStrategy = ["click", "drag"], duration, limit = 3, placement = "start",...props } = options$1; if (limit) { if (getLimit(placement) !== limit) updateLimit({ limit, placement }); } const closeStrategies = (0, utils_exports.isArray)(closeStrategy) ? closeStrategy : [closeStrategy]; const resolvedOptions = { dismissible: closeStrategies.includes("drag") && closable, duration: duration ?? Number.POSITIVE_INFINITY, toasterId: getId(placement) }; return toast.custom((id) => /* @__PURE__ */ jsx(NoticeItem, { ...props, id, closable, closeStrategies }), resolvedOptions); }; notice.close = (id) => toast.dismiss(id); notice.closeAll = () => toast.dismiss(); notice.update = (id, options$1) => { toast.dismiss(id); return notice(options$1); }; return notice; }, [ getLimit, getOptions, getId, updateLimit ]); }; //#endregion export { useNotice }; //# sourceMappingURL=use-notice.js.map