UNPKG

@yamada-ui/react

Version:

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

1 lines 5.01 kB
{"version":3,"file":"use-notice.cjs","names":["useNoticeContext","useSystem","options","toast","NoticeItem"],"sources":["../../../../src/components/notice/use-notice.tsx"],"sourcesContent":["\"use client\"\n\nimport type { ReactNode } from \"react\"\nimport type { HTMLStyledProps, NoticeConfig } from \"../../core\"\nimport type { Alert } from \"../alert\"\nimport type { CloseButtonProps } from \"../close-button\"\nimport type { StatusScheme } from \"../status\"\nimport { useCallback, useMemo } from \"react\"\nimport { toast } from \"sonner\"\nimport { useSystem } from \"../../core\"\nimport { isArray, omitObject } from \"../../utils\"\nimport { NoticeItem } from \"./notice\"\nimport { useNoticeContext } from \"./notice-provider\"\n\nexport interface UseNoticeOptions\n extends Omit<NoticeConfig, \"expand\">,\n Omit<Alert.RootProps, \"direction\" | \"id\" | \"status\" | \"title\">,\n Pick<Alert.LoadingProps, \"loadingScheme\"> {\n /**\n * Unique identifier for the notice.\n */\n id?: number | string\n /**\n * The description of the notice.\n */\n description?: ReactNode\n /**\n * The status of the notice.\n */\n status?: StatusScheme\n /**\n * The title of the notice.\n */\n title?: ReactNode\n /**\n * If `true`, shows an icon.\n */\n withIcon?: boolean\n /**\n * Props for the close button.\n */\n closeButtonProps?: CloseButtonProps\n /**\n * Props for the content container.\n */\n contentProps?: HTMLStyledProps\n /**\n * Props for the description.\n */\n descriptionProps?: Alert.DescriptionProps\n /**\n * Props for the icon.\n */\n iconProps?: Alert.IconProps\n /**\n * Props for the loading component.\n */\n loadingProps?: Alert.LoadingProps\n /**\n * Props for the title.\n */\n titleProps?: Alert.TitleProps\n}\n\nexport interface NoticeComponentProps {\n onClose: () => void\n}\n\n/**\n * `useNotice` is a custom hook that controls the notifications of the application using Sonner.\n *\n * @see https://yamada-ui.com/docs/hooks/use-notice\n */\nexport const useNotice = (options: UseNoticeOptions = {}) => {\n const { getId, getLimit, updateLimit } = useNoticeContext()\n const { config } = useSystem()\n\n const systemOptions = useMemo(\n () => omitObject(config.notice ?? {}, [\"expand\"]),\n [config],\n )\n const defaultOptions = useMemo(\n () => ({ ...systemOptions, ...options }),\n [options, systemOptions],\n )\n\n const getOptions = useCallback(\n (options: UseNoticeOptions) => ({ ...defaultOptions, ...options }),\n [defaultOptions],\n )\n\n return useMemo(() => {\n const notice = (options: UseNoticeOptions = {}) => {\n options = getOptions(options)\n\n const {\n closable = true,\n closeStrategy = [\"click\", \"drag\"],\n duration,\n limit = 3,\n placement = \"start\",\n ...props\n } = options\n\n if (limit) {\n const currentLimit = getLimit(placement)\n\n if (currentLimit !== limit) updateLimit({ limit, placement })\n }\n\n const closeStrategies = isArray(closeStrategy)\n ? closeStrategy\n : [closeStrategy]\n\n const resolvedOptions = {\n dismissible: closeStrategies.includes(\"drag\") && closable,\n duration: duration ?? Number.POSITIVE_INFINITY,\n toasterId: getId(placement),\n }\n\n return toast.custom(\n (id) => (\n <NoticeItem\n {...props}\n id={id}\n closable={closable}\n closeStrategies={closeStrategies}\n />\n ),\n resolvedOptions,\n )\n }\n\n notice.close = (id: number | string) => toast.dismiss(id)\n\n notice.closeAll = () => toast.dismiss()\n\n notice.update = (id: number | string, options: UseNoticeOptions) => {\n toast.dismiss(id)\n\n return notice(options)\n }\n\n return notice\n }, [getLimit, getOptions, getId, updateLimit])\n}\n\nexport type UseNoticeReturn = ReturnType<typeof useNotice>\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAyEA,MAAa,aAAa,UAA4B,EAAE,KAAK;CAC3D,MAAM,EAAE,OAAO,UAAU,gBAAgBA,0CAAkB;CAC3D,MAAM,EAAE,WAAWC,mCAAW;CAE9B,MAAM,2FACa,OAAO,UAAU,EAAE,EAAE,CAAC,SAAS,CAAC,EACjD,CAAC,OAAO,CACT;CACD,MAAM,2CACG;EAAE,GAAG;EAAe,GAAG;EAAS,GACvC,CAAC,SAAS,cAAc,CACzB;CAED,MAAM,qCACH,eAA+B;EAAE,GAAG;EAAgB,GAAGC;EAAS,GACjE,CAAC,eAAe,CACjB;AAED,iCAAqB;EACnB,MAAM,UAAU,YAA4B,EAAE,KAAK;AACjD,eAAU,WAAWA,UAAQ;GAE7B,MAAM,EACJ,WAAW,MACX,gBAAgB,CAAC,SAAS,OAAO,EACjC,UACA,QAAQ,GACR,YAAY,QACZ,GAAG,UACDA;AAEJ,OAAI,OAGF;QAFqB,SAAS,UAAU,KAEnB,MAAO,aAAY;KAAE;KAAO;KAAW,CAAC;;GAG/D,MAAM,iEAA0B,cAAc,GAC1C,gBACA,CAAC,cAAc;GAEnB,MAAM,kBAAkB;IACtB,aAAa,gBAAgB,SAAS,OAAO,IAAI;IACjD,UAAU,YAAY,OAAO;IAC7B,WAAW,MAAM,UAAU;IAC5B;AAED,UAAOC,aAAM,QACV,OACC,2CAACC;IACC,GAAI;IACA;IACM;IACO;KACjB,EAEJ,gBACD;;AAGH,SAAO,SAAS,OAAwBD,aAAM,QAAQ,GAAG;AAEzD,SAAO,iBAAiBA,aAAM,SAAS;AAEvC,SAAO,UAAU,IAAqB,cAA8B;AAClE,gBAAM,QAAQ,GAAG;AAEjB,UAAO,OAAOD,UAAQ;;AAGxB,SAAO;IACN;EAAC;EAAU;EAAY;EAAO;EAAY,CAAC"}