@truzyx/consent-widget 0.1.0 → 0.2.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.
- package/README.md +21 -13
- package/dist/core/types.d.ts +1 -1
- package/dist/core/widget.d.ts +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/react.cjs +1 -1
- package/dist/react.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @truzyx/consent-widget
|
|
2
2
|
|
|
3
|
-
Framework-agnostic consent + preferences widget for third-party sites. Opens the Truzyx consent gateway (consent
|
|
3
|
+
Framework-agnostic consent + preferences widget for third-party sites. Opens the Truzyx consent gateway (consent, preferences, or direct-consent dialog) in a modal iframe overlay. Zero runtime dependencies for the vanilla entry; the React entry needs only `react` (optional peer).
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -18,8 +18,9 @@ No bundler? Use the script-tag build served from your gateway host instead (glob
|
|
|
18
18
|
|
|
19
19
|
| Value | Where it comes from |
|
|
20
20
|
|-----------|---------------------|
|
|
21
|
-
| `baseUrl` | Your gateway origin
|
|
22
|
-
| `
|
|
21
|
+
| `baseUrl` | Your gateway origin. Optional — precedence is: explicit `baseUrl` → `env` → `<script>` tag origin → current origin. |
|
|
22
|
+
| `env` | `"live"` (`https://ui.gateway.nodbook.in`) or `"sandbox"` (`https://sandbox-ui.gateway.nodbook.in`). Used only when `baseUrl` is omitted. |
|
|
23
|
+
| `token` | Principal JWT for the end user, issued by your backend. Sent to the gateway as `?auth_token=` and via `postMessage`. |
|
|
23
24
|
| `requestId` | Consent request id created via the gateway API. Required for the consent dialog only. |
|
|
24
25
|
|
|
25
26
|
## Usage
|
|
@@ -39,6 +40,11 @@ ConsentWidget.openPreferences("<principal-jwt>", {
|
|
|
39
40
|
baseUrl: "https://consent.truzyx.com",
|
|
40
41
|
});
|
|
41
42
|
|
|
43
|
+
// Or direct-consent dialog for purpose(s)
|
|
44
|
+
ConsentWidget.openDirectConsent("purpose_123", "<principal-jwt>", {
|
|
45
|
+
env: "sandbox", // or live; ignored when baseUrl is set
|
|
46
|
+
});
|
|
47
|
+
|
|
42
48
|
// React to flow completion
|
|
43
49
|
const off = ConsentWidget.onCallback((event) => {
|
|
44
50
|
console.log(event.type, event);
|
|
@@ -89,7 +95,9 @@ The hook subscribes/unsubscribes the callback automatically and closes the dialo
|
|
|
89
95
|
|
|
90
96
|
## API reference
|
|
91
97
|
|
|
92
|
-
### Methods
|
|
98
|
+
### Methods
|
|
99
|
+
|
|
100
|
+
Vanilla singleton methods and hook return value are the same, except the hook additionally exposes `open(options)` (routes to consent when `requestId` is set, else preferences) and names the subscribe function `onEvent` instead of `onCallback`.
|
|
93
101
|
|
|
94
102
|
| Method | Signature | Description |
|
|
95
103
|
|--------|-----------|-------------|
|
|
@@ -107,7 +115,7 @@ The hook subscribes/unsubscribes the callback automatically and closes the dialo
|
|
|
107
115
|
|-------|------|---------|-------------|
|
|
108
116
|
| `token` | `string` | — (required) | Principal JWT. |
|
|
109
117
|
| `requestId` | `string` | — | Consent request id. Required for `mode: "consent"`. |
|
|
110
|
-
| `mode` | `"consent" \| "preferences"` | `"consent"` if `requestId` is set, else `"preferences"` | Which dialog to open. |
|
|
118
|
+
| `mode` | `"consent" \| "preferences" \| "direct_consent"` | `"consent"` if `requestId` is set, else `"preferences"` | Which dialog to open. Set implicitly by `openConsent` / `openPreferences` / `openDirectConsent`. |
|
|
111
119
|
| `baseUrl` | `string` | script-tag origin, else current origin | Gateway origin. Optional — when provided it wins and `env` is ignored. |
|
|
112
120
|
| `env` | `"live" \| "sandbox"` | — (no default) | Picks the gateway host when `baseUrl` is omitted: live → `https://ui.gateway.nodbook.in`, sandbox → `https://sandbox-ui.gateway.nodbook.in`. |
|
|
113
121
|
| `autoClose` | `boolean` | `true` | Auto-close on terminal events (`consent_accepted`, `consent_declined`, `already_resolved`, `redirecting`). |
|
|
@@ -118,24 +126,24 @@ The hook subscribes/unsubscribes the callback automatically and closes the dialo
|
|
|
118
126
|
|
|
119
127
|
| `type` | Extra fields | Meaning |
|
|
120
128
|
|--------|--------------|---------|
|
|
121
|
-
| `opened` | `requestId?` | Dialog opened. |
|
|
122
|
-
| `consent_accepted` | `requestId?` | User accepted (all or selected purposes). |
|
|
123
|
-
| `consent_declined` | `requestId?` | User declined/cancelled. |
|
|
129
|
+
| `opened` | `requestId?`, `data?` | Dialog opened (`data.purposeIds` for direct consent). |
|
|
130
|
+
| `consent_accepted` | `requestId?`, `data?` | User accepted (all or selected purposes). Direct consent includes `data.purposeIds`. |
|
|
131
|
+
| `consent_declined` | `requestId?`, `data?` | User declined/cancelled. Direct consent includes `data.purposeIds`. |
|
|
124
132
|
| `already_resolved` | `requestId?` | Request was already approved/declined. |
|
|
125
|
-
| `redirecting` | `requestId?`, `redirectUrl` | Gateway is redirecting (e.g. back to your site). |
|
|
126
|
-
| `error` | `requestId?`, `message?` | Load/submit failure. |
|
|
133
|
+
| `redirecting` | `requestId?`, `redirectUrl`, `data?` | Gateway is redirecting (e.g. back to your site). |
|
|
134
|
+
| `error` | `requestId?`, `message?`, `data?` | Load/submit failure. |
|
|
127
135
|
| `closed` | `requestId?` | Dialog closed (×, ESC, backdrop, or `close()`). |
|
|
128
136
|
|
|
129
137
|
## How it works
|
|
130
138
|
|
|
131
|
-
- The widget renders a modal overlay with an `<iframe>` pointing at `{baseUrl}/consent_manager/consent/{requestId}` or `{baseUrl}/consent_manager/
|
|
139
|
+
- The widget renders a modal overlay with an `<iframe>` pointing at `{baseUrl}/consent_manager/consent/{requestId}`, `{baseUrl}/consent_manager/preferences`, or `{baseUrl}/consent_manager/direct_consent`, with `?auth_token=<token>&embed=true&origin=<your-origin>` (plus `?purpose_id=`, `?org_id=`, `?locale=` for direct consent).
|
|
132
140
|
- The token is also delivered via `postMessage` (`PRINCIPAL_TOKEN`) when the iframe signals readiness.
|
|
133
141
|
- Flow completion is reported back as `CONSENT_WIDGET_EVENT` messages, validated against the gateway origin, and fanned out to your callbacks. In embed mode the gateway pages render chromeless (no extra card/background) so only the widget's single card is visible.
|
|
134
142
|
- Because the dialog is an iframe, there is no CSS leakage in either direction.
|
|
135
143
|
|
|
136
144
|
## Trying it locally
|
|
137
145
|
|
|
138
|
-
The gateway repo ships a dev harness at `/consent_manager/check` (query params `requestId`, `token`/`
|
|
146
|
+
The gateway repo ships a dev harness at `/consent_manager/check` (query params `requestId`, `token`/`auth_token`, `baseUrl`) that drives this same core and logs every callback event — useful to validate your `requestId`/`token` before integrating.
|
|
139
147
|
|
|
140
148
|
## Package layout
|
|
141
149
|
|
|
@@ -144,4 +152,4 @@ The gateway repo ships a dev harness at `/consent_manager/check` (query params `
|
|
|
144
152
|
| `.` | `dist/index.js` (ESM), `dist/index.cjs` (CJS), `dist/index.d.ts` | Any framework / plain JS. |
|
|
145
153
|
| `./react` | `dist/react.js`, `dist/react.cjs`, `dist/react.d.ts` | `useConsentWidget` hook (`react` stays external). |
|
|
146
154
|
|
|
147
|
-
Source of truth is `src/core/` (`widget.ts`, `types.ts`). Version: `0.
|
|
155
|
+
Source of truth is `src/core/` (`widget.ts`, `types.ts`). Version: `0.2.0`.
|
package/dist/core/types.d.ts
CHANGED
|
@@ -28,7 +28,7 @@ export type WidgetCallback = (event: WidgetEvent) => void;
|
|
|
28
28
|
export interface WidgetInitOptions {
|
|
29
29
|
/** Consent request id. Required for mode "consent", omitted for "preferences". */
|
|
30
30
|
requestId?: string;
|
|
31
|
-
/** Principal JWT used by the gateway APIs (sent as ?
|
|
31
|
+
/** Principal JWT used by the gateway APIs (sent as ?auth_token= + postMessage). */
|
|
32
32
|
token: string;
|
|
33
33
|
/** Which dialog to open. Defaults to "consent" when requestId is present. */
|
|
34
34
|
mode?: WidgetMode;
|
package/dist/core/widget.d.ts
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* plain <script> inclusion (see `vite.widget.config.ts` at the repo root).
|
|
14
14
|
*
|
|
15
15
|
* How it works: opens the gateway consent / preferences page in a modal
|
|
16
|
-
* iframe overlay. Auth token travels as `?
|
|
16
|
+
* iframe overlay. Auth token travels as `?auth_token=` (already supported by
|
|
17
17
|
* `usePrincipalTokenHandshake`) and via postMessage PRINCIPAL_TOKEN.
|
|
18
18
|
* Completion events flow back as CONSENT_WIDGET_EVENT messages and are
|
|
19
19
|
* fanned out to `onCallback`.
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var p=Object.defineProperty;var y=(n,e,t)=>e in n?p(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var s=(n,e,t)=>y(n,typeof e!="symbol"?e+"":e,t);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const c={live:"https://ui.gateway.nodbook.in",sandbox:"https://sandbox-ui.gateway.nodbook.in"},b="CONSENT_WIDGET_EVENT",h="GATEWAY_UI_READY",u="PRINCIPAL_TOKEN";function E(n){return typeof n=="object"&&n!==null&&n.type===b&&typeof n.event=="object"}function
|
|
1
|
+
"use strict";var p=Object.defineProperty;var y=(n,e,t)=>e in n?p(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var s=(n,e,t)=>y(n,typeof e!="symbol"?e+"":e,t);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const c={live:"https://ui.gateway.nodbook.in",sandbox:"https://sandbox-ui.gateway.nodbook.in"},b="CONSENT_WIDGET_EVENT",h="GATEWAY_UI_READY",u="PRINCIPAL_TOKEN";function E(n){return typeof n=="object"&&n!==null&&n.type===b&&typeof n.event=="object"}function _(n){return typeof n=="object"&&n!==null&&n.type===h}const I="__consent_widget_overlay__",k=600,x=640;function A(n,e){var t;if(n)return n.replace(/\/$/,"");if(e&&c[e])return c[e];try{const r=document.getElementsByTagName("script");for(let i=r.length-1;i>=0;i--){const a=((t=r[i])==null?void 0:t.getAttribute("src"))??"";if(a.includes("consent-widget"))return new URL(a,window.location.href).origin}const o=document.currentScript;if(o!=null&&o.src)return new URL(o.src,window.location.href).origin}catch{}return window.location.origin}function C(n){var t;const e=new URLSearchParams({auth_token:n.token,embed:"true",origin:window.location.origin});return n.mode==="preferences"?`${n.baseUrl}/consent_manager/preferences?${e.toString()}`:n.mode==="direct_consent"?((t=n.purposeIds)!=null&&t.length&&e.set("purpose_id",n.purposeIds.join(",")),n.orgId&&e.set("org_id",n.orgId),n.locale&&e.set("locale",n.locale),`${n.baseUrl}/consent_manager/direct_consent?${e.toString()}`):`${n.baseUrl}/consent_manager/consent/${encodeURIComponent(n.requestId??"")}?${e.toString()}`}class v{constructor(){s(this,"overlay",null);s(this,"iframe",null);s(this,"session",null);s(this,"callbacks",new Set);s(this,"listening",!1);s(this,"escHandler",e=>{var t;e.key==="Escape"&&this.close({type:"closed",requestId:(t=this.session)==null?void 0:t.requestId})});s(this,"messageHandler",e=>{var r,o;if(!this.session||!this.iframe)return;let t="";try{t=new URL(this.session.baseUrl).origin}catch{t=""}if(!(t&&e.origin!==t)&&e.source===this.iframe.contentWindow){if(_(e.data)){try{(r=this.iframe.contentWindow)==null||r.postMessage({type:u,token:this.session.token},t||"*")}catch{}return}if(typeof e.data=="object"&&e.data!==null&&e.data.type===h){try{(o=this.iframe.contentWindow)==null||o.postMessage({type:u,token:this.session.token},t||"*")}catch{}return}if(E(e.data)){const i=e.data.event;this.emit(i),this.session.autoClose&&(i.type==="consent_accepted"||i.type==="consent_declined"||i.type==="already_resolved"||i.type==="redirecting")&&this.teardown()}}})}openConsent(e,t,r){this.openDialog({...r,requestId:e,token:(r==null?void 0:r.token)??t})}openPreferences(e,t){this.openDialog({...t,token:(t==null?void 0:t.token)??e})}openDirectConsent(e,t,r){const o=(Array.isArray(e)?e:[e]).map(i=>i.trim()).filter(Boolean);if(o.length===0)throw new Error("ConsentWidget.openDirectConsent: at least one `purposeId` is required.");this.openDialog({...r,purposeIds:o,token:(r==null?void 0:r.token)??t,mode:"direct_consent"})}openDialog(e){var r;if(!e||typeof e.token!="string"||e.token.length===0)throw new Error("ConsentWidget: `token` is required.");const t=e.mode??(e.requestId?"consent":"preferences");if(t==="consent"&&!e.requestId)throw new Error("ConsentWidget: `requestId` is required for the consent dialog.");this.open({requestId:e.requestId,token:e.token,mode:t,purposeIds:e.purposeIds,orgId:e.orgId??null,locale:e.locale,baseUrl:A(e.baseUrl,e.env),autoClose:e.autoClose??!0}),this.render(e.allowClose??!0,e.width??k,e.height??x),this.emit({type:"opened",requestId:e.requestId,data:(r=e.purposeIds)!=null&&r.length?{purposeIds:e.purposeIds}:void 0})}onCallback(e){return this.callbacks.add(e),()=>{this.callbacks.delete(e)}}close(e){var o;const t=e??{type:"closed",requestId:(o=this.session)==null?void 0:o.requestId},r=this.overlay!==null;this.teardown(),r&&this.emit(t)}destroy(){this.callbacks.clear(),this.teardown()}isOpen(){return this.overlay!==null}open(e){this.teardown(),this.session=e,this.ensureListening()}ensureListening(){this.listening||(window.addEventListener("message",this.messageHandler),window.addEventListener("keydown",this.escHandler),this.listening=!0)}render(e,t,r){if(!this.session)return;const o=C(this.session),i=document.createElement("div");i.id=I,i.setAttribute("role","dialog"),i.setAttribute("aria-modal","true"),i.style.cssText=["position:fixed","inset:0","z-index:2147483647","display:flex","align-items:center","justify-content:center","background:rgba(15,23,42,0.55)","padding:16px","box-sizing:border-box"].join(";");const a=document.createElement("div"),f=Math.min(t,window.innerWidth-32),m=Math.min(r,window.innerHeight-32);if(a.style.cssText=[`width:${f}px`,"max-width:100%",`height:${m}px`,"max-height:100%","background:#fff","border-radius:16px","overflow:hidden","position:relative","box-shadow:0 25px 50px -12px rgba(0,0,0,0.35)","display:flex","flex-direction:column"].join(";"),e){const l=document.createElement("button");l.setAttribute("aria-label","Close"),l.textContent="×",l.style.cssText=["position:absolute","top:8px","right:12px","z-index:2","width:28px","height:28px","border-radius:9999px","border:none","background:rgba(15,23,42,0.06)","color:#475569","font-size:18px","line-height:1","cursor:pointer"].join(";"),l.onclick=()=>this.close(),a.appendChild(l),i.addEventListener("mousedown",w=>{w.target===i&&this.close()})}const d=document.createElement("iframe");d.src=o,d.title="Consent manager",d.setAttribute("allow","clipboard-write"),d.style.cssText=["flex:1","width:100%","height:100%","border:0","background:#fff"].join(";"),a.appendChild(d),i.appendChild(a),document.body.appendChild(i),document.documentElement.style.overflow="hidden",this.overlay=i,this.iframe=d}teardown(){var e;(e=this.overlay)!=null&&e.parentNode&&this.overlay.parentNode.removeChild(this.overlay),this.overlay=null,this.iframe=null,this.session=null,this.listening&&(window.removeEventListener("message",this.messageHandler),window.removeEventListener("keydown",this.escHandler),this.listening=!1);try{document.documentElement.style.overflow=""}catch{}}emit(e){this.callbacks.forEach(t=>{try{t(e)}catch{}})}}const g=new v;exports.ConsentWidget=g;exports.GATEWAY_BASE_URLS=c;exports.default=g;
|
package/dist/index.js
CHANGED
package/dist/react.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var E=Object.defineProperty;var k=(n,e,t)=>e in n?E(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var h=(n,e,t)=>k(n,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("react"),w={live:"https://ui.gateway.nodbook.in",sandbox:"https://sandbox-ui.gateway.nodbook.in"},C="CONSENT_WIDGET_EVENT",y="GATEWAY_UI_READY",b="PRINCIPAL_TOKEN";function I(n){return typeof n=="object"&&n!==null&&n.type===C&&typeof n.event=="object"}function _(n){return typeof n=="object"&&n!==null&&n.type===y}const x="__consent_widget_overlay__",v=600,A=640;function T(n,e){var t;if(n)return n.replace(/\/$/,"");if(e&&w[e])return w[e];try{const r=document.getElementsByTagName("script");for(let s=r.length-1;s>=0;s--){const c=((t=r[s])==null?void 0:t.getAttribute("src"))??"";if(c.includes("consent-widget"))return new URL(c,window.location.href).origin}const i=document.currentScript;if(i!=null&&i.src)return new URL(i.src,window.location.href).origin}catch{}return window.location.origin}function W(n){var t;const e=new URLSearchParams({
|
|
1
|
+
"use strict";var E=Object.defineProperty;var k=(n,e,t)=>e in n?E(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var h=(n,e,t)=>k(n,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("react"),w={live:"https://ui.gateway.nodbook.in",sandbox:"https://sandbox-ui.gateway.nodbook.in"},C="CONSENT_WIDGET_EVENT",y="GATEWAY_UI_READY",b="PRINCIPAL_TOKEN";function I(n){return typeof n=="object"&&n!==null&&n.type===C&&typeof n.event=="object"}function _(n){return typeof n=="object"&&n!==null&&n.type===y}const x="__consent_widget_overlay__",v=600,A=640;function T(n,e){var t;if(n)return n.replace(/\/$/,"");if(e&&w[e])return w[e];try{const r=document.getElementsByTagName("script");for(let s=r.length-1;s>=0;s--){const c=((t=r[s])==null?void 0:t.getAttribute("src"))??"";if(c.includes("consent-widget"))return new URL(c,window.location.href).origin}const i=document.currentScript;if(i!=null&&i.src)return new URL(i.src,window.location.href).origin}catch{}return window.location.origin}function W(n){var t;const e=new URLSearchParams({auth_token:n.token,embed:"true",origin:window.location.origin});return n.mode==="preferences"?`${n.baseUrl}/consent_manager/preferences?${e.toString()}`:n.mode==="direct_consent"?((t=n.purposeIds)!=null&&t.length&&e.set("purpose_id",n.purposeIds.join(",")),n.orgId&&e.set("org_id",n.orgId),n.locale&&e.set("locale",n.locale),`${n.baseUrl}/consent_manager/direct_consent?${e.toString()}`):`${n.baseUrl}/consent_manager/consent/${encodeURIComponent(n.requestId??"")}?${e.toString()}`}class U{constructor(){h(this,"overlay",null);h(this,"iframe",null);h(this,"session",null);h(this,"callbacks",new Set);h(this,"listening",!1);h(this,"escHandler",e=>{var t;e.key==="Escape"&&this.close({type:"closed",requestId:(t=this.session)==null?void 0:t.requestId})});h(this,"messageHandler",e=>{var r,i;if(!this.session||!this.iframe)return;let t="";try{t=new URL(this.session.baseUrl).origin}catch{t=""}if(!(t&&e.origin!==t)&&e.source===this.iframe.contentWindow){if(_(e.data)){try{(r=this.iframe.contentWindow)==null||r.postMessage({type:b,token:this.session.token},t||"*")}catch{}return}if(typeof e.data=="object"&&e.data!==null&&e.data.type===y){try{(i=this.iframe.contentWindow)==null||i.postMessage({type:b,token:this.session.token},t||"*")}catch{}return}if(I(e.data)){const s=e.data.event;this.emit(s),this.session.autoClose&&(s.type==="consent_accepted"||s.type==="consent_declined"||s.type==="already_resolved"||s.type==="redirecting")&&this.teardown()}}})}openConsent(e,t,r){this.openDialog({...r,requestId:e,token:(r==null?void 0:r.token)??t})}openPreferences(e,t){this.openDialog({...t,token:(t==null?void 0:t.token)??e})}openDirectConsent(e,t,r){const i=(Array.isArray(e)?e:[e]).map(s=>s.trim()).filter(Boolean);if(i.length===0)throw new Error("ConsentWidget.openDirectConsent: at least one `purposeId` is required.");this.openDialog({...r,purposeIds:i,token:(r==null?void 0:r.token)??t,mode:"direct_consent"})}openDialog(e){var r;if(!e||typeof e.token!="string"||e.token.length===0)throw new Error("ConsentWidget: `token` is required.");const t=e.mode??(e.requestId?"consent":"preferences");if(t==="consent"&&!e.requestId)throw new Error("ConsentWidget: `requestId` is required for the consent dialog.");this.open({requestId:e.requestId,token:e.token,mode:t,purposeIds:e.purposeIds,orgId:e.orgId??null,locale:e.locale,baseUrl:T(e.baseUrl,e.env),autoClose:e.autoClose??!0}),this.render(e.allowClose??!0,e.width??v,e.height??A),this.emit({type:"opened",requestId:e.requestId,data:(r=e.purposeIds)!=null&&r.length?{purposeIds:e.purposeIds}:void 0})}onCallback(e){return this.callbacks.add(e),()=>{this.callbacks.delete(e)}}close(e){var i;const t=e??{type:"closed",requestId:(i=this.session)==null?void 0:i.requestId},r=this.overlay!==null;this.teardown(),r&&this.emit(t)}destroy(){this.callbacks.clear(),this.teardown()}isOpen(){return this.overlay!==null}open(e){this.teardown(),this.session=e,this.ensureListening()}ensureListening(){this.listening||(window.addEventListener("message",this.messageHandler),window.addEventListener("keydown",this.escHandler),this.listening=!0)}render(e,t,r){if(!this.session)return;const i=W(this.session),s=document.createElement("div");s.id=x,s.setAttribute("role","dialog"),s.setAttribute("aria-modal","true"),s.style.cssText=["position:fixed","inset:0","z-index:2147483647","display:flex","align-items:center","justify-content:center","background:rgba(15,23,42,0.55)","padding:16px","box-sizing:border-box"].join(";");const c=document.createElement("div"),g=Math.min(t,window.innerWidth-32),p=Math.min(r,window.innerHeight-32);if(c.style.cssText=[`width:${g}px`,"max-width:100%",`height:${p}px`,"max-height:100%","background:#fff","border-radius:16px","overflow:hidden","position:relative","box-shadow:0 25px 50px -12px rgba(0,0,0,0.35)","display:flex","flex-direction:column"].join(";"),e){const o=document.createElement("button");o.setAttribute("aria-label","Close"),o.textContent="×",o.style.cssText=["position:absolute","top:8px","right:12px","z-index:2","width:28px","height:28px","border-radius:9999px","border:none","background:rgba(15,23,42,0.06)","color:#475569","font-size:18px","line-height:1","cursor:pointer"].join(";"),o.onclick=()=>this.close(),c.appendChild(o),s.addEventListener("mousedown",l=>{l.target===s&&this.close()})}const u=document.createElement("iframe");u.src=i,u.title="Consent manager",u.setAttribute("allow","clipboard-write"),u.style.cssText=["flex:1","width:100%","height:100%","border:0","background:#fff"].join(";"),c.appendChild(u),s.appendChild(c),document.body.appendChild(s),document.documentElement.style.overflow="hidden",this.overlay=s,this.iframe=u}teardown(){var e;(e=this.overlay)!=null&&e.parentNode&&this.overlay.parentNode.removeChild(this.overlay),this.overlay=null,this.iframe=null,this.session=null,this.listening&&(window.removeEventListener("message",this.messageHandler),window.removeEventListener("keydown",this.escHandler),this.listening=!1);try{document.documentElement.style.overflow=""}catch{}}emit(e){this.callbacks.forEach(t=>{try{t(e)}catch{}})}}const a=new U;function q(n){const{onEvent:e,closeOnUnmount:t=!0}=n??{};d.useEffect(()=>{if(e)return a.onCallback(e)},[e]),d.useEffect(()=>{if(t)return()=>{a.isOpen()&&a.close()}},[t]);const r=d.useCallback(o=>{const{requestId:l,token:f,...m}=o;l?a.openConsent(l,f,{...m,token:f}):a.openPreferences(f,m)},[]),i=d.useCallback((o,l,f)=>{a.openConsent(o,l,f)},[]),s=d.useCallback((o,l)=>{a.openPreferences(o,l)},[]),c=d.useCallback((o,l,f)=>{a.openDirectConsent(o,l,f)},[]),g=d.useCallback(o=>{a.close(o)},[]),p=d.useCallback(o=>a.onCallback(o),[]),u=d.useCallback(()=>a.isOpen(),[]);return{open:r,openConsent:i,openPreferences:s,openDirectConsent:c,close:g,onEvent:p,isOpen:u}}exports.useConsentWidget=q;
|
package/dist/react.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@truzyx/consent-widget",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Framework-agnostic consent + preferences widget (iframe overlay) with React hook. Reuses the gateway embed core, zero runtime dependencies for the vanilla entry.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|