@superbased/feedback-widget 0.0.1-placeholder.0 → 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/NOTICE +4 -0
- package/README.md +262 -4
- package/dist/define.d.ts +2 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +8 -0
- package/dist/define.js.map +1 -0
- package/dist/index.d.ts +138 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +457 -0
- package/dist/index.js.map +1 -0
- package/dist/react.d.ts +64 -0
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +89 -0
- package/dist/react.js.map +1 -0
- package/package.json +68 -4
- package/src/define.ts +9 -0
- package/src/index.ts +490 -0
- package/src/react.ts +132 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@superbased/feedback-widget",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "superbased end-user feedback widget: a tiny, framework-agnostic thumbs up/down (and optional comment) web component, with a React wrapper. It posts to your own backend, never to superbased, and holds no SDK key.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "superbased <contact@superbased.app>",
|
|
7
7
|
"homepage": "https://superbased.app",
|
|
@@ -10,6 +10,70 @@
|
|
|
10
10
|
"url": "git+https://github.com/superbasedapp/observer.git",
|
|
11
11
|
"directory": "sdk/widget"
|
|
12
12
|
},
|
|
13
|
-
"
|
|
14
|
-
"
|
|
13
|
+
"type": "module",
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"module": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"default": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./define": {
|
|
23
|
+
"types": "./dist/define.d.ts",
|
|
24
|
+
"default": "./dist/define.js"
|
|
25
|
+
},
|
|
26
|
+
"./react": {
|
|
27
|
+
"types": "./dist/react.d.ts",
|
|
28
|
+
"default": "./dist/react.js"
|
|
29
|
+
},
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"src",
|
|
35
|
+
"README.md",
|
|
36
|
+
"LICENSE",
|
|
37
|
+
"NOTICE"
|
|
38
|
+
],
|
|
39
|
+
"sideEffects": [
|
|
40
|
+
"./dist/define.js"
|
|
41
|
+
],
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsc -p tsconfig.json",
|
|
44
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
45
|
+
"test": "npm run build && node --test test/element.test.mjs test/react.test.mjs test/server.test.mjs",
|
|
46
|
+
"prepare": "npm run build",
|
|
47
|
+
"prepublishOnly": "npm test"
|
|
48
|
+
},
|
|
49
|
+
"keywords": [
|
|
50
|
+
"feedback",
|
|
51
|
+
"thumbs",
|
|
52
|
+
"web-component",
|
|
53
|
+
"custom-element",
|
|
54
|
+
"react",
|
|
55
|
+
"llm",
|
|
56
|
+
"observability"
|
|
57
|
+
],
|
|
58
|
+
"engines": {
|
|
59
|
+
"node": ">=20.6.0"
|
|
60
|
+
},
|
|
61
|
+
"publishConfig": {
|
|
62
|
+
"access": "public"
|
|
63
|
+
},
|
|
64
|
+
"peerDependencies": {
|
|
65
|
+
"react": ">=18"
|
|
66
|
+
},
|
|
67
|
+
"peerDependenciesMeta": {
|
|
68
|
+
"react": {
|
|
69
|
+
"optional": true
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
"devDependencies": {
|
|
73
|
+
"@types/react": "^19.3.0",
|
|
74
|
+
"happy-dom": "^21.0.4",
|
|
75
|
+
"react": "^19.3.1",
|
|
76
|
+
"react-dom": "^19.3.1",
|
|
77
|
+
"typescript": "^5.9.0"
|
|
78
|
+
}
|
|
15
79
|
}
|
package/src/define.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Side-effect entry: `import "@superbased/feedback-widget/define"` registers
|
|
3
|
+
* `<superbased-feedback>` (in a browser; a no-op elsewhere).
|
|
4
|
+
*/
|
|
5
|
+
import { defineFeedbackElement } from "./index.js";
|
|
6
|
+
|
|
7
|
+
defineFeedbackElement();
|
|
8
|
+
|
|
9
|
+
export { TAG } from "./index.js";
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `<superbased-feedback>`: an end user's thumbs up / down, and an optional
|
|
3
|
+
* comment, on one AI answer (plan W5-6). Framework-agnostic, no runtime
|
|
4
|
+
* dependencies, keyboard and screen-reader accessible, themeable with CSS
|
|
5
|
+
* variables.
|
|
6
|
+
*
|
|
7
|
+
* Security: the browser never holds a superbased SDK key. The widget only
|
|
8
|
+
* dispatches a `superbased-feedback` event and, when `endpoint` is set,
|
|
9
|
+
* POSTs to YOUR backend (same origin by default). Your backend calls the
|
|
10
|
+
* SDK's `feedback()` with the trace and span id it rendered the answer
|
|
11
|
+
* with. See the README for that server half in each SDK.
|
|
12
|
+
*
|
|
13
|
+
* ```html
|
|
14
|
+
* <superbased-feedback trace-id="4bf9..." span-id="00f0..." endpoint="/api/feedback" comment="optional"></superbased-feedback>
|
|
15
|
+
* <script type="module">import { defineFeedbackElement } from "@superbased/feedback-widget"; defineFeedbackElement();</script>
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/** The custom element's default tag name. */
|
|
20
|
+
export const TAG = "superbased-feedback";
|
|
21
|
+
/** The event the widget dispatches (bubbles, composed, cancelable: `preventDefault()` skips the POST). */
|
|
22
|
+
export const FEEDBACK_EVENT = "superbased-feedback";
|
|
23
|
+
/** Dispatched after the POST answered 2xx (`detail`: the payload and the status). */
|
|
24
|
+
export const SENT_EVENT = "superbased-feedback-sent";
|
|
25
|
+
/** Dispatched when the POST failed (`detail`: the payload, the status (0 = no answer) and the error). */
|
|
26
|
+
export const ERROR_EVENT = "superbased-feedback-error";
|
|
27
|
+
/** The score name the SDK's `feedback()` uses by default. */
|
|
28
|
+
export const DEFAULT_NAME = "user_feedback";
|
|
29
|
+
|
|
30
|
+
/** The body the widget POSTs, and the `detail` of its event (snake_case, the SDKs' field names). */
|
|
31
|
+
export interface FeedbackPayload {
|
|
32
|
+
/** The trace the answer belongs to (32 hex), as your server rendered it. */
|
|
33
|
+
trace_id: string;
|
|
34
|
+
/** The answer's span (16 hex), when your server gave one. */
|
|
35
|
+
span_id?: string;
|
|
36
|
+
/** true = thumbs up, false = thumbs down. */
|
|
37
|
+
value: boolean;
|
|
38
|
+
/** The end user's comment, when they wrote one. */
|
|
39
|
+
comment?: string;
|
|
40
|
+
/** The score name (default `user_feedback`). */
|
|
41
|
+
name: string;
|
|
42
|
+
/** An opaque value your server rendered with the answer (for example an HMAC of the trace id), passed through untouched. */
|
|
43
|
+
token?: string;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Comment box: `off` (default), `optional` (offered after a vote) or `required` (a thumbs-down needs one). */
|
|
47
|
+
export type CommentMode = "off" | "optional" | "required";
|
|
48
|
+
|
|
49
|
+
/** The texts the widget shows; every one is an attribute (`question`, `label-up`, ...). */
|
|
50
|
+
export interface FeedbackTexts {
|
|
51
|
+
question: string;
|
|
52
|
+
labelUp: string;
|
|
53
|
+
labelDown: string;
|
|
54
|
+
commentLabel: string;
|
|
55
|
+
commentPlaceholder: string;
|
|
56
|
+
submit: string;
|
|
57
|
+
skip: string;
|
|
58
|
+
thanks: string;
|
|
59
|
+
error: string;
|
|
60
|
+
commentRequired: string;
|
|
61
|
+
commentTooLong: string;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const DEFAULT_TEXTS: Readonly<FeedbackTexts> = {
|
|
65
|
+
question: "Was this answer helpful?",
|
|
66
|
+
labelUp: "Helpful",
|
|
67
|
+
labelDown: "Not helpful",
|
|
68
|
+
commentLabel: "Tell us more (optional)",
|
|
69
|
+
commentPlaceholder: "",
|
|
70
|
+
submit: "Send",
|
|
71
|
+
skip: "Skip",
|
|
72
|
+
thanks: "Thanks for your feedback.",
|
|
73
|
+
error: "Your feedback could not be sent.",
|
|
74
|
+
commentRequired: "Please add a comment.",
|
|
75
|
+
commentTooLong: "Your comment is too long.",
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const TEXT_ATTRS: Record<keyof FeedbackTexts, string> = {
|
|
79
|
+
question: "question",
|
|
80
|
+
labelUp: "label-up",
|
|
81
|
+
labelDown: "label-down",
|
|
82
|
+
commentLabel: "comment-label",
|
|
83
|
+
commentPlaceholder: "comment-placeholder",
|
|
84
|
+
submit: "submit-label",
|
|
85
|
+
skip: "skip-label",
|
|
86
|
+
thanks: "thanks",
|
|
87
|
+
error: "error-text",
|
|
88
|
+
commentRequired: "comment-required-text",
|
|
89
|
+
commentTooLong: "comment-too-long-text",
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const TRACE = /^[0-9a-f]{32}$/;
|
|
93
|
+
const SPAN = /^[0-9a-f]{16}$/;
|
|
94
|
+
const NAME = /^[a-z0-9_.-]{1,64}$/;
|
|
95
|
+
/** The SDKs' comment bound (scores.md: up to 2,000 bytes). */
|
|
96
|
+
export const MAX_COMMENT_BYTES = 2000;
|
|
97
|
+
|
|
98
|
+
/** Why a payload cannot be sent ("" when it can). The same rules the SDKs apply to `feedback()`. */
|
|
99
|
+
/** A comment's size in UTF-8 bytes, the unit the SDKs bound it in. */
|
|
100
|
+
export function commentBytes(comment: string): number {
|
|
101
|
+
return new TextEncoder().encode(comment).length;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function validatePayload(p: FeedbackPayload): string {
|
|
105
|
+
if (!TRACE.test(p.trace_id) || /^0+$/.test(p.trace_id)) return "trace-id must be 32 lowercase hex characters, not all zero";
|
|
106
|
+
if (p.span_id !== undefined && (!SPAN.test(p.span_id) || /^0+$/.test(p.span_id))) return "span-id must be 16 lowercase hex characters";
|
|
107
|
+
if (typeof p.value !== "boolean") return "value must be true or false";
|
|
108
|
+
if (!NAME.test(p.name)) return "name must be 1-64 chars of a-z 0-9 _ . -";
|
|
109
|
+
if (p.comment !== undefined && commentBytes(p.comment) > MAX_COMMENT_BYTES) return `comment is over ${MAX_COMMENT_BYTES} bytes`;
|
|
110
|
+
return "";
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Whether `endpoint` may be posted to from `origin`: relative and same-origin
|
|
115
|
+
* URLs always; another origin only with `allowCrossOrigin` (the trace id and
|
|
116
|
+
* the comment are the app's data, so they go to the app's own backend by
|
|
117
|
+
* default). Never a non-http(s) scheme.
|
|
118
|
+
*/
|
|
119
|
+
export function endpointAllowed(endpoint: string, origin: string, allowCrossOrigin = false): boolean {
|
|
120
|
+
let u: URL;
|
|
121
|
+
try {
|
|
122
|
+
u = new URL(endpoint, origin);
|
|
123
|
+
} catch {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
if (u.protocol !== "http:" && u.protocol !== "https:") return false;
|
|
127
|
+
return allowCrossOrigin || u.origin === origin;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The theme variables (set them on the element or any ancestor). */
|
|
131
|
+
export const CSS_VARIABLES = [
|
|
132
|
+
"--sb-feedback-font",
|
|
133
|
+
"--sb-feedback-fg",
|
|
134
|
+
"--sb-feedback-muted",
|
|
135
|
+
"--sb-feedback-bg",
|
|
136
|
+
"--sb-feedback-border",
|
|
137
|
+
"--sb-feedback-accent",
|
|
138
|
+
"--sb-feedback-accent-fg",
|
|
139
|
+
"--sb-feedback-radius",
|
|
140
|
+
"--sb-feedback-gap",
|
|
141
|
+
"--sb-feedback-focus",
|
|
142
|
+
"--sb-feedback-error",
|
|
143
|
+
] as const;
|
|
144
|
+
|
|
145
|
+
const STYLE = `
|
|
146
|
+
:host { display: inline-block; font: var(--sb-feedback-font, inherit); color: var(--sb-feedback-fg, inherit); }
|
|
147
|
+
:host([hidden]) { display: none; }
|
|
148
|
+
.root { display: flex; flex-direction: column; gap: var(--sb-feedback-gap, 0.5em); }
|
|
149
|
+
.row { display: flex; align-items: center; gap: var(--sb-feedback-gap, 0.5em); flex-wrap: wrap; }
|
|
150
|
+
.question { color: var(--sb-feedback-muted, inherit); }
|
|
151
|
+
button { font: inherit; color: inherit; cursor: pointer; background: var(--sb-feedback-bg, transparent);
|
|
152
|
+
border: 1px solid var(--sb-feedback-border, currentColor); border-radius: var(--sb-feedback-radius, 0.5em);
|
|
153
|
+
padding: 0.25em 0.6em; min-width: 2.75em; min-height: 2.75em; }
|
|
154
|
+
button[aria-pressed="true"], button.primary { background: var(--sb-feedback-accent, #2b6cb0); color: var(--sb-feedback-accent-fg, #fff);
|
|
155
|
+
border-color: var(--sb-feedback-accent, #2b6cb0); }
|
|
156
|
+
button:disabled { cursor: default; opacity: 0.6; }
|
|
157
|
+
button:focus-visible, textarea:focus-visible { outline: 2px solid var(--sb-feedback-focus, #2b6cb0); outline-offset: 2px; }
|
|
158
|
+
textarea { font: inherit; color: inherit; background: var(--sb-feedback-bg, transparent); width: 100%; box-sizing: border-box;
|
|
159
|
+
border: 1px solid var(--sb-feedback-border, currentColor); border-radius: var(--sb-feedback-radius, 0.5em); padding: 0.4em; min-height: 4em; }
|
|
160
|
+
.status { min-height: 1.2em; }
|
|
161
|
+
.status.error, .hint { color: var(--sb-feedback-error, #c53030); }
|
|
162
|
+
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
|
|
163
|
+
`;
|
|
164
|
+
|
|
165
|
+
/** One constructed sheet per document (a sheet is adoptable only in the document that built it). */
|
|
166
|
+
const sheets = new WeakMap<Document, CSSStyleSheet>();
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Put the styles on the shadow root without inline CSS where the browser
|
|
170
|
+
* can: a constructed stylesheet (`adoptedStyleSheets`) is not inline style,
|
|
171
|
+
* so a strict `style-src` Content Security Policy allows it. Older browsers
|
|
172
|
+
* get a `<style>` element carrying the element's `nonce` attribute, which a
|
|
173
|
+
* nonce-based policy allows. Returns which one it used.
|
|
174
|
+
*/
|
|
175
|
+
export function applyStyles(root: ShadowRoot, nonce: string | null): "adopted" | "style" {
|
|
176
|
+
const doc = root.ownerDocument;
|
|
177
|
+
const Sheet = (doc.defaultView as (Window & { CSSStyleSheet?: typeof CSSStyleSheet }) | null)?.CSSStyleSheet;
|
|
178
|
+
const canAdopt = "adoptedStyleSheets" in root && typeof Sheet === "function" && typeof (Sheet.prototype as { replaceSync?: unknown }).replaceSync === "function";
|
|
179
|
+
if (canAdopt) {
|
|
180
|
+
try {
|
|
181
|
+
let sheet = sheets.get(doc);
|
|
182
|
+
if (!sheet) {
|
|
183
|
+
sheet = new Sheet();
|
|
184
|
+
sheet.replaceSync(STYLE);
|
|
185
|
+
sheets.set(doc, sheet);
|
|
186
|
+
}
|
|
187
|
+
const own = sheet;
|
|
188
|
+
root.adoptedStyleSheets = [...root.adoptedStyleSheets.filter((x) => x !== own), own];
|
|
189
|
+
return "adopted";
|
|
190
|
+
} catch {
|
|
191
|
+
// fall through to the element
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
const el = root.ownerDocument.createElement("style");
|
|
195
|
+
if (nonce) el.setAttribute("nonce", nonce);
|
|
196
|
+
el.textContent = STYLE;
|
|
197
|
+
root.prepend(el);
|
|
198
|
+
return "style";
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
type FetchLike = (input: string, init: RequestInit) => Promise<{ ok: boolean; status: number }>;
|
|
202
|
+
|
|
203
|
+
/* A base class that also loads where there is no DOM (server rendering, Node): the element is only defined in a browser. */
|
|
204
|
+
const Base: typeof HTMLElement = (typeof HTMLElement !== "undefined" ? HTMLElement : class {}) as typeof HTMLElement;
|
|
205
|
+
|
|
206
|
+
let seq = 0;
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* The element. Attributes: `trace-id` (required), `span-id`, `endpoint`,
|
|
210
|
+
* `name` (default `user_feedback`), `comment` (`off` | `optional` |
|
|
211
|
+
* `required`), `token`, `cross-origin` (allow an endpoint on another
|
|
212
|
+
* origin), `credentials` (`same-origin` default, `include`, `omit`),
|
|
213
|
+
* `disabled`, `nonce` (for a nonce-based CSP in browsers without
|
|
214
|
+
* constructable stylesheets), and the texts (`question`, `label-up`,
|
|
215
|
+
* `label-down`, `comment-label`, `comment-placeholder`, `submit-label`,
|
|
216
|
+
* `skip-label`, `thanks`, `error-text`, `comment-required-text`,
|
|
217
|
+
* `comment-too-long-text`). Properties: `fetch` (a
|
|
218
|
+
* fetch to use instead of the global one), `value` (the vote, or null),
|
|
219
|
+
* `reset()`.
|
|
220
|
+
*/
|
|
221
|
+
export class FeedbackElement extends Base {
|
|
222
|
+
static get observedAttributes(): string[] {
|
|
223
|
+
return ["trace-id", "span-id", "comment", "disabled", ...Object.values(TEXT_ATTRS)];
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** A fetch to use instead of the global one (tests, an app's own client). */
|
|
227
|
+
fetch: FetchLike | null = null;
|
|
228
|
+
|
|
229
|
+
private vote: boolean | null = null;
|
|
230
|
+
private stage: "vote" | "comment" | "sending" | "done" | "error" = "vote";
|
|
231
|
+
private lastPayload: FeedbackPayload | null = null;
|
|
232
|
+
private readonly uid = `sbf${++seq}`;
|
|
233
|
+
private built = false;
|
|
234
|
+
|
|
235
|
+
/** The end user's vote (true up, false down), or null before one. */
|
|
236
|
+
get value(): boolean | null {
|
|
237
|
+
return this.vote;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Back to the start (for example when the element is reused for another answer). */
|
|
241
|
+
reset(): void {
|
|
242
|
+
this.vote = null;
|
|
243
|
+
this.stage = "vote";
|
|
244
|
+
this.lastPayload = null;
|
|
245
|
+
this.render();
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
connectedCallback(): void {
|
|
249
|
+
if (!this.built) this.build();
|
|
250
|
+
this.render();
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null): void {
|
|
254
|
+
if (oldValue === newValue || !this.built) return;
|
|
255
|
+
if (name === "trace-id" || name === "span-id") this.vote = null; // a new answer: a new vote
|
|
256
|
+
if (name === "trace-id" || name === "span-id") this.stage = "vote";
|
|
257
|
+
this.render();
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
private texts(): FeedbackTexts {
|
|
261
|
+
const out = { ...DEFAULT_TEXTS };
|
|
262
|
+
for (const [k, attr] of Object.entries(TEXT_ATTRS) as [keyof FeedbackTexts, string][]) {
|
|
263
|
+
const v = this.getAttribute(attr);
|
|
264
|
+
if (v !== null) out[k] = v;
|
|
265
|
+
}
|
|
266
|
+
if (this.commentMode() === "required" && this.getAttribute("comment-label") === null) out.commentLabel = "Tell us more";
|
|
267
|
+
return out;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
private commentMode(): CommentMode {
|
|
271
|
+
const m = (this.getAttribute("comment") ?? "off").trim().toLowerCase();
|
|
272
|
+
return m === "optional" || m === "required" ? m : "off";
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private $(sel: string): HTMLElement {
|
|
276
|
+
return this.shadowRoot!.querySelector(sel) as HTMLElement;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
private build(): void {
|
|
280
|
+
const root = this.shadowRoot ?? this.attachShadow({ mode: "open" });
|
|
281
|
+
const id = this.uid;
|
|
282
|
+
root.innerHTML = `<div class="root" part="root">
|
|
283
|
+
<div class="row" role="group" aria-labelledby="${id}-q" part="vote">
|
|
284
|
+
<span class="question" id="${id}-q" part="question"></span>
|
|
285
|
+
<button type="button" class="up" part="button up" aria-pressed="false"><span aria-hidden="true">👍</span><span class="sr up-label"></span></button>
|
|
286
|
+
<button type="button" class="down" part="button down" aria-pressed="false"><span aria-hidden="true">👎</span><span class="sr down-label"></span></button>
|
|
287
|
+
</div>
|
|
288
|
+
<form class="comment" part="comment" hidden novalidate>
|
|
289
|
+
<label for="${id}-c" class="comment-label" part="comment-label"></label>
|
|
290
|
+
<textarea id="${id}-c" part="textarea" maxlength="${MAX_COMMENT_BYTES}" aria-describedby="${id}-h"></textarea>
|
|
291
|
+
<span class="hint" id="${id}-h" part="hint" aria-live="assertive"></span>
|
|
292
|
+
<div class="row">
|
|
293
|
+
<button type="submit" class="primary send" part="button submit"></button>
|
|
294
|
+
<button type="button" class="skip" part="button skip"></button>
|
|
295
|
+
</div>
|
|
296
|
+
</form>
|
|
297
|
+
<div class="status" part="status" role="status" aria-live="polite"></div>
|
|
298
|
+
</div>`;
|
|
299
|
+
applyStyles(root, this.getAttribute("nonce"));
|
|
300
|
+
this.$(".up").addEventListener("click", () => this.choose(true));
|
|
301
|
+
this.$(".down").addEventListener("click", () => this.choose(false));
|
|
302
|
+
const form = this.$(".comment") as HTMLFormElement;
|
|
303
|
+
form.addEventListener("submit", (e) => {
|
|
304
|
+
e.preventDefault();
|
|
305
|
+
this.submitComment(false);
|
|
306
|
+
});
|
|
307
|
+
this.$(".skip").addEventListener("click", () => this.submitComment(true));
|
|
308
|
+
const ta = this.$("textarea") as HTMLTextAreaElement;
|
|
309
|
+
// maxlength counts characters; the bound is bytes, so say so as they type.
|
|
310
|
+
ta.addEventListener("input", () => this.checkLength());
|
|
311
|
+
ta.addEventListener("keydown", (e) => {
|
|
312
|
+
const k = e as KeyboardEvent;
|
|
313
|
+
if (k.key === "Escape") {
|
|
314
|
+
k.preventDefault();
|
|
315
|
+
this.submitComment(true);
|
|
316
|
+
} else if (k.key === "Enter" && (k.ctrlKey || k.metaKey)) {
|
|
317
|
+
k.preventDefault();
|
|
318
|
+
this.submitComment(false);
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
this.built = true;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
private render(): void {
|
|
325
|
+
if (!this.built) return;
|
|
326
|
+
const t = this.texts();
|
|
327
|
+
const disabled = this.hasAttribute("disabled") || this.stage === "sending" || this.stage === "done";
|
|
328
|
+
this.$(".question").textContent = t.question;
|
|
329
|
+
for (const [cls, label, on] of [
|
|
330
|
+
["up", t.labelUp, this.vote === true],
|
|
331
|
+
["down", t.labelDown, this.vote === false],
|
|
332
|
+
] as const) {
|
|
333
|
+
const b = this.$(`.${cls}`) as HTMLButtonElement;
|
|
334
|
+
b.setAttribute("aria-pressed", String(on));
|
|
335
|
+
b.setAttribute("aria-label", label);
|
|
336
|
+
b.title = label;
|
|
337
|
+
(this.$(`.${cls}-label`) as HTMLElement).textContent = label;
|
|
338
|
+
b.disabled = disabled;
|
|
339
|
+
}
|
|
340
|
+
const form = this.$(".comment") as HTMLFormElement;
|
|
341
|
+
form.hidden = this.stage !== "comment";
|
|
342
|
+
this.$(".comment-label").textContent = t.commentLabel;
|
|
343
|
+
const ta = this.$("textarea") as HTMLTextAreaElement;
|
|
344
|
+
ta.placeholder = t.commentPlaceholder;
|
|
345
|
+
const required = this.commentMode() === "required" && this.vote === false;
|
|
346
|
+
if (required) ta.setAttribute("aria-required", "true");
|
|
347
|
+
else ta.removeAttribute("aria-required");
|
|
348
|
+
this.$(".send").textContent = t.submit;
|
|
349
|
+
const skip = this.$(".skip") as HTMLButtonElement;
|
|
350
|
+
skip.textContent = t.skip;
|
|
351
|
+
skip.hidden = required;
|
|
352
|
+
const status = this.$(".status");
|
|
353
|
+
status.classList.toggle("error", this.stage === "error");
|
|
354
|
+
status.textContent = this.stage === "done" ? t.thanks : this.stage === "error" ? t.error : "";
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
private choose(value: boolean): void {
|
|
358
|
+
if (this.hasAttribute("disabled") || this.stage === "sending" || this.stage === "done") return;
|
|
359
|
+
this.vote = value;
|
|
360
|
+
this.$(".hint").textContent = "";
|
|
361
|
+
if (this.commentMode() === "off") {
|
|
362
|
+
void this.send(undefined);
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
this.stage = "comment";
|
|
366
|
+
this.render();
|
|
367
|
+
(this.$("textarea") as HTMLTextAreaElement).focus();
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/** Mark the comment invalid while it is over the byte bound; true when it fits. */
|
|
371
|
+
private checkLength(): boolean {
|
|
372
|
+
const ta = this.$("textarea") as HTMLTextAreaElement;
|
|
373
|
+
const over = commentBytes(ta.value.trim()) > MAX_COMMENT_BYTES;
|
|
374
|
+
if (over) {
|
|
375
|
+
ta.setAttribute("aria-invalid", "true");
|
|
376
|
+
this.$(".hint").textContent = this.texts().commentTooLong;
|
|
377
|
+
} else if (this.$(".hint").textContent === this.texts().commentTooLong) {
|
|
378
|
+
ta.removeAttribute("aria-invalid");
|
|
379
|
+
this.$(".hint").textContent = "";
|
|
380
|
+
}
|
|
381
|
+
return !over;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
private submitComment(skip: boolean): void {
|
|
385
|
+
if (this.stage !== "comment" || this.vote === null) return;
|
|
386
|
+
const ta = this.$("textarea") as HTMLTextAreaElement;
|
|
387
|
+
const text = skip ? "" : ta.value.trim();
|
|
388
|
+
if (!skip && !this.checkLength()) {
|
|
389
|
+
ta.focus();
|
|
390
|
+
return; // the end user shortens it; the vote is kept
|
|
391
|
+
}
|
|
392
|
+
if (!text && this.commentMode() === "required" && this.vote === false) {
|
|
393
|
+
if (skip) return; // a required comment cannot be skipped
|
|
394
|
+
this.$(".hint").textContent = this.texts().commentRequired;
|
|
395
|
+
ta.setAttribute("aria-invalid", "true");
|
|
396
|
+
ta.focus();
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
ta.removeAttribute("aria-invalid");
|
|
400
|
+
void this.send(text || undefined);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/** Build the payload from the attributes; null (with an error event) when it cannot be sent. */
|
|
404
|
+
private payload(comment: string | undefined): FeedbackPayload | null {
|
|
405
|
+
const p: FeedbackPayload = {
|
|
406
|
+
trace_id: (this.getAttribute("trace-id") ?? "").trim().toLowerCase(),
|
|
407
|
+
value: this.vote as boolean,
|
|
408
|
+
name: (this.getAttribute("name") ?? DEFAULT_NAME).trim().toLowerCase(),
|
|
409
|
+
};
|
|
410
|
+
const span = (this.getAttribute("span-id") ?? "").trim().toLowerCase();
|
|
411
|
+
if (span) p.span_id = span;
|
|
412
|
+
if (comment) p.comment = comment;
|
|
413
|
+
const token = this.getAttribute("token");
|
|
414
|
+
if (token) p.token = token;
|
|
415
|
+
const why = validatePayload(p);
|
|
416
|
+
if (why) {
|
|
417
|
+
this.fail(p, 0, why);
|
|
418
|
+
return null;
|
|
419
|
+
}
|
|
420
|
+
return p;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
private fail(p: FeedbackPayload, status: number, error: string): void {
|
|
424
|
+
this.stage = "error";
|
|
425
|
+
this.render();
|
|
426
|
+
this.dispatchEvent(new CustomEvent(ERROR_EVENT, { bubbles: true, composed: true, detail: { payload: p, status, error } }));
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
private async send(comment: string | undefined): Promise<void> {
|
|
430
|
+
const p = this.payload(comment);
|
|
431
|
+
if (!p) return;
|
|
432
|
+
this.lastPayload = p;
|
|
433
|
+
const go = this.dispatchEvent(new CustomEvent(FEEDBACK_EVENT, { bubbles: true, composed: true, cancelable: true, detail: p }));
|
|
434
|
+
const endpoint = this.getAttribute("endpoint");
|
|
435
|
+
if (!go || !endpoint) {
|
|
436
|
+
// The app handles the event itself (or asked the widget not to POST).
|
|
437
|
+
this.stage = "done";
|
|
438
|
+
this.render();
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
const origin = typeof location !== "undefined" ? location.origin : "http://localhost";
|
|
442
|
+
if (!endpointAllowed(endpoint, origin, this.hasAttribute("cross-origin"))) {
|
|
443
|
+
this.fail(p, 0, `endpoint ${endpoint} is not on this page's origin (add the cross-origin attribute to allow it)`);
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
this.stage = "sending";
|
|
447
|
+
this.render();
|
|
448
|
+
const f: FetchLike | null = this.fetch ?? (typeof fetch === "function" ? (i, n) => fetch(i, n) : null);
|
|
449
|
+
if (!f) {
|
|
450
|
+
this.fail(p, 0, "no fetch available");
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
const cred = this.getAttribute("credentials");
|
|
454
|
+
try {
|
|
455
|
+
const resp = await f(endpoint, {
|
|
456
|
+
method: "POST",
|
|
457
|
+
headers: { "Content-Type": "application/json", Accept: "application/json" },
|
|
458
|
+
body: JSON.stringify(p),
|
|
459
|
+
credentials: cred === "include" || cred === "omit" ? cred : "same-origin",
|
|
460
|
+
redirect: "error",
|
|
461
|
+
});
|
|
462
|
+
if (!resp.ok) {
|
|
463
|
+
this.fail(p, resp.status, `HTTP ${resp.status}`);
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
this.stage = "done";
|
|
467
|
+
this.render();
|
|
468
|
+
this.dispatchEvent(new CustomEvent(SENT_EVENT, { bubbles: true, composed: true, detail: { payload: p, status: resp.status } }));
|
|
469
|
+
} catch (err) {
|
|
470
|
+
this.fail(p, 0, err instanceof Error ? err.message : String(err));
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/** Send the last vote again after an error (no-op otherwise). */
|
|
475
|
+
retry(): void {
|
|
476
|
+
if (this.stage !== "error" || !this.lastPayload) return;
|
|
477
|
+
this.vote = this.lastPayload.value;
|
|
478
|
+
void this.send(this.lastPayload.comment);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Register the element (idempotent; a no-op where there is no
|
|
484
|
+
* `customElements`, such as server rendering). Returns the tag name.
|
|
485
|
+
*/
|
|
486
|
+
export function defineFeedbackElement(tag: string = TAG): string {
|
|
487
|
+
const reg = typeof customElements !== "undefined" ? customElements : null;
|
|
488
|
+
if (reg && !reg.get(tag)) reg.define(tag, tag === TAG ? FeedbackElement : class extends FeedbackElement {});
|
|
489
|
+
return tag;
|
|
490
|
+
}
|