UNPKG

@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.7 kB
"use strict";var e=require("react"),n=require("clsx");function t(e){return e&&e.__esModule?e:{default:e}}var o=t(e),s=t(n),r=Object.create,a=Object.defineProperty,l=Object.defineProperties,i=Object.getOwnPropertyDescriptor,c=Object.getOwnPropertyDescriptors,u=Object.getOwnPropertyNames,d=Object.getOwnPropertySymbols,f=Object.getPrototypeOf,m=Object.prototype.hasOwnProperty,h=Object.prototype.propertyIsEnumerable,p=(e,n,t)=>n in e?a(e,n,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[n]=t,b=(e,n)=>{for(var t in n||(n={}))m.call(n,t)&&p(e,t,n[t]);if(d)for(var t of d(n))h.call(n,t)&&p(e,t,n[t]);return e},g=(e,n)=>l(e,c(n)),v=(e,n)=>function(){return n||(0,e[u(e)[0]])((n={exports:{}}).exports,n),n.exports},C=v({"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}}),S=v({"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}}),y=v({"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)}}}),B=v({"node_modules/md5/md5.js"(e,n){var t,o,s,r,a;t=C(),o=S().utf8,s=y(),r=S().bin,(a=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 l=t.bytesToWords(e),i=8*e.length,c=1732584193,u=-271733879,d=-1732584194,f=271733878,m=0;m<l.length;m++)l[m]=16711935&(l[m]<<8|l[m]>>>24)|4278255360&(l[m]<<24|l[m]>>>8);l[i>>>5]|=128<<i%32,l[14+(i+64>>>9<<4)]=i;var h=a._ff,p=a._gg,b=a._hh,g=a._ii;for(m=0;m<l.length;m+=16){var v=c,C=u,S=d,y=f;c=h(c,u,d,f,l[m+0],7,-680876936),f=h(f,c,u,d,l[m+1],12,-389564586),d=h(d,f,c,u,l[m+2],17,606105819),u=h(u,d,f,c,l[m+3],22,-1044525330),c=h(c,u,d,f,l[m+4],7,-176418897),f=h(f,c,u,d,l[m+5],12,1200080426),d=h(d,f,c,u,l[m+6],17,-1473231341),u=h(u,d,f,c,l[m+7],22,-45705983),c=h(c,u,d,f,l[m+8],7,1770035416),f=h(f,c,u,d,l[m+9],12,-1958414417),d=h(d,f,c,u,l[m+10],17,-42063),u=h(u,d,f,c,l[m+11],22,-1990404162),c=h(c,u,d,f,l[m+12],7,1804603682),f=h(f,c,u,d,l[m+13],12,-40341101),d=h(d,f,c,u,l[m+14],17,-1502002290),c=p(c,u=h(u,d,f,c,l[m+15],22,1236535329),d,f,l[m+1],5,-165796510),f=p(f,c,u,d,l[m+6],9,-1069501632),d=p(d,f,c,u,l[m+11],14,643717713),u=p(u,d,f,c,l[m+0],20,-373897302),c=p(c,u,d,f,l[m+5],5,-701558691),f=p(f,c,u,d,l[m+10],9,38016083),d=p(d,f,c,u,l[m+15],14,-660478335),u=p(u,d,f,c,l[m+4],20,-405537848),c=p(c,u,d,f,l[m+9],5,568446438),f=p(f,c,u,d,l[m+14],9,-1019803690),d=p(d,f,c,u,l[m+3],14,-187363961),u=p(u,d,f,c,l[m+8],20,1163531501),c=p(c,u,d,f,l[m+13],5,-1444681467),f=p(f,c,u,d,l[m+2],9,-51403784),d=p(d,f,c,u,l[m+7],14,1735328473),c=b(c,u=p(u,d,f,c,l[m+12],20,-1926607734),d,f,l[m+5],4,-378558),f=b(f,c,u,d,l[m+8],11,-2022574463),d=b(d,f,c,u,l[m+11],16,1839030562),u=b(u,d,f,c,l[m+14],23,-35309556),c=b(c,u,d,f,l[m+1],4,-1530992060),f=b(f,c,u,d,l[m+4],11,1272893353),d=b(d,f,c,u,l[m+7],16,-155497632),u=b(u,d,f,c,l[m+10],23,-1094730640),c=b(c,u,d,f,l[m+13],4,681279174),f=b(f,c,u,d,l[m+0],11,-358537222),d=b(d,f,c,u,l[m+3],16,-722521979),u=b(u,d,f,c,l[m+6],23,76029189),c=b(c,u,d,f,l[m+9],4,-640364487),f=b(f,c,u,d,l[m+12],11,-421815835),d=b(d,f,c,u,l[m+15],16,530742520),c=g(c,u=b(u,d,f,c,l[m+2],23,-995338651),d,f,l[m+0],6,-198630844),f=g(f,c,u,d,l[m+7],10,1126891415),d=g(d,f,c,u,l[m+14],15,-1416354905),u=g(u,d,f,c,l[m+5],21,-57434055),c=g(c,u,d,f,l[m+12],6,1700485571),f=g(f,c,u,d,l[m+3],10,-1894986606),d=g(d,f,c,u,l[m+10],15,-1051523),u=g(u,d,f,c,l[m+1],21,-2054922799),c=g(c,u,d,f,l[m+8],6,1873313359),f=g(f,c,u,d,l[m+15],10,-30611744),d=g(d,f,c,u,l[m+6],15,-1560198380),u=g(u,d,f,c,l[m+13],21,1309151649),c=g(c,u,d,f,l[m+4],6,-145523070),f=g(f,c,u,d,l[m+11],10,-1120210379),d=g(d,f,c,u,l[m+2],15,718787259),u=g(u,d,f,c,l[m+9],21,-343485551),c=c+v>>>0,u=u+C>>>0,d=d+S>>>0,f=f+y>>>0}return t.endian([c,u,d,f])})._ff=function(e,n,t,o,s,r,a){var l=e+(n&t|~n&o)+(s>>>0)+a;return(l<<r|l>>>32-r)+n},a._gg=function(e,n,t,o,s,r,a){var l=e+(n&o|t&~o)+(s>>>0)+a;return(l<<r|l>>>32-r)+n},a._hh=function(e,n,t,o,s,r,a){var l=e+(n^t^o)+(s>>>0)+a;return(l<<r|l>>>32-r)+n},a._ii=function(e,n,t,o,s,r,a){var l=e+(t^(n|~o))+(s>>>0)+a;return(l<<r|l>>>32-r)+n},a._blocksize=16,a._digestsize=16,n.exports=function(e,n){if(null==e)throw new Error("Illegal argument "+e);var o=t.wordsToBytes(a(e,n));return n&&n.asBytes?o:n&&n.asString?r.bytesToString(o):t.bytesToHex(o)}}}),I=e.createContext({consent:[],isBannerVisible:!0,isDetailsVisible:!1,hasConsent:()=>!0,toggleConsentBanner:()=>({}),toggleConsentModal:()=>({}),setConsent:()=>({}),options:{services:[]}}),E=I.Provider,k=I;var w=function(){const{consent:n,isBannerVisible:t,isDetailsVisible:o,hasConsent:s,toggleConsentBanner:r,toggleConsentModal:a,setConsent:l,options:i}=e.useContext(k);return{consent:n,isBannerVisible:t,isDetailsVisible:o,hasConsent:s,toggleConsentBanner:r,toggleConsentModal:a,setConsent:l,options:i}};var N=function(){const{setConsent:n,options:{services:t}}=w(),o=e.useCallback((e=>{n(e||t.map((({id:e})=>e)))}),[t,n]);return{onDecline:e.useCallback((()=>{n(t.filter((({mandatory:e})=>e)).map((({id:e})=>e)))}),[t,n]),onApprove:o}};var V=function(){const{consent:n}=w(),[t,o]=e.useState(n);return{selectedServices:t,handleSelectedServiceChange:e.useCallback(((e,n)=>{o((t=>n?[...t,e]:t.filter((n=>n!==e))))}),[])}},U="SUI-ConsentItem-module-item-leSvO",O="SUI-ConsentItem-module-title-jjIjN",j="SUI-ConsentItem-module-label-UP2Jl",M="SUI-ConsentItem-module-mandatory-ro8jv",T="SUI-ConsentItem-module-description-YRQH4",x="SUI-Switch-module-switch-PKzsW",D="SUI-Switch-module-inputRoot-k5hXr",A="SUI-Switch-module-switchCheckbox-q6QVK",_="SUI-Switch-module-switchLabel-mNtkk",P="SUI-Switch-module-switchSwitch-w8MgH",J="SUI-Switch-module-switchInnerChecked-9Z9On",R="SUI-Switch-module-switchSwitchChecked-OepRX",H=e.forwardRef((({name:e,onChange:n,checked:t=!1},r)=>o.default.createElement("label",{htmlFor:`switch_${e}`,className:x},o.default.createElement("div",{className:D},o.default.createElement("input",{ref:r,className:A,name:e,type:"checkbox",checked:t,onChange:n,id:`switch_${e}`}),o.default.createElement("div",{className:s.default(_,{[J]:t})},o.default.createElement("span",{className:s.default(P,{[R]:t})}))))));H.displayName="Switch";var $=H;var L=function({onChange:n,id:t,name:s,description:r,mandatory:a=!1}){const{consent:l}=w(),[i,c]=e.useState(l.includes(t)),u=e.useCallback((e=>{const{name:t,type:o,checked:s}=e.target;c((e=>"checkbox"===o?!e:e)),n(t,s)}),[n]);return o.default.createElement("div",{className:U},o.default.createElement("div",{className:O},o.default.createElement("h3",{className:j},s),a?o.default.createElement("span",{className:M},"Always Active"):o.default.createElement($,{name:t,checked:i,onChange:u})),r&&o.default.createElement("p",{className:T},r))},q="SUI-ConsentModal-module-modal-O5-BH",z="SUI-ConsentModal-module-content-rihew",K="SUI-ConsentModal-module-header-Lhfpc",F="SUI-ConsentModal-module-main-nfmL3",W="SUI-ConsentModal-module-footer-nEdCA",X="SUI-ConsentModal-module-shadow-JfdKw",Z="SUI-ConsentModal-module-secondary-CTmkJ",Q="SUI-ConsentModal-module-primary-9Ogv2";var G=function({onToggle:n=()=>({}),modal:t}){var s,r,a,l;const{options:{services:i}}=w(),{onDecline:c,onApprove:u}=N(),{selectedServices:d,handleSelectedServiceChange:f}=V(),m=e.useCallback((()=>n()),[n]),h=e.useCallback((()=>{c(),n()}),[c,n]),p=e.useCallback((()=>{u(d),n()}),[u,n,d]),b=e.useCallback((()=>{u(),n()}),[u,n]);return o.default.createElement("div",{className:q,onClick:m},o.default.createElement("div",{className:z,onClick:e=>e.stopPropagation()},o.default.createElement("div",{className:K},o.default.createElement("h2",null,t.title),o.default.createElement("p",null,t.description)),o.default.createElement("div",{className:F},i.map((({id:e,name:n,description:t,mandatory:s})=>o.default.createElement(L,{id:e,name:n,description:t,mandatory:s,onChange:f,key:e})))),o.default.createElement("div",{className:W},o.default.createElement("span",{className:X}),!(null==(s=t.decline)?void 0:s.hidden)&&o.default.createElement("button",{className:Z,onClick:h},(null==(r=t.decline)?void 0:r.label)||"Reject All"),o.default.createElement("button",{className:Z,onClick:p},(null==(a=t.approve)?void 0:a.label)||"Save My Preferences"),o.default.createElement("button",{className:Q,onClick:b},(null==(l=t.approveAll)?void 0:l.label)||"Accept All"))))},Y="SUI-ConsentBanner-module-banner-C-l7X",ee="SUI-ConsentBanner-module-content-qKhUp",ne="SUI-ConsentBanner-module-message-B45vh",te="SUI-ConsentBanner-module-actions-RNSjn",oe="SUI-ConsentBanner-module-secondary-du8tj",se="SUI-ConsentBanner-module-primary-MnZfH";var re=function({children:e,settings:n,approve:t,decline:s}){const{isBannerVisible:r,isDetailsVisible:a,toggleConsentModal:l}=w(),{onDecline:i,onApprove:c}=N();return o.default.createElement(o.default.Fragment,null,r&&!a&&o.default.createElement("div",{className:Y},o.default.createElement("div",{className:ee},o.default.createElement("div",{className:ne},e),o.default.createElement("div",{className:te},!(null==n?void 0:n.hidden)&&o.default.createElement("button",{className:oe,onClick:l},(null==n?void 0:n.label)||"Customize"),!(null==s?void 0:s.hidden)&&o.default.createElement("button",{className:oe,onClick:i},(null==s?void 0:s.label)||"Decline"),o.default.createElement("button",{className:se,onClick:()=>c()},(null==t?void 0:t.label)||"Accept")))),a&&o.default.createElement(G,{onToggle:l,modal:(null==n?void 0:n.modal)||{title:"",description:""}}))};function ae(e,n){const t=document.createElement("script");t.id=n,t.innerHTML=e,document.body.appendChild(t)}function le(e,n){const t=document.createElement("script");t.id=n,t.src=e,t.async=!0,document.body.appendChild(t)}function ie(e){return!!e.id&&!!e.code}function ce(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&&le(o.src,s),ie(o)&&ae(o.code,s))}}(e,n)}))}function ue(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 de(e,n,t){const o=e.services.filter((e=>!n.includes(e.id))),s=e.services.filter((e=>n.includes(e.id)));ue(o),ce(s),function(e,n){localStorage.setItem("cookie-consent",JSON.stringify({consent:e,hash:n,updated:new Date}))}(n,t)}var fe,me,he=(fe=B(),me=null!=fe?r(f(fe)):{},((e,n,t,o)=>{if(n&&"object"==typeof n||"function"==typeof n)for(let s of u(n))m.call(e,s)||s===t||a(e,s,{get:()=>n[s],enumerable:!(o=i(n,s))||o.enumerable});return e})(fe&&fe.__esModule?me:a(me,"default",{value:fe,enumerable:!0}),fe));var pe=function(n){const[t,o]=e.useState({consent:[],isBannerVisible:!1,isDetailsVisible:!1,hash:(0,he.default)(JSON.stringify(n))});e.useEffect((()=>{if(!function(e){const n=localStorage.getItem("cookie-consent");if(!n)return!1;const{hash:t}=JSON.parse(n);return t===e}(t.hash)){const e=n.services.filter((e=>e.mandatory)).map((e=>e.id));return void o((n=>g(b({},n),{consent:e,isBannerVisible:!0,isDetailsVisible:!1})))}const{consent:e,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}}(t.hash);o((n=>g(b({},n),{consent:e,isBannerVisible:s,isDetailsVisible:r})));ce(n.services.filter((n=>e.includes(n.id))))}),[n.services,t.hash]);const s=e.useCallback((e=>{o((n=>g(b({},n),{consent:e,isBannerVisible:!1}))),de(n,e,t.hash)}),[n,t.hash]),r=e.useCallback((e=>t.consent.includes(e)),[t.consent]),a=e.useCallback((()=>{o((e=>g(b({},e),{isBannerVisible:!e.isBannerVisible})))}),[]),l=e.useCallback((()=>{o((e=>g(b({},e),{isDetailsVisible:!e.isDetailsVisible})))}),[]);return{consent:t.consent,hasConsent:r,isBannerVisible:t.isBannerVisible,isDetailsVisible:t.isDetailsVisible,toggleConsentBanner:a,toggleConsentModal:l,setConsent:s}};var be=function({options:e,children:n}){const{consent:t,hasConsent:s,isBannerVisible:r,toggleConsentBanner:a,isDetailsVisible:l,toggleConsentModal:i,setConsent:c}=pe(e);return o.default.createElement(E,{value:{consent:t,hasConsent:s,isBannerVisible:r,toggleConsentBanner:a,isDetailsVisible:l,toggleConsentModal:i,setConsent:c,options:e}},n)}; /*! Bundled license information: is-buffer/index.js: (*! * Determine if an object is a Buffer * * @author Feross Aboukhadijeh <https://feross.org> * @license MIT *) */exports.ConsentBanner=re,exports.ConsentProvider=be,exports.useConsent=w;