@softistx/pulsarix-widget 0.3.1 → 0.4.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 CHANGED
@@ -6,9 +6,28 @@ Shadow DOM, so your page's styles and the widget's never touch.
6
6
 
7
7
  ## Install
8
8
 
9
- The package is published on npmjs (public), no registry setup needed:
9
+ The package is published on npmjs with restricted access: installing it needs an npm read token for
10
+ the `@softistx` scope. Point the scope at npmjs with the token read from an environment variable
11
+ (never commit its value).
12
+
13
+ In `bunfig.toml` (Bun):
14
+
15
+ ```toml
16
+ [install.scopes]
17
+ "@softistx" = { url = "https://registry.npmjs.org", token = "$NPM_SOFTISTX_READ_TOKEN" }
18
+ ```
19
+
20
+ Or in `.npmrc` (npm, pnpm, yarn):
21
+
22
+ ```ini
23
+ @softistx:registry=https://registry.npmjs.org
24
+ //registry.npmjs.org/:_authToken=${NPM_SOFTISTX_READ_TOKEN}
25
+ ```
26
+
27
+ Then:
10
28
 
11
29
  ```sh
30
+ export NPM_SOFTISTX_READ_TOKEN=<your read token>
12
31
  npm install @softistx/pulsarix-widget
13
32
  ```
14
33
 
@@ -28,9 +47,10 @@ and isolated in the widget's shadow root, so it never clashes with the page's ow
28
47
 
29
48
  Attributes (all optional but the key): `data-locale` (`en` default, `fr`), `data-theme`
30
49
  (`light`, `dark`, `auto` default), `data-position` (`bottom-right` default, `bottom-left`),
