@softistx/pulsarix-widget 0.3.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +181 -21
- package/dist/index.js +35 -35
- package/dist/server.js +60 -0
- package/dist/types/config.d.ts +27 -1
- package/dist/types/element.d.ts +20 -46
- package/dist/types/index.d.ts +3 -2
- package/dist/types/mount.d.ts +75 -0
- package/dist/types/server.d.ts +22 -0
- package/docs/integration.md +550 -0
- package/package.json +29 -10
|
@@ -0,0 +1,550 @@
|
|
|
1
|
+
# Integrating the pulsarix widget in a host app
|
|
2
|
+
|
|
3
|
+
This guide takes a host app (the site or product that embeds the widget) from nothing to reports
|
|
4
|
+
and "My requests" working. Reference for every option and event: the [README](../README.md).
|
|
5
|
+
|
|
6
|
+
1. [Get a project and a widget key](#1-get-a-project-and-a-widget-key)
|
|
7
|
+
2. [Add the widget](#2-add-the-widget)
|
|
8
|
+
3. [Identify your signed-in users](#3-identify-your-signed-in-users)
|
|
9
|
+
- [Signing from a root loader with an optional viewer](#signing-from-a-root-loader-with-an-optional-viewer)
|
|
10
|
+
4. [Content Security Policy](#4-content-security-policy)
|
|
11
|
+
5. [Options and metadata](#5-options-and-metadata)
|
|
12
|
+
6. [Events](#6-events)
|
|
13
|
+
7. [Local development origins](#7-local-development-origins)
|
|
14
|
+
8. [Troubleshooting](#8-troubleshooting)
|
|
15
|
+
9. [Inheriting the host app's theme](#9-inheriting-the-host-apps-theme)
|
|
16
|
+
10. [Changing theme, locale and metadata at run time](#10-changing-theme-locale-and-metadata-at-run-time)
|
|
17
|
+
11. [The launcher: look, position and visibility](#11-the-launcher-look-position-and-visibility)
|
|
18
|
+
- [Sticky bottom bars: `holdLauncher()`](#sticky-bottom-bars-holdlauncher)
|
|
19
|
+
|
|
20
|
+
## 1. Get a project and a widget key
|
|
21
|
+
|
|
22
|
+
In pulsarix, create (or open) the project that will receive the reports, then add a widget key to it.
|
|
23
|
+
A key has:
|
|
24
|
+
|
|
25
|
+
- a **public key**, `pk_live_…`: it goes in your page and is not secret;
|
|
26
|
+
- **allowedOrigins**: the exact origins that may send reports with that key, one per line, each
|
|
27
|
+
`https://host[:port]` (plain `http://` only for `localhost`, `127.0.0.1` and `[::1]`). No path, query,
|
|
28
|
+
fragment, credentials or wildcard; `https://app.example.com`, never `https://app.example.com/page`
|
|
29
|
+
or `https://*.example.com`. A request from any other origin, or with no `Origin`, is refused;
|
|
30
|
+
- an optional **identity secret** for signed-in users (section 3): in the key's "Signed-in users" card,
|
|
31
|
+
press "Generate secret". It is **shown once**: copy it into your server's environment right away
|
|
32
|
+
(for example `PULSARIX_IDENTITY_SECRET`), with the public key beside it as `PULSARIX_PROJECT_KEY`. It cannot be read again; "Regenerate" makes a new one and
|
|
33
|
+
the old one stops working at once.
|
|
34
|
+
|
|
35
|
+
The key also has a switch (disabled keys refuse everything) and a "Screenshots and files" switch.
|
|
36
|
+
|
|
37
|
+
## 2. Add the widget
|
|
38
|
+
|
|
39
|
+
### Script tag
|
|
40
|
+
|
|
41
|
+
```html
|
|
42
|
+
<script src="https://<pulsarix-host>/widget/v1/loader.js" async
|
|
43
|
+
data-project-key="pk_live_xxx"></script>
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The loader is tiny (at most 5 KB gzipped) and loads the widget bundle on first use. The widget's API
|
|
47
|
+
origin is the host that served the loader; override it with `data-api-base`.
|
|
48
|
+
|
|
49
|
+
### npm `mount()`
|
|
50
|
+
|
|
51
|
+
For an app that installs the package (see the README's "Install" for the read token):
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { mount } from "@softistx/pulsarix-widget";
|
|
55
|
+
|
|
56
|
+
const widget = mount({
|
|
57
|
+
projectKey: "pk_live_xxx",
|
|
58
|
+
apiBase: "https://<pulsarix-host>", // required on the npm route
|
|
59
|
+
locale: "fr",
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
The module is browser-only: in an SSR framework import it on the client, inside an effect:
|
|
64
|
+
|
|
65
|
+
```tsx
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
let widget: { destroy(): void } | undefined;
|
|
68
|
+
import("@softistx/pulsarix-widget").then(({ mount }) => {
|
|
69
|
+
widget = mount({ projectKey, apiBase });
|
|
70
|
+
});
|
|
71
|
+
return () => widget?.destroy();
|
|
72
|
+
}, [projectKey, apiBase]);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
One widget per page: use the script tag or `mount()`, not both.
|
|
76
|
+
|
|
77
|
+
## 3. Identify your signed-in users
|
|
78
|
+
|
|
79
|
+
Without an identity the widget is the anonymous report form. With one, reports are filed under the
|
|
80
|
+
reporter, who also gets a "My requests" tab (threads, replies, unread counts, "Delete my data").
|
|
81
|
+
|
|
82
|
+
Your server signs a short-lived token for the signed-in user with the key's identity secret; the browser
|
|
83
|
+
only carries the token. The helper does the signing:
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
// server only
|
|
87
|
+
import { signPulsarixIdentity } from "@softistx/pulsarix-widget/server";
|
|
88
|
+
|
|
89
|
+
export function pulsarixTokenFor(user: {
|
|
90
|
+
id: string;
|
|
91
|
+
email: string;
|
|
92
|
+
name: string;
|
|
93
|
+
}) {
|
|
94
|
+
return signPulsarixIdentity({
|
|
95
|
+
secret: process.env.PULSARIX_IDENTITY_SECRET as string,
|
|
96
|
+
publicKey: process.env.PULSARIX_PROJECT_KEY as string, // pk_live_xxx
|
|
97
|
+
sub: user.id,
|
|
98
|
+
email: user.email,
|
|
99
|
+
name: user.name,
|
|
100
|
+
ttlSeconds: 3600, // 1 hour (the default); 24 hours at most
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Import the signer only in server code (a loader, or a `*.server.ts` file), never in the browser. Then hand
|
|
106
|
+
the token to the widget, and clear it at sign-out. In a strict-TypeScript host the script tag's global is
|
|
107
|
+
not typed: declare it once with
|
|
108
|
+
`declare global { interface Window { PulsarixWidget?: { identify(token: string | null): void; holdLauncher(): () => void } } }`,
|
|
109
|
+
or use the npm route's typed handle:
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
// browser, after sign-in or on each page load (script tag)
|
|
113
|
+
window.PulsarixWidget.identify(token);
|
|
114
|
+
|
|
115
|
+
// at sign-out
|
|
116
|
+
window.PulsarixWidget.identify(null);
|
|
117
|
+
|
|
118
|
+
// npm route: the handle mount() returned
|
|
119
|
+
widget.identify(token);
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
or give it at the start: `data-identity="<token>"` on the script tag, or `identity` in `init` / `mount`.
|
|
123
|
+
|
|
124
|
+
Rules of the token (the helper enforces them and throws otherwise):
|
|
125
|
+
|
|
126
|
+
| Part | Rule |
|
|
127
|
+
| ----------- | ------------------------------------------------------------------------------ |
|
|
128
|
+
| `sub` | 1 to 200 characters, your stable user id; pulsarix keeps only a digest per key |
|
|
129
|
+
| `email` | optional, a valid address, 254 characters at most |
|
|
130
|
+
| `name` | optional, trimmed, 100 characters at most |
|
|
131
|
+
| `aud` | the key's public key (the helper's `publicKey`) |
|
|
132
|
+
| `iat`/`exp` | integer seconds; a life of 24 hours at most, 1 hour recommended |
|
|
133
|
+
| whole token | 2 048 characters at most |
|
|
134
|
+
|
|
135
|
+
The token is `<payload>.<signature>`, both base64url without padding; the signature is the
|
|
136
|
+
HMAC-SHA256 of the payload text, keyed with the secret **as the raw string** (not base64url-decoded).
|
|
137
|
+
The API allows 5 minutes of clock skew between your server and itself.
|
|
138
|
+
|
|
139
|
+
**Re-sign before expiry.** A token is not renewed by the widget. Sign a new one on each request that
|
|
140
|
+
renders a page, or from a small endpoint your page calls every half life, and call `identify(newToken)`.
|
|
141
|
+
A tab that stays open longer than the token lives gets `pulsarix:error` with
|
|
142
|
+
`intake.errors.identity-expired` (or `identity-invalid`) until you do.
|
|
143
|
+
|
|
144
|
+
Keep the secret in an environment variable on the server. It must never reach a page, a public
|
|
145
|
+
environment variable, a repository or a log.
|
|
146
|
+
|
|
147
|
+
### React Router (loader)
|
|
148
|
+
|
|
149
|
+
```tsx
|
|
150
|
+
import { signPulsarixIdentity } from "@softistx/pulsarix-widget/server";
|
|
151
|
+
import { Outlet, useLoaderData } from "react-router";
|
|
152
|
+
import { useEffect } from "react";
|
|
153
|
+
|
|
154
|
+
export async function loader({ request }: Route.LoaderArgs) {
|
|
155
|
+
const user = await requireUser(request);
|
|
156
|
+
return {
|
|
157
|
+
pulsarixIdentity: signPulsarixIdentity({
|
|
158
|
+
secret: process.env.PULSARIX_IDENTITY_SECRET as string,
|
|
159
|
+
publicKey: process.env.PULSARIX_PROJECT_KEY as string,
|
|
160
|
+
sub: user.id,
|
|
161
|
+
email: user.email,
|
|
162
|
+
name: user.name,
|
|
163
|
+
}),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export default function Layout() {
|
|
168
|
+
const { pulsarixIdentity } = useLoaderData<typeof loader>();
|
|
169
|
+
useEffect(() => {
|
|
170
|
+
window.PulsarixWidget?.identify(pulsarixIdentity);
|
|
171
|
+
}, [pulsarixIdentity]);
|
|
172
|
+
return <Outlet />;
|
|
173
|
+
}
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Signing from a root loader with an optional viewer
|
|
177
|
+
|
|
178
|
+
The loader above sits under a layout that already requires a signed-in user. When the viewer is
|
|
179
|
+
optional (a public site with an account), sign from the root loader instead and give `identify()`
|
|
180
|
+
`null` for a visitor. Drive it from the loader data, so it also covers sign-out: the loader reruns,
|
|
181
|
+
the token becomes `null` and the widget drops "My requests", with no sign-out hook in your app.
|
|
182
|
+
|
|
183
|
+
```tsx
|
|
184
|
+
// app/root.tsx
|
|
185
|
+
import { signPulsarixIdentity } from "@softistx/pulsarix-widget/server";
|
|
186
|
+
import { useEffect } from "react";
|
|
187
|
+
import { Outlet, useLoaderData } from "react-router";
|
|
188
|
+
|
|
189
|
+
export async function loader({ request }: Route.LoaderArgs) {
|
|
190
|
+
const user = await getUser(request); // your own session; `null` for a visitor
|
|
191
|
+
return {
|
|
192
|
+
pulsarixIdentity: user
|
|
193
|
+
? signPulsarixIdentity({
|
|
194
|
+
secret: process.env.PULSARIX_IDENTITY_SECRET as string,
|
|
195
|
+
publicKey: process.env.PULSARIX_PROJECT_KEY as string,
|
|
196
|
+
sub: user.id,
|
|
197
|
+
email: user.email,
|
|
198
|
+
name: user.name,
|
|
199
|
+
})
|
|
200
|
+
: null,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export default function Root() {
|
|
205
|
+
const { pulsarixIdentity } = useLoaderData<typeof loader>();
|
|
206
|
+
useEffect(() => {
|
|
207
|
+
window.PulsarixWidget?.identify(pulsarixIdentity);
|
|
208
|
+
}, [pulsarixIdentity]);
|
|
209
|
+
return <Outlet />;
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
The cost: a root loader runs on every document request and on every revalidation, so a signed-in
|
|
214
|
+
viewer is signed once per request (an HMAC, microseconds) and the token in the page changes each
|
|
215
|
+
time, which makes the response differ per user: do not cache it in a shared cache. Keep the life
|
|
216
|
+
short (the default hour) and `identify()` again with the fresh token on each run, as the effect does.
|
|
217
|
+
To sign less often, give the loader `shouldRevalidate` so it reruns only after a sign-in or sign-out,
|
|
218
|
+
and re-sign before expiry (see above). A visitor costs nothing: no signature, and `identify(null)`.
|
|
219
|
+
|
|
220
|
+
A token given with `identify()` stays in the widget's memory; one in `data-identity` is an attribute of
|
|
221
|
+
the element and visible in the DOM, so prefer `identify()`.
|
|
222
|
+
|
|
223
|
+
## 4. Content Security Policy
|
|
224
|
+
|
|
225
|
+
Allow the pulsarix host in `script-src` (the loader and the bundle) and in `connect-src` (the API calls,
|
|
226
|
+
the file uploads and the live stream of an open thread; the same origin for all three):
|
|
227
|
+
|
|
228
|
+
```
|
|
229
|
+
Content-Security-Policy:
|
|
230
|
+
script-src 'self' 'nonce-<per-request>' https://<pulsarix-host>;
|
|
231
|
+
connect-src 'self' https://<pulsarix-host>;
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
- Nothing else is needed for images or capture: thumbnails and the screenshot editor draw on canvases,
|
|
235
|
+
so there is no `img-src blob:`. Screen capture needs `display-capture` in your `Permissions-Policy`
|
|
236
|
+
(uploading an image works without it).
|
|
237
|
+
- The widget's styles are adopted into its shadow root: no `style-src 'unsafe-inline'`. The requests
|
|
238
|
+
thread injects one `<style>` element; under a strict `style-src` give your page a nonce, which the
|
|
239
|
+
widget reads from a `nonce` attribute on a `style` or `script` element or from this tag:
|
|
240
|
+
|
|
241
|
+
```html
|
|
242
|
+
<meta property="csp-nonce" content="<per-request nonce>">
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
## 5. Options and metadata
|
|
246
|
+
|
|
247
|
+
Script-tag attributes and `init` / `mount` options (all optional except the key):
|
|
248
|
+
|
|
249
|
+
| Attribute / option | Effect |
|
|
250
|
+
| --------------------------------------------------- | ------------------------------------------------------------------ |
|
|
251
|
+
| `data-project-key` / `projectKey` | the public key (required) |
|
|
252
|
+
| `data-api-base` / `apiBase` | the pulsarix origin; required on the npm route |
|
|
253
|
+
| `data-locale` / `locale` | `en` (default) or `fr` |
|
|
254
|
+
| `data-theme` / `theme` | `light`, `dark` or `auto` (default) |
|
|
255
|
+
| `data-position` / `position` | `bottom-right` (default) or `bottom-left` |
|
|
256
|
+
| `data-accent-color` / `accentColor` | a hex seed for the theme |
|
|
257
|
+
| `data-launcher` / `launcher` | `auto` (default), `pill`, `icon`, or `false` for none (section 11) |
|
|
258
|
+
| `data-offset-bottom`, `data-offset-side` / `offset` | launcher distance from the bottom and its side (section 11) |
|
|
259
|
+
| `data-attachments="false"` | no screenshot and no file controls |
|
|
260
|
+
| `data-mask-text="true"` | "Mask the text on the page" starts on |
|
|
261
|
+
| `data-privacy-url` / `privacyUrl` | an http(s) link to your privacy page |
|
|
262
|
+
| `data-prefill-email`, `data-prefill-title` | starting values of the form |
|
|
263
|
+
| `data-identity` / `identity` | the signed token (section 3) |
|
|
264
|
+
| `data-metadata` / `metadata` | a JSON object of strings stored with each report |
|
|
265
|
+
|
|
266
|
+
`metadata` carries facts about the session that help triage: at most 20 entries, keys of 64 characters,
|
|
267
|
+
values of 512, 2 KB in all; the rest is dropped.
|
|
268
|
+
|
|
269
|
+
```html
|
|
270
|
+
<script src="https://<pulsarix-host>/widget/v1/loader.js" async
|
|
271
|
+
data-project-key="pk_live_xxx"
|
|
272
|
+
data-locale="fr"
|
|
273
|
+
data-metadata='{"plan":"pro","build":"2026.11.3"}'></script>
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
PulsarixWidget.init({
|
|
278
|
+
projectKey: "pk_live_xxx",
|
|
279
|
+
metadata: { plan: "pro", build: "2026.11.3" },
|
|
280
|
+
prefill: { email: user.email },
|
|
281
|
+
});
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
The page address is sent without its query or fragment.
|
|
285
|
+
|
|
286
|
+
## 6. Events
|
|
287
|
+
|
|
288
|
+
Heard on the element and with `on(name, callback)`, which returns the function that unsubscribes:
|
|
289
|
+
|
|
290
|
+
| Event | `detail` | When |
|
|
291
|
+
| ----------------- | --------------------- | ------------------------------------------------------------------------------ |
|
|
292
|
+
| `pulsarix:ready` | none | the widget is mounted (asynchronous) |
|
|
293
|
+
| `pulsarix:open` | none | the dialog opened |
|
|
294
|
+
| `pulsarix:close` | none | the dialog closed |
|
|
295
|
+
| `pulsarix:submit` | `{ reportId, files }` | a report was sent |
|
|
296
|
+
| `pulsarix:reply` | `{ requestId }` | the team replied to one of the reporter's requests |
|
|
297
|
+
| `pulsarix:unread` | `{ count }` | the reporter's unread replies changed |
|
|
298
|
+
| `pulsarix:erase` | `{ requests }` | the reporter erased their data |
|
|
299
|
+
| `pulsarix:error` | `{ code }` | a failure; the API's `intake.errors.*`, `network`, `config` or `http-<status>` |
|
|
300
|
+
|
|
301
|
+
```ts
|
|
302
|
+
const off = PulsarixWidget.on("pulsarix:submit", (e) =>
|
|
303
|
+
analytics.track("report", e.detail.reportId),
|
|
304
|
+
);
|
|
305
|
+
PulsarixWidget.on("pulsarix:error", (e) =>
|
|
306
|
+
console.warn("pulsarix", e.detail.code),
|
|
307
|
+
);
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
## 7. Local development origins
|
|
311
|
+
|
|
312
|
+
A key only accepts the origins on its allow-list, so list the one your dev server uses, with its port:
|
|
313
|
+
|
|
314
|
+
```
|
|
315
|
+
https://app.example.com
|
|
316
|
+
http://localhost:5173
|
|
317
|
+
http://127.0.0.1:5173
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
Plain `http` is accepted for `localhost`, `127.0.0.1` and `[::1]` only; any other dev host (a LAN
|
|
321
|
+
address, `*.local`) needs `https`. Use a separate key for development and production so a dev origin
|
|
322
|
+
never sits on the production key.
|
|
323
|
+
|
|
324
|
+
## 8. Troubleshooting
|
|
325
|
+
|
|
326
|
+
| Symptom or code | Cause | Fix |
|
|
327
|
+
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
328
|
+
| `intake.errors.unknown-key` (404), `http-404` | the public key is mistyped, revoked, or not a key | copy the key again from the project |
|
|
329
|
+
| `intake.errors.origin-not-allowed` (403) | the page's origin is not on the key's `allowedOrigins`, or the request has no `Origin` | add the exact origin (scheme, host, port; no path); see section 7 |
|
|
330
|
+
| `network` with no console message from the API | the browser hid a CORS refusal: the origin is not allowed, or the key is unknown | check the browser's network tab, then `allowedOrigins`; the origin must match exactly, `www.` and ports included |
|
|
331
|
+
| `intake.errors.key-disabled` (403) | the key's switch is off | switch it on in the project |
|
|
332
|
+
| `intake.errors.identity-invalid` (401) | the token is forged, signed with another secret, for another key (`aud`), not yet valid (clock), older than its `exp`, longer than 24 hours, over 2 048 characters, or the key has no identity secret. The API gives one answer for all of these | sign with `signPulsarixIdentity` using the key's current secret and its own public key; check the server clock; generate a secret if the key has none; regenerate invalidates every older token |
|
|
333
|
+
| `intake.errors.identity-expired` | an open live thread outlived its token | sign a new token and call `identify(newToken)`; re-sign every half life |
|
|
334
|
+
| `intake.errors.too-many-requests` (429) | a rate limit: reports per address, per key, per e-mail, per identified reporter, or reads | wait the time the message gives (`retryAfter`); do not retry in a loop |
|
|
335
|
+
| `intake.errors.too-fast` (400) | the form was sent less than 3 seconds after opening | the widget waits it out; only a script that posts the form hits it |
|
|
336
|
+
| `intake.errors.key-not-found` (404), `intake.errors.not-found` (404) | the key or the request no longer exists | check the key; refresh the list |
|
|
337
|
+
| `intake.errors.file-unsupported-type` (415), `file-undecodable` (422), `file-too-large` (413), `file-too-many-pixels` (422), `too-many-files` (400), `files-too-large` (413) | an attached file the API refuses (PNG, JPEG, WebP or PDF; 5 MB each, 12 MB and 3 files per report, 25 megapixels) | the widget checks these first and shows the reason; attach a smaller or supported file |
|
|
338
|
+
| `intake.errors.ticket-invalid`, `ticket-expired`, `ticket-used`, `upload-mismatch`, `quarantine-full` | an upload ticket is damaged, older than 15 minutes, already used, or the organisation's quarantine is full | send the report again; if it persists, tell the project's owners |
|
|
339
|
+
| `intake.errors.request-declined` (409), `reopen-expired` (409) | a declined request is read-only; a request resolved over 30 days ago cannot be reopened | open a new request |
|
|
340
|
+
| `intake.errors.attachments-disabled` (403) | the key's "Screenshots and files" switch is off | switch it on, or set `data-attachments="false"` |
|
|
341
|
+
| `config` | no project key, or no `apiBase` on the npm route | give both |
|
|
342
|
+
| `intake.errors.unavailable` (503) | the service cannot check its limits | retry shortly |
|
|
343
|
+
| `signPulsarixIdentity: ... must be ...` | an option the API would refuse | fix the named option; the message says which |
|
|
344
|
+
| Blocked by CSP in the console | the pulsarix host is missing from `script-src` or `connect-src`, or a strict `style-src` has no nonce | section 4 |
|
|
345
|
+
| `identify()` seems ignored | the token is malformed (anything that is not `<payload>.<signature>` is dropped) | pass the string `signPulsarixIdentity` returned, unchanged |
|
|
346
|
+
|
|
347
|
+
## 9. Inheriting the host app's theme
|
|
348
|
+
|
|
349
|
+
When the host app is built on `@softistx/material`, the widget wears the host's theme (its
|
|
350
|
+
colours, radius and light or dark mode) instead of its own `lavande` one, and follows the host
|
|
351
|
+
when the user changes theme. Nothing to configure: `inheritTheme` is `true` by default.
|
|
352
|
+
|
|
353
|
+
```ts
|
|
354
|
+
mount({ projectKey: "pk_live_xxx", apiBase: "https://pulsarix.example.com" });
|
|
355
|
+
// the host's theme picker, dark mode and primary now apply to the dialog
|
|
356
|
+
|
|
357
|
+
mount({ projectKey: "pk_live_xxx", inheritTheme: false }); // the widget's own look, as before
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
Script tag: `data-inherit-theme="false"`. Element attribute: `inherit-theme="false"`.
|
|
361
|
+
|
|
362
|
+
**`theme` at mount does not turn host inheritance off.** On a host that provides tokens, the widget
|
|
363
|
+
keeps wearing the host's colours and its light or dark mode; the `theme` option (`light`, `dark`,
|
|
364
|
+
`auto`) only decides the mode when the host does not say. To keep the widget's own look, pass
|
|
365
|
+
`inheritTheme: false`; to force the mode at run time, call `setTheme("light" | "dark")`.
|
|
366
|
+
|
|
367
|
+
### How it works
|
|
368
|
+
|
|
369
|
+
The widget lives in a shadow root and declares its own tokens there, so the page's custom
|
|
370
|
+
properties cannot reach it by plain CSS inheritance. Instead, the widget reads the host's tokens
|
|
371
|
+
from `getComputedStyle(document.documentElement)` and sets the valid ones on its own root as
|
|
372
|
+
inline custom properties:
|
|
373
|
+
|
|
374
|
+
- **Which tokens**: the literal colours of the built-in theme (`--primary`, `--background`,
|
|
375
|
+
`--foreground`, `--card`, `--border`, `--muted`, `--error`, ...), `--radius` and the few
|
|
376
|
+
percentage tokens. Tokens the theme derives from others (`--ground`, `--primary-text`, ...)
|
|
377
|
+
are recomputed by the widget from the inherited ones. Fonts are never inherited: the widget
|
|
378
|
+
keeps the system font stack.
|
|
379
|
+
- **Fallback**: a token that is missing, empty or not valid (a colour the browser rejects, a
|
|
380
|
+
length that is not `px` or `rem`) keeps the widget's own value. A host that is not on
|
|
381
|
+
`@softistx/material`, or whose tokens are bare HSL triplets (`222 47% 11%`), therefore looks
|
|
382
|
+
exactly as before. `rem` radii are converted to `px` with the host's root font size.
|
|
383
|
+
- **Light and dark**: the inherited values are the host's current mode's, so the widget's scheme
|
|
384
|
+
follows the host's: its `dark` / `light` class on `<html>`, else its `color-scheme`. When the
|
|
385
|
+
host says neither, the `theme` option decides as before.
|
|
386
|
+
- **`accentColor` wins**: with an explicit `accentColor`, the host's primary family
|
|
387
|
+
(`--primary`, `--primary-foreground`, `--inverse-primary*`) is not inherited; the other tokens
|
|
388
|
+
still are.
|
|
389
|
+
- **Follows the host**: a `MutationObserver` on `<html>` (`class`, `style`, `data-theme`,
|
|
390
|
+
`data-mode`, `data-nxgt-theme`, `data-color-scheme`) and the `prefers-color-scheme` media query
|
|
391
|
+
re-read the tokens (`data-theme` and `data-mode` only trigger a re-read; they never decide
|
|
392
|
+
dark or light). It stops when the widget is removed (`destroy()`).
|
|
393
|
+
|
|
394
|
+
### Limits
|
|
395
|
+
|
|
396
|
+
- The tokens are read from `<html>`. A host that scopes its theme to a wrapper element (not
|
|
397
|
+
`<html>`) is not read: the widget keeps its own look.
|
|
398
|
+
- Custom properties set at run time through a host's stylesheet swap (a new `<link>`) without an
|
|
399
|
+
attribute change on `<html>` are picked up at the next attribute change or scheme change.
|
|
400
|
+
- Pairing: the inherited colours are the host's own, so the widget is as readable as the host.
|
|
401
|
+
Mixing a few inherited tokens with the widget's own for the rest (a partial host theme) can
|
|
402
|
+
lower contrast; set `inheritTheme: false` when the host only defines part of the palette.
|
|
403
|
+
|
|
404
|
+
## 10. Changing theme, locale and metadata at run time
|
|
405
|
+
|
|
406
|
+
Part of the host integration: the host switches its organisation, language or light/dark mode while a
|
|
407
|
+
reporter may have a dialog open. Never remount the widget for that (a remount drops the report being typed);
|
|
408
|
+
use the setters on the mount handle or on the script-tag global.
|
|
409
|
+
|
|
410
|
+
```ts
|
|
411
|
+
const widget = mount({ projectKey, apiBase, theme: "light", locale: "en" });
|
|
412
|
+
|
|
413
|
+
// the app's mode changed (resolve "Système" yourself, from the app's own state)
|
|
414
|
+
widget.setTheme(resolvedMode); // "light" | "dark" | "auto"
|
|
415
|
+
|
|
416
|
+
// the app's language changed
|
|
417
|
+
widget.setLocale("fr"); // "en" | "fr"
|
|
418
|
+
|
|
419
|
+
// the app's organisation changed: the next report carries these entries
|
|
420
|
+
widget.setMetadata({ org: orgId, plan });
|
|
421
|
+
widget.setMetadata(null); // none
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
```js
|
|
425
|
+
PulsarixWidget.setTheme("dark");
|
|
426
|
+
PulsarixWidget.setLocale("fr");
|
|
427
|
+
PulsarixWidget.setMetadata({ org: "acme" });
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
| Call | Effect | Kept |
|
|
431
|
+
| ---------------------- | ----------------------------------------------------------- | ----------------------------- |
|
|
432
|
+
| `setTheme(theme)` | the widget's own light or dark mode, at once | the open dialog and its draft |
|
|
433
|
+
| `setLocale(locale)` | the strings re-render in that language | the open dialog and its draft |
|
|
434
|
+
| `setMetadata(entries)` | replaces the metadata of the next report (`null` clears it) | the open dialog and its draft |
|
|
435
|
+
|
|
436
|
+
Rules:
|
|
437
|
+
|
|
438
|
+
- `"light"` and `"dark"` are applied exactly. `"auto"` follows the operating system's
|
|
439
|
+
`prefers-color-scheme`, never the host page's class or attribute. A host with its own mode switch passes the
|
|
440
|
+
resolved `"light"` or `"dark"` to `setTheme` on each change.
|
|
441
|
+
- `setTheme` is independent of `inheritTheme` (section 9). With `inheritTheme`, the widget follows the host's
|
|
442
|
+
mode when the host provides tokens, and the `theme` given at mount (or written later as the `theme`
|
|
443
|
+
attribute) decides only when it does not. `setTheme("light")` or `setTheme("dark")` wins over the
|
|
444
|
+
host's mode until the `theme` attribute is written again (that gives the mode back to the host);
|
|
445
|
+
`setTheme("auto")` gives it back at once. An inherited token still wins for its colour value.
|
|
446
|
+
- `setTheme` only sets the widget's own mode. The accent (`accentColor`) is a mount option and does not change.
|
|
447
|
+
- `setMetadata` replaces; it does not merge. The limits are those of `metadata` at mount (20 entries, keys of
|
|
448
|
+
64 characters, values of 512, 2 KB in all). A report already sent is not changed.
|
|
449
|
+
- `setLocale` changes the widget's own strings at once. The built-in labels of `@softistx/material` (the close
|
|
450
|
+
button, the file list, ...) are read once by its i18n context, so they change when the form is next idle:
|
|
451
|
+
- On an idle form, the typed fields, the chosen type and impact and the selected tab are kept; files and a
|
|
452
|
+
screenshot already attached are dropped.
|
|
453
|
+
- While a report is being sent, on the confirmation screen, or on the "My requests" tab, nothing is rebuilt:
|
|
454
|
+
those built-in labels switch when the reporter returns to the form (or the dialog closes), and no
|
|
455
|
+
attachment is lost.
|
|
456
|
+
- `setTheme` and `setMetadata` drop nothing.
|
|
457
|
+
- An unsupported theme or locale is ignored.
|
|
458
|
+
- Calls before the script-tag bundle has loaded are queued, replayed in order; a repeated call to the same
|
|
459
|
+
setter replaces the earlier one, so only the last `setTheme`, `setLocale` and `setMetadata` run.
|
|
460
|
+
|
|
461
|
+
## 11. The launcher: look, position and visibility
|
|
462
|
+
|
|
463
|
+
The floating button that opens the dialog. Three options shape it; each has a script-tag
|
|
464
|
+
attribute and a `mount()` option.
|
|
465
|
+
|
|
466
|
+
### Look: `launcher`
|
|
467
|
+
|
|
468
|
+
| Value | Result |
|
|
469
|
+
| ------------------ | --------------------------------------------------------------------------------------------------------------- |
|
|
470
|
+
| `"auto"` (default) | The icon below 640 px of viewport width, the text pill from 640 px up. Live: it follows a resize or a rotation. |
|
|
471
|
+
| `"pill"` | Always the text pill (the 0.3 look). |
|
|
472
|
+
| `"icon"` | Always a 48 px round button with a speech-bubble icon. |
|
|
473
|
+
| `false` | No launcher; open the dialog with `open()`. |
|
|
474
|
+
|
|
475
|
+
The icon button keeps the localized label as its accessible name (and as its `title`, so a
|
|
476
|
+
pointer hover shows it), carries the unread count in that name, and shows a visible focus ring.
|
|
477
|
+
|
|
478
|
+
```html
|
|
479
|
+
<script src="https://<pulsarix-host>/widget/v1/loader.js" async
|
|
480
|
+
data-project-key="pk_live_xxx" data-launcher="icon"></script>
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
```ts
|
|
484
|
+
mount({ projectKey: "pk_live_xxx", launcher: "auto" });
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
### Offset: `offset` and the CSS custom properties
|
|
488
|
+
|
|
489
|
+
The launcher sits 16 px from the bottom and from its side, plus the device's safe area
|
|
490
|
+
(`env(safe-area-inset-bottom)`) at the bottom. Move it to clear a sticky action bar:
|
|
491
|
+
|
|
492
|
+
```ts
|
|
493
|
+
mount({ projectKey: "pk_live_xxx", offset: { bottom: 88, side: "1.5rem" } });
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
```html
|
|
497
|
+
<script ... data-offset-bottom="88" data-offset-side="1.5rem"></script>
|
|
498
|
+
```
|
|
499
|
+
|
|
500
|
+
A number is pixels; a string is any CSS length (`"2rem"`, `"calc(var(--bar-height) + 8px)"`,
|
|
501
|
+
`"env(...)"`). Negative values and anything that is not a length are ignored. The option sets two custom
|
|
502
|
+
properties on the `<pulsarix-widget>` element, which a host can also set in its own stylesheet (for example
|
|
503
|
+
from a media query, or following the height of its bar); the widget only ever removes a property it set itself:
|
|
504
|
+
|
|
505
|
+
```css
|
|
506
|
+
pulsarix-widget {
|
|
507
|
+
--pulsarix-launcher-bottom: 88px;
|
|
508
|
+
--pulsarix-launcher-side: 24px;
|
|
509
|
+
}
|
|
510
|
+
```
|
|
511
|
+
|
|
512
|
+
`side` is the distance from the edge named by `position` (`bottom-right` by default).
|
|
513
|
+
|
|
514
|
+
### Visibility: `setLauncherVisible(visible)`
|
|
515
|
+
|
|
516
|
+
Hides or shows the launcher only, for a screen where the button is in the way:
|
|
517
|
+
|
|
518
|
+
```ts
|
|
519
|
+
const widget = mount({ projectKey: "pk_live_xxx" });
|
|
520
|
+
widget.setLauncherVisible(false); // on the checkout step
|
|
521
|
+
widget.setLauncherVisible(true);
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
An open dialog is never hidden or closed by it, and `open()` still works while the launcher is hidden.
|
|
525
|
+
With the script tag: `window.PulsarixWidget.setLauncherVisible(false)`. `launcher: false` still
|
|
526
|
+
removes the launcher for good.
|
|
527
|
+
|
|
528
|
+
### Sticky bottom bars: `holdLauncher()`
|
|
529
|
+
|
|
530
|
+
A host's own sticky bottom bar (a checkout bar, a cookie banner, an editor toolbar) can sit under the
|
|
531
|
+
launcher. `holdLauncher()` hides the launcher while the bar is on screen and returns a function that
|
|
532
|
+
gives it back. It is ref-counted, so two bars at once keep it hidden until both are released, and the
|
|
533
|
+
returned function is safe to call more than once.
|
|
534
|
+
|
|
535
|
+
```tsx
|
|
536
|
+
import { useEffect } from "react";
|
|
537
|
+
|
|
538
|
+
function StickyCheckoutBar() {
|
|
539
|
+
useEffect(() => {
|
|
540
|
+
// The script tag's global queues the call until the bundle has loaded.
|
|
541
|
+
const release = window.PulsarixWidget?.holdLauncher();
|
|
542
|
+
return () => release?.();
|
|
543
|
+
}, []);
|
|
544
|
+
return <div className="sticky bottom-0">…</div>;
|
|
545
|
+
}
|
|
546
|
+
```
|
|
547
|
+
|
|
548
|
+
With the npm route, `const release = widget.holdLauncher()`. It combines with `setLauncherVisible`: the
|
|
549
|
+
launcher shows only when `setLauncherVisible` is not `false` and no hold is open. Like it, a hold never
|
|
550
|
+
closes or hides an open dialog, and `launcher: false` still removes the launcher for good.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@softistx/pulsarix-widget",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Embeddable client-issue intake widget for pulsarix, rendered in a Shadow DOM",
|
|
6
6
|
"license": "MIT",
|
|
@@ -10,21 +10,25 @@
|
|
|
10
10
|
},
|
|
11
11
|
"publishConfig": {
|
|
12
12
|
"registry": "https://registry.npmjs.org/",
|
|
13
|
-
"access": "
|
|
13
|
+
"access": "restricted"
|
|
14
14
|
},
|
|
15
15
|
"devDependencies": {
|
|
16
|
-
"@biomejs/biome": "2.
|
|
16
|
+
"@biomejs/biome": "2.6.8",
|
|
17
|
+
"@graphql-codegen/cli": "^8.0.0",
|
|
18
|
+
"@graphql-codegen/typescript": "^7.0.1",
|
|
19
|
+
"@graphql-codegen/typescript-operations": "^7.0.1",
|
|
17
20
|
"@happy-dom/global-registrator": "^21.0.4",
|
|
18
|
-
"@softistx/material": "^3.
|
|
19
|
-
"@softistx/material-core": "^0.
|
|
21
|
+
"@softistx/material": "^3.268.1",
|
|
22
|
+
"@softistx/material-core": "^0.96.1",
|
|
20
23
|
"@tailwindcss/cli": "^4.3.3",
|
|
21
24
|
"@testing-library/dom": "^10.4.2",
|
|
22
25
|
"@testing-library/react": "^16.3.3",
|
|
23
|
-
"@testing-library/user-event": "^14.6.
|
|
24
|
-
"@types/bun": "^1.4.
|
|
26
|
+
"@testing-library/user-event": "^14.6.13",
|
|
27
|
+
"@types/bun": "^1.4.5",
|
|
25
28
|
"@types/react": "^19.3.0",
|
|
26
29
|
"@types/react-dom": "^19.3.0",
|
|
27
|
-
"
|
|
30
|
+
"graphql": "^17.1.0",
|
|
31
|
+
"playwright-core": "^1.65.1",
|
|
28
32
|
"react": "^19.3.1",
|
|
29
33
|
"react-dom": "^19.3.1",
|
|
30
34
|
"react-hook-form": "^7.92.0",
|
|
@@ -38,11 +42,17 @@
|
|
|
38
42
|
"typecheck": "bun check",
|
|
39
43
|
"test": "bun test",
|
|
40
44
|
"size": "bun scripts/check-size.ts",
|
|
45
|
+
"check:deps": "bun scripts/check-deps.ts",
|
|
46
|
+
"generate": "bun scripts/generate.ts",
|
|
47
|
+
"generate:check": "bun scripts/generate.ts --check",
|
|
48
|
+
"schema:sync": "bun scripts/schema-sync.ts",
|
|
49
|
+
"schema:check": "bun scripts/schema-sync.ts --check",
|
|
41
50
|
"test:browser": "bun scripts/leak-test.ts && bun scripts/screenshot-test.ts && bun scripts/files-test.ts && bun scripts/thread-test.ts && bun scripts/privacy-test.ts",
|
|
42
51
|
"fixture": "bun scripts/serve-fixture.ts",
|
|
43
|
-
"verify": "bun run lint && bun run build && bun run typecheck && bun run test && bun run size",
|
|
52
|
+
"verify": "bun run lint && bun run schema:check && bun run generate:check && bun run build && bun run typecheck && bun run test && bun run check:deps && bun run size",
|
|
44
53
|
"release": "bun scripts/publish.ts",
|
|
45
54
|
"capture": "bun scripts/capture.ts",
|
|
55
|
+
"capture:theme": "bun scripts/capture-theme.ts",
|
|
46
56
|
"capture:requests": "bun scripts/capture-requests.ts",
|
|
47
57
|
"capture:thread": "bun scripts/capture-thread.ts",
|
|
48
58
|
"capture:privacy": "bun scripts/capture-privacy.ts"
|
|
@@ -54,13 +64,22 @@
|
|
|
54
64
|
"types": "./dist/types/index.d.ts",
|
|
55
65
|
"import": "./dist/index.js",
|
|
56
66
|
"default": "./dist/index.js"
|
|
67
|
+
},
|
|
68
|
+
"./server": {
|
|
69
|
+
"types": "./dist/types/server.d.ts",
|
|
70
|
+
"import": "./dist/server.js",
|
|
71
|
+
"default": "./dist/server.js"
|
|
57
72
|
}
|
|
58
73
|
},
|
|
59
74
|
"files": [
|
|
60
75
|
"dist/index.js",
|
|
61
76
|
"dist/types/index.d.ts",
|
|
62
77
|
"dist/types/config.d.ts",
|
|
63
|
-
"dist/types/element.d.ts"
|
|
78
|
+
"dist/types/element.d.ts",
|
|
79
|
+
"dist/types/mount.d.ts",
|
|
80
|
+
"dist/server.js",
|
|
81
|
+
"dist/types/server.d.ts",
|
|
82
|
+
"docs/integration.md"
|
|
64
83
|
],
|
|
65
84
|
"types": "./dist/types/index.d.ts",
|
|
66
85
|
"sideEffects": true
|