@hubpav/hwio-web-runtime 0.9.0 → 0.9.1

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.
@@ -24,6 +24,10 @@ declare global {
24
24
  * Lazy explicit Turnstile: the third-party script loads on first form interaction (focus or
25
25
  * pointer-enter) and the widget id is stored on its form so the submit handler resets exactly
26
26
  * the token it consumed. Markup: <div class="cf-turnstile" data-sitekey="..."> inside a <form>.
27
+ *
28
+ * The widget takes the page's scheme when it renders, and Turnstile cannot change the theme of a
29
+ * rendered widget, so a switch of the site theme (hwio:themechange) redraws every widget in the
30
+ * new scheme; the redrawn widget issues a fresh token.
27
31
  */
28
32
  export declare function hwioTurnstileRuntime(): import("./mount.js").HWioBinder;
29
33
  /** Waits (bounded) for the lazily rendered widget to produce a token before the form is snapshotted. */
package/dist/turnstile.js CHANGED
@@ -35,22 +35,51 @@ function loadApi() {
35
35
  });
36
36
  return window.__hwioTurnstileLoader;
37
37
  }
38
+ function draw(api, container) {
39
+ return api.render(container, { sitekey: container.dataset.sitekey ?? '', theme: hwioResolvedScheme(), size: widgetSize(container) });
40
+ }
41
+ function storeId(form, id) {
42
+ form.hwioTurnstileWidgetId = id;
43
+ form.hwTurnstileWidgetId = id; // legacy-ok
44
+ }
38
45
  /**
39
46
  * Lazy explicit Turnstile: the third-party script loads on first form interaction (focus or
40
47
  * pointer-enter) and the widget id is stored on its form so the submit handler resets exactly
41
48
  * the token it consumed. Markup: <div class="cf-turnstile" data-sitekey="..."> inside a <form>.
49
+ *
50
+ * The widget takes the page's scheme when it renders, and Turnstile cannot change the theme of a
51
+ * rendered widget, so a switch of the site theme (hwio:themechange) redraws every widget in the
52
+ * new scheme; the redrawn widget issues a fresh token.
42
53
  */
43
54
  export function hwioTurnstileRuntime() {
44
55
  let controller = null;
45
- let widgetIds = [];
56
+ let widgets = [];
57
+ let themeBound = false;
46
58
  function cleanup() {
47
59
  controller?.abort();
48
60
  controller = null;
49
- widgetIds.forEach((id) => { try {
61
+ themeBound = false;
62
+ widgets.forEach(({ id }) => { try {
50
63
  window.turnstile?.remove(id);
51
64
  }
52
65
  catch { /* gone */ } });
53
- widgetIds = [];
66
+ widgets = [];
67
+ }
68
+ function redraw() {
69
+ const api = window.turnstile;
70
+ if (!api)
71
+ return;
72
+ widgets = widgets.filter((w) => w.container.isConnected).map((w) => {
73
+ try {
74
+ api.remove(w.id);
75
+ }
76
+ catch { /* gone */ }
77
+ const id = draw(api, w.container);
78
+ if (id === undefined)
79
+ return w;
80
+ storeId(w.form, id);
81
+ return { ...w, id };
82
+ });
54
83
  }
55
84
  function bind() {
56
85
  const containers = Array.from(document.querySelectorAll('.cf-turnstile:not([data-hwio-turnstile-bound])'));
@@ -58,6 +87,10 @@ export function hwioTurnstileRuntime() {
58
87
  return;
59
88
  controller ??= new AbortController();
60
89
  const { signal } = controller;
90
+ if (!themeBound) {
91
+ themeBound = true;
92
+ window.addEventListener('hwio:themechange', redraw, { signal });
93
+ }
61
94
  containers.forEach((container) => {
62
95
  const form = container.closest('form');
63
96
  if (!form)
@@ -68,11 +101,10 @@ export function hwioTurnstileRuntime() {
68
101
  if (signal.aborted || !container.isConnected || state === 'rendered')
69
102
  return;
70
103
  state = 'rendered';
71
- const id = api.render(container, { sitekey: container.dataset.sitekey ?? '', theme: hwioResolvedScheme(), size: widgetSize(container) });
104
+ const id = draw(api, container);
72
105
  if (id !== undefined) {
73
- widgetIds.push(id);
74
- form.hwioTurnstileWidgetId = id;
75
- form.hwTurnstileWidgetId = id; // legacy-ok
106
+ widgets.push({ container, form, id });
107
+ storeId(form, id);
76
108
  }
77
109
  };
78
110
  const load = () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hubpav/hwio-web-runtime",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "description": "Framework-free browser runtime for the HARDWARIO websites: consent (Consent Mode v2 + GTM), HubSpot forms with Turnstile, attribution, theme, CTA tracking and small behaviours. View-transition safe.",
5
5
  "license": "MIT",
6
6
  "type": "module",