UNPKG

react-notification-npm

Version:

react notification library

1 lines 5.39 kB
{"version":3,"file":"index.mjs","sources":["../node_modules/style-inject/dist/style-inject.es.js","../src/hooks/useNotification.tsx"],"sourcesContent":["function styleInject(css, ref) {\n if ( ref === void 0 ) ref = {};\n var insertAt = ref.insertAt;\n\n if (!css || typeof document === 'undefined') { return; }\n\n var head = document.head || document.getElementsByTagName('head')[0];\n var style = document.createElement('style');\n style.type = 'text/css';\n\n if (insertAt === 'top') {\n if (head.firstChild) {\n head.insertBefore(style, head.firstChild);\n } else {\n head.appendChild(style);\n }\n } else {\n head.appendChild(style);\n }\n\n if (style.styleSheet) {\n style.styleSheet.cssText = css;\n } else {\n style.appendChild(document.createTextNode(css));\n }\n}\n\nexport default styleInject;\n","import styles from \"./notification.module.scss\";\r\nimport { useCallback, useMemo, useState } from \"react\";\r\nimport { colord } from \"colord\";\r\nimport {\r\n AiOutlineCheckCircle,\r\n AiOutlineClose,\r\n AiOutlineCloseCircle,\r\n AiOutlineInfoCircle,\r\n AiOutlineWarning,\r\n} from \"react-icons/ai\";\r\n\r\ntype ToastProps = {\r\n id: number;\r\n text: string;\r\n type?: \"warn\" | \"error\" | \"success\" | \"info\";\r\n};\r\n\r\nconst colors = {\r\n warn: \"#FEEC37\",\r\n success: \"#C2FFC7\",\r\n error: \"#FF748B\",\r\n info: \"#C4D9FF\",\r\n};\r\n\r\nconst icons = {\r\n success: <AiOutlineCheckCircle />,\r\n info: <AiOutlineInfoCircle />,\r\n warn: <AiOutlineWarning />,\r\n error: <AiOutlineCloseCircle />,\r\n};\r\n\r\nconst useNotification = (duration: number = 3000) => {\r\n const [toasts, setToasts] = useState<ToastProps[]>([]);\r\n\r\n const trigger = useCallback(\r\n (text: string, type: ToastProps[\"type\"] = \"success\") => {\r\n const id = Date.now();\r\n setToasts((prev) => [...prev, { id, text, type }]);\r\n\r\n setTimeout(() => {\r\n setToasts((prev) => prev.filter((toast) => toast.id !== id));\r\n }, duration + 500);\r\n },\r\n [duration]\r\n );\r\n\r\n const Toasts = useMemo(() => {\r\n if (toasts.length === 0) return null;\r\n\r\n return (\r\n <>\r\n {toasts.map(({ id, text, type = \"success\" }, index) => {\r\n const notificationColor = colors[type];\r\n return (\r\n <section\r\n role=\"alert\"\r\n key={id}\r\n className={`${styles[\"notification\"]} ${styles[\"notification-open\"]}`}\r\n style={{\r\n top: 20 + index * 50,\r\n backgroundColor: notificationColor,\r\n borderColor: colord(notificationColor).darken(0.1).toHex(),\r\n }}>\r\n <div className={styles[\"notification-text\"]}>\r\n {icons[type]}\r\n {text}\r\n </div>\r\n <AiOutlineClose\r\n onClick={() =>\r\n setToasts((prev) => prev.filter((toast) => toast.id !== id))\r\n }\r\n fill=\"currentColor\"\r\n style={{ cursor: \"pointer\" }}\r\n />\r\n </section>\r\n );\r\n })}\r\n </>\r\n );\r\n }, [toasts]);\r\n\r\n return { trigger, Toasts };\r\n};\r\n\r\nexport default useNotification;\r\n"],"names":["css","ref","insertAt","document","head","getElementsByTagName","style","createElement","type","firstChild","insertBefore","appendChild","styleSheet","cssText","createTextNode","colors","warn","success","error","info","icons","_jsx","AiOutlineCheckCircle","AiOutlineInfoCircle","AiOutlineWarning","AiOutlineCloseCircle","useNotification","duration","toasts","setToasts","useState","trigger","useCallback","text","id","Date","now","prev","setTimeout","filter","toast","Toasts","useMemo","length","_Fragment","children","map","index","notificationColor","_jsxs","role","className","styles","top","backgroundColor","borderColor","colord","darken","toHex","AiOutlineClose","onClick","fill","cursor"],"mappings":"qcAAA,SAAqBA,EAAKC,QACX,IAARA,IAAiBA,EAAM,CAAE,GAC9B,IAAIC,EAAWD,EAAIC,SAEnB,GAAgC,oBAAbC,SAAnB,CAEA,IAAIC,EAAOD,SAASC,MAAQD,SAASE,qBAAqB,QAAQ,GAC9DC,EAAQH,SAASI,cAAc,SACnCD,EAAME,KAAO,WAEI,QAAbN,GACEE,EAAKK,WACPL,EAAKM,aAAaJ,EAAOF,EAAKK,YAKhCL,EAAKO,YAAYL,GAGfA,E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