@clivly/chat-widget 0.10.9 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +87 -4
- package/dist/browser-mount.cjs +126 -13
- package/dist/browser-mount.d.cts +25 -8
- package/dist/browser-mount.d.mts +25 -8
- package/dist/browser-mount.mjs +127 -14
- package/dist/browser.cjs +126 -13
- package/dist/browser.d.cts +25 -8
- package/dist/browser.d.mts +25 -8
- package/dist/browser.mjs +127 -14
- package/dist/index.cjs +126 -13
- package/dist/index.d.cts +37 -19
- package/dist/index.d.mts +37 -19
- package/dist/index.mjs +127 -14
- package/dist/widget.global.js +115 -83
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createRoot } from "react-dom/client";
|
|
2
|
-
import { useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
3
3
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
4
4
|
//#region src/theme.ts
|
|
5
5
|
const DARK_MEDIA_QUERY = "(prefers-color-scheme: dark)";
|
|
@@ -255,10 +255,42 @@ const WIDGET_STYLES = `
|
|
|
255
255
|
padding: 0.5rem 1rem;
|
|
256
256
|
text-align: center;
|
|
257
257
|
}
|
|
258
|
+
/* Only the name carries the gradient. "Powered by" is ordinary running text
|
|
259
|
+
and inherits the muted colour of the line, so the mark reads as a signature
|
|
260
|
+
rather than the whole sentence being branded. */
|
|
258
261
|
.clivly-chat-widget__powered a {
|
|
259
262
|
color: inherit;
|
|
260
263
|
text-decoration: none;
|
|
261
264
|
}
|
|
265
|
+
/* The attribution is Clivly's mark, not the host's, so it uses the stops from
|
|
266
|
+
the logo itself rather than --clivly-chat-accent: the accent is driven by
|
|
267
|
+
each customer's own brandColor, and a signature that changed colour per
|
|
268
|
+
customer would stop reading as a signature.
|
|
269
|
+
|
|
270
|
+
The color declaration is the fallback and must stay a real colour:
|
|
271
|
+
background-clip is painted over it, so anything that drops the background —
|
|
272
|
+
forced-colors, an older engine, a copied-out snippet — degrades to solid
|
|
273
|
+
coral rather than to invisible text. */
|
|
274
|
+
.clivly-chat-widget__powered-mark {
|
|
275
|
+
background-image: linear-gradient(90deg, #ff5a4a 0%, #c0231a 100%);
|
|
276
|
+
-webkit-background-clip: text;
|
|
277
|
+
background-clip: text;
|
|
278
|
+
color: #c0231a;
|
|
279
|
+
-webkit-text-fill-color: transparent;
|
|
280
|
+
}
|
|
281
|
+
/* Forced-colors mode strips background-image outright, which would leave the
|
|
282
|
+
text painted in a transparent fill. Hand the colour back to the UA so the
|
|
283
|
+
mark inherits the link colour the UA picked rather than re-pinning coral. */
|
|
284
|
+
@media (forced-colors: active) {
|
|
285
|
+
.clivly-chat-widget__powered a {
|
|
286
|
+
color: LinkText;
|
|
287
|
+
}
|
|
288
|
+
.clivly-chat-widget__powered-mark {
|
|
289
|
+
background-image: none;
|
|
290
|
+
color: currentColor;
|
|
291
|
+
-webkit-text-fill-color: currentColor;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
262
294
|
.clivly-chat-widget__powered a:hover,
|
|
263
295
|
.clivly-chat-widget__powered a:focus-visible {
|
|
264
296
|
text-decoration: underline;
|
|
@@ -482,7 +514,27 @@ function MessageList({ greeting, hasSession, messages }) {
|
|
|
482
514
|
});
|
|
483
515
|
return /* @__PURE__ */ jsx(Fragment, { children: messages.map((message) => /* @__PURE__ */ jsx(MessageBubble, { message }, message.id)) });
|
|
484
516
|
}
|
|
485
|
-
|
|
517
|
+
/**
|
|
518
|
+
* Whether a seeded visitor is complete enough to skip the form.
|
|
519
|
+
*
|
|
520
|
+
* Both fields have to be present and non-blank: the server requires each, so
|
|
521
|
+
* auto-starting on a half-filled identity would trade a form the visitor can
|
|
522
|
+
* complete for an error they cannot.
|
|
523
|
+
*/
|
|
524
|
+
function isCompleteVisitor(visitor) {
|
|
525
|
+
return Boolean(visitor?.name?.trim() && visitor?.email?.trim());
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* What the composer says about itself. "Connecting…" claims a socket is in
|
|
529
|
+
* flight, so it is as wrong as "Conversation ready" when no transport exists —
|
|
530
|
+
* the visitor is told to wait for something that will never arrive. The third
|
|
531
|
+
* string keeps the status honest in all three states.
|
|
532
|
+
*/
|
|
533
|
+
function composerStatusText(hasTransport, isReady) {
|
|
534
|
+
if (!hasTransport) return "Chat is unavailable";
|
|
535
|
+
return isReady ? "Conversation ready" : "Connecting…";
|
|
536
|
+
}
|
|
537
|
+
function ChatWidget({ className, createTransport, defaultVisitor, getSession, initiallyOpen = false, launcherLabel = "Chat with us", storage = DEFAULT_STORAGE, style, theme = "auto", widgetId }) {
|
|
486
538
|
const [isOpen, setIsOpen] = useState(initiallyOpen);
|
|
487
539
|
const [error, setError] = useState(null);
|
|
488
540
|
const [isConnecting, setIsConnecting] = useState(false);
|
|
@@ -491,11 +543,15 @@ function ChatWidget({ className, createTransport, getSession, initiallyOpen = fa
|
|
|
491
543
|
const [messages, setMessages] = useState([]);
|
|
492
544
|
const [session, setSession] = useState(null);
|
|
493
545
|
const [visitor, setVisitor] = useState({
|
|
494
|
-
email: "",
|
|
495
|
-
name: ""
|
|
546
|
+
email: defaultVisitor?.email ?? "",
|
|
547
|
+
name: defaultVisitor?.name ?? ""
|
|
496
548
|
});
|
|
497
549
|
const transportRef = useRef(null);
|
|
498
550
|
const listRef = useRef(null);
|
|
551
|
+
useEffect(() => {
|
|
552
|
+
if (createTransport) return;
|
|
553
|
+
console.warn("[clivly] <ChatWidget> was given no createTransport, so messages cannot be sent. Pass createTransport={createClivlyTransport()} — see https://www.npmjs.com/package/@clivly/chat-widget#five-minute-quick-start");
|
|
554
|
+
}, []);
|
|
499
555
|
const themeStyle = useThemeStyle(session, style);
|
|
500
556
|
const resolvedTheme = useResolvedTheme(theme);
|
|
501
557
|
useEffect(() => () => {
|
|
@@ -538,15 +594,13 @@ function ChatWidget({ className, createTransport, getSession, initiallyOpen = fa
|
|
|
538
594
|
if (transportRef.current === connection) transportRef.current = null;
|
|
539
595
|
};
|
|
540
596
|
}, [createTransport, session]);
|
|
541
|
-
const
|
|
542
|
-
event.preventDefault();
|
|
597
|
+
const startSession = useCallback(async (identity) => {
|
|
543
598
|
setError(null);
|
|
544
599
|
setIsConnecting(true);
|
|
545
600
|
try {
|
|
546
|
-
const existingVisitorToken = storage?.getItem(createStorageKey(widgetId)) ?? null;
|
|
547
601
|
const nextSession = await getSession({
|
|
548
|
-
visitor,
|
|
549
|
-
visitorToken:
|
|
602
|
+
visitor: identity,
|
|
603
|
+
visitorToken: storage?.getItem(createStorageKey(widgetId)) ?? null,
|
|
550
604
|
widgetId
|
|
551
605
|
});
|
|
552
606
|
storage?.setItem(createStorageKey(widgetId), nextSession.visitorToken);
|
|
@@ -558,7 +612,30 @@ function ChatWidget({ className, createTransport, getSession, initiallyOpen = fa
|
|
|
558
612
|
} finally {
|
|
559
613
|
setIsConnecting(false);
|
|
560
614
|
}
|
|
615
|
+
}, [
|
|
616
|
+
getSession,
|
|
617
|
+
storage,
|
|
618
|
+
widgetId
|
|
619
|
+
]);
|
|
620
|
+
const submitVisitor = async (event) => {
|
|
621
|
+
event.preventDefault();
|
|
622
|
+
await startSession(visitor);
|
|
561
623
|
};
|
|
624
|
+
useEffect(() => {
|
|
625
|
+
if (!(isOpen && isCompleteVisitor(defaultVisitor))) return;
|
|
626
|
+
if (session || isConnecting || error) return;
|
|
627
|
+
startSession({
|
|
628
|
+
email: defaultVisitor.email.trim(),
|
|
629
|
+
name: defaultVisitor.name.trim()
|
|
630
|
+
}).catch(() => {});
|
|
631
|
+
}, [
|
|
632
|
+
defaultVisitor,
|
|
633
|
+
error,
|
|
634
|
+
isConnecting,
|
|
635
|
+
isOpen,
|
|
636
|
+
session,
|
|
637
|
+
startSession
|
|
638
|
+
]);
|
|
562
639
|
const submitMessage = (event) => {
|
|
563
640
|
event?.preventDefault();
|
|
564
641
|
if (!(session && transportRef.current)) {
|
|
@@ -590,6 +667,7 @@ function ChatWidget({ className, createTransport, getSession, initiallyOpen = fa
|
|
|
590
667
|
setError(resolved.message);
|
|
591
668
|
}
|
|
592
669
|
};
|
|
670
|
+
const canSend = Boolean(createTransport) && isReady;
|
|
593
671
|
const greeting = session?.widget.config.greeting ?? "Tell us a little about yourself and we’ll pick up the conversation from there.";
|
|
594
672
|
const panelTitle = session?.widget.name ?? "Start a conversation";
|
|
595
673
|
const sessionSummary = sessionSummaryText(session, greeting);
|
|
@@ -657,7 +735,7 @@ function ChatWidget({ className, createTransport, getSession, initiallyOpen = fa
|
|
|
657
735
|
onKeyDown: (event) => {
|
|
658
736
|
if (!isSendKey(event)) return;
|
|
659
737
|
event.preventDefault();
|
|
660
|
-
if (messageDraft.trim() &&
|
|
738
|
+
if (messageDraft.trim() && canSend) submitMessage();
|
|
661
739
|
},
|
|
662
740
|
placeholder: "Write your message",
|
|
663
741
|
value: messageDraft
|
|
@@ -666,10 +744,10 @@ function ChatWidget({ className, createTransport, getSession, initiallyOpen = fa
|
|
|
666
744
|
className: "clivly-chat-widget__actions",
|
|
667
745
|
children: [/* @__PURE__ */ jsx("span", {
|
|
668
746
|
className: "clivly-chat-widget__subtitle",
|
|
669
|
-
children:
|
|
747
|
+
children: composerStatusText(Boolean(createTransport), isReady)
|
|
670
748
|
}), /* @__PURE__ */ jsx("button", {
|
|
671
749
|
className: "clivly-chat-widget__button",
|
|
672
|
-
disabled: !(messageDraft.trim() &&
|
|
750
|
+
disabled: !(messageDraft.trim() && canSend),
|
|
673
751
|
type: "submit",
|
|
674
752
|
children: "Send"
|
|
675
753
|
})]
|
|
@@ -731,11 +809,18 @@ function ChatWidget({ className, createTransport, getSession, initiallyOpen = fa
|
|
|
731
809
|
}),
|
|
732
810
|
/* @__PURE__ */ jsx("p", {
|
|
733
811
|
className: "clivly-chat-widget__powered",
|
|
734
|
-
children: /* @__PURE__ */
|
|
812
|
+
children: /* @__PURE__ */ jsxs("a", {
|
|
735
813
|
href: "https://clivly.com",
|
|
736
814
|
rel: "noopener",
|
|
737
815
|
target: "_blank",
|
|
738
|
-
children:
|
|
816
|
+
children: [
|
|
817
|
+
"Powered by",
|
|
818
|
+
" ",
|
|
819
|
+
/* @__PURE__ */ jsx("span", {
|
|
820
|
+
className: "clivly-chat-widget__powered-mark",
|
|
821
|
+
children: "Clivly"
|
|
822
|
+
})
|
|
823
|
+
]
|
|
739
824
|
})
|
|
740
825
|
})
|
|
741
826
|
]
|
|
@@ -898,6 +983,33 @@ function createSessionFetcher({ fetchImpl = fetch, sessionUrl }) {
|
|
|
898
983
|
//#endregion
|
|
899
984
|
//#region src/browser-mount.tsx
|
|
900
985
|
const MOUNTED_DATA_ATTR = "clivlyMounted";
|
|
986
|
+
/**
|
|
987
|
+
* The identity a script-tag host already knows, read off the tag.
|
|
988
|
+
*
|
|
989
|
+
* The React component takes `defaultVisitor` as a prop; a script tag has only
|
|
990
|
+
* strings, so `data-visitor-name` / `data-visitor-email` are the same seam. A
|
|
991
|
+
* host that server-renders its page can template the signed-in user's details
|
|
992
|
+
* straight into the tag and the visitor never sees the pre-chat form.
|
|
993
|
+
*
|
|
994
|
+
* Returns `undefined` rather than an empty object when neither attribute is
|
|
995
|
+
* present, so the component's own "no identity" path stays untouched. Partial
|
|
996
|
+
* values are passed through deliberately: `ChatWidget` treats one field as a
|
|
997
|
+
* prefill and still renders the form, which is the right outcome for a host
|
|
998
|
+
* that knows an email but not a name.
|
|
999
|
+
*
|
|
1000
|
+
* This is self-reported identity — anyone can edit the attribute in devtools.
|
|
1001
|
+
* `resolveUser` on the host backend is what makes it trustworthy; this only
|
|
1002
|
+
* removes the typing.
|
|
1003
|
+
*/
|
|
1004
|
+
function readScriptVisitor(dataset) {
|
|
1005
|
+
const email = dataset.visitorEmail?.trim();
|
|
1006
|
+
const name = dataset.visitorName?.trim();
|
|
1007
|
+
if (!(email || name)) return;
|
|
1008
|
+
return {
|
|
1009
|
+
...email ? { email } : {},
|
|
1010
|
+
...name ? { name } : {}
|
|
1011
|
+
};
|
|
1012
|
+
}
|
|
901
1013
|
function resolveSocketUrl(socketUrlTemplate, conversationId, token) {
|
|
902
1014
|
return socketUrlTemplate.replaceAll("{conversationId}", encodeURIComponent(conversationId)).replaceAll("{token}", encodeURIComponent(token));
|
|
903
1015
|
}
|
|
@@ -942,6 +1054,7 @@ function mountChatWidgetScript$1(script = document.currentScript) {
|
|
|
942
1054
|
shadowRoot.append(container);
|
|
943
1055
|
const { root, setTheme } = createWidgetMount({
|
|
944
1056
|
container,
|
|
1057
|
+
defaultVisitor: readScriptVisitor(script.dataset),
|
|
945
1058
|
sessionUrl,
|
|
946
1059
|
socketUrlTemplate: script.dataset.socketUrlTemplate,
|
|
947
1060
|
theme: parseTheme(script.dataset.theme),
|