31
- `data-accent-color` (a hex seed for the theme; without it the widget wears material's built-in `lavande` theme), `data-api-base` (the
50
+ `data-accent-color` (a hex seed for the theme; without it the widget wears the host's `@softistx/material` theme, else material's built-in `lavande`), `data-inherit-theme="false"` (never inherit the host's theme; see "Theme"), `data-api-base` (the
32
51
  pulsarix origin; defaults to the host that served the loader), `data-launcher="false"` (no
33
- floating button; open it with `window.PulsarixWidget.open()`), `data-mask-text="true"` (the
52
+ floating button; open it with `window.PulsarixWidget.open()`) or `data-launcher="auto|pill|icon"`
53
+ (`auto` is the default: icon below 640 px, pill above), `data-offset-bottom` and `data-offset-side` (see "The launcher"), `data-mask-text="true"` (the
34
54
  "Mask the text on the page" screenshot option starts on; see "Screenshots"), `data-attachments="false"`
35
55
  (no screenshot and no file controls; see "Attached files"), `data-privacy-url` (an http(s) link to your
36
56
  own privacy page, shown after the privacy line), `data-prefill-email` and `data-prefill-title` (starting
@@ -48,7 +68,9 @@ Without the attributes, drive it from script. Calls made before the bundle has l
48
68
 
49
69
  ```js
50
70
  PulsarixWidget.init({ projectKey: "pk_live_xxx", locale: "fr" });
51
- const off = PulsarixWidget.on("pulsarix:submit", (e) => console.log(e.detail.reportId));
71
+ const off = PulsarixWidget.on("pulsarix:submit", (e) =>
72
+ console.log(e.detail.reportId),
73
+ );
52
74
  PulsarixWidget.open();
53
75
  // later
54
76
  off();
@@ -84,32 +106,88 @@ handle. To mount again, destroy first:
84
106
 
85
107
  ```ts
86
108
  widget.destroy();
87
- const next = mount({ projectKey: "pk_live_xxx", apiBase: "https://<pulsarix-host>", locale: "fr" });
109
+ const next = mount({
110
+ projectKey: "pk_live_xxx",
111
+ apiBase: "https://<pulsarix-host>",
112
+ locale: "fr",
113
+ });
88
114
  ```
89
115
 
90
116
  A script-tag loader and an npm import are two separate instances: use one route per page.
91
117
 
92
118
  ### Exports
93
119
 
120
+ Server helper: `@softistx/pulsarix-widget/server` exports `signPulsarixIdentity` and the type `SignPulsarixIdentityOptions` (see "Signing the identity on your server").
121
+
94
122
  `mount`, `defineWidget` (registers the element; `mount` calls it), `PulsarixWidgetElement`,
95
123
  `TAG_NAME` (`"pulsarix-widget"`), and the types `MountOptions`,
96
- `WidgetHandle`, `WidgetEvent`, `WidgetConfig`, `Locale`, `Position`, `ThemeMode`.
97
-
98
- | `MountOptions` | Type | Default |
99
- | --- | --- | --- |
100
- | `projectKey` | `string` | required; missing reports `pulsarix:error` `config` |
101
- | `apiBase` | `string` | none; required on the npm route |
102
- | `locale` | `"en" \| "fr"` | `"en"` |
103
- | `theme` | `"light" \| "dark" \| "auto"` | `"auto"` |
104
- | `position` | `"bottom-right" \| "bottom-left"` | `"bottom-right"` |
105
- | `accentColor` | hex string | none: the built-in `lavande` theme |
106
- | `launcher` | `boolean` | `true` (`false` hides the floating button) |
107
- | `identity` | `string \| null` | none: today's form only (see "Identified reporters") |
108
- | `target` | `Element` | `document.body` |
109
-
110
- `WidgetHandle`: `open()`, `close()`, `destroy()`, `identify(token \| null)`, `on(name, callback)` (returns the function that
124
+ `WidgetHandle`, `WidgetEvent`, `WidgetConfig`, `Locale`, `Position`, `ThemeMode`, `LauncherMode`,
125
+ `LauncherOffset`.
126
+
127
+ | `MountOptions` | Type | Default |
128
+ | -------------- | --------------------------------------------------- | ---------------------------------------------------- |
129
+ | `projectKey` | `string` | required; missing reports `pulsarix:error` `config` |
130
+ | `apiBase` | `string` | none; required on the npm route |
131
+ | `locale` | `"en" \| "fr"` | `"en"` |
132
+ | `theme` | `"light" \| "dark" \| "auto"` | `"auto"` |
133
+ | `position` | `"bottom-right" \| "bottom-left"` | `"bottom-right"` |
134
+ | `accentColor` | hex string | none: the built-in `lavande` theme |
135
+ | `inheritTheme` | `boolean` | `true`: wear the host's `@softistx/material` theme |
136
+ | `launcher` | `boolean \| "auto" \| "pill" \| "icon"` | `"auto"` (`false` removes the floating button) |
137
+ | `offset` | `{ bottom?, side? }` (number = px, or a CSS length) | 16 px each, plus the bottom safe area |
138
+ | `identity` | `string \| null` | none: today's form only (see "Identified reporters") |
139
+ | `target` | `Element` | `document.body` |
140
+
141
+ `WidgetHandle`: `open()`, `close()`, `setLauncherVisible(visible)`, `destroy()`, `identify(token \| null)`, `setTheme(theme)`, `setLocale(locale)`,
142
+ `setMetadata(metadata \| null)` (see "Changing theme, locale and metadata at run time"), `on(name, callback)` (returns the function that
111
143
  unsubscribes) and `element`, the `<pulsarix-widget>` (`element.isOpen`, `element.config`).
112
144
 
145
+ ## Changing theme, locale and metadata at run time
146
+
147
+ When your app switches organisation, language or light/dark mode, do not remount the widget: a remount loses
148
+ a report being typed. Call the setters instead. Each applies live, keeps the dialog open and keeps what the
149
+ reporter typed.
150
+
151
+ ```ts
152
+ widget.setTheme("dark"); // "light" | "dark" | "auto"
153
+ widget.setLocale("fr"); // "en" | "fr": the strings re-render
154
+ widget.setMetadata({ org: "acme", plan: "pro" }); // replaces the metadata of the next report; null clears it
155
+ ```
156
+
157
+ The same three exist on the script-tag global (`PulsarixWidget.setTheme("dark")`, ...); a call made before the
158
+ bundle has loaded is queued. An unsupported theme or locale is ignored.
159
+
160
+ - `"auto"` follows the operating system (`prefers-color-scheme`), not your page's own mode. To follow your
161
+ app's Clair / Sombre / Système switch, pass the resolved `"light"` or `"dark"` to `setTheme` whenever it changes.
162
+ - With `inheritTheme`, `setTheme("light" | "dark")` wins over the host's mode and `"auto"` gives it back to the
163
+ host; inherited colour tokens still win for their value.
164
+ - `setMetadata` replaces the whole object (same limits as `metadata` at mount); a report already sent keeps
165
+ the metadata it was sent with.
166
+ - `setLocale` changes the widget's own strings at once. The built-in labels of `@softistx/material` follow
167
+ when the form is next idle: on an idle form the typed fields, the type, the impact and the tab are kept
168
+ and files or a screenshot already attached are dropped; while a report is being sent, on the confirmation
169
+ or on the "My requests" tab nothing is rebuilt (and nothing is lost), and those labels switch once the
170
+ reporter is back on the form or the dialog closes.
171
+ - A repeated call before the bundle loads replaces the earlier one: only the last of each setter runs.
172
+ - With `inheritTheme`, an explicit `setTheme` wins over the host's mode until the `theme` attribute is
173
+ written again; see [docs/integration.md](docs/integration.md#10-changing-theme-locale-and-metadata-at-run-time).
174
+
175
+ ## Theme
176
+
177
+ The widget wears the host app's `@softistx/material` theme (colours, radius, light or dark) when the host has
178
+ one, and follows its theme picker; its own `lavande` values (or `accentColor`) are the fallback for every token
179
+ the host lacks. `inheritTheme: false` (`data-inherit-theme="false"`) keeps the widget's own look. How it
180
+ works, and its limits: [docs/integration.md](docs/integration.md#9-inheriting-the-host-apps-theme).
181
+
182
+ ## The launcher
183
+
184
+ `launcher: "auto"` (default) shows a 48 px round icon button below 640 px of viewport width and the text
185
+ pill above, switching live. `"pill"` and `"icon"` force one, `false` removes it. To clear a sticky bar,
186
+ `offset: { bottom: 88 }` (or `--pulsarix-launcher-bottom` / `--pulsarix-launcher-side` in your own CSS), and
187
+ `setLauncherVisible(false)` hides only the launcher, never an open dialog; with the script tag it is
188
+ `window.PulsarixWidget.setLauncherVisible(false)`. A negative or malformed offset is ignored. Details and examples:
189
+ [docs/integration.md](docs/integration.md).
190
+
113
191
  ## The report form
114
192
 
115
193
  The launcher opens a dialog with a title, a description and an optional e-mail. With an e-mail,
@@ -134,7 +212,7 @@ and returns focus to the launcher (or to whatever opened it).
134
212
 
135
213
  ## Identified reporters
136
214
 
137
- Give the widget the token your server signed for the signed-in user (`data-identity`,
215
+ Give the widget the token your server signed for the signed-in user (see "Signing the identity on your server"; `data-identity`,
138
216
  `identity` in `init` / `mount`, or `PulsarixWidget.identify(token)` after sign-in; call
139
217
  `identify(null)` at sign-out). The secret that signs the token stays on your server and never
140
218
  reaches the browser; the widget only sends the token, in the `x-pulsarix-identity` header, and
@@ -175,6 +253,88 @@ attribute of the element, so it is visible in the DOM (prefer `identify()` when
175
253
  - Everyone (identified or not) can give an optional type (bug, question, suggestion) and an
176
254
  impact (blocking, annoying, minor) in the form's Details group.
177
255
 
256
+ ## Signing the identity on your server
257
+
258
+ `@softistx/pulsarix-widget/server` signs the token for you, so you never hand-write the HMAC. It is
259
+ server-only (`node:crypto`, works on Node and Bun) and is a separate file: the browser bundle and the
260
+ loader never contain it. Never import it in client code.
261
+
262
+ ```ts
263
+ import { signPulsarixIdentity } from "@softistx/pulsarix-widget/server";
264
+
265
+ const token = signPulsarixIdentity({
266
+ secret: process.env.PULSARIX_IDENTITY_SECRET!, // shown once in pulsarix; keep it on the server
267
+ publicKey: "pk_live_xxx", // the widget key's public key
268
+ sub: user.id, // 1 to 200 characters, stable per user
269
+ email: user.email, // optional, at most 254 characters
270
+ name: user.name, // optional, at most 100 characters
271
+ // ttlSeconds: 3600, // default 1 hour; 24 hours (86400) at most
272
+ });
273
+ ```
274
+
275
+ | Option | Type | Notes |
276
+ | ------------ | -------------- | -------------------------------------------------------------------- |
277
+ | `secret` | `string` | required; used as the raw string, as the API does |
278
+ | `publicKey` | `string` | required; the token's audience |
279
+ | `sub` | `string` | required; 1 to 200 characters; never stored, only its digest per key |
280
+ | `email` | `string` | a valid address |
281
+ | `name` | `string` | trimmed; blank is left out |
282
+ | `ttlSeconds` | `number` | whole seconds, 1 to 86 400; default 3 600 |
283
+ | `now` | `() => number` | milliseconds; default `Date.now` (tests) |
284
+
285
+ It throws an `Error` naming the option for input the API would refuse (an empty secret, a `sub` of 0 or
286
+ over 200 characters, a `ttlSeconds` over 24 hours or not a positive whole number, a bad e-mail, a token
287
+ over 2 048 characters), instead of letting the widget meet an `intake.errors.identity-invalid` later.
288
+
289
+ The secret lives in an environment variable such as `PULSARIX_IDENTITY_SECRET`, never in the page,
290
+ the repository or a public config. Tokens expire: sign a fresh one on each request that renders the
291
+ page (a loader, a session endpoint) and call `identify(newToken)` before the old one lapses; 1 hour is
292
+ the recommended life.
293
+
294
+ A React Router loader signs, the component hands the token to the widget, and sign-out clears it:
295
+
296
+ ```tsx
297
+ // app/routes/app.tsx
298
+ // `Route` is React Router's generated type and `requireUser` is your own session helper.
299
+ // Import the signer in a route's loader only (or in a `*.server.ts` file the loader calls), never in
300
+ // code that runs in the browser.
301
+ import { signPulsarixIdentity } from "@softistx/pulsarix-widget/server";
302
+ import { useEffect } from "react";
303
+ import { Outlet, useLoaderData } from "react-router";
304
+
305
+ export async function loader({ request }: Route.LoaderArgs) {
306
+ const user = await requireUser(request); // your own session
307
+ return {
308
+ pulsarixIdentity: signPulsarixIdentity({
309
+ secret: process.env.PULSARIX_IDENTITY_SECRET as string,
310
+ publicKey: process.env.PULSARIX_PUBLIC_KEY as string,
311
+ sub: user.id,
312
+ email: user.email,
313
+ name: user.name,
314
+ }),
315
+ };
316
+ }
317
+
318
+ export default function App() {
319
+ const { pulsarixIdentity } = useLoaderData<typeof loader>();
320
+ useEffect(() => {
321
+ // The script tag's PulsarixWidget queues the call until the bundle has loaded.
322
+ window.PulsarixWidget?.identify(pulsarixIdentity);
323
+ }, [pulsarixIdentity]);
324
+ return <Outlet />;
325
+ }
326
+
327
+ // at sign-out, in the browser, before the session ends:
328
+ // window.PulsarixWidget?.identify(null);
329
+ ```
330
+
331
+ In a strict-TypeScript host, the script tag's global is not typed: declare it once
332
+ (`declare global { interface Window { PulsarixWidget?: { identify(token: string | null): void } } }`)
333
+ or use the npm route.
334
+
335
+ With the npm route, call `widget.identify(token)` on the handle `mount()` returned. See
336
+ [docs/integration.md](docs/integration.md) for the whole integration, CSP and troubleshooting.
337
+
178
338
  ## Screenshots
179
339
 
180
340
  A report can carry one optional screenshot, never taken without a click (and a per-report