@squeletteapp/widget 0.1.0 → 0.3.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 +112 -0
- package/dist/app-BpPatA6a.js +42 -0
- package/dist/index-B0eQqkb_.js +7951 -0
- package/dist/types/app/app.d.ts +15 -0
- package/dist/types/banner/banner-modal.d.ts +15 -0
- package/dist/types/banner/banner.d.ts +14 -0
- package/dist/types/banner/element.d.ts +7 -0
- package/dist/types/banner/index.d.ts +19 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/utils.d.ts +2 -0
- package/dist/types/widget/__tests__/widget.test.d.ts +1 -0
- package/dist/types/widget/components/widget-shell.d.ts +8 -0
- package/dist/types/widget/constants.d.ts +9 -0
- package/dist/types/widget/core/custom-element.d.ts +142 -0
- package/dist/types/widget/core/global-api.d.ts +97 -0
- package/dist/types/widget/core/identity.d.ts +73 -0
- package/dist/types/widget/core/panel-dimensions.d.ts +36 -0
- package/dist/types/widget/core/positioning.d.ts +63 -0
- package/dist/types/widget/core/scroll-lock.d.ts +28 -0
- package/dist/types/widget/element.d.ts +90 -0
- package/dist/types/widget/events.d.ts +13 -0
- package/dist/types/widget/factories.d.ts +41 -0
- package/dist/types/widget/styles.d.ts +8 -0
- package/dist/types/widget/types.d.ts +79 -0
- package/dist/types/widget/utils.d.ts +12 -0
- package/dist/widget.es.js +14 -0
- package/dist/widget.js +168 -15
- package/package.json +32 -18
- package/dist/button-component.d.ts +0 -2
- package/dist/button-component.js +0 -158
- package/dist/index.d.ts +0 -26
- package/dist/index.js +0 -230
- package/dist/styles.css +0 -567
- package/dist/widget.d.ts +0 -42
package/package.json
CHANGED
|
@@ -1,39 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@squeletteapp/widget",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Embeddable Squelette widget",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/widget.js",
|
|
7
|
+
"module": "dist/widget.es.js",
|
|
8
|
+
"types": "dist/types/index.d.ts",
|
|
6
9
|
"exports": {
|
|
7
10
|
".": {
|
|
8
|
-
"types": "./dist/index.d.ts",
|
|
9
|
-
"
|
|
11
|
+
"types": "./dist/types/index.d.ts",
|
|
12
|
+
"import": "./dist/widget.es.js",
|
|
13
|
+
"default": "./dist/widget.js"
|
|
10
14
|
}
|
|
11
15
|
},
|
|
12
16
|
"files": [
|
|
13
17
|
"dist"
|
|
14
18
|
],
|
|
15
19
|
"scripts": {
|
|
16
|
-
"
|
|
17
|
-
"build
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"
|
|
20
|
+
"dev": "vite",
|
|
21
|
+
"build": "rimraf dist && tsc --emitDeclarationOnly && vite build",
|
|
22
|
+
"preview": "vite preview",
|
|
23
|
+
"test": "vitest run",
|
|
24
|
+
"test:watch": "vitest"
|
|
21
25
|
},
|
|
22
26
|
"keywords": [
|
|
23
27
|
"widget",
|
|
24
28
|
"feedback",
|
|
25
29
|
"squelette",
|
|
26
|
-
"
|
|
30
|
+
"web-component",
|
|
31
|
+
"preact"
|
|
27
32
|
],
|
|
28
33
|
"author": "Squelette",
|
|
29
34
|
"license": "MIT",
|
|
30
|
-
"
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@preact/signals": "^2.3.2",
|
|
37
|
+
"clsx": "^2.1.1",
|
|
38
|
+
"motion": "^12.23.24",
|
|
39
|
+
"object-hash": "^3.0.0",
|
|
40
|
+
"preact": "^10.27.2",
|
|
41
|
+
"tailwind-merge": "^3.3.1"
|
|
42
|
+
},
|
|
31
43
|
"devDependencies": {
|
|
32
|
-
"autoprefixer": "^10.4.
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"tailwindcss": "^3.4.
|
|
37
|
-
"typescript": "^5.
|
|
44
|
+
"autoprefixer": "^10.4.20",
|
|
45
|
+
"@preact/preset-vite": "^2.10.2",
|
|
46
|
+
"happy-dom": "^13.10.1",
|
|
47
|
+
"rimraf": "^6.0.1",
|
|
48
|
+
"tailwindcss": "^3.4.13",
|
|
49
|
+
"typescript": "^5.6.3",
|
|
50
|
+
"vite": "^5.4.8",
|
|
51
|
+
"vitest": "^1.6.0"
|
|
38
52
|
}
|
|
39
53
|
}
|
package/dist/button-component.js
DELETED
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SqueletteWidgetButton = void 0;
|
|
4
|
-
const svgIcon = `
|
|
5
|
-
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-gift-icon lucide-gift"><rect x="3" y="8" width="18" height="4" rx="1"/><path d="M12 8v13"/><path d="M19 12v7a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2v-7"/><path d="M7.5 8a2.5 2.5 0 0 1 0-5A4.8 8 0 0 1 12 8a4.8 8 0 0 1 4.5-5 2.5 2.5 0 0 1 0 5"/></svg>
|
|
6
|
-
`;
|
|
7
|
-
// Define the class only in browser environment
|
|
8
|
-
let SqueletteWidgetButton = null;
|
|
9
|
-
exports.SqueletteWidgetButton = SqueletteWidgetButton;
|
|
10
|
-
if (typeof window !== "undefined" && typeof HTMLElement !== "undefined") {
|
|
11
|
-
exports.SqueletteWidgetButton = SqueletteWidgetButton = class extends HTMLElement {
|
|
12
|
-
constructor() {
|
|
13
|
-
super();
|
|
14
|
-
this.isOpen = false;
|
|
15
|
-
this.attachShadow({ mode: "open" });
|
|
16
|
-
}
|
|
17
|
-
connectedCallback() {
|
|
18
|
-
this.render();
|
|
19
|
-
this.setupEventListeners();
|
|
20
|
-
}
|
|
21
|
-
disconnectedCallback() {
|
|
22
|
-
// Cleanup if needed
|
|
23
|
-
}
|
|
24
|
-
static get observedAttributes() {
|
|
25
|
-
return ["data-open"];
|
|
26
|
-
}
|
|
27
|
-
attributeChangedCallback(name, oldValue, newValue) {
|
|
28
|
-
if (name === "data-open") {
|
|
29
|
-
this.isOpen = newValue === "true";
|
|
30
|
-
this.updateButtonText();
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
setToggleHandler(handler) {
|
|
34
|
-
this.onToggle = handler;
|
|
35
|
-
}
|
|
36
|
-
toggle() {
|
|
37
|
-
this.isOpen = !this.isOpen;
|
|
38
|
-
this.setAttribute("data-open", this.isOpen.toString());
|
|
39
|
-
this.updateButtonText();
|
|
40
|
-
this.onToggle?.(this.isOpen);
|
|
41
|
-
}
|
|
42
|
-
render() {
|
|
43
|
-
if (!this.shadowRoot)
|
|
44
|
-
return;
|
|
45
|
-
// Create style element
|
|
46
|
-
const style = document.createElement("style");
|
|
47
|
-
style.textContent = `
|
|
48
|
-
@keyframes slideInFromBottom {
|
|
49
|
-
from {
|
|
50
|
-
transform: translateY(100px);
|
|
51
|
-
opacity: 0;
|
|
52
|
-
}
|
|
53
|
-
to {
|
|
54
|
-
transform: translateY(0);
|
|
55
|
-
opacity: 1;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
:host {
|
|
60
|
-
position: fixed;
|
|
61
|
-
bottom: 20px;
|
|
62
|
-
right: 20px;
|
|
63
|
-
z-index: 9998;
|
|
64
|
-
animation: slideInFromBottom 0.6s cubic-bezier(0.16, 1, 0.3, 1) forwards;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
.widget-button {
|
|
68
|
-
border-radius: 9999px;
|
|
69
|
-
height: 36px;
|
|
70
|
-
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
|
|
71
|
-
border: 1px solid #e5e7eb;
|
|
72
|
-
font-size: 14px;
|
|
73
|
-
display: flex;
|
|
74
|
-
align-items: center;
|
|
75
|
-
background-color: #1f2937;
|
|
76
|
-
color: white;
|
|
77
|
-
justify-content: center;
|
|
78
|
-
transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
|
|
79
|
-
padding-left: 12px;
|
|
80
|
-
padding-right: 12px;
|
|
81
|
-
gap: 4px;
|
|
82
|
-
font-weight: 500;
|
|
83
|
-
cursor: pointer;
|
|
84
|
-
font-family: system-ui, -apple-system, sans-serif;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
.widget-button:hover {
|
|
88
|
-
background-color: #374151;
|
|
89
|
-
transform: scale(1.05);
|
|
90
|
-
box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.15);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
.widget-button:active {
|
|
94
|
-
transform: scale(0.98);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
.widget-button-icon {
|
|
98
|
-
display: inline-flex;
|
|
99
|
-
align-items: center;
|
|
100
|
-
width: 16px;
|
|
101
|
-
height: 16px;
|
|
102
|
-
flex-shrink: 0;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
.widget-button-text {
|
|
106
|
-
display: inline-flex;
|
|
107
|
-
align-items: center;
|
|
108
|
-
transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
|
|
109
|
-
overflow: hidden;
|
|
110
|
-
white-space: nowrap;
|
|
111
|
-
max-width: 288px;
|
|
112
|
-
}
|
|
113
|
-
`;
|
|
114
|
-
// Create button element
|
|
115
|
-
const button = document.createElement("button");
|
|
116
|
-
button.className = "widget-button";
|
|
117
|
-
button.setAttribute("aria-label", "Toggle feedback widget");
|
|
118
|
-
const iconDiv = document.createElement("div");
|
|
119
|
-
iconDiv.className = "widget-button-icon";
|
|
120
|
-
iconDiv.innerHTML = svgIcon;
|
|
121
|
-
const span = document.createElement("span");
|
|
122
|
-
span.className = "widget-button-text";
|
|
123
|
-
span.textContent = "What's new ?";
|
|
124
|
-
button.appendChild(iconDiv);
|
|
125
|
-
button.appendChild(span);
|
|
126
|
-
this.shadowRoot.appendChild(style);
|
|
127
|
-
this.shadowRoot.appendChild(button);
|
|
128
|
-
}
|
|
129
|
-
setupEventListeners() {
|
|
130
|
-
const button = this.shadowRoot?.querySelector(".widget-button");
|
|
131
|
-
if (button) {
|
|
132
|
-
button.addEventListener("click", () => this.toggle());
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
updateButtonText() {
|
|
136
|
-
const span = this.shadowRoot?.querySelector(".widget-button-text");
|
|
137
|
-
const iconDiv = this.shadowRoot?.querySelector(".widget-button-icon");
|
|
138
|
-
if (span && iconDiv) {
|
|
139
|
-
if (this.isOpen) {
|
|
140
|
-
span.textContent = "✕";
|
|
141
|
-
iconDiv.style.display = "none";
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
span.textContent = "What's new ?";
|
|
145
|
-
iconDiv.style.display = "inline-block";
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
// Register the custom element only in browser environment
|
|
152
|
-
if (typeof window !== "undefined" &&
|
|
153
|
-
typeof customElements !== "undefined" &&
|
|
154
|
-
SqueletteWidgetButton) {
|
|
155
|
-
if (!customElements.get("squelette-widget-button")) {
|
|
156
|
-
customElements.define("squelette-widget-button", SqueletteWidgetButton);
|
|
157
|
-
}
|
|
158
|
-
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
interface WidgetOptions {
|
|
2
|
-
url: string;
|
|
3
|
-
buttonSelector: string;
|
|
4
|
-
position?: 'top' | 'bottom' | 'left' | 'right';
|
|
5
|
-
onLoad?: () => void;
|
|
6
|
-
onOpenChange?: (isOpen: boolean) => void;
|
|
7
|
-
}
|
|
8
|
-
export declare function createRoadmapWidget(workspaceSlug: string, boardSlug: string, options: Omit<WidgetOptions, 'url'> & {
|
|
9
|
-
theme?: string;
|
|
10
|
-
}): {
|
|
11
|
-
destroy: () => void;
|
|
12
|
-
};
|
|
13
|
-
export declare function createChangelogWidget(workspaceSlug: string, options: Omit<WidgetOptions, 'url'> & {
|
|
14
|
-
theme?: string;
|
|
15
|
-
}): {
|
|
16
|
-
destroy: () => void;
|
|
17
|
-
};
|
|
18
|
-
export declare function createFeedbackWidget(workspaceSlug: string, boardSlug: string | undefined, options: Omit<WidgetOptions, 'url'> & {
|
|
19
|
-
theme?: string;
|
|
20
|
-
}): {
|
|
21
|
-
destroy: () => void;
|
|
22
|
-
};
|
|
23
|
-
export declare function createWidget(options: WidgetOptions): {
|
|
24
|
-
destroy: () => void;
|
|
25
|
-
};
|
|
26
|
-
export {};
|
package/dist/index.js
DELETED
|
@@ -1,230 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createRoadmapWidget = createRoadmapWidget;
|
|
4
|
-
exports.createChangelogWidget = createChangelogWidget;
|
|
5
|
-
exports.createFeedbackWidget = createFeedbackWidget;
|
|
6
|
-
exports.createWidget = createWidget;
|
|
7
|
-
// For now, the production one
|
|
8
|
-
const baseUrl = 'https://squelette.app';
|
|
9
|
-
function createRoadmapWidget(workspaceSlug, boardSlug, options) {
|
|
10
|
-
const params = new URLSearchParams({ board: boardSlug });
|
|
11
|
-
if (options.theme)
|
|
12
|
-
params.set('theme', options.theme);
|
|
13
|
-
const url = `${baseUrl}/widget/${workspaceSlug}/roadmap?${params.toString()}`;
|
|
14
|
-
const { theme: _, ...widgetOptions } = options;
|
|
15
|
-
return createWidget({ ...widgetOptions, url });
|
|
16
|
-
}
|
|
17
|
-
function createChangelogWidget(workspaceSlug, options) {
|
|
18
|
-
const params = new URLSearchParams();
|
|
19
|
-
if (options.theme)
|
|
20
|
-
params.set('theme', options.theme);
|
|
21
|
-
const url = `${baseUrl}/widget/${workspaceSlug}/changelog${params.toString() ? `?${params.toString()}` : ''}`;
|
|
22
|
-
const { theme: _, ...widgetOptions } = options;
|
|
23
|
-
return createWidget({ ...widgetOptions, url });
|
|
24
|
-
}
|
|
25
|
-
function createFeedbackWidget(workspaceSlug, boardSlug, options) {
|
|
26
|
-
const params = new URLSearchParams();
|
|
27
|
-
if (boardSlug)
|
|
28
|
-
params.set('board', boardSlug);
|
|
29
|
-
if (options.theme)
|
|
30
|
-
params.set('theme', options.theme);
|
|
31
|
-
const url = `${baseUrl}/widget/${workspaceSlug}/feedback${params.toString() ? `?${params.toString()}` : ''}`;
|
|
32
|
-
const { theme: _, ...widgetOptions } = options;
|
|
33
|
-
return createWidget({ ...widgetOptions, url });
|
|
34
|
-
}
|
|
35
|
-
function createWidget(options) {
|
|
36
|
-
const { url, buttonSelector, position = 'top', onLoad, onOpenChange, } = options;
|
|
37
|
-
// Store initial URL to reset to when closing
|
|
38
|
-
const initialUrl = url;
|
|
39
|
-
// Create iframe element (initially hidden but preloaded)
|
|
40
|
-
const iframe = document.createElement('iframe');
|
|
41
|
-
iframe.src = url;
|
|
42
|
-
iframe.loading = 'eager';
|
|
43
|
-
// Function to update iframe dimensions based on URL
|
|
44
|
-
const updateIframeDimensions = (currentUrl) => {
|
|
45
|
-
const urlPath = new URL(currentUrl).pathname;
|
|
46
|
-
const isFeedback = urlPath.endsWith('/feedback');
|
|
47
|
-
const isChangelog = urlPath.endsWith('/changelog');
|
|
48
|
-
const isRoadmap = urlPath.endsWith('/roadmap');
|
|
49
|
-
const width = '400px';
|
|
50
|
-
let height = '540px';
|
|
51
|
-
switch (true) {
|
|
52
|
-
case isFeedback:
|
|
53
|
-
height = '250px';
|
|
54
|
-
break;
|
|
55
|
-
case isChangelog:
|
|
56
|
-
height = '500px';
|
|
57
|
-
break;
|
|
58
|
-
case isRoadmap:
|
|
59
|
-
height = '400px';
|
|
60
|
-
break;
|
|
61
|
-
default:
|
|
62
|
-
height = '540px';
|
|
63
|
-
break;
|
|
64
|
-
}
|
|
65
|
-
iframe.style.width = width;
|
|
66
|
-
iframe.style.height = height;
|
|
67
|
-
};
|
|
68
|
-
// Initial dimensions
|
|
69
|
-
updateIframeDimensions(url);
|
|
70
|
-
// Get user-provided button element
|
|
71
|
-
const button = document.querySelector(buttonSelector);
|
|
72
|
-
if (!button) {
|
|
73
|
-
console.error(`Button element not found with selector: ${buttonSelector}`);
|
|
74
|
-
return { destroy: () => { } };
|
|
75
|
-
}
|
|
76
|
-
// Position calculation helper function
|
|
77
|
-
const calculatePosition = (buttonRect, iframeWidth, iframeHeight, preferredPosition) => {
|
|
78
|
-
const viewport = {
|
|
79
|
-
width: window.innerWidth,
|
|
80
|
-
height: window.innerHeight,
|
|
81
|
-
};
|
|
82
|
-
const margin = 12; // Gap between button and iframe
|
|
83
|
-
let left;
|
|
84
|
-
let top;
|
|
85
|
-
switch (preferredPosition) {
|
|
86
|
-
case 'right':
|
|
87
|
-
left = buttonRect.right + margin;
|
|
88
|
-
top = buttonRect.top;
|
|
89
|
-
break;
|
|
90
|
-
case 'left':
|
|
91
|
-
left = buttonRect.left - iframeWidth - margin;
|
|
92
|
-
top = buttonRect.top;
|
|
93
|
-
break;
|
|
94
|
-
case 'top':
|
|
95
|
-
left = buttonRect.left - (iframeWidth - buttonRect.width) / 2;
|
|
96
|
-
top = buttonRect.top - iframeHeight - margin;
|
|
97
|
-
break;
|
|
98
|
-
case 'bottom':
|
|
99
|
-
left = buttonRect.left - (iframeWidth - buttonRect.width) / 2;
|
|
100
|
-
top = buttonRect.bottom + margin;
|
|
101
|
-
break;
|
|
102
|
-
default:
|
|
103
|
-
left = buttonRect.left - (iframeWidth - buttonRect.width) / 2;
|
|
104
|
-
top = buttonRect.top - iframeHeight - margin;
|
|
105
|
-
}
|
|
106
|
-
// Ensure iframe stays within viewport bounds
|
|
107
|
-
left = Math.max(10, Math.min(left, viewport.width - iframeWidth - 10));
|
|
108
|
-
top = Math.max(10, Math.min(top, viewport.height - iframeHeight - 10));
|
|
109
|
-
return { left, top };
|
|
110
|
-
};
|
|
111
|
-
// Function to update iframe position based on button position
|
|
112
|
-
const updateIframePosition = () => {
|
|
113
|
-
if (!button)
|
|
114
|
-
return;
|
|
115
|
-
const buttonRect = button.getBoundingClientRect();
|
|
116
|
-
const iframeWidth = Number.parseInt(iframe.style.width || '400px', 10);
|
|
117
|
-
const iframeHeight = Number.parseInt(iframe.style.height || '540px', 10);
|
|
118
|
-
const pos = calculatePosition(buttonRect, iframeWidth, iframeHeight, position);
|
|
119
|
-
iframe.style.left = `${pos.left}px`;
|
|
120
|
-
iframe.style.top = `${pos.top}px`;
|
|
121
|
-
iframe.style.bottom = 'auto';
|
|
122
|
-
iframe.style.right = 'auto';
|
|
123
|
-
};
|
|
124
|
-
iframe.style.cssText = `
|
|
125
|
-
position: fixed;
|
|
126
|
-
width: ${iframe.style.width || '400px'};
|
|
127
|
-
height: ${iframe.style.height || '540px'};
|
|
128
|
-
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
|
|
129
|
-
border-radius: 16px;
|
|
130
|
-
z-index: 9999;
|
|
131
|
-
background: transparent;
|
|
132
|
-
opacity: 0;
|
|
133
|
-
transform: translateY(20px);
|
|
134
|
-
transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
|
|
135
|
-
pointer-events: none;
|
|
136
|
-
border: none;
|
|
137
|
-
box-sizing: border-box;
|
|
138
|
-
`;
|
|
139
|
-
// Update position on scroll
|
|
140
|
-
window.addEventListener('scroll', updateIframePosition);
|
|
141
|
-
window.addEventListener('resize', updateIframePosition);
|
|
142
|
-
// Initial position update
|
|
143
|
-
updateIframePosition();
|
|
144
|
-
// Listen for navigation changes and close requests inside iframe
|
|
145
|
-
window.addEventListener('message', (event) => {
|
|
146
|
-
if (event.source === iframe.contentWindow) {
|
|
147
|
-
if (event.data?.type === 'navigation') {
|
|
148
|
-
updateIframeDimensions(event.data.url);
|
|
149
|
-
updateIframePosition(); // Update position when dimensions change
|
|
150
|
-
}
|
|
151
|
-
else if (event.data?.type === 'close-iframe' && isIframeVisible) {
|
|
152
|
-
// Close the iframe when requested
|
|
153
|
-
// Reset iframe URL to initial URL before closing
|
|
154
|
-
iframe.src = initialUrl;
|
|
155
|
-
toggleIframe();
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
// Track iframe visibility state
|
|
160
|
-
let isIframeVisible = false;
|
|
161
|
-
// Toggle iframe visibility
|
|
162
|
-
const toggleIframe = () => {
|
|
163
|
-
isIframeVisible = !isIframeVisible;
|
|
164
|
-
if (isIframeVisible) {
|
|
165
|
-
iframe.style.pointerEvents = 'auto';
|
|
166
|
-
iframe.style.opacity = '1';
|
|
167
|
-
iframe.style.transform = 'translateY(0)';
|
|
168
|
-
}
|
|
169
|
-
else {
|
|
170
|
-
iframe.style.pointerEvents = 'none';
|
|
171
|
-
iframe.style.opacity = '0';
|
|
172
|
-
iframe.style.transform = 'translateY(20px)';
|
|
173
|
-
// Reset iframe URL to initial URL when closing
|
|
174
|
-
iframe.src = initialUrl;
|
|
175
|
-
}
|
|
176
|
-
if (onOpenChange) {
|
|
177
|
-
onOpenChange(isIframeVisible);
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
// Set up button click handler
|
|
181
|
-
button.addEventListener('click', toggleIframe);
|
|
182
|
-
// Close iframe on click outside
|
|
183
|
-
const handleClickOutside = (event) => {
|
|
184
|
-
const target = event.target;
|
|
185
|
-
if (isIframeVisible &&
|
|
186
|
-
!iframe.contains(target) &&
|
|
187
|
-
!button.contains(target)) {
|
|
188
|
-
toggleIframe();
|
|
189
|
-
}
|
|
190
|
-
};
|
|
191
|
-
document.addEventListener('click', handleClickOutside);
|
|
192
|
-
// Call onLoad callback when iframe is loaded
|
|
193
|
-
iframe.addEventListener('load', () => {
|
|
194
|
-
if (onLoad) {
|
|
195
|
-
onLoad();
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
// Check if iframe with same button already exists and remove it
|
|
199
|
-
const existingIframe = document.querySelector(`iframe[data-widget-button="${buttonSelector}"]`);
|
|
200
|
-
if (existingIframe) {
|
|
201
|
-
existingIframe.remove();
|
|
202
|
-
}
|
|
203
|
-
// Mark iframe with button selector for cleanup
|
|
204
|
-
iframe.setAttribute('data-widget-button', buttonSelector);
|
|
205
|
-
// Append iframe to body
|
|
206
|
-
document.body.appendChild(iframe);
|
|
207
|
-
// Return cleanup function
|
|
208
|
-
return {
|
|
209
|
-
destroy: () => {
|
|
210
|
-
// Send onOpenChange(false) if widget is currently open
|
|
211
|
-
if (isIframeVisible && onOpenChange) {
|
|
212
|
-
onOpenChange(false);
|
|
213
|
-
}
|
|
214
|
-
// Remove event listeners
|
|
215
|
-
window.removeEventListener('scroll', updateIframePosition);
|
|
216
|
-
window.removeEventListener('resize', updateIframePosition);
|
|
217
|
-
button.removeEventListener('click', toggleIframe);
|
|
218
|
-
document.removeEventListener('click', handleClickOutside);
|
|
219
|
-
iframe.removeEventListener('load', () => {
|
|
220
|
-
if (onLoad) {
|
|
221
|
-
onLoad();
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
// Remove iframe from DOM
|
|
225
|
-
if (iframe.parentNode) {
|
|
226
|
-
iframe.parentNode.removeChild(iframe);
|
|
227
|
-
}
|
|
228
|
-
},
|
|
229
|
-
};
|
|
230
|
-
}
|