@tokenoftrust/storefront-runner 6.12.16 → 6.12.18
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/apps/storefront/package.json +1 -0
- package/package.json +1 -1
- package/packages/feedback/fixtures/v1/expected-findings.json +16 -0
- package/packages/feedback/fixtures/v1/feedback-action/invalid/carries-transport-credential.json +14 -0
- package/packages/feedback/fixtures/v1/feedback-action/invalid/control-char-in-description.json +13 -0
- package/packages/feedback/fixtures/v1/feedback-action/invalid/legacy-schema-version.json +13 -0
- package/packages/feedback/fixtures/v1/feedback-action/invalid/page-path-with-query.json +14 -0
- package/packages/feedback/fixtures/v1/feedback-action/invalid/title-too-short.json +13 -0
- package/packages/feedback/fixtures/v1/feedback-action/valid/element-problem.json +33 -0
- package/packages/feedback/fixtures/v1/feedback-action/valid/page-comment.json +20 -0
- package/packages/feedback/fixtures/v1/feedback-action/valid/region-improvement.json +24 -0
- package/packages/feedback/fixtures/v1/feedback-action/valid/text-selection-feature.json +30 -0
- package/packages/feedback/fixtures/v1/submission-receipt/invalid/accepted-at-not-utc.json +9 -0
- package/packages/feedback/fixtures/v1/submission-receipt/invalid/bad-feedback-id.json +9 -0
- package/packages/feedback/fixtures/v1/submission-receipt/invalid/legacy-contract-version.json +9 -0
- package/packages/feedback/fixtures/v1/submission-receipt/invalid/missing-replayed.json +8 -0
- package/packages/feedback/fixtures/v1/submission-receipt/valid/accepted.json +9 -0
- package/packages/feedback/fixtures/v1/submission-receipt/valid/fractional-seconds.json +9 -0
- package/packages/feedback/fixtures/v1/submission-receipt/valid/replayed.json +9 -0
- package/packages/feedback/fixtures/v1/target-evidence/invalid/page-kind-claims-structure.json +6 -0
- package/packages/feedback/fixtures/v1/target-evidence/invalid/path-too-deep.json +17 -0
- package/packages/feedback/fixtures/v1/target-evidence/invalid/relative-region-out-of-range.json +11 -0
- package/packages/feedback/fixtures/v1/target-evidence/invalid/selector-uses-class.json +7 -0
- package/packages/feedback/fixtures/v1/target-evidence/invalid/source-precision-without-anchor.json +6 -0
- package/packages/feedback/fixtures/v1/target-evidence/valid/element-structure.json +21 -0
- package/packages/feedback/fixtures/v1/target-evidence/valid/host-iframe.json +14 -0
- package/packages/feedback/fixtures/v1/target-evidence/valid/source-anchor.json +12 -0
- package/packages/feedback/fixtures/v1/target-evidence/valid/text-selection.json +26 -0
- package/packages/feedback/package.json +29 -0
- package/packages/feedback/src/index.ts +1 -0
- package/packages/feedback/src/primitives/composer.ts +639 -0
- package/packages/feedback/src/primitives/dialog.ts +263 -0
- package/packages/feedback/src/primitives/hostAdapter.ts +116 -0
- package/packages/feedback/src/primitives/index.ts +10 -0
- package/packages/feedback/src/primitives/launcher.ts +81 -0
- package/packages/feedback/src/primitives/picker.ts +663 -0
- package/packages/feedback/src/primitives/redaction.ts +204 -0
- package/packages/feedback/src/primitives/selection.ts +49 -0
- package/packages/feedback/src/primitives/shiftAccelerator.ts +160 -0
- package/packages/feedback/src/primitives/styles.ts +64 -0
- package/packages/feedback/src/primitives/surface.ts +124 -0
- package/packages/feedback/src/primitives/target.ts +293 -0
- package/packages/feedback/src/v1/guards.ts +318 -0
- package/packages/feedback/src/v1/index.ts +2 -0
- package/packages/feedback/src/v1/types.ts +177 -0
- package/packages/feedback/tsconfig.json +9 -0
- package/packages/feedback/vitest.config.cts +20 -0
- package/pnpm-lock.runner.yaml +15 -0
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The feedback dialog: a non-blocking panel that opens from the launcher, the API, or a guarded
|
|
3
|
+
* double-Shift, and hands focus back to where it came from when it closes.
|
|
4
|
+
*
|
|
5
|
+
* The double-Shift guard (see shiftAccelerator.ts) ignores key repeat, modifier combinations, IME
|
|
6
|
+
* composition, typing in inputs/textareas/contenteditable, the dialog's own events, and any Shift
|
|
7
|
+
* tap while the dialog is already open. It never calls preventDefault.
|
|
8
|
+
*/
|
|
9
|
+
import { isSurfacePermitted, type HostAdapter } from "./hostAdapter.js";
|
|
10
|
+
import { attachDoubleShift, type DoubleShiftBinding } from "./shiftAccelerator.js";
|
|
11
|
+
import { createSurface, eventFromHost, h } from "./surface.js";
|
|
12
|
+
|
|
13
|
+
export type DialogTrigger = "shortcut" | "api";
|
|
14
|
+
export type DialogCloseReason = "escape" | "close-button" | "api" | "unmount";
|
|
15
|
+
|
|
16
|
+
export interface DialogOptions {
|
|
17
|
+
win: Window;
|
|
18
|
+
doc: Document;
|
|
19
|
+
adapter?: HostAdapter;
|
|
20
|
+
title: string;
|
|
21
|
+
/**
|
|
22
|
+
* Trap Tab inside the dialog and mark it `aria-modal`. `"narrow"` does so only while the viewport
|
|
23
|
+
* is in the narrow bottom-sheet layout, re-evaluated on resize. Default false.
|
|
24
|
+
*/
|
|
25
|
+
modal?: boolean | "narrow";
|
|
26
|
+
/** Install the guarded double-Shift opener on mount. Default true (also needs the "shortcut" surface). */
|
|
27
|
+
shortcut?: boolean | { windowMs?: number; cooldownMs?: number };
|
|
28
|
+
/**
|
|
29
|
+
* Offer Move left / Move right / Reset position buttons and a title-bar drag so the dialog can be
|
|
30
|
+
* shifted off whatever it covers. Inactive in the narrow bottom-sheet layout. Default false.
|
|
31
|
+
*/
|
|
32
|
+
movable?: boolean;
|
|
33
|
+
/** Extra veto for the shortcut (for example, while the picker is active). */
|
|
34
|
+
isShortcutBlocked?(): boolean;
|
|
35
|
+
/** Where focus goes on close when the element that had it is gone (usually the launcher button). */
|
|
36
|
+
focusFallback?(): HTMLElement | null;
|
|
37
|
+
onOpen?(trigger: DialogTrigger): void;
|
|
38
|
+
onClose?(reason: DialogCloseReason): void;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface Dialog {
|
|
42
|
+
/** Attach the surface and install listeners. Idempotent. Returns false when not permitted. */
|
|
43
|
+
mount(): boolean;
|
|
44
|
+
/** Close if open, remove listeners and the surface. Idempotent. */
|
|
45
|
+
unmount(): void;
|
|
46
|
+
/**
|
|
47
|
+
* Open. `returnFocus` names the element to refocus on close; default is the page element that
|
|
48
|
+
* had focus. A no-op that returns false when already open or not permitted.
|
|
49
|
+
*/
|
|
50
|
+
open(trigger?: DialogTrigger, returnFocus?: HTMLElement | null): boolean;
|
|
51
|
+
close(reason?: DialogCloseReason): void;
|
|
52
|
+
readonly isOpen: boolean;
|
|
53
|
+
readonly isMounted: boolean;
|
|
54
|
+
/** Container for the dialog's content. */
|
|
55
|
+
readonly body: HTMLElement;
|
|
56
|
+
/** The dialog element itself (role="dialog"). */
|
|
57
|
+
readonly element: HTMLElement;
|
|
58
|
+
/** The element placed in the page. */
|
|
59
|
+
readonly host: HTMLElement;
|
|
60
|
+
setTitle(title: string): void;
|
|
61
|
+
setShortcutEnabled(enabled: boolean): void;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const FOCUSABLE =
|
|
65
|
+
"button:not([disabled]), input:not([disabled]), textarea:not([disabled]), select:not([disabled]), summary, [tabindex]:not([tabindex='-1'])";
|
|
66
|
+
|
|
67
|
+
export function createDialog(options: DialogOptions): Dialog {
|
|
68
|
+
const { win, doc, adapter } = options;
|
|
69
|
+
const surface = createSurface({ win, doc, adapter, tag: "feedback-dialog" });
|
|
70
|
+
const titleId = "fb-dialog-title";
|
|
71
|
+
const titleEl = h(doc, "h2", { id: titleId, text: options.title });
|
|
72
|
+
const closeBtn = h(doc, "button", { type: "button", "aria-label": "Close", text: "✕" });
|
|
73
|
+
const body = h(doc, "div", { class: "body" });
|
|
74
|
+
const element = h(doc, "div", { class: "dialog", role: "dialog", "aria-labelledby": titleId, hidden: true });
|
|
75
|
+
const NARROW_PX = 600;
|
|
76
|
+
const isModal = () => options.modal === true || (options.modal === "narrow" && win.innerWidth <= NARROW_PX);
|
|
77
|
+
const syncModal = () => {
|
|
78
|
+
if (isModal()) element.setAttribute("aria-modal", "true");
|
|
79
|
+
else element.removeAttribute("aria-modal");
|
|
80
|
+
};
|
|
81
|
+
syncModal();
|
|
82
|
+
const titlebar = h(doc, "div", { class: "titlebar" }, titleEl);
|
|
83
|
+
element.append(titlebar, body);
|
|
84
|
+
surface.root.append(element);
|
|
85
|
+
|
|
86
|
+
// ---- optional movement: a pure offset, so Reset is exact and the layout CSS stays the source of position
|
|
87
|
+
const MOVE_STEP = 48;
|
|
88
|
+
let offset = { dx: 0, dy: 0 };
|
|
89
|
+
const applyOffset = () => {
|
|
90
|
+
// Keep the dialog on screen: clamp against its un-offset position.
|
|
91
|
+
const r = element.getBoundingClientRect();
|
|
92
|
+
const baseLeft = r.left - offset.dx;
|
|
93
|
+
const baseTop = r.top - offset.dy;
|
|
94
|
+
const dx = Math.min(Math.max(8 - baseLeft, offset.dx), win.innerWidth - 8 - (baseLeft + r.width));
|
|
95
|
+
const dy = Math.min(Math.max(8 - baseTop, offset.dy), win.innerHeight - 8 - (baseTop + r.height));
|
|
96
|
+
offset = { dx: Number.isFinite(dx) ? dx : 0, dy: Number.isFinite(dy) ? dy : 0 };
|
|
97
|
+
element.style.setProperty("translate", offset.dx === 0 && offset.dy === 0 ? "none" : `${offset.dx}px ${offset.dy}px`);
|
|
98
|
+
};
|
|
99
|
+
const moveBy = (dx: number, reset = false) => {
|
|
100
|
+
if (win.innerWidth <= NARROW_PX) return;
|
|
101
|
+
offset = reset ? { dx: 0, dy: 0 } : { dx: offset.dx + dx, dy: offset.dy };
|
|
102
|
+
applyOffset();
|
|
103
|
+
};
|
|
104
|
+
if (options.movable) {
|
|
105
|
+
const move = (label: string, text: string, onClick: () => void) => {
|
|
106
|
+
const b = h(doc, "button", { type: "button", class: "move", "aria-label": label, text });
|
|
107
|
+
b.addEventListener("click", (e) => {
|
|
108
|
+
e.preventDefault();
|
|
109
|
+
onClick();
|
|
110
|
+
});
|
|
111
|
+
return b;
|
|
112
|
+
};
|
|
113
|
+
titlebar.append(
|
|
114
|
+
move("Move left", "◀", () => moveBy(-MOVE_STEP)),
|
|
115
|
+
move("Move right", "▶", () => moveBy(MOVE_STEP)),
|
|
116
|
+
move("Reset position", "Reset", () => moveBy(0, true)),
|
|
117
|
+
);
|
|
118
|
+
titlebar.addEventListener("pointerdown", (e) => {
|
|
119
|
+
const pe = e as PointerEvent;
|
|
120
|
+
if (pe.button > 0 || win.innerWidth <= NARROW_PX || (pe.target as Element).closest?.("button")) return;
|
|
121
|
+
const start = { x: pe.clientX, y: pe.clientY, base: { ...offset } };
|
|
122
|
+
const onMove = (ev: Event) => {
|
|
123
|
+
const m = ev as PointerEvent;
|
|
124
|
+
offset = { dx: start.base.dx + m.clientX - start.x, dy: start.base.dy + m.clientY - start.y };
|
|
125
|
+
applyOffset();
|
|
126
|
+
};
|
|
127
|
+
const onUp = () => {
|
|
128
|
+
win.removeEventListener("pointermove", onMove, true);
|
|
129
|
+
win.removeEventListener("pointerup", onUp, true);
|
|
130
|
+
win.removeEventListener("pointercancel", onUp, true);
|
|
131
|
+
};
|
|
132
|
+
win.addEventListener("pointermove", onMove, true);
|
|
133
|
+
win.addEventListener("pointerup", onUp, true);
|
|
134
|
+
win.addEventListener("pointercancel", onUp, true);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
let open = false;
|
|
139
|
+
let mounted = false;
|
|
140
|
+
let returnFocus: HTMLElement | null = null;
|
|
141
|
+
let shortcutEnabled = options.shortcut !== false;
|
|
142
|
+
let binding: DoubleShiftBinding | null = null;
|
|
143
|
+
|
|
144
|
+
const own = (e: Event) => eventFromHost(e, surface.host);
|
|
145
|
+
|
|
146
|
+
const restoreFocus = () => {
|
|
147
|
+
const target = returnFocus;
|
|
148
|
+
returnFocus = null;
|
|
149
|
+
const usable = target && target.isConnected && target !== doc.body && target !== doc.documentElement;
|
|
150
|
+
(usable ? target : (options.focusFallback?.() ?? null))?.focus();
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const focusInitial = () => {
|
|
154
|
+
const first = body.querySelector<HTMLElement>(FOCUSABLE);
|
|
155
|
+
(first ?? closeBtn).focus();
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const onKeyDown = (e: Event) => {
|
|
159
|
+
const ke = e as KeyboardEvent;
|
|
160
|
+
if (!open) return;
|
|
161
|
+
if (ke.key === "Escape" && !ke.defaultPrevented) {
|
|
162
|
+
ke.preventDefault();
|
|
163
|
+
api.close("escape");
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (ke.key === "Tab" && isModal() && own(ke)) {
|
|
167
|
+
const items = Array.from(element.querySelectorAll<HTMLElement>(FOCUSABLE));
|
|
168
|
+
const first = items[0];
|
|
169
|
+
const last = items[items.length - 1];
|
|
170
|
+
const active = surface.shadow.activeElement;
|
|
171
|
+
if (ke.shiftKey && active === first) {
|
|
172
|
+
ke.preventDefault();
|
|
173
|
+
last?.focus();
|
|
174
|
+
} else if (!ke.shiftKey && active === last) {
|
|
175
|
+
ke.preventDefault();
|
|
176
|
+
first?.focus();
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
titlebar.append(closeBtn);
|
|
182
|
+
closeBtn.addEventListener("click", (e) => {
|
|
183
|
+
e.preventDefault();
|
|
184
|
+
api.close("close-button");
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
const syncShortcut = () => {
|
|
188
|
+
const want = mounted && shortcutEnabled && isSurfacePermitted(adapter, "shortcut") && isSurfacePermitted(adapter, "dialog");
|
|
189
|
+
if (want && !binding) {
|
|
190
|
+
const cfg = typeof options.shortcut === "object" ? options.shortcut : {};
|
|
191
|
+
binding = attachDoubleShift(win, {
|
|
192
|
+
...cfg,
|
|
193
|
+
isOwnEvent: own,
|
|
194
|
+
isSuppressed: () => open || (options.isShortcutBlocked?.() ?? false),
|
|
195
|
+
onActivate: () => {
|
|
196
|
+
api.open("shortcut");
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
} else if (!want && binding) {
|
|
200
|
+
binding.detach();
|
|
201
|
+
binding = null;
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
const api: Dialog = {
|
|
206
|
+
mount() {
|
|
207
|
+
if (!isSurfacePermitted(adapter, "dialog")) return false;
|
|
208
|
+
if (mounted) return true;
|
|
209
|
+
mounted = true;
|
|
210
|
+
surface.attach();
|
|
211
|
+
win.addEventListener("keydown", onKeyDown, true);
|
|
212
|
+
if (options.modal === "narrow") win.addEventListener("resize", syncModal);
|
|
213
|
+
syncModal();
|
|
214
|
+
syncShortcut();
|
|
215
|
+
return true;
|
|
216
|
+
},
|
|
217
|
+
unmount() {
|
|
218
|
+
if (!mounted) return;
|
|
219
|
+
if (open) api.close("unmount");
|
|
220
|
+
mounted = false;
|
|
221
|
+
win.removeEventListener("keydown", onKeyDown, true);
|
|
222
|
+
win.removeEventListener("resize", syncModal);
|
|
223
|
+
syncShortcut();
|
|
224
|
+
surface.detach();
|
|
225
|
+
},
|
|
226
|
+
open(trigger = "api", explicitReturnFocus) {
|
|
227
|
+
if (open || !api.mount()) return false;
|
|
228
|
+
const active = doc.activeElement as HTMLElement | null;
|
|
229
|
+
returnFocus =
|
|
230
|
+
explicitReturnFocus ??
|
|
231
|
+
(active && active !== doc.body && active !== doc.documentElement && active !== surface.host ? active : null);
|
|
232
|
+
open = true;
|
|
233
|
+
element.hidden = false;
|
|
234
|
+
focusInitial();
|
|
235
|
+
options.onOpen?.(trigger);
|
|
236
|
+
return true;
|
|
237
|
+
},
|
|
238
|
+
close(reason = "api") {
|
|
239
|
+
if (!open) return;
|
|
240
|
+
open = false;
|
|
241
|
+
element.hidden = true;
|
|
242
|
+
restoreFocus();
|
|
243
|
+
options.onClose?.(reason);
|
|
244
|
+
},
|
|
245
|
+
get isOpen() {
|
|
246
|
+
return open;
|
|
247
|
+
},
|
|
248
|
+
get isMounted() {
|
|
249
|
+
return mounted;
|
|
250
|
+
},
|
|
251
|
+
body,
|
|
252
|
+
element,
|
|
253
|
+
host: surface.host,
|
|
254
|
+
setTitle(title) {
|
|
255
|
+
titleEl.textContent = title;
|
|
256
|
+
},
|
|
257
|
+
setShortcutEnabled(enabled) {
|
|
258
|
+
shortcutEnabled = enabled;
|
|
259
|
+
syncShortcut();
|
|
260
|
+
},
|
|
261
|
+
};
|
|
262
|
+
return api;
|
|
263
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The seam between the framework-agnostic primitives and whatever page embeds them.
|
|
3
|
+
*
|
|
4
|
+
* Every host concern is injected here: where the UI mounts, how it looks, which surfaces may
|
|
5
|
+
* appear, what page context to attach, and how a finished action reaches the host's intake. The
|
|
6
|
+
* primitives import nothing from a host and use pure DOM APIs only.
|
|
7
|
+
*/
|
|
8
|
+
import type { FeedbackActionV1, FeedbackImpact, FeedbackIntent, PageContext, SubmissionReceiptV1 } from "../v1/types.js";
|
|
9
|
+
|
|
10
|
+
/** The independently permissible pieces of UI. A host lists the ones it allows. */
|
|
11
|
+
export type FeedbackSurface = "launcher" | "shortcut" | "dialog" | "picker";
|
|
12
|
+
|
|
13
|
+
export const ALL_SURFACES: readonly FeedbackSurface[] = ["launcher", "shortcut", "dialog", "picker"];
|
|
14
|
+
|
|
15
|
+
/** Theme tokens, applied as CSS custom properties on each primitive's root. Values are CSS text. */
|
|
16
|
+
export interface ThemeTokens {
|
|
17
|
+
accent?: string;
|
|
18
|
+
accentInk?: string;
|
|
19
|
+
ink?: string;
|
|
20
|
+
muted?: string;
|
|
21
|
+
background?: string;
|
|
22
|
+
line?: string;
|
|
23
|
+
error?: string;
|
|
24
|
+
ok?: string;
|
|
25
|
+
fontFamily?: string;
|
|
26
|
+
/** Stacking order of the launcher; the dialog and picker chrome sit above it. */
|
|
27
|
+
zIndex?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface HostTheme {
|
|
31
|
+
tokens?: ThemeTokens;
|
|
32
|
+
/** CSP nonce for the fallback `<style>` element when constructable stylesheets are unavailable. */
|
|
33
|
+
nonce?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** A report the intake rewrote (for example after its own redaction) and wants the reviewer to approve. */
|
|
37
|
+
export interface SanitizedReportV1 {
|
|
38
|
+
intent: FeedbackIntent;
|
|
39
|
+
impact: FeedbackImpact;
|
|
40
|
+
title: string;
|
|
41
|
+
description: string;
|
|
42
|
+
suggestedFix?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type TransportResult =
|
|
46
|
+
| { ok: true; receipt: SubmissionReceiptV1 }
|
|
47
|
+
| { ok: false; outcome?: undefined; code: string; retryable: boolean; retryAfterSeconds?: number }
|
|
48
|
+
/** Nothing was stored; the intake returned a version for the reviewer to approve. Sending it is a new submission. */
|
|
49
|
+
| {
|
|
50
|
+
ok: false;
|
|
51
|
+
outcome: "needs_review";
|
|
52
|
+
code: string;
|
|
53
|
+
retryable: false;
|
|
54
|
+
sanitized: SanitizedReportV1;
|
|
55
|
+
policyVersion: string;
|
|
56
|
+
redactions: Record<string, number>;
|
|
57
|
+
}
|
|
58
|
+
/** The submission id was already used for different content. It is shown and never resubmitted. */
|
|
59
|
+
| { ok: false; outcome: "conflict"; code: string; retryable: false };
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Delivers a finished action to the host's intake. The host owns same-origin routing and
|
|
63
|
+
* authorization (credentials, headers, receipts); the primitives never see a credential. The
|
|
64
|
+
* action's `submissionId` is stable across retries, so a transport may be called again with the
|
|
65
|
+
* same action after a retryable failure.
|
|
66
|
+
*/
|
|
67
|
+
export type FeedbackTransport = (
|
|
68
|
+
action: FeedbackActionV1,
|
|
69
|
+
options: { signal?: AbortSignal },
|
|
70
|
+
) => Promise<TransportResult>;
|
|
71
|
+
|
|
72
|
+
export interface HostAdapter {
|
|
73
|
+
/**
|
|
74
|
+
* Attach the primitive's root element to the page. Called with the element each time a
|
|
75
|
+
* primitive mounts; may return a cleanup, which runs on unmount before the element is removed.
|
|
76
|
+
* Default: append to `document.documentElement`.
|
|
77
|
+
*/
|
|
78
|
+
mount?(root: HTMLElement): void | (() => void);
|
|
79
|
+
theme?: HostTheme;
|
|
80
|
+
/** Surfaces the host allows. Default: all. A primitive for a refused surface does not mount. */
|
|
81
|
+
permittedSurfaces?: readonly FeedbackSurface[];
|
|
82
|
+
/** Page context attached to target evidence. Path only; the host must not include query or fragment. */
|
|
83
|
+
context?(): PageContext | undefined;
|
|
84
|
+
transport?: FeedbackTransport;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function isSurfacePermitted(adapter: HostAdapter | undefined, surface: FeedbackSurface): boolean {
|
|
88
|
+
const list = adapter?.permittedSurfaces;
|
|
89
|
+
return list === undefined || list.includes(surface);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const TOKEN_VARS: Record<keyof ThemeTokens, string> = {
|
|
93
|
+
accent: "--fb-acc",
|
|
94
|
+
accentInk: "--fb-acc-ink",
|
|
95
|
+
ink: "--fb-ink",
|
|
96
|
+
muted: "--fb-muted",
|
|
97
|
+
background: "--fb-bg",
|
|
98
|
+
line: "--fb-line",
|
|
99
|
+
error: "--fb-err",
|
|
100
|
+
ok: "--fb-ok",
|
|
101
|
+
fontFamily: "--fb-font",
|
|
102
|
+
zIndex: "--fb-z",
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/** Write the host's theme tokens onto an element as custom properties. */
|
|
106
|
+
export function applyTheme(el: HTMLElement, theme: HostTheme | undefined): void {
|
|
107
|
+
const tokens = theme?.tokens;
|
|
108
|
+
if (!tokens) return;
|
|
109
|
+
for (const key of Object.keys(TOKEN_VARS) as (keyof ThemeTokens)[]) {
|
|
110
|
+
const value = tokens[key];
|
|
111
|
+
// Custom property values are CSS text; refuse anything that could close the declaration.
|
|
112
|
+
if (typeof value === "string" && value.length > 0 && value.length <= 200 && !/[;{}<>]/.test(value)) {
|
|
113
|
+
el.style.setProperty(TOKEN_VARS[key], value);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./composer.js";
|
|
2
|
+
export * from "./dialog.js";
|
|
3
|
+
export * from "./hostAdapter.js";
|
|
4
|
+
export * from "./launcher.js";
|
|
5
|
+
export * from "./picker.js";
|
|
6
|
+
export * from "./redaction.js";
|
|
7
|
+
export * from "./selection.js";
|
|
8
|
+
export * from "./shiftAccelerator.js";
|
|
9
|
+
export * from "./surface.js";
|
|
10
|
+
export * from "./target.js";
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The floating trigger. Mounts one button in its own isolated surface; `mount` and `unmount` are
|
|
3
|
+
* idempotent and never leave a stray element behind.
|
|
4
|
+
*/
|
|
5
|
+
import { isSurfacePermitted, type HostAdapter } from "./hostAdapter.js";
|
|
6
|
+
import { createSurface, h } from "./surface.js";
|
|
7
|
+
|
|
8
|
+
export interface LauncherOptions {
|
|
9
|
+
win: Window;
|
|
10
|
+
doc: Document;
|
|
11
|
+
adapter?: HostAdapter;
|
|
12
|
+
label?: string;
|
|
13
|
+
ariaLabel?: string;
|
|
14
|
+
/** Called when the button is activated (click, Enter or Space). */
|
|
15
|
+
onActivate(): void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface Launcher {
|
|
19
|
+
/** Mount into the page. Returns false (and mounts nothing) when the host does not permit the launcher. */
|
|
20
|
+
mount(): boolean;
|
|
21
|
+
unmount(): void;
|
|
22
|
+
readonly isMounted: boolean;
|
|
23
|
+
/** The button, for focus management. */
|
|
24
|
+
readonly button: HTMLButtonElement;
|
|
25
|
+
/** The element placed in the page. */
|
|
26
|
+
readonly host: HTMLElement;
|
|
27
|
+
setLabel(label: string): void;
|
|
28
|
+
/** Reflect whether the dialog it controls is open (`aria-expanded`). */
|
|
29
|
+
setExpanded(expanded: boolean): void;
|
|
30
|
+
/** Show or hide a small attention dot. */
|
|
31
|
+
setBadge(on: boolean): void;
|
|
32
|
+
focus(): void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function createLauncher(options: LauncherOptions): Launcher {
|
|
36
|
+
const { win, doc, adapter } = options;
|
|
37
|
+
const surface = createSurface({ win, doc, adapter, tag: "feedback-launcher" });
|
|
38
|
+
const button = h(doc, "button", {
|
|
39
|
+
class: "fab",
|
|
40
|
+
type: "button",
|
|
41
|
+
"aria-haspopup": "dialog",
|
|
42
|
+
"aria-expanded": "false",
|
|
43
|
+
...(options.ariaLabel ? { "aria-label": options.ariaLabel } : {}),
|
|
44
|
+
});
|
|
45
|
+
const labelEl = h(doc, "span", { text: options.label ?? "Feedback" });
|
|
46
|
+
const dot = h(doc, "span", { class: "dot", "aria-hidden": "true", hidden: true });
|
|
47
|
+
button.append(labelEl, dot);
|
|
48
|
+
surface.root.append(button);
|
|
49
|
+
button.addEventListener("click", (e) => {
|
|
50
|
+
e.preventDefault();
|
|
51
|
+
options.onActivate();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
mount() {
|
|
56
|
+
if (!isSurfacePermitted(adapter, "launcher")) return false;
|
|
57
|
+
surface.attach();
|
|
58
|
+
return true;
|
|
59
|
+
},
|
|
60
|
+
unmount() {
|
|
61
|
+
surface.detach();
|
|
62
|
+
},
|
|
63
|
+
get isMounted() {
|
|
64
|
+
return surface.isAttached;
|
|
65
|
+
},
|
|
66
|
+
button,
|
|
67
|
+
host: surface.host,
|
|
68
|
+
setLabel(label) {
|
|
69
|
+
labelEl.textContent = label;
|
|
70
|
+
},
|
|
71
|
+
setExpanded(expanded) {
|
|
72
|
+
button.setAttribute("aria-expanded", String(expanded));
|
|
73
|
+
},
|
|
74
|
+
setBadge(on) {
|
|
75
|
+
dot.hidden = !on;
|
|
76
|
+
},
|
|
77
|
+
focus() {
|
|
78
|
+
button.focus();
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|