@eox/feedback 0.1.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/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@eox/feedback",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "devDependencies": {
6
+ "vite": "^6.0.3"
7
+ },
8
+ "dependencies": {
9
+ "@eox/ui": "^0.1.11",
10
+ "region-screenshot-js": "^1.1.0"
11
+ },
12
+ "files": [
13
+ "types",
14
+ "dist",
15
+ "src"
16
+ ],
17
+ "main": "./src/main.js",
18
+ "types": "./types/main.d.ts",
19
+ "typesVersions": {
20
+ "*": {
21
+ ".": [
22
+ "./types/main.d.ts"
23
+ ],
24
+ "src/*": [
25
+ "./types/*"
26
+ ]
27
+ }
28
+ },
29
+ "scripts": {
30
+ "types:generate": "tsc --project tsconfig.build.json || true",
31
+ "prepack": "npm run types:generate",
32
+ "build": "vite build",
33
+ "watch": "vite build --watch"
34
+ }
35
+ }
package/src/css.d.ts ADDED
@@ -0,0 +1 @@
1
+ declare module "*.css?inline";
@@ -0,0 +1 @@
1
+ export { TEST_SELECTORS } from "./test";
@@ -0,0 +1,4 @@
1
+ export const TEST_SELECTORS = {
2
+ feedbackElement: "eox-feedback",
3
+ feedbackButtonElement: "eox-feedback-button",
4
+ };
@@ -0,0 +1,82 @@
1
+ import { style } from "./style";
2
+ import { styleEOX } from "./style.eox";
3
+
4
+ /**
5
+ * @attr {string} [position=top-right] - The position of the button. Can be "top-left", "top-right", "bottom-left", or "bottom-right".
6
+ * @attr {string|null} [unstyled=undefined] - If set, the button will not be styled with EOX styles.
7
+ */
8
+ export class EOxFeedbackButton extends HTMLElement {
9
+ static observedAttributes = ["position", "unstyled"];
10
+
11
+ constructor() {
12
+ super();
13
+ this.attachShadow({ mode: "open" });
14
+
15
+ this.modal = null;
16
+
17
+ this.position = "top-right";
18
+ }
19
+
20
+ attributeChangedCallback(name, oldValue, newValue) {
21
+ if (newValue !== oldValue) {
22
+ if (name === "position" && newValue !== this.position) {
23
+ this.position = newValue;
24
+ this.render();
25
+ }
26
+ }
27
+ }
28
+
29
+ connectedCallback() {
30
+ this.render();
31
+ this.shadowRoot
32
+ .querySelector("button")
33
+ .addEventListener("click", () => this.onButtonClick());
34
+ }
35
+
36
+ disconnectedCallback() {
37
+ this.shadowRoot
38
+ .querySelector("button")
39
+ .removeEventListener("click", () => this.onButtonClick());
40
+ }
41
+
42
+ onButtonClick() {
43
+ if (!this.modal) {
44
+ this.modal = document.createElement("eox-feedback");
45
+ if (this.getAttribute("unstyled") !== null) {
46
+ this.modal.setAttribute("unstyled", this.getAttribute("unstyled"));
47
+ }
48
+ document.body.appendChild(this.modal);
49
+ this.modal.addEventListener("close", () => (this.modal = null));
50
+ } else {
51
+ this.modal.remove();
52
+ this.modal = null;
53
+ }
54
+ }
55
+
56
+ render() {
57
+ this.shadowRoot.innerHTML = `
58
+ <style>
59
+ ${style}
60
+ ${this.getAttribute("unstyled") === null && styleEOX}
61
+ :host {
62
+ position: fixed !important;
63
+ margin: 0px 20px !important;
64
+ ${this.position.includes("top") ? "top: 0px;" : ""}
65
+ ${this.position.includes("right") ? "right: 0px;" : ""}
66
+ ${this.position.includes("bottom") ? "bottom: 0px;" : ""}
67
+ ${this.position.includes("left") ? "left: 0px;" : ""}
68
+ }
69
+ </style>
70
+ <button
71
+ class="${this.position.includes("top") ? "bottom" : ""}${this.position.includes("bottom") ? "top" : ""}-round small-round"
72
+ >
73
+ <i>
74
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title>send-circle-outline</title><path d="M8,7.71L18,12L8,16.29V12.95L15.14,12L8,11.05V7.71M12,2A10,10 0 0,1 22,12A10,10 0 0,1 12,22A10,10 0 0,1 2,12A10,10 0 0,1 12,2M12,4A8,8 0 0,0 4,12A8,8 0 0,0 12,20A8,8 0 0,0 20,12A8,8 0 0,0 12,4Z" /></svg>
75
+ </i>
76
+ <span>Feedback</span>
77
+ </button>
78
+ `;
79
+ }
80
+ }
81
+
82
+ customElements.define("eox-feedback-button", EOxFeedbackButton);
package/src/main.js ADDED
@@ -0,0 +1,282 @@
1
+ import RegionScreenshot from "region-screenshot-js";
2
+ import { style } from "./style";
3
+ import { styleEOX } from "./style.eox";
4
+ import "./feedback-button.js";
5
+
6
+ /**
7
+ * @attr {string} endpoint - The endpoint to send the feedback to as POST message.
8
+ * @attr {string|null} [unstyled=undefined] - If provided, the element will not be styled with EOX styles.
9
+ *
10
+ * @fires close - The modal was closed.
11
+ * @fires submit - The feedback was submitted. The `CustomEvent.detail` contains the FormData object.
12
+ */
13
+ export class EOxFeedback extends HTMLElement {
14
+ static observedAttributes = ["endpoint", "unstyled"];
15
+
16
+ constructor() {
17
+ super();
18
+ this.attachShadow({ mode: "open" });
19
+
20
+ this.includeScreenshot = null;
21
+ this.areaSelection = null;
22
+
23
+ this.regionScreenshot = null;
24
+
25
+ this.screenShotFile = null;
26
+
27
+ this.endpoint =
28
+ this.getAttribute("endpoint") ||
29
+ document.querySelector("eox-feedback-button")?.getAttribute("endpoint");
30
+ }
31
+
32
+ connectedCallback() {
33
+ this.render();
34
+ this.shadowRoot
35
+ .querySelector("input[type=checkbox]")
36
+ .addEventListener("click", (e) => this.onToggleScreenshot(e));
37
+ this.shadowRoot
38
+ .querySelector("button#submit")
39
+ .addEventListener("click", () => this.onSubmit());
40
+ this.shadowRoot
41
+ .querySelector("button#cancel")
42
+ .addEventListener("click", () => this.onCancel());
43
+ this.shadowRoot
44
+ .querySelector("textarea")
45
+ .addEventListener("input", (e) => this.onTextInput(e));
46
+ document.addEventListener("keyup", (e) => this.onKeyboardInput(e));
47
+ }
48
+
49
+ disconnectedCallback() {
50
+ this.shadowRoot
51
+ .querySelector("input[type=checkbox]")
52
+ .removeEventListener("click", (e) => this.onToggleScreenshot(e));
53
+ this.shadowRoot
54
+ .querySelector("button#submit")
55
+ .removeEventListener("click", () => this.onSubmit());
56
+ this.shadowRoot
57
+ .querySelector("button#cancel")
58
+ .removeEventListener("click", () => this.onCancel());
59
+ this.shadowRoot
60
+ .querySelector("textarea")
61
+ .removeEventListener("input", (e) => this.onTextInput(e));
62
+ document.removeEventListener("keyup", (e) => this.onKeyboardInput(e));
63
+ }
64
+
65
+ onKeyboardInput(e) {
66
+ if (e.keyCode === 27) {
67
+ this.onCancel();
68
+ }
69
+ if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) {
70
+ if (this.shadowRoot.querySelector("textarea").value.length > 0) {
71
+ this.onSubmit();
72
+ }
73
+ }
74
+ }
75
+
76
+ onOverlayDraw(e) {
77
+ this.areaSelection = e.detail;
78
+ }
79
+
80
+ onTextInput(e) {
81
+ const submitButton = this.shadowRoot.querySelector("button#submit");
82
+ if (e.target.value.length > 0) {
83
+ submitButton.classList.remove("disabled");
84
+ } else {
85
+ submitButton.classList.add("disabled");
86
+ }
87
+ }
88
+
89
+ async onToggleScreenshot(e) {
90
+ this.includeScreenshot = e.target.checked;
91
+
92
+ this.shadowRoot
93
+ .querySelector("#screenshot-info")
94
+ .classList.toggle("hidden");
95
+
96
+ const existingScreenshot =
97
+ this.shadowRoot.querySelector("#screenshot > img");
98
+
99
+ if (!e.target.checked) {
100
+ this.regionScreenshot.close();
101
+
102
+ if (existingScreenshot) {
103
+ existingScreenshot.remove();
104
+ }
105
+ return;
106
+ }
107
+
108
+ const inputField = this.shadowRoot.querySelector("textarea");
109
+ const submitButton = this.shadowRoot.querySelector("button#submit");
110
+ submitButton.classList.add("disabled");
111
+
112
+ this.regionScreenshot = new RegionScreenshot();
113
+
114
+ ["closed", "errorCreated", "screenshotDownload"].forEach((event) => {
115
+ this.regionScreenshot.on(event, () => {
116
+ const checkbox = this.shadowRoot.querySelector("input[type=checkbox]");
117
+ if (!(checkbox instanceof HTMLInputElement)) return;
118
+ checkbox.checked = false;
119
+ this.includeScreenshot = false;
120
+ if (inputField.value.length > 0) {
121
+ submitButton.classList.remove("disabled");
122
+ }
123
+ });
124
+ });
125
+
126
+ this.regionScreenshot.on("screenshotGenerated", (dataUrl) => {
127
+ const screenshot = document.createElement("img");
128
+ screenshot.src = dataUrl;
129
+ screenshot.classList.add("border");
130
+ this.shadowRoot.querySelector("#screenshot").appendChild(screenshot);
131
+
132
+ function dataURLtoFile(dataurl, filename) {
133
+ var arr = dataurl.split(","),
134
+ mime = arr[0].match(/:(.*?);/)[1],
135
+ bstr = atob(arr[arr.length - 1]),
136
+ n = bstr.length,
137
+ u8arr = new Uint8Array(n);
138
+ while (n--) {
139
+ u8arr[n] = bstr.charCodeAt(n);
140
+ }
141
+ return new File([u8arr], filename, { type: mime });
142
+ }
143
+ this.screenShotFile = dataURLtoFile(dataUrl, "screenshot.png");
144
+ submitButton.classList.remove("disabled");
145
+ });
146
+ }
147
+
148
+ async onSubmit() {
149
+ const container = this.shadowRoot.querySelector("article > div");
150
+ const form = this.shadowRoot.querySelector("#form");
151
+ const progress = this.shadowRoot.querySelector("progress");
152
+
153
+ container.classList.add("disabled");
154
+ progress.classList.remove("hidden");
155
+
156
+ const requestBody = new FormData();
157
+ if (this.includeScreenshot) {
158
+ requestBody.append("file", this.screenShotFile);
159
+ }
160
+
161
+ requestBody.append(
162
+ "message",
163
+ this.shadowRoot.querySelector("textarea").value,
164
+ );
165
+
166
+ requestBody.append("userAgent", window.navigator.userAgent);
167
+
168
+ requestBody.append("location", window.location.href);
169
+
170
+ if (!this.endpoint) {
171
+ throw new Error("No endpoint attribute defined!");
172
+ }
173
+
174
+ const message = document.createElement("div");
175
+ try {
176
+ await fetch(this.endpoint, {
177
+ method: "POST",
178
+ body: requestBody,
179
+ });
180
+ message.innerHTML = `
181
+ <p class="green-text middle-align center-align">Thank you for submitting your feedback!</p>
182
+ `;
183
+ } catch (_) {
184
+ message.innerHTML = `
185
+ <p class="red-text middle-align center-align">Oh no - something went wrong! Apologies, we'll try to find out what happened.</p>
186
+ `;
187
+ }
188
+ this.dispatchEvent(new CustomEvent("submit", { detail: requestBody }));
189
+
190
+ form.classList.add("hidden");
191
+ container.appendChild(message);
192
+
193
+ container.classList.remove("disabled");
194
+ progress.classList.add("hidden");
195
+ setTimeout(() => {
196
+ this.onCancel();
197
+ }, 4000);
198
+ }
199
+
200
+ onCancel() {
201
+ if (this.regionScreenshot) {
202
+ this.regionScreenshot.close();
203
+ }
204
+ this.remove();
205
+ this.dispatchEvent(new Event("close"));
206
+ }
207
+
208
+ render() {
209
+ /**
210
+ * @type {import("./feedback-button").EOxFeedbackButton}
211
+ */
212
+ const feedbackButton = document.querySelector("eox-feedback-button");
213
+ this.shadowRoot.innerHTML = `
214
+ <style>
215
+ ${style}
216
+ ${this.getAttribute("unstyled") === null && styleEOX}
217
+ :host {
218
+ display: block;
219
+ position: fixed !important;
220
+ width: 320px;
221
+ ${
222
+ feedbackButton
223
+ ? `
224
+ margin: 50px 27px !important;
225
+ ${feedbackButton.position?.includes("top") ? "top: 0px;" : ""}
226
+ ${feedbackButton.position?.includes("right") ? "right: 0px;" : ""}
227
+ ${feedbackButton.position?.includes("bottom") ? "bottom: 0px;" : ""}
228
+ ${feedbackButton.position?.includes("left") ? "left: 0px;" : ""}
229
+ `
230
+ : ``
231
+ }
232
+ z-index: 10000;
233
+ }
234
+ .hidden {
235
+ display: none !important;
236
+ }
237
+ .disabled {
238
+ opacity: .5;
239
+ pointer-events: none;
240
+ }
241
+ .field > :is(input, textarea, select) {
242
+ font-size: 0.875rem !important;
243
+ }
244
+ #screenshot > img {
245
+ max-width: 100%;
246
+ max-height: 100px;
247
+ }
248
+ .region-screenshot_tools {
249
+ z-index: 10001;
250
+ }
251
+ </style>
252
+ <article class="no-margin small-round">
253
+ <div ${!this.endpoint ? 'class="disabled"' : ""}>
254
+ <div id="form">
255
+ <nav>
256
+ <label class="switch">
257
+ <input type="checkbox">
258
+ <span></span>
259
+ </label>
260
+ <div class="max">
261
+ <div>Include a screenshot</div>
262
+ <small id="screenshot-info" class="italic hidden">Drag to highlight part of the page</small>
263
+ </div>
264
+ </nav>
265
+ <div id="screenshot" class="middle-align center-align"></div>
266
+ <div class="field textarea border">
267
+ <textarea placeholder="${!this.endpoint ? "Missing endpoint attribute!" : "Type your feedback here! Also include some contact info if you'd like us to reach out to you."}"></textarea>
268
+ </div>
269
+ <nav>
270
+ <progress class="circle small hidden"></progress>
271
+ <div class="max"></div>
272
+ <button id="cancel" class="border">Cancel</button>
273
+ <button id="submit" class="disabled">Submit</button>
274
+ </nav>
275
+ </div>
276
+ </div>
277
+ </article>
278
+ `;
279
+ }
280
+ }
281
+
282
+ customElements.define("eox-feedback", EOxFeedback);
@@ -0,0 +1,5 @@
1
+ import eoxStyle from "@eox/ui/style.css?inline";
2
+
3
+ export const styleEOX = `
4
+ ${eoxStyle}
5
+ `;
package/src/style.js ADDED
@@ -0,0 +1,2 @@
1
+ export const style = `
2
+ `;
@@ -0,0 +1,2 @@
1
+ export { TEST_SELECTORS } from "./test";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/enums/index.js"],"names":[],"mappings":""}
@@ -0,0 +1,5 @@
1
+ export namespace TEST_SELECTORS {
2
+ let feedbackElement: string;
3
+ let feedbackButtonElement: string;
4
+ }
5
+ //# sourceMappingURL=test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test.d.ts","sourceRoot":"","sources":["../../src/enums/test.js"],"names":[],"mappings":""}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @attr {string} [position=top-right] - The position of the button. Can be "top-left", "top-right", "bottom-left", or "bottom-right".
3
+ * @attr {string|null} [unstyled=undefined] - If set, the button will not be styled with EOX styles.
4
+ */
5
+ export class EOxFeedbackButton extends HTMLElement {
6
+ static observedAttributes: string[];
7
+ modal: HTMLElement;
8
+ position: string;
9
+ attributeChangedCallback(name: any, oldValue: any, newValue: any): void;
10
+ connectedCallback(): void;
11
+ disconnectedCallback(): void;
12
+ onButtonClick(): void;
13
+ render(): void;
14
+ }
15
+ //# sourceMappingURL=feedback-button.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"feedback-button.d.ts","sourceRoot":"","sources":["../src/feedback-button.js"],"names":[],"mappings":"AAGA;;;GAGG;AACH;IACE,oCAAqD;IAMnD,mBAAiB;IAEjB,iBAA2B;IAG7B,wEAOC;IAED,0BAKC;IAED,6BAIC;IAED,sBAYC;IAED,eAuBC;CACF"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * @attr {string} endpoint - The endpoint to send the feedback to as POST message.
3
+ * @attr {string|null} [unstyled=undefined] - If provided, the element will not be styled with EOX styles.
4
+ *
5
+ * @fires close - The modal was closed.
6
+ * @fires submit - The feedback was submitted. The `CustomEvent.detail` contains the FormData object.
7
+ */
8
+ export class EOxFeedback extends HTMLElement {
9
+ static observedAttributes: string[];
10
+ includeScreenshot: any;
11
+ areaSelection: any;
12
+ regionScreenshot: any;
13
+ screenShotFile: File;
14
+ endpoint: string;
15
+ connectedCallback(): void;
16
+ disconnectedCallback(): void;
17
+ onKeyboardInput(e: any): void;
18
+ onOverlayDraw(e: any): void;
19
+ onTextInput(e: any): void;
20
+ onToggleScreenshot(e: any): Promise<void>;
21
+ onSubmit(): Promise<void>;
22
+ onCancel(): void;
23
+ render(): void;
24
+ }
25
+ //# sourceMappingURL=main.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.js"],"names":[],"mappings":"AAKA;;;;;;GAMG;AACH;IACE,oCAAqD;IAMnD,uBAA6B;IAC7B,mBAAyB;IAEzB,sBAA4B;IAE5B,qBAA0B;IAE1B,iBAEyE;IAG3E,0BAeC;IAED,6BAcC;IAED,8BASC;IAED,4BAEC;IAED,0BAOC;IAED,0CAyDC;IAED,0BAkDC;IAED,iBAMC;IAED,eAuEC;CACF"}
@@ -0,0 +1,2 @@
1
+ export const style: "\n";
2
+ //# sourceMappingURL=style.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"style.d.ts","sourceRoot":"","sources":["../src/style.js"],"names":[],"mappings":"AAAA,yBACE"}
@@ -0,0 +1,2 @@
1
+ export const styleEOX: string;
2
+ //# sourceMappingURL=style.eox.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"style.eox.d.ts","sourceRoot":"","sources":["../src/style.eox.js"],"names":[],"mappings":"AAEA,8BAEE"}