@softistx/pulsarix-widget 0.4.0 → 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 +3 -3
- package/dist/index.js +24 -24
- package/dist/types/mount.d.ts +6 -0
- package/docs/integration.md +81 -6
- package/package.json +2 -2
package/dist/types/mount.d.ts
CHANGED
|
@@ -45,6 +45,12 @@ export interface WidgetHandle {
|
|
|
45
45
|
close(): void;
|
|
46
46
|
/** Shows or hides the launcher only; an open dialog is never closed or hidden by it. */
|
|
47
47
|
setLauncherVisible(visible: boolean): void;
|
|
48
|
+
/**
|
|
49
|
+
* Hides the launcher until the returned function is called, for a host's own sticky bar.
|
|
50
|
+
* Ref-counted: the launcher stays hidden while any hold is open, and shows again once every
|
|
51
|
+
* hold is released and `setLauncherVisible` is not `false`. Releasing twice does nothing.
|
|
52
|
+
*/
|
|
53
|
+
holdLauncher(): () => void;
|
|
48
54
|
/** Signs the reporter in with a signed identity token, or out with `null`. */
|
|
49
55
|
identify(token: string | null): void;
|
|
50
56
|
/** Light or dark now, without a remount; `"auto"` follows the OS, not the host page. */
|
package/docs/integration.md
CHANGED
|
@@ -6,6 +6,7 @@ and "My requests" working. Reference for every option and event: the [README](..
|
|
|
6
6
|
1. [Get a project and a widget key](#1-get-a-project-and-a-widget-key)
|
|
7
7
|
2. [Add the widget](#2-add-the-widget)
|
|
8
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)
|
|
9
10
|
4. [Content Security Policy](#4-content-security-policy)
|
|
10
11
|
5. [Options and metadata](#5-options-and-metadata)
|
|
11
12
|
6. [Events](#6-events)
|
|
@@ -14,6 +15,7 @@ and "My requests" working. Reference for every option and event: the [README](..
|
|
|
14
15
|
9. [Inheriting the host app's theme](#9-inheriting-the-host-apps-theme)
|
|
15
16
|
10. [Changing theme, locale and metadata at run time](#10-changing-theme-locale-and-metadata-at-run-time)
|
|
16
17
|
11. [The launcher: look, position and visibility](#11-the-launcher-look-position-and-visibility)
|
|
18
|
+
- [Sticky bottom bars: `holdLauncher()`](#sticky-bottom-bars-holdlauncher)
|
|
17
19
|
|
|
18
20
|
## 1. Get a project and a widget key
|
|
19
21
|
|
|
@@ -27,7 +29,7 @@ A key has:
|
|
|
27
29
|
or `https://*.example.com`. A request from any other origin, or with no `Origin`, is refused;
|
|
28
30
|
- an optional **identity secret** for signed-in users (section 3): in the key's "Signed-in users" card,
|
|
29
31
|
press "Generate secret". It is **shown once**: copy it into your server's environment right away
|
|
30
|
-
(for example `PULSARIX_IDENTITY_SECRET`)
|
|
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
|
|
31
33
|
the old one stops working at once.
|
|
32
34
|
|
|
33
35
|
The key also has a switch (disabled keys refuse everything) and a "Screenshots and files" switch.
|
|
@@ -91,7 +93,7 @@ export function pulsarixTokenFor(user: {
|
|
|
91
93
|
}) {
|
|
92
94
|
return signPulsarixIdentity({
|
|
93
95
|
secret: process.env.PULSARIX_IDENTITY_SECRET as string,
|
|
94
|
-
publicKey: process.env.
|
|
96
|
+
publicKey: process.env.PULSARIX_PROJECT_KEY as string, // pk_live_xxx
|
|
95
97
|
sub: user.id,
|
|
96
98
|
email: user.email,
|
|
97
99
|
name: user.name,
|
|
@@ -103,7 +105,7 @@ export function pulsarixTokenFor(user: {
|
|
|
103
105
|
Import the signer only in server code (a loader, or a `*.server.ts` file), never in the browser. Then hand
|
|
104
106
|
the token to the widget, and clear it at sign-out. In a strict-TypeScript host the script tag's global is
|
|
105
107
|
not typed: declare it once with
|
|
106
|
-
`declare global { interface Window { PulsarixWidget?: { identify(token: string | null): void } } }`,
|
|
108
|
+
`declare global { interface Window { PulsarixWidget?: { identify(token: string | null): void; holdLauncher(): () => void } } }`,
|
|
107
109
|
or use the npm route's typed handle:
|
|
108
110
|
|
|
109
111
|
```ts
|
|
@@ -154,7 +156,7 @@ export async function loader({ request }: Route.LoaderArgs) {
|
|
|
154
156
|
return {
|
|
155
157
|
pulsarixIdentity: signPulsarixIdentity({
|
|
156
158
|
secret: process.env.PULSARIX_IDENTITY_SECRET as string,
|
|
157
|
-
publicKey: process.env.
|
|
159
|
+
publicKey: process.env.PULSARIX_PROJECT_KEY as string,
|
|
158
160
|
sub: user.id,
|
|
159
161
|
email: user.email,
|
|
160
162
|
name: user.name,
|
|
@@ -171,6 +173,50 @@ export default function Layout() {
|
|
|
171
173
|
}
|
|
172
174
|
```
|
|
173
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
|
+
|
|
174
220
|
A token given with `identify()` stays in the widget's memory; one in `data-identity` is an attribute of
|
|
175
221
|
the element and visible in the DOM, so prefer `identify()`.
|
|
176
222
|
|
|
@@ -313,6 +359,11 @@ mount({ projectKey: "pk_live_xxx", inheritTheme: false }); // the widget's own l
|
|
|
313
359
|
|
|
314
360
|
Script tag: `data-inherit-theme="false"`. Element attribute: `inherit-theme="false"`.
|
|
315
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
|
+
|
|
316
367
|
### How it works
|
|
317
368
|
|
|
318
369
|
The widget lives in a shadow root and declares its own tokens there, so the page's custom
|
|
@@ -388,8 +439,8 @@ Rules:
|
|
|
388
439
|
`prefers-color-scheme`, never the host page's class or attribute. A host with its own mode switch passes the
|
|
389
440
|
resolved `"light"` or `"dark"` to `setTheme` on each change.
|
|
390
441
|
- `setTheme` is independent of `inheritTheme` (section 9). With `inheritTheme`, the widget follows the host's
|
|
391
|
-
mode
|
|
392
|
-
|
|
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
|
|
393
444
|
host's mode until the `theme` attribute is written again (that gives the mode back to the host);
|
|
394
445
|
`setTheme("auto")` gives it back at once. An inherited token still wins for its colour value.
|
|
395
446
|
- `setTheme` only sets the widget's own mode. The accent (`accentColor`) is a mount option and does not change.
|
|
@@ -473,3 +524,27 @@ widget.setLauncherVisible(true);
|
|
|
473
524
|
An open dialog is never hidden or closed by it, and `open()` still works while the launcher is hidden.
|
|
474
525
|
With the script tag: `window.PulsarixWidget.setLauncherVisible(false)`. `launcher: false` still
|
|
475
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",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"@graphql-codegen/typescript": "^7.0.1",
|
|
19
19
|
"@graphql-codegen/typescript-operations": "^7.0.1",
|
|
20
20
|
"@happy-dom/global-registrator": "^21.0.4",
|
|
21
|
-
"@softistx/material": "^3.
|
|
21
|
+
"@softistx/material": "^3.268.1",
|
|
22
22
|
"@softistx/material-core": "^0.96.1",
|
|
23
23
|
"@tailwindcss/cli": "^4.3.3",
|
|
24
24
|
"@testing-library/dom": "^10.4.2",
|