@start-base/react-consent-manager
Version:
JavaScript library designed to simplify the management of user consents in web applications developed with React. This package is particularly useful for complying with data protection regulations such as the General Data Protection Regulation (GDPR) in t
11 lines (10 loc) • 14.2 kB
JavaScript
import e,{createContext as n,forwardRef as t,useContext as o,useCallback as s,useState as r,useEffect as i}from"react";import a from"clsx";var l=Object.create,c=Object.defineProperty,u=Object.defineProperties,d=Object.getOwnPropertyDescriptor,m=Object.getOwnPropertyDescriptors,f=Object.getOwnPropertyNames,h=Object.getOwnPropertySymbols,p=Object.getPrototypeOf,g=Object.prototype.hasOwnProperty,v=Object.prototype.propertyIsEnumerable,b=(e,n,t)=>n in e?c(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t,C=(e,n)=>{for(var t in n||(n={}))g.call(n,t)&&b(e,t,n[t]);if(h)for(var t of h(n))v.call(n,t)&&b(e,t,n[t]);return e},y=(e,n)=>u(e,m(n)),S=(e,n)=>function(){return n||(0,e[f(e)[0]])((n={exports:{}}).exports,n),n.exports},B=S({"node_modules/crypt/crypt.js"(e,n){var t,o;t="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",o={rotl:function(e,n){return e<<n|e>>>32-n},rotr:function(e,n){return e<<32-n|e>>>n},endian:function(e){if(e.constructor==Number)return 16711935&o.rotl(e,8)|4278255360&o.rotl(e,24);for(var n=0;n<e.length;n++)e[n]=o.endian(e[n]);return e},randomBytes:function(e){for(var n=[];e>0;e--)n.push(Math.floor(256*Math.random()));return n},bytesToWords:function(e){for(var n=[],t=0,o=0;t<e.length;t++,o+=8)n[o>>>5]|=e[t]<<24-o%32;return n},wordsToBytes:function(e){for(var n=[],t=0;t<32*e.length;t+=8)n.push(e[t>>>5]>>>24-t%32&255);return n},bytesToHex:function(e){for(var n=[],t=0;t<e.length;t++)n.push((e[t]>>>4).toString(16)),n.push((15&e[t]).toString(16));return n.join("")},hexToBytes:function(e){for(var n=[],t=0;t<e.length;t+=2)n.push(parseInt(e.substr(t,2),16));return n},bytesToBase64:function(e){for(var n=[],o=0;o<e.length;o+=3)for(var s=e[o]<<16|e[o+1]<<8|e[o+2],r=0;r<4;r++)8*o+6*r<=8*e.length?n.push(t.charAt(s>>>6*(3-r)&63)):n.push("=");return n.join("")},base64ToBytes:function(e){e=e.replace(/[^A-Z0-9+\/]/gi,"");for(var n=[],o=0,s=0;o<e.length;s=++o%4)0!=s&&n.push((t.indexOf(e.charAt(o-1))&Math.pow(2,-2*s+8)-1)<<2*s|t.indexOf(e.charAt(o))>>>6-2*s);return n}},n.exports=o}}),I=S({"node_modules/charenc/charenc.js"(e,n){var t={utf8:{stringToBytes:function(e){return t.bin.stringToBytes(unescape(encodeURIComponent(e)))},bytesToString:function(e){return decodeURIComponent(escape(t.bin.bytesToString(e)))}},bin:{stringToBytes:function(e){for(var n=[],t=0;t<e.length;t++)n.push(255&e.charCodeAt(t));return n},bytesToString:function(e){for(var n=[],t=0;t<e.length;t++)n.push(String.fromCharCode(e[t]));return n.join("")}}};n.exports=t}}),E=S({"node_modules/is-buffer/index.js"(e,n){function t(e){return!!e.constructor&&"function"==typeof e.constructor.isBuffer&&e.constructor.isBuffer(e)}n.exports=function(e){return null!=e&&(t(e)||function(e){return"function"==typeof e.readFloatLE&&"function"==typeof e.slice&&t(e.slice(0,0))}(e)||!!e._isBuffer)}}}),N=S({"node_modules/md5/md5.js"(e,n){var t,o,s,r,i;t=B(),o=I().utf8,s=E(),r=I().bin,(i=function(e,n){e.constructor==String?e=n&&"binary"===n.encoding?r.stringToBytes(e):o.stringToBytes(e):s(e)?e=Array.prototype.slice.call(e,0):Array.isArray(e)||e.constructor===Uint8Array||(e=e.toString());for(var a=t.bytesToWords(e),l=8*e.length,c=1732584193,u=-271733879,d=-1732584194,m=271733878,f=0;f<a.length;f++)a[f]=16711935&(a[f]<<8|a[f]>>>24)|4278255360&(a[f]<<24|a[f]>>>8);a[l>>>5]|=128<<l%32,a[14+(l+64>>>9<<4)]=l;var h=i._ff,p=i._gg,g=i._hh,v=i._ii;for(f=0;f<a.length;f+=16){var b=c,C=u,y=d,S=m;c=h(c,u,d,m,a[f+0],7,-680876936),m=h(m,c,u,d,a[f+1],12,-389564586),d=h(d,m,c,u,a[f+2],17,606105819),u=h(u,d,m,c,a[f+3],22,-1044525330),c=h(c,u,d,m,a[f+4],7,-176418897),m=h(m,c,u,d,a[f+5],12,1200080426),d=h(d,m,c,u,a[f+6],17,-1473231341),u=h(u,d,m,c,a[f+7],22,-45705983),c=h(c,u,d,m,a[f+8],7,1770035416),m=h(m,c,u,d,a[f+9],12,-1958414417),d=h(d,m,c,u,a[f+10],17,-42063),u=h(u,d,m,c,a[f+11],22,-1990404162),c=h(c,u,d,m,a[f+12],7,1804603682),m=h(m,c,u,d,a[f+13],12,-40341101),d=h(d,m,c,u,a[f+14],17,-1502002290),c=p(c,u=h(u,d,m,c,a[f+15],22,1236535329),d,m,a[f+1],5,-165796510),m=p(m,c,u,d,a[f+6],9,-1069501632),d=p(d,m,c,u,a[f+11],14,643717713),u=p(u,d,m,c,a[f+0],20,-373897302),c=p(c,u,d,m,a[f+5],5,-701558691),m=p(m,c,u,d,a[f+10],9,38016083),d=p(d,m,c,u,a[f+15],14,-660478335),u=p(u,d,m,c,a[f+4],20,-405537848),c=p(c,u,d,m,a[f+9],5,568446438),m=p(m,c,u,d,a[f+14],9,-1019803690),d=p(d,m,c,u,a[f+3],14,-187363961),u=p(u,d,m,c,a[f+8],20,1163531501),c=p(c,u,d,m,a[f+13],5,-1444681467),m=p(m,c,u,d,a[f+2],9,-51403784),d=p(d,m,c,u,a[f+7],14,1735328473),c=g(c,u=p(u,d,m,c,a[f+12],20,-1926607734),d,m,a[f+5],4,-378558),m=g(m,c,u,d,a[f+8],11,-2022574463),d=g(d,m,c,u,a[f+11],16,1839030562),u=g(u,d,m,c,a[f+14],23,-35309556),c=g(c,u,d,m,a[f+1],4,-1530992060),m=g(m,c,u,d,a[f+4],11,1272893353),d=g(d,m,c,u,a[f+7],16,-155497632),u=g(u,d,m,c,a[f+10],23,-1094730640),c=g(c,u,d,m,a[f+13],4,681279174),m=g(m,c,u,d,a[f+0],11,-358537222),d=g(d,m,c,u,a[f+3],16,-722521979),u=g(u,d,m,c,a[f+6],23,76029189),c=g(c,u,d,m,a[f+9],4,-640364487),m=g(m,c,u,d,a[f+12],11,-421815835),d=g(d,m,c,u,a[f+15],16,530742520),c=v(c,u=g(u,d,m,c,a[f+2],23,-995338651),d,m,a[f+0],6,-198630844),m=v(m,c,u,d,a[f+7],10,1126891415),d=v(d,m,c,u,a[f+14],15,-1416354905),u=v(u,d,m,c,a[f+5],21,-57434055),c=v(c,u,d,m,a[f+12],6,1700485571),m=v(m,c,u,d,a[f+3],10,-1894986606),d=v(d,m,c,u,a[f+10],15,-1051523),u=v(u,d,m,c,a[f+1],21,-2054922799),c=v(c,u,d,m,a[f+8],6,1873313359),m=v(m,c,u,d,a[f+15],10,-30611744),d=v(d,m,c,u,a[f+6],15,-1560198380),u=v(u,d,m,c,a[f+13],21,1309151649),c=v(c,u,d,m,a[f+4],6,-145523070),m=v(m,c,u,d,a[f+11],10,-1120210379),d=v(d,m,c,u,a[f+2],15,718787259),u=v(u,d,m,c,a[f+9],21,-343485551),c=c+b>>>0,u=u+C>>>0,d=d+y>>>0,m=m+S>>>0}return t.endian([c,u,d,m])})._ff=function(e,n,t,o,s,r,i){var a=e+(n&t|~n&o)+(s>>>0)+i;return(a<<r|a>>>32-r)+n},i._gg=function(e,n,t,o,s,r,i){var a=e+(n&o|t&~o)+(s>>>0)+i;return(a<<r|a>>>32-r)+n},i._hh=function(e,n,t,o,s,r,i){var a=e+(n^t^o)+(s>>>0)+i;return(a<<r|a>>>32-r)+n},i._ii=function(e,n,t,o,s,r,i){var a=e+(t^(n|~o))+(s>>>0)+i;return(a<<r|a>>>32-r)+n},i._blocksize=16,i._digestsize=16,n.exports=function(e,n){if(null==e)throw new Error("Illegal argument "+e);var o=t.wordsToBytes(i(e,n));return n&&n.asBytes?o:n&&n.asString?r.bytesToString(o):t.bytesToHex(o)}}}),w=n({consent:[],isBannerVisible:!0,isDetailsVisible:!1,hasConsent:()=>!0,toggleConsentBanner:()=>({}),toggleConsentModal:()=>({}),setConsent:()=>({}),options:{services:[]}}),V=w.Provider,U=w;var k=function(){const{consent:e,isBannerVisible:n,isDetailsVisible:t,hasConsent:s,toggleConsentBanner:r,toggleConsentModal:i,setConsent:a,options:l}=o(U);return{consent:e,isBannerVisible:n,isDetailsVisible:t,hasConsent:s,toggleConsentBanner:r,toggleConsentModal:i,setConsent:a,options:l}};var O=function(){const{setConsent:e,options:{services:n}}=k(),t=s((t=>{e(t||n.map((({id:e})=>e)))}),[n,e]);return{onDecline:s((()=>{e(n.filter((({mandatory:e})=>e)).map((({id:e})=>e)))}),[n,e]),onApprove:t}};var j=function(){const{consent:e}=k(),[n,t]=r(e);return{selectedServices:n,handleSelectedServiceChange:s(((e,n)=>{t((t=>n?[...t,e]:t.filter((n=>n!==e))))}),[])}},M="SUI-ConsentItem-module-item-leSvO",T="SUI-ConsentItem-module-title-jjIjN",D="SUI-ConsentItem-module-label-UP2Jl",A="SUI-ConsentItem-module-mandatory-ro8jv",x="SUI-ConsentItem-module-description-YRQH4",_="SUI-Switch-module-switch-PKzsW",P="SUI-Switch-module-inputRoot-k5hXr",J="SUI-Switch-module-switchCheckbox-q6QVK",H="SUI-Switch-module-switchLabel-mNtkk",R="SUI-Switch-module-switchSwitch-w8MgH",$="SUI-Switch-module-switchInnerChecked-9Z9On",L="SUI-Switch-module-switchSwitchChecked-OepRX",z=t((({name:n,onChange:t,checked:o=!1},s)=>e.createElement("label",{htmlFor:`switch_${n}`,className:_},e.createElement("div",{className:P},e.createElement("input",{ref:s,className:J,name:n,type:"checkbox",checked:o,onChange:t,id:`switch_${n}`}),e.createElement("div",{className:a(H,{[$]:o})},e.createElement("span",{className:a(R,{[L]:o})}))))));z.displayName="Switch";var K=z;var F=function({onChange:n,id:t,name:o,description:i,mandatory:a=!1}){const{consent:l}=k(),[c,u]=r(l.includes(t)),d=s((e=>{const{name:t,type:o,checked:s}=e.target;u((e=>"checkbox"===o?!e:e)),n(t,s)}),[n]);return e.createElement("div",{className:M},e.createElement("div",{className:T},e.createElement("h3",{className:D},o),a?e.createElement("span",{className:A},"Always Active"):e.createElement(K,{name:t,checked:c,onChange:d})),i&&e.createElement("p",{className:x},i))},W="SUI-ConsentModal-module-modal-O5-BH",X="SUI-ConsentModal-module-content-rihew",Z="SUI-ConsentModal-module-header-Lhfpc",q="SUI-ConsentModal-module-main-nfmL3",Q="SUI-ConsentModal-module-footer-nEdCA",G="SUI-ConsentModal-module-shadow-JfdKw",Y="SUI-ConsentModal-module-secondary-CTmkJ",ee="SUI-ConsentModal-module-primary-9Ogv2";var ne=function({onToggle:n=()=>({}),modal:t}){var o,r,i,a;const{options:{services:l}}=k(),{onDecline:c,onApprove:u}=O(),{selectedServices:d,handleSelectedServiceChange:m}=j(),f=s((()=>n()),[n]),h=s((()=>{c(),n()}),[c,n]),p=s((()=>{u(d),n()}),[u,n,d]),g=s((()=>{u(),n()}),[u,n]);return e.createElement("div",{className:W,onClick:f},e.createElement("div",{className:X,onClick:e=>e.stopPropagation()},e.createElement("div",{className:Z},e.createElement("h2",null,t.title),e.createElement("p",null,t.description)),e.createElement("div",{className:q},l.map((({id:n,name:t,description:o,mandatory:s})=>e.createElement(F,{id:n,name:t,description:o,mandatory:s,onChange:m,key:n})))),e.createElement("div",{className:Q},e.createElement("span",{className:G}),!(null==(o=t.decline)?void 0:o.hidden)&&e.createElement("button",{className:Y,onClick:h},(null==(r=t.decline)?void 0:r.label)||"Reject All"),e.createElement("button",{className:Y,onClick:p},(null==(i=t.approve)?void 0:i.label)||"Save My Preferences"),e.createElement("button",{className:ee,onClick:g},(null==(a=t.approveAll)?void 0:a.label)||"Accept All"))))},te="SUI-ConsentBanner-module-banner-C-l7X",oe="SUI-ConsentBanner-module-content-qKhUp",se="SUI-ConsentBanner-module-message-B45vh",re="SUI-ConsentBanner-module-actions-RNSjn",ie="SUI-ConsentBanner-module-secondary-du8tj",ae="SUI-ConsentBanner-module-primary-MnZfH";var le=function({children:n,settings:t,approve:o,decline:s}){const{isBannerVisible:r,isDetailsVisible:i,toggleConsentModal:a}=k(),{onDecline:l,onApprove:c}=O();return e.createElement(e.Fragment,null,r&&!i&&e.createElement("div",{className:te},e.createElement("div",{className:oe},e.createElement("div",{className:se},n),e.createElement("div",{className:re},!(null==t?void 0:t.hidden)&&e.createElement("button",{className:ie,onClick:a},(null==t?void 0:t.label)||"Customize"),!(null==s?void 0:s.hidden)&&e.createElement("button",{className:ie,onClick:l},(null==s?void 0:s.label)||"Decline"),e.createElement("button",{className:ae,onClick:()=>c()},(null==o?void 0:o.label)||"Accept")))),i&&e.createElement(ne,{onToggle:a,modal:(null==t?void 0:t.modal)||{title:"",description:""}}))};function ce(e,n){const t=document.createElement("script");t.id=n,t.innerHTML=e,document.body.appendChild(t)}function ue(e,n){const t=document.createElement("script");t.id=n,t.src=e,t.async=!0,document.body.appendChild(t)}function de(e){return!!e.id&&!!e.code}function me(e){e.forEach((({id:e,scripts:n})=>{!function(e,n){var t;if(n)for(const o of n){const{id:n}=o,s=`${e}-${n}`;document.getElementById(s)||((t=o).id&&t.src&&ue(o.src,s),de(o)&&ce(o.code,s))}}(e,n)}))}function fe(e){e.forEach((({id:e,scripts:n=[],cookies:t=[],localStorage:o=[],sessionStorage:s=[]})=>{!function(e,n){if(n)for(const{id:t}of n){const n=document.getElementById(`${e}-${t}`);n&&n.remove()}}(e,n),function(e){if(e)for(const n of e)document.cookie=`${n.pattern}=;expires=Thu, 01 Jan 1970 00:00:00 GMT`}(t),function(e){if(e)for(const n of e)localStorage.removeItem(n)}(o),function(e){if(e)for(const n of e)sessionStorage.removeItem(n)}(s)}))}function he(e,n,t){const o=e.services.filter((e=>!n.includes(e.id))),s=e.services.filter((e=>n.includes(e.id)));fe(o),me(s),function(e,n){localStorage.setItem("cookie-consent",JSON.stringify({consent:e,hash:n,updated:new Date}))}(n,t)}var pe,ge,ve=(pe=N(),ge=null!=pe?l(p(pe)):{},((e,n,t,o)=>{if(n&&"object"==typeof n||"function"==typeof n)for(let s of f(n))g.call(e,s)||s===t||c(e,s,{get:()=>n[s],enumerable:!(o=d(n,s))||o.enumerable});return e})(pe&&pe.__esModule?ge:c(ge,"default",{value:pe,enumerable:!0}),pe));var be=function(e){const[n,t]=r({consent:[],isBannerVisible:!1,isDetailsVisible:!1,hash:(0,ve.default)(JSON.stringify(e))});i((()=>{if(!function(e){const n=localStorage.getItem("cookie-consent");if(!n)return!1;const{hash:t}=JSON.parse(n);return t===e}(n.hash)){const n=e.services.filter((e=>e.mandatory)).map((e=>e.id));return void t((e=>y(C({},e),{consent:n,isBannerVisible:!0,isDetailsVisible:!1})))}const{consent:o,isBannerVisible:s,isDetailsVisible:r}=function(e){const n=localStorage.getItem("cookie-consent");if(!n)return{consent:[],isBannerVisible:!0,isDetailsVisible:!1};const{consent:t,hash:o}=JSON.parse(n),s=o!==e;return{consent:t&&t.length>0?t:[],isBannerVisible:s,isDetailsVisible:!1}}(n.hash);t((e=>y(C({},e),{consent:o,isBannerVisible:s,isDetailsVisible:r})));me(e.services.filter((e=>o.includes(e.id))))}),[e.services,n.hash]);const o=s((o=>{t((e=>y(C({},e),{consent:o,isBannerVisible:!1}))),he(e,o,n.hash)}),[e,n.hash]),a=s((e=>n.consent.includes(e)),[n.consent]),l=s((()=>{t((e=>y(C({},e),{isBannerVisible:!e.isBannerVisible})))}),[]),c=s((()=>{t((e=>y(C({},e),{isDetailsVisible:!e.isDetailsVisible})))}),[]);return{consent:n.consent,hasConsent:a,isBannerVisible:n.isBannerVisible,isDetailsVisible:n.isDetailsVisible,toggleConsentBanner:l,toggleConsentModal:c,setConsent:o}};var Ce=function({options:n,children:t}){const{consent:o,hasConsent:s,isBannerVisible:r,toggleConsentBanner:i,isDetailsVisible:a,toggleConsentModal:l,setConsent:c}=be(n);return e.createElement(V,{value:{consent:o,hasConsent:s,isBannerVisible:r,toggleConsentBanner:i,isDetailsVisible:a,toggleConsentModal:l,setConsent:c,options:n}},t)};
/*! Bundled license information:
is-buffer/index.js:
(*!
* Determine if an object is a Buffer
*
* @author Feross Aboukhadijeh <https://feross.org>
* @license MIT
*)
*/export{le as ConsentBanner,Ce as ConsentProvider,k as useConsent};