vanilla-agent 0.1.0 → 0.2.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/dist/index.cjs +4 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.global.js +11 -11
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/runtime/init.ts +22 -0
package/package.json
CHANGED
package/src/runtime/init.ts
CHANGED
|
@@ -32,6 +32,26 @@ const widgetCssHref = (): string | null => {
|
|
|
32
32
|
const mountStyles = (root: ShadowRoot | HTMLElement) => {
|
|
33
33
|
const href = widgetCssHref();
|
|
34
34
|
|
|
35
|
+
const adoptExistingStylesheet = () => {
|
|
36
|
+
if (!(root instanceof ShadowRoot)) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (root.querySelector('link[data-vanilla-agent]')) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const globalLink = document.head.querySelector<HTMLLinkElement>(
|
|
45
|
+
'link[data-vanilla-agent]'
|
|
46
|
+
);
|
|
47
|
+
if (!globalLink) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const clonedLink = globalLink.cloneNode(true) as HTMLLinkElement;
|
|
52
|
+
root.insertBefore(clonedLink, root.firstChild);
|
|
53
|
+
};
|
|
54
|
+
|
|
35
55
|
if (root instanceof ShadowRoot) {
|
|
36
56
|
// For shadow DOM, we need to load CSS into the shadow root
|
|
37
57
|
if (href) {
|
|
@@ -40,6 +60,8 @@ const mountStyles = (root: ShadowRoot | HTMLElement) => {
|
|
|
40
60
|
link.href = href;
|
|
41
61
|
link.setAttribute("data-vanilla-agent", "true");
|
|
42
62
|
root.insertBefore(link, root.firstChild);
|
|
63
|
+
} else {
|
|
64
|
+
adoptExistingStylesheet();
|
|
43
65
|
}
|
|
44
66
|
// If href is null (IIFE build), CSS should already be loaded globally
|
|
45
67
|
} else {
|