UNPKG

react-chat-ai-widget

Version:

Enhance user experience with a smart widget that combines AI assistants and live chat. Automate support, stay available 24/7, and boost customer engagement effortlessly.

190 lines (189 loc) 6.3 kB
import { jsx as t, jsxs as i, Fragment as d } from "react/jsx-runtime"; import s, { useMemo as u, useRef as j, useEffect as m, useState as b, useCallback as p, Suspense as D, lazy as R } from "react"; import { createPortal as V } from "react-dom"; const g = s.createContext(null), B = ({ children: e, direction: r = "left", onClose: o = () => { }, ...a }) => { const n = u(() => ({ onClose: o, direction: r || "left", ...a }), [a, r, o]); return /* @__PURE__ */ t(g.Provider, { value: n, children: e }); }, A = () => { const e = s.useContext(g), r = s.useMemo(() => e, [e]); if (r === null) throw new Error("useChatContext must be used within a `Context` provider"); return r; }, O = () => { const e = j(null); return m(() => { if (e.current = document.getElementById("portal"), !e.current) { const r = document.createElement("div"); r.setAttribute("id", "portal"), document.body.appendChild(r), e.current = document.getElementById("portal"); } }, []), e.current; }, W = { bubble: "bottom-4 left-14", chatInterface: "bottom-16 left-24" }, P = { bubble: "right-6 bottom-6", chatInterface: "bottom-16 right-32" }, x = (e) => ({ right: P, left: W })[e] ?? { bubble: "", chatInterface: "" }, Z = ({ onClick: e }) => /* @__PURE__ */ i( "svg", { role: "opener-chat", onClick: e, width: "35px", "aria-label": "opener-chat", height: "35px", strokeWidth: "1.5", viewBox: "0 0 24 24", fill: "none", xmlns: "http://www.w3.org/2000/svg", color: "#7e7e7e", children: [ /* @__PURE__ */ t( "path", { d: "M9 9C9 5.49997 14.5 5.5 14.5 9C14.5 11.5 12 10.9999 12 13.9999", stroke: "#414141", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round" } ), /* @__PURE__ */ t( "path", { d: "M12 18.01L12.01 17.9989", stroke: "#414141", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round" } ), /* @__PURE__ */ t( "path", { d: "M12 22C17.5228 22 22 17.5228 22 12C22 6.47715 17.5228 2 12 2C6.47715 2 2 6.47715 2 12C2 13.8214 2.48697 15.5291 3.33782 17L2.5 21.5L7 20.6622C8.47087 21.513 10.1786 22 12 22Z", stroke: "#414141", strokeWidth: "1.5", strokeLinecap: "round", strokeLinejoin: "round" } ) ] } ), w = () => /* @__PURE__ */ t("div", { role: "loading-chat", children: /* @__PURE__ */ i( "svg", { "aria-hidden": "true", className: "w-8 h-8 text-gray-200 animate-spin dark:text-gray-600 fill-slate-400", viewBox: "0 0 100 101", fill: "none", xmlns: "http://www.w3.org/2000/svg", children: [ /* @__PURE__ */ t( "path", { d: "M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z", fill: "currentColor" } ), /* @__PURE__ */ t( "path", { d: "M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z", fill: "currentFill" } ) ] } ) }), F = ({ isOpen: e, direction: r, handleClick: o, isLoading: a, Loader: n }) => { const c = u( () => x(r), [r] ), l = s.isValidElement(n); return /* @__PURE__ */ i( "div", { className: `w-16 h-16 flex justify-center items-center rounded-full cursor-pointer fixed ${c.bubble} -translate-x-1/2 bg-white shadow-lg border border-gray-200 transition-all duration-300 ease-out hover:scale-110 `, "data-cy": "default-opener", tabIndex: 0, role: "button", "aria-label": "Open chat", "aria-pressed": e, children: [ a && l && /* @__PURE__ */ t(d, { children: n }), a && !n && /* @__PURE__ */ t(w, {}), !a && /* @__PURE__ */ t(Z, { onClick: o }) ] } ); }, N = R(() => import("./ChatInterface-BBw8u0yo.js")), q = ({ direction: e = "left", children: r, Loader: o, ...a }) => { const [n, c] = b(!1), [l, h] = b(!1), C = p(() => { c((M) => !M); }, []), k = p(() => c(!1), []), v = u( () => x(e), [e] ), f = O(), y = s.isValidElement(r), I = s.isValidElement(o), L = () => (m(() => (h(!0), () => h(!1)), []), /* @__PURE__ */ t(d, {})), E = () => !n || !f ? null : /* @__PURE__ */ t(D, { fallback: /* @__PURE__ */ t(L, {}), children: V(/* @__PURE__ */ t(N, { isOpen: n }), f) }); return /* @__PURE__ */ i(d, { children: [ y ? /* @__PURE__ */ i( "div", { className: `w-16 h-16 flex justify-center items-center rounded-full cursor-pointer fixed ${v.bubble} -translate-x-1/2 bg-white shadow-lg border border-gray-200 transition-all duration-300 ease-out hover:scale-110 `, tabIndex: 0, role: "button", "aria-pressed": n, onClick: C, "data-testid": "open-chat-children", "aria-label": "Open chat", children: [ l && I && /* @__PURE__ */ t(d, { children: o }), l && !o && /* @__PURE__ */ t(w, {}), !l && r ] } ) : /* @__PURE__ */ t( F, { Loader: o, isOpen: n, isLoading: l, direction: e, handleClick: C } ), /* @__PURE__ */ t(B, { direction: e, onClose: k, ...a, children: E() }) ] }); }; export { q as C, x as g, A as u };