@goflowstate/widget-sdk 0.3.0 → 0.3.2

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.
@@ -1,4 +1,4 @@
1
- // @goflowstate/widget-sdk v0.3.0
1
+ // @goflowstate/widget-sdk v0.3.2
2
2
  // https://github.com/goflowstate/goflowstate-sdk
3
3
 
4
4
  "use strict";
@@ -7,6 +7,9 @@ var CanvasWidgetSDK = (() => {
7
7
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
8
8
  var __getOwnPropNames = Object.getOwnPropertyNames;
9
9
  var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __esm = (fn, res) => function __init() {
11
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
12
+ };
10
13
  var __export = (target, all) => {
11
14
  for (var name in all)
12
15
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -21,6 +24,18 @@ var CanvasWidgetSDK = (() => {
21
24
  };
22
25
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
23
26
 
27
+ // no-rasterizer:modern-screenshot
28
+ var modern_screenshot_exports = {};
29
+ __export(modern_screenshot_exports, {
30
+ domToCanvas: () => domToCanvas
31
+ });
32
+ var domToCanvas;
33
+ var init_modern_screenshot = __esm({
34
+ "no-rasterizer:modern-screenshot"() {
35
+ domToCanvas = () => Promise.reject(new Error("no rasterizer in the script-tag bundle"));
36
+ }
37
+ });
38
+
24
39
  // src/sdk.ts
25
40
  var sdk_exports = {};
26
41
  __export(sdk_exports, {
@@ -62,6 +77,787 @@ var CanvasWidgetSDK = (() => {
62
77
  };
63
78
  }
64
79
 
80
+ // src/feedback.ts
81
+ var FEEDBACK_HOST_ATTR = "data-fs-feedback-host";
82
+ var SCREENSHOT_MAX_BYTES = 15e4;
83
+ var SCREENSHOT_MAX_SIDE = 1280;
84
+ var CAPTURE_TIMEOUT_MS = 4e3;
85
+ function parseColor(raw) {
86
+ const value = (raw ?? "").trim().toLowerCase();
87
+ if (!value || value === "transparent") return null;
88
+ const hex = /^#([0-9a-f]{3,8})$/.exec(value);
89
+ if (hex) {
90
+ const h = hex[1];
91
+ const full = h.length <= 4 ? h.split("").map((c) => c + c).join("") : h;
92
+ if (full.length !== 6 && full.length !== 8) return null;
93
+ const n = (i) => parseInt(full.slice(i, i + 2), 16);
94
+ return [n(0), n(2), n(4), full.length === 8 ? n(6) / 255 : 1];
95
+ }
96
+ const fn = /^rgba?\(([^)]+)\)$/.exec(value);
97
+ if (!fn) return null;
98
+ const parts = fn[1].split(/[\s,/]+/).filter(Boolean);
99
+ if (parts.length < 3) return null;
100
+ const channel = (p) => p.endsWith("%") ? parseFloat(p) * 255 / 100 : parseFloat(p);
101
+ const alpha = parts[3] === void 0 ? 1 : parts[3].endsWith("%") ? parseFloat(parts[3]) / 100 : parseFloat(parts[3]);
102
+ const out = [channel(parts[0]), channel(parts[1]), channel(parts[2]), alpha];
103
+ return out.some((v) => Number.isNaN(v)) ? null : out;
104
+ }
105
+ var opaque = (c) => c !== null && c[3] >= 0.9;
106
+ var css = (c, alpha = c[3]) => alpha >= 1 ? `rgb(${Math.round(c[0])}, ${Math.round(c[1])}, ${Math.round(c[2])})` : `rgba(${Math.round(c[0])}, ${Math.round(c[1])}, ${Math.round(c[2])}, ${alpha})`;
107
+ var mix = (a, b, t) => [
108
+ a[0] + (b[0] - a[0]) * t,
109
+ a[1] + (b[1] - a[1]) * t,
110
+ a[2] + (b[2] - a[2]) * t,
111
+ 1
112
+ ];
113
+ var luminance = (c) => {
114
+ const lin = (v) => {
115
+ const s = v / 255;
116
+ return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
117
+ };
118
+ return 0.2126 * lin(c[0]) + 0.7152 * lin(c[1]) + 0.0722 * lin(c[2]);
119
+ };
120
+ var contrast = (a, b) => {
121
+ const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);
122
+ return (hi + 0.05) / (lo + 0.05);
123
+ };
124
+ var WHITE = [255, 255, 255, 1];
125
+ var BLACK = [17, 17, 20, 1];
126
+ var styleOf = (el) => getComputedStyle(el);
127
+ var rootVar = (doc, names) => {
128
+ for (const el of [doc.documentElement, doc.body]) {
129
+ if (!el) continue;
130
+ const style = styleOf(el);
131
+ for (const name of names) {
132
+ const value = style.getPropertyValue(name).trim();
133
+ if (value) return value;
134
+ }
135
+ }
136
+ return "";
137
+ };
138
+ var visible = (el) => {
139
+ const rect = el.getBoundingClientRect();
140
+ const style = styleOf(el);
141
+ return style.display !== "none" && style.visibility !== "hidden" && rect.width * rect.height >= 0;
142
+ };
143
+ var inOurs = (el) => el.closest(`[${FEEDBACK_HOST_ATTR}]`) !== null;
144
+ var primaryButton = (doc, ground) => {
145
+ let best = null;
146
+ let bestArea = -1;
147
+ doc.querySelectorAll(
148
+ 'button, [role="button"], input[type="submit"], a[class*="btn"], a[class*="cta"]'
149
+ ).forEach((el) => {
150
+ if (inOurs(el) || !visible(el)) return;
151
+ const fill = parseColor(styleOf(el).backgroundColor);
152
+ if (!opaque(fill) || contrast(fill, ground) < 1.25) return;
153
+ const rect = el.getBoundingClientRect();
154
+ const area = rect.width * rect.height;
155
+ if (area > bestArea) {
156
+ best = el;
157
+ bestArea = area;
158
+ }
159
+ });
160
+ return best;
161
+ };
162
+ function readFlowTheme(doc = document) {
163
+ const body = doc.body;
164
+ const stage = body.querySelector(":scope > :not(script):not(style):not([data-fs-feedback-host])");
165
+ const bodyStyle = styleOf(body);
166
+ const sampledGround = [stage, body, doc.documentElement].map((el) => el ? parseColor(styleOf(el).backgroundColor) : null).find(opaque);
167
+ const sampledInk = parseColor((stage ? styleOf(stage) : bodyStyle).color) ?? parseColor(bodyStyle.color);
168
+ const tokenGround = parseColor(rootVar(doc, ["--sb-ground", "--sb-bg", "--color-ink-050"]));
169
+ const ink0 = parseColor(rootVar(doc, ["--fs-feedback-ink"])) ?? sampledInk;
170
+ let ground = parseColor(rootVar(doc, ["--fs-feedback-ground"])) ?? sampledGround ?? (tokenGround && opaque(tokenGround) ? tokenGround : null) ?? (ink0 && luminance(ink0) > 0.5 ? BLACK : WHITE);
171
+ ground = [ground[0], ground[1], ground[2], 1];
172
+ const dark = luminance(ground) < 0.35;
173
+ let ink = ink0 && contrast([ink0[0], ink0[1], ink0[2], 1], ground) >= 4.5 ? ink0 : dark ? WHITE : BLACK;
174
+ ink = [ink[0], ink[1], ink[2], 1];
175
+ const button = primaryButton(doc, ground);
176
+ const buttonStyle = button ? styleOf(button) : null;
177
+ const pinnedCta = parseColor(rootVar(doc, ["--fs-feedback-cta"]));
178
+ const tokenCta = parseColor(rootVar(doc, ["--sb-cta", "--sb-accent", "--color-cream"]));
179
+ const sampledCta = buttonStyle ? parseColor(buttonStyle.backgroundColor) : null;
180
+ const cta = pinnedCta ?? sampledCta ?? (tokenCta && contrast(tokenCta, ground) >= 1.25 ? tokenCta : null) ?? ink;
181
+ const pinnedCtaInk = parseColor(rootVar(doc, ["--fs-feedback-cta-ink"]));
182
+ const sampledCtaInk = !pinnedCta && sampledCta && buttonStyle ? parseColor(buttonStyle.color) : null;
183
+ const ctaInk = pinnedCtaInk ?? (sampledCtaInk && contrast(sampledCtaInk, cta) >= 3 ? sampledCtaInk : null) ?? (luminance(cta) > 0.4 ? BLACK : WHITE);
184
+ const heading = Array.from(doc.querySelectorAll('h1, h2, [class*="title"]')).find(
185
+ (el) => !inOurs(el) && visible(el)
186
+ );
187
+ const headingStyle = heading ? styleOf(heading) : null;
188
+ const fontBody = rootVar(doc, ["--fs-feedback-font"]) || (stage ? styleOf(stage).fontFamily : "") || bodyStyle.fontFamily || rootVar(doc, ["--sb-font-body", "--font-body"]) || "system-ui, sans-serif";
189
+ const fontDisplay = rootVar(doc, ["--fs-feedback-font-display"]) || headingStyle?.fontFamily || rootVar(doc, ["--sb-font-display", "--sb-font-title", "--font-display"]) || fontBody;
190
+ const field = Array.from(
191
+ doc.querySelectorAll('textarea, input[type="text"], input:not([type])')
192
+ ).find((el) => !inOurs(el) && visible(el));
193
+ const px = (value) => {
194
+ const n = parseFloat(value ?? "");
195
+ return Number.isFinite(n) ? n : null;
196
+ };
197
+ const cornerOf = (style) => style ? px(style.borderTopLeftRadius || style.borderRadius) : null;
198
+ const buttonRadius = cornerOf(buttonStyle);
199
+ const fieldRadius = cornerOf(field ? styleOf(field) : null);
200
+ const radius = rootVar(doc, ["--fs-feedback-radius"]) || `${Math.min(fieldRadius ?? Math.min(buttonRadius ?? 16, 22), 28)}px`;
201
+ return {
202
+ ground: css(ground),
203
+ surface: css(dark ? mix(ground, WHITE, 0.07) : mix(ground, WHITE, 0.6)),
204
+ ink: css(ink),
205
+ inkDim: css(mix(ink, ground, 0.32)),
206
+ line: css(ink, 0.16),
207
+ fill: css(ink, 0.05),
208
+ cta: css([cta[0], cta[1], cta[2], 1]),
209
+ ctaInk: css([ctaInk[0], ctaInk[1], ctaInk[2], 1]),
210
+ fontBody,
211
+ fontDisplay,
212
+ displayWeight: headingStyle?.fontWeight || "500",
213
+ radius,
214
+ buttonRadius: buttonRadius === null ? "999px" : `${Math.min(buttonRadius, 999)}px`,
215
+ dark
216
+ };
217
+ }
218
+ var dataUrlBytes = (url) => Math.floor((url.length - url.indexOf(",") - 1) * 3 / 4);
219
+ function encodeScreenshot(canvas) {
220
+ let source = canvas;
221
+ for (let pass = 0; pass < 3; pass += 1) {
222
+ for (const quality of [0.82, 0.7, 0.58, 0.46, 0.34]) {
223
+ const url = source.toDataURL("image/jpeg", quality);
224
+ if (url.startsWith("data:image/jpeg") && dataUrlBytes(url) <= SCREENSHOT_MAX_BYTES)
225
+ return url;
226
+ }
227
+ const smaller = document.createElement("canvas");
228
+ smaller.width = Math.max(1, Math.round(source.width * 0.75));
229
+ smaller.height = Math.max(1, Math.round(source.height * 0.75));
230
+ const ctx = smaller.getContext("2d");
231
+ if (!ctx) return null;
232
+ ctx.drawImage(source, 0, 0, smaller.width, smaller.height);
233
+ source = smaller;
234
+ }
235
+ return null;
236
+ }
237
+ async function captureViewport(ground) {
238
+ const { domToCanvas: domToCanvas2 } = await Promise.resolve().then(() => (init_modern_screenshot(), modern_screenshot_exports));
239
+ const width = window.innerWidth;
240
+ const height = window.innerHeight;
241
+ const scale = Math.min(
242
+ window.devicePixelRatio || 1,
243
+ SCREENSHOT_MAX_SIDE / Math.max(width, height)
244
+ );
245
+ const canvas = await domToCanvas2(document.documentElement, {
246
+ width,
247
+ height,
248
+ scale,
249
+ backgroundColor: ground,
250
+ style: { transform: `translate(${-window.scrollX}px, ${-window.scrollY}px)` },
251
+ filter: (node) => !(node instanceof Element && node.hasAttribute(FEEDBACK_HOST_ATTR))
252
+ });
253
+ return encodeScreenshot(canvas);
254
+ }
255
+ var withTimeout = (work, ms) => new Promise((resolve) => {
256
+ const timer = setTimeout(() => resolve(null), ms);
257
+ work.then(
258
+ (value) => {
259
+ clearTimeout(timer);
260
+ resolve(value);
261
+ },
262
+ () => {
263
+ clearTimeout(timer);
264
+ resolve(null);
265
+ }
266
+ );
267
+ });
268
+ var PLACEMENT_BASE = 16;
269
+ var PLACEMENT_STEP = 8;
270
+ var PLACEMENT_PAD = 8;
271
+ var PLACEMENT_MAX_SHARE = 0.6;
272
+ var PLACEMENT_READ_COST = 240;
273
+ var EMPTY = 0;
274
+ var READ = 1;
275
+ var TAP = 2;
276
+ var SAMPLES = 5;
277
+ function choosePlacement(box, viewportHeight, at) {
278
+ const max = Math.max(PLACEMENT_BASE, viewportHeight * PLACEMENT_MAX_SHARE);
279
+ const span = box.size + PLACEMENT_PAD * 2;
280
+ const offsets = Array.from({ length: SAMPLES }, (_, i) => span * i / (SAMPLES - 1));
281
+ const x0 = box.left - PLACEMENT_PAD;
282
+ let best = PLACEMENT_BASE;
283
+ let bestCost = Infinity;
284
+ for (let bottom = PLACEMENT_BASE; bottom <= max; bottom += PLACEMENT_STEP) {
285
+ if (bottom >= bestCost) break;
286
+ const y0 = box.top - (bottom - box.bottom) - PLACEMENT_PAD;
287
+ let worst = EMPTY;
288
+ for (const dy of offsets) {
289
+ for (const dx of offsets) {
290
+ const found = at(x0 + dx, y0 + dy);
291
+ if (found > worst) worst = found;
292
+ if (worst === TAP) break;
293
+ }
294
+ if (worst === TAP) break;
295
+ }
296
+ if (worst === TAP) continue;
297
+ const cost = bottom + (worst === READ ? PLACEMENT_READ_COST : 0);
298
+ if (cost < bestCost) {
299
+ bestCost = cost;
300
+ best = bottom;
301
+ }
302
+ }
303
+ return best;
304
+ }
305
+ var TAPPABLE = 'a[href],button,input,select,textarea,summary,label,[role="button"],[role="link"],[role="tab"],[role="switch"],[role="checkbox"],[role="radio"],[role="slider"],[role="option"],[tabindex]:not([tabindex="-1"]),[contenteditable=""],[contenteditable="true"],[onclick]';
306
+ var PICTURE = "img,svg,canvas,video,picture";
307
+ var SURFACE_SHARE = 0.8;
308
+ function occupancyReader(host, doc = document) {
309
+ if (typeof doc.elementsFromPoint !== "function") return () => EMPTY;
310
+ const view = doc.defaultView;
311
+ const width = view?.innerWidth ?? 1;
312
+ const height = view?.innerHeight ?? 1;
313
+ const seen = /* @__PURE__ */ new Map();
314
+ const read = (el) => {
315
+ const known = seen.get(el);
316
+ if (known) return known;
317
+ const style = view ? view.getComputedStyle(el) : null;
318
+ let tap = el.matches(TAPPABLE) || style?.cursor === "pointer";
319
+ if (tap) {
320
+ const r = el.getBoundingClientRect();
321
+ if (r.width * r.height >= width * height * SURFACE_SHARE) tap = false;
322
+ }
323
+ const text = [];
324
+ if (typeof doc.createRange === "function") {
325
+ for (const node of Array.from(el.childNodes)) {
326
+ if (node.nodeType !== 3 || !node.nodeValue || !node.nodeValue.trim()) continue;
327
+ const range = doc.createRange();
328
+ range.selectNodeContents(node);
329
+ if (typeof range.getClientRects === "function")
330
+ text.push(...Array.from(range.getClientRects()));
331
+ }
332
+ }
333
+ const ground = parseColor(style?.backgroundColor);
334
+ const image = style?.backgroundImage ?? "";
335
+ const reading = {
336
+ tap,
337
+ picture: el.matches(PICTURE) || el.closest("svg") !== null,
338
+ text,
339
+ opaque: ground !== null && ground[3] >= 0.5 || image !== "" && image !== "none"
340
+ };
341
+ seen.set(el, reading);
342
+ return reading;
343
+ };
344
+ return (x, y) => {
345
+ const cx = Math.min(Math.max(x, 0), width - 1);
346
+ const cy = Math.min(Math.max(y, 0), height - 1);
347
+ const stack = doc.elementsFromPoint(cx, cy).filter((el) => el !== host && el !== doc.documentElement && el !== doc.body);
348
+ for (let el = stack[0] ?? null; el && el !== doc.body; el = el.parentElement) {
349
+ if (read(el).tap) return TAP;
350
+ }
351
+ for (const el of stack) {
352
+ const r = read(el);
353
+ if (r.picture || r.text.some((t) => cx >= t.left && cx <= t.right && cy >= t.top && cy <= t.bottom))
354
+ return READ;
355
+ if (r.opaque) break;
356
+ }
357
+ return EMPTY;
358
+ };
359
+ }
360
+ var BUTTON_SIZE = 44;
361
+ var TAB_WIDTH = 26;
362
+ var TAB_HEIGHT = 54;
363
+ var EDGE_MARGIN = 12;
364
+ var DOCK_PAST = 14;
365
+ var SPOT_KEY = "fs-feedback-spot";
366
+ var projectMomentum = (v, deceleration = 0.992) => v / 1e3 * deceleration / (1 - deceleration);
367
+ var rubberband = (overshoot, dimension, constant = 0.55) => overshoot * dimension * constant / (dimension + constant * Math.abs(overshoot));
368
+ var clampY = (y, height, view) => Math.min(
369
+ Math.max(y, view.top + EDGE_MARGIN),
370
+ Math.max(view.top + EDGE_MARGIN, view.height - view.bottom - EDGE_MARGIN - height)
371
+ );
372
+ function restingSpot(state, left, y, view) {
373
+ if (state === "float") {
374
+ return {
375
+ state,
376
+ x: left ? EDGE_MARGIN : view.width - BUTTON_SIZE - EDGE_MARGIN,
377
+ y: clampY(y, BUTTON_SIZE, view)
378
+ };
379
+ }
380
+ return { state, x: left ? 0 : view.width - TAB_WIDTH, y: clampY(y, TAB_HEIGHT, view) };
381
+ }
382
+ function settleRelease(from, velocity, view) {
383
+ const py = from.y + projectMomentum(velocity.y);
384
+ if (from.state !== "float") return restingSpot(from.state, from.state === "dock-left", py, view);
385
+ const px = from.x + projectMomentum(velocity.x);
386
+ const maxX = view.width - BUTTON_SIZE;
387
+ const tabY = py + (BUTTON_SIZE - TAB_HEIGHT) / 2;
388
+ if (px < -DOCK_PAST || from.x < 3) return restingSpot("dock-left", true, tabY, view);
389
+ if (px > maxX + DOCK_PAST || from.x > maxX - 3)
390
+ return restingSpot("dock-right", false, tabY, view);
391
+ return restingSpot("float", px + BUTTON_SIZE / 2 < view.width / 2, py, view);
392
+ }
393
+ var BUG_ICON = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="8" y="7" width="8" height="12" rx="4"/><path d="M9.5 7a2.5 2.5 0 0 1 5 0M12 11v8M4 13h4M16 13h4M5 7l3.2 2.4M19 7l-3.2 2.4M5 20l3.2-2.6M19 20l-3.2-2.6"/></svg>';
394
+ var STYLE = `
395
+ :host { all: initial; }
396
+ * { box-sizing: border-box; }
397
+ .bug {
398
+ position: fixed; z-index: 2147483000; left: 0; top: 0;
399
+ width: 44px; height: 44px; border-radius: 22px; padding: 0; margin: 0;
400
+ display: var(--fs-feedback-display, grid); place-items: center; cursor: grab;
401
+ background: var(--surface); color: var(--ink); border: 1px solid var(--line);
402
+ box-shadow: 0 10px 24px rgba(0, 0, 0, 0.2), 0 2px 5px rgba(0, 0, 0, 0.12);
403
+ -webkit-tap-highlight-color: transparent; touch-action: none; user-select: none; -webkit-user-select: none;
404
+ will-change: transform;
405
+ }
406
+ @supports ((-webkit-backdrop-filter: blur(1px)) or (backdrop-filter: blur(1px))) {
407
+ .bug {
408
+ background: color-mix(in srgb, var(--surface) 74%, transparent);
409
+ -webkit-backdrop-filter: blur(14px) saturate(160%); backdrop-filter: blur(14px) saturate(160%);
410
+ }
411
+ }
412
+ .bug svg { width: 20px; height: 20px; pointer-events: none; }
413
+ .bug.down svg { transform: scale(0.9); }
414
+ .bug.dragging { cursor: grabbing; box-shadow: 0 18px 36px rgba(0, 0, 0, 0.26), 0 3px 8px rgba(0, 0, 0, 0.14); }
415
+ .bug[data-state^="dock"] { width: 26px; height: 54px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.14); opacity: 0.92; }
416
+ .bug[data-state="dock-right"] { border-radius: 16px 0 0 16px; border-right-width: 0; }
417
+ .bug[data-state="dock-left"] { border-radius: 0 16px 16px 0; border-left-width: 0; }
418
+ .bug[data-state^="dock"] svg { width: 16px; height: 16px; }
419
+ .probe {
420
+ position: fixed; visibility: hidden; pointer-events: none; width: 0; height: 0;
421
+ padding: env(safe-area-inset-top, 0px) 0 env(safe-area-inset-bottom, 0px) 0;
422
+ }
423
+ .tuck { align-self: center; font-size: 13px; color: var(--ink-dim); }
424
+ @media (prefers-reduced-transparency: reduce) {
425
+ .bug { background: var(--surface); -webkit-backdrop-filter: none; backdrop-filter: none; }
426
+ }
427
+ @media (prefers-contrast: more) {
428
+ .bug { background: var(--surface); border-color: var(--ink); -webkit-backdrop-filter: none; backdrop-filter: none; }
429
+ }
430
+ .bug:focus-visible, .send:focus-visible, .link:focus-visible, textarea:focus-visible {
431
+ outline: 2px solid var(--ink); outline-offset: 2px;
432
+ }
433
+ .scrim { position: fixed; inset: 0; z-index: 2147483001; background: rgba(0, 0, 0, 0.42); }
434
+ .sheet {
435
+ position: fixed; left: 0; right: 0; bottom: 0; z-index: 2147483002;
436
+ margin: 0 auto; max-width: 480px;
437
+ background: var(--surface); color: var(--ink); border: 1px solid var(--line); border-bottom: 0;
438
+ border-radius: calc(var(--radius) + 6px) calc(var(--radius) + 6px) 0 0;
439
+ padding: 12px 18px calc(20px + env(safe-area-inset-bottom, 0px));
440
+ display: flex; flex-direction: column; gap: 12px;
441
+ font-family: var(--font-body); font-size: 15px; line-height: 1.4;
442
+ }
443
+ .grab { width: 36px; height: 4px; border-radius: 2px; background: var(--line); align-self: center; }
444
+ h2 { margin: 0; font-family: var(--font-display); font-weight: var(--display-weight); font-size: 19px; line-height: 1.2; }
445
+ .shot { display: flex; gap: 12px; align-items: center; font-size: 13px; color: var(--ink-dim); }
446
+ .shot img { width: 50px; max-height: 108px; object-fit: cover; object-position: top; border-radius: 8px; border: 1px solid var(--line); }
447
+ .link { background: none; border: 0; padding: 0; font: inherit; color: var(--ink); text-decoration: underline; text-underline-offset: 3px; cursor: pointer; }
448
+ textarea {
449
+ width: 100%; min-height: 84px; resize: none; font: inherit; font-size: 16px; color: var(--ink);
450
+ background: var(--fill); border: 1px solid var(--line); border-radius: var(--radius); padding: 13px 15px;
451
+ }
452
+ textarea::placeholder { color: var(--ink-dim); }
453
+ .send {
454
+ font: inherit; font-weight: 500; cursor: pointer; border: 0; padding: 14px;
455
+ background: var(--cta); color: var(--cta-ink); border-radius: var(--button-radius);
456
+ }
457
+ .send[disabled] { opacity: 0.5; cursor: default; }
458
+ .status { margin: 0; font-size: 13px; color: var(--ink-dim); min-height: 1.4em; }
459
+ [hidden] { display: none !important; }
460
+ @media (prefers-reduced-motion: no-preference) {
461
+ .bug {
462
+ transition: width 0.22s cubic-bezier(0.2, 0.8, 0.2, 1), height 0.22s cubic-bezier(0.2, 0.8, 0.2, 1),
463
+ border-radius 0.22s cubic-bezier(0.2, 0.8, 0.2, 1), box-shadow 0.22s ease;
464
+ }
465
+ .bug svg { transition: transform 0.1s ease-out, width 0.22s ease, height 0.22s ease; }
466
+ .sheet { animation: rise 0.22s ease-out; }
467
+ @keyframes rise { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
468
+ }
469
+ `;
470
+ var mounted = null;
471
+ var feedbackOptedOut = (doc = document) => doc.documentElement.getAttribute("data-fs-feedback") === "off";
472
+ var SCROLL_OPTIONS = { capture: true, passive: true };
473
+ function mountFlowFeedback(options) {
474
+ if (typeof document === "undefined" || !document.body) return null;
475
+ if (mounted) return mounted;
476
+ if (feedbackOptedOut()) return null;
477
+ const host = document.createElement("div");
478
+ host.setAttribute(FEEDBACK_HOST_ATTR, "");
479
+ const root = host.attachShadow({ mode: "open" });
480
+ root.innerHTML = `<style>${STYLE}</style>
481
+ <button type="button" class="bug" data-state="float" aria-label="Send feedback on this page">${BUG_ICON}</button>
482
+ <div class="probe" aria-hidden="true"></div>
483
+ <div class="scrim" hidden></div>
484
+ <div class="sheet" role="dialog" aria-modal="true" aria-labelledby="fsf-title" hidden>
485
+ <div class="grab" aria-hidden="true"></div>
486
+ <h2 id="fsf-title">Feedback on this page</h2>
487
+ <div class="shot" hidden><img alt="Screenshot of this page" /><span><span>Screenshot attached</span><br /><button type="button" class="link remove">Remove screenshot</button></span></div>
488
+ <textarea aria-labelledby="fsf-title" placeholder="What should change?" maxlength="2000"></textarea>
489
+ <p class="status" role="status" aria-live="polite"></p>
490
+ <button type="button" class="send" disabled>Send feedback</button>
491
+ <button type="button" class="link tuck">Tuck the button to the side</button>
492
+ </div>`;
493
+ const q = (selector) => root.querySelector(selector);
494
+ const bug = q(".bug");
495
+ const scrim = q(".scrim");
496
+ const sheet = q(".sheet");
497
+ const shot = q(".shot");
498
+ const img = q(".shot img");
499
+ const remove = q(".remove");
500
+ const text = q("textarea");
501
+ const status = q(".status");
502
+ const send = q(".send");
503
+ const tuck = q(".tuck");
504
+ const probe = q(".probe");
505
+ let theme = readFlowTheme();
506
+ let screenshot = null;
507
+ let busy = false;
508
+ let closeTimer = null;
509
+ let placeTimer = null;
510
+ let autoBottom = PLACEMENT_BASE;
511
+ let spot = { state: "float", x: 0, y: 0 };
512
+ let placedByPerson = false;
513
+ let velocity = { x: 0, y: 0 };
514
+ let frame = 0;
515
+ const px = (name) => {
516
+ const raw = getComputedStyle(host).getPropertyValue(name).trim();
517
+ const n = raw ? parseFloat(raw) : NaN;
518
+ return Number.isFinite(n) ? n : null;
519
+ };
520
+ const viewport = () => {
521
+ const style = getComputedStyle(probe);
522
+ return {
523
+ width: window.innerWidth,
524
+ height: window.innerHeight,
525
+ top: parseFloat(style.paddingTop) || 0,
526
+ bottom: parseFloat(style.paddingBottom) || 0
527
+ };
528
+ };
529
+ const draw = () => {
530
+ bug.style.transform = `translate3d(${spot.x}px, ${spot.y}px, 0)`;
531
+ };
532
+ const setState = (next) => {
533
+ spot.state = next;
534
+ bug.dataset.state = next;
535
+ };
536
+ const remember = (spot2) => {
537
+ if (!placedByPerson) return;
538
+ try {
539
+ const view = viewport();
540
+ const width = spot2.state === "float" ? BUTTON_SIZE : TAB_WIDTH;
541
+ localStorage.setItem(
542
+ SPOT_KEY,
543
+ JSON.stringify({
544
+ state: spot2.state,
545
+ left: spot2.x + width / 2 < view.width / 2,
546
+ y: view.height > 0 ? spot2.y / view.height : 0
547
+ })
548
+ );
549
+ } catch {
550
+ }
551
+ };
552
+ const still = () => typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
553
+ const springTo = (to, flicked) => {
554
+ cancelAnimationFrame(frame);
555
+ frame = 0;
556
+ setState(to.state);
557
+ remember(to);
558
+ if (still() || typeof requestAnimationFrame !== "function") {
559
+ spot = { ...to };
560
+ velocity = { x: 0, y: 0 };
561
+ draw();
562
+ return;
563
+ }
564
+ const omega = 2 * Math.PI / 0.4;
565
+ const k = omega * omega;
566
+ const c = 2 * (flicked ? 0.8 : 1) * omega;
567
+ let last = performance.now();
568
+ const tick = (now) => {
569
+ const dt = Math.min(0.032, (now - last) / 1e3);
570
+ last = now;
571
+ velocity.x += (-k * (spot.x - to.x) - c * velocity.x) * dt;
572
+ velocity.y += (-k * (spot.y - to.y) - c * velocity.y) * dt;
573
+ spot.x += velocity.x * dt;
574
+ spot.y += velocity.y * dt;
575
+ const close2 = Math.abs(spot.x - to.x) < 0.3 && Math.abs(spot.y - to.y) < 0.3 && Math.hypot(velocity.x, velocity.y) < 6;
576
+ if (close2) {
577
+ spot = { ...to };
578
+ velocity = { x: 0, y: 0 };
579
+ frame = 0;
580
+ draw();
581
+ return;
582
+ }
583
+ draw();
584
+ frame = requestAnimationFrame(tick);
585
+ };
586
+ frame = requestAnimationFrame(tick);
587
+ };
588
+ const place = () => {
589
+ placeTimer = null;
590
+ if (!sheet.hidden || bug.hidden || drag || frame) return;
591
+ const view = viewport();
592
+ if (placedByPerson || spot.state !== "float") {
593
+ const width = spot.state === "float" ? BUTTON_SIZE : TAB_WIDTH;
594
+ spot = restingSpot(spot.state, spot.x + width / 2 < view.width / 2, spot.y, view);
595
+ draw();
596
+ return;
597
+ }
598
+ const right = px("--fs-feedback-right") ?? 14;
599
+ const left = view.width - right - BUTTON_SIZE;
600
+ const pinned = px("--fs-feedback-bottom");
601
+ if (pinned === null && typeof document.elementsFromPoint === "function") {
602
+ autoBottom = choosePlacement(
603
+ {
604
+ left,
605
+ top: view.height - view.bottom - autoBottom - BUTTON_SIZE,
606
+ size: BUTTON_SIZE,
607
+ bottom: autoBottom
608
+ },
609
+ view.height,
610
+ occupancyReader(host)
611
+ );
612
+ }
613
+ const bottom = pinned ?? autoBottom;
614
+ spot = { state: "float", x: left, y: view.height - view.bottom - bottom - BUTTON_SIZE };
615
+ draw();
616
+ };
617
+ const schedulePlace = () => {
618
+ if (placeTimer) clearTimeout(placeTimer);
619
+ placeTimer = setTimeout(place, 150);
620
+ };
621
+ let drag = null;
622
+ let suppressClick = false;
623
+ const onDown = (event) => {
624
+ if (!sheet.hidden || busy) return;
625
+ bug.setPointerCapture?.(event.pointerId);
626
+ cancelAnimationFrame(frame);
627
+ frame = 0;
628
+ velocity = { x: 0, y: 0 };
629
+ drag = {
630
+ id: event.pointerId,
631
+ ox: event.clientX - spot.x,
632
+ oy: event.clientY - spot.y,
633
+ sx: event.clientX,
634
+ sy: event.clientY,
635
+ moved: false,
636
+ history: [{ t: event.timeStamp, x: event.clientX, y: event.clientY }]
637
+ };
638
+ bug.classList.add("down");
639
+ };
640
+ const onMove = (event) => {
641
+ if (!drag || event.pointerId !== drag.id) return;
642
+ const dx = event.clientX - drag.sx;
643
+ const dy = event.clientY - drag.sy;
644
+ if (!drag.moved && Math.hypot(dx, dy) < 8) return;
645
+ if (!drag.moved) {
646
+ drag.moved = true;
647
+ bug.classList.add("dragging");
648
+ }
649
+ drag.history.push({ t: event.timeStamp, x: event.clientX, y: event.clientY });
650
+ if (drag.history.length > 6) drag.history.shift();
651
+ const view = viewport();
652
+ if (spot.state !== "float") {
653
+ const inward = spot.state === "dock-left" ? dx : -dx;
654
+ if (inward <= 18) {
655
+ spot = restingSpot(spot.state, spot.state === "dock-left", event.clientY - drag.oy, view);
656
+ draw();
657
+ return;
658
+ }
659
+ setState("float");
660
+ drag.ox = BUTTON_SIZE / 2;
661
+ drag.oy = BUTTON_SIZE / 2;
662
+ }
663
+ let x = event.clientX - drag.ox;
664
+ let y = event.clientY - drag.oy;
665
+ const maxX = view.width - BUTTON_SIZE;
666
+ const maxY = view.height - BUTTON_SIZE;
667
+ if (x < 0) x = rubberband(x, 60);
668
+ else if (x > maxX) x = maxX + rubberband(x - maxX, 60);
669
+ if (y < 0) y = rubberband(y, 80);
670
+ else if (y > maxY) y = maxY + rubberband(y - maxY, 80);
671
+ spot.x = x;
672
+ spot.y = y;
673
+ draw();
674
+ };
675
+ const onUp = (event) => {
676
+ if (!drag || event.pointerId !== drag.id) return;
677
+ const ended = drag;
678
+ drag = null;
679
+ bug.classList.remove("down", "dragging");
680
+ if (!ended.moved) return;
681
+ suppressClick = true;
682
+ setTimeout(() => {
683
+ suppressClick = false;
684
+ }, 0);
685
+ const first = ended.history[0];
686
+ const last = ended.history[ended.history.length - 1];
687
+ const dt = Math.max(1, last.t - first.t);
688
+ const paused = event.timeStamp - last.t > 90;
689
+ velocity = paused ? { x: 0, y: 0 } : { x: (last.x - first.x) / dt * 1e3, y: (last.y - first.y) / dt * 1e3 };
690
+ placedByPerson = true;
691
+ springTo(settleRelease(spot, velocity, viewport()), Math.hypot(velocity.x, velocity.y) > 350);
692
+ };
693
+ const observer = typeof MutationObserver === "function" ? new MutationObserver((records) => {
694
+ if (records.some((r) => r.target !== host)) schedulePlace();
695
+ }) : null;
696
+ const paint = () => {
697
+ theme = readFlowTheme();
698
+ const vars = {
699
+ "--surface": theme.surface,
700
+ "--ink": theme.ink,
701
+ "--ink-dim": theme.inkDim,
702
+ "--line": theme.line,
703
+ "--fill": theme.fill,
704
+ "--cta": theme.cta,
705
+ "--cta-ink": theme.ctaInk,
706
+ "--font-body": theme.fontBody,
707
+ "--font-display": theme.fontDisplay,
708
+ "--display-weight": theme.displayWeight,
709
+ "--radius": theme.radius,
710
+ "--button-radius": theme.buttonRadius
711
+ };
712
+ for (const [name, value] of Object.entries(vars)) host.style.setProperty(name, value);
713
+ host.style.colorScheme = theme.dark ? "dark" : "light";
714
+ };
715
+ const sync = () => {
716
+ send.disabled = busy || text.value.trim().length === 0;
717
+ };
718
+ const setShot = (url) => {
719
+ screenshot = url;
720
+ shot.hidden = url === null;
721
+ if (url) img.src = url;
722
+ else img.removeAttribute("src");
723
+ };
724
+ const close = () => {
725
+ if (closeTimer) clearTimeout(closeTimer);
726
+ closeTimer = null;
727
+ sheet.hidden = true;
728
+ scrim.hidden = true;
729
+ bug.hidden = false;
730
+ setShot(null);
731
+ text.value = "";
732
+ status.textContent = "";
733
+ busy = false;
734
+ sync();
735
+ schedulePlace();
736
+ };
737
+ const open = async () => {
738
+ if (!sheet.hidden || busy) return;
739
+ busy = true;
740
+ paint();
741
+ bug.hidden = true;
742
+ const capture = options.capture ?? (() => captureViewport(theme.ground));
743
+ const url = await withTimeout(Promise.resolve().then(capture), CAPTURE_TIMEOUT_MS);
744
+ busy = false;
745
+ setShot(url);
746
+ tuck.hidden = spot.state !== "float";
747
+ scrim.hidden = false;
748
+ sheet.hidden = false;
749
+ sync();
750
+ text.focus();
751
+ };
752
+ const submit = async () => {
753
+ const message = text.value.trim();
754
+ if (!message || busy) return;
755
+ busy = true;
756
+ sync();
757
+ status.textContent = "";
758
+ try {
759
+ await options.submit({
760
+ message,
761
+ ...screenshot ? { screenshot } : {},
762
+ ...document.body.dataset.pageKey ? { pageKey: document.body.dataset.pageKey } : {},
763
+ pagePath: window.location.pathname.slice(-200),
764
+ viewport: { width: window.innerWidth, height: window.innerHeight }
765
+ });
766
+ status.textContent = "Sent.";
767
+ closeTimer = setTimeout(() => {
768
+ close();
769
+ bug.focus();
770
+ }, 1200);
771
+ } catch {
772
+ busy = false;
773
+ status.textContent = "That did not send. Try again.";
774
+ sync();
775
+ }
776
+ };
777
+ bug.addEventListener("click", () => {
778
+ if (!suppressClick) void open();
779
+ });
780
+ bug.addEventListener("pointerdown", onDown);
781
+ bug.addEventListener("pointermove", onMove);
782
+ bug.addEventListener("pointerup", onUp);
783
+ bug.addEventListener("pointercancel", onUp);
784
+ tuck.addEventListener("click", () => {
785
+ const view = viewport();
786
+ const left = spot.x + BUTTON_SIZE / 2 < view.width / 2;
787
+ close();
788
+ placedByPerson = true;
789
+ springTo(
790
+ restingSpot(
791
+ left ? "dock-left" : "dock-right",
792
+ left,
793
+ spot.y + (BUTTON_SIZE - TAB_HEIGHT) / 2,
794
+ view
795
+ ),
796
+ false
797
+ );
798
+ });
799
+ scrim.addEventListener("click", close);
800
+ remove.addEventListener("click", () => setShot(null));
801
+ text.addEventListener("input", sync);
802
+ send.addEventListener("click", () => void submit());
803
+ sheet.addEventListener("keydown", (event) => {
804
+ if (event.key === "Escape") {
805
+ close();
806
+ bug.focus();
807
+ }
808
+ event.stopPropagation();
809
+ });
810
+ document.body.appendChild(host);
811
+ paint();
812
+ try {
813
+ const saved = JSON.parse(localStorage.getItem(SPOT_KEY) ?? "null");
814
+ if (saved && typeof saved.y === "number" && (saved.state === "float" || saved.state === "dock-left" || saved.state === "dock-right")) {
815
+ const view = viewport();
816
+ placedByPerson = true;
817
+ setState(saved.state);
818
+ spot = restingSpot(saved.state, saved.left === true, saved.y * view.height, view);
819
+ }
820
+ } catch {
821
+ }
822
+ place();
823
+ schedulePlace();
824
+ observer?.observe(document.body, {
825
+ childList: true,
826
+ subtree: true,
827
+ attributes: true,
828
+ attributeFilter: ["class", "style", "hidden"]
829
+ });
830
+ window.addEventListener("resize", schedulePlace);
831
+ window.addEventListener("load", schedulePlace);
832
+ window.addEventListener("scroll", schedulePlace, SCROLL_OPTIONS);
833
+ document.addEventListener("transitionend", schedulePlace, SCROLL_OPTIONS);
834
+ document.addEventListener("animationend", schedulePlace, SCROLL_OPTIONS);
835
+ options.onReady?.();
836
+ mounted = {
837
+ host,
838
+ refresh: () => {
839
+ if (sheet.hidden) paint();
840
+ },
841
+ open,
842
+ close,
843
+ unmount: () => {
844
+ if (closeTimer) clearTimeout(closeTimer);
845
+ if (placeTimer) clearTimeout(placeTimer);
846
+ cancelAnimationFrame(frame);
847
+ observer?.disconnect();
848
+ window.removeEventListener("resize", schedulePlace);
849
+ window.removeEventListener("load", schedulePlace);
850
+ window.removeEventListener("scroll", schedulePlace, SCROLL_OPTIONS);
851
+ document.removeEventListener("transitionend", schedulePlace, SCROLL_OPTIONS);
852
+ document.removeEventListener("animationend", schedulePlace, SCROLL_OPTIONS);
853
+ host.remove();
854
+ mounted = null;
855
+ }
856
+ };
857
+ return mounted;
858
+ }
859
+ var unmountFlowFeedback = () => mounted?.unmount();
860
+
65
861
  // src/session.ts
66
862
  function readSessionLaunchParams() {
67
863
  if (typeof window === "undefined") return null;
@@ -149,11 +945,13 @@ var CanvasWidgetSDK = (() => {
149
945
  apiBase;
150
946
  tokenPromise;
151
947
  opener;
948
+ hostOrigin;
152
949
  constructor(params, opener) {
153
950
  this.slug = params.slug;
154
951
  this.webBase = params.webBase;
155
952
  this.mobileBase = params.mobileBase ?? null;
156
953
  this.apiBase = params.apiBase;
954
+ this.hostOrigin = hostOriginOf(params);
157
955
  this.opener = opener ?? parentOpener(hostOriginOf(params));
158
956
  this.tokenPromise = this.acquireTicket(params).then((t) => this.redeem(t));
159
957
  }
@@ -187,13 +985,32 @@ var CanvasWidgetSDK = (() => {
187
985
  }
188
986
  const json = await res.json();
189
987
  const data = json.data;
190
- const view = toStateView(data.state ?? data);
988
+ const wire = data.state ?? data;
989
+ const view = toStateView(wire);
191
990
  this.latestClaims = view.viewer?.claims ?? null;
991
+ this.syncFeedback(wire.feedbackEnabled === true);
192
992
  return view;
193
993
  }
194
994
  get claims() {
195
995
  return this.latestClaims;
196
996
  }
997
+ /** Flow feedback: every state read says whether the bug icon is on for this ring. On, the
998
+ * icon mounts once and re-reads the page's look on each read; off, it leaves. */
999
+ syncFeedback(enabled) {
1000
+ if (typeof document === "undefined") return;
1001
+ if (!enabled) {
1002
+ unmountFlowFeedback();
1003
+ return;
1004
+ }
1005
+ const handle = mountFlowFeedback({
1006
+ submit: (report) => this.request("feedback", report),
1007
+ onReady: () => {
1008
+ if (window.parent !== window)
1009
+ window.parent.postMessage({ type: "flowstate:feedback-ready" }, this.hostOrigin);
1010
+ }
1011
+ });
1012
+ handle?.refresh();
1013
+ }
197
1014
  join() {
198
1015
  return this.call("/enter", { method: "POST" });
199
1016
  }