@tantainnovative/ndpr-toolkit
Version:
Nigeria Data Protection Toolkit — enterprise-grade compliance components for the Nigeria Data Protection Act (NDPA) 2023
1 lines • 8 kB
JavaScript
import {b,a}from'./chunk-QNXLY5EJ.mjs';import {a as a$2}from'./chunk-SFGW37LE.mjs';import {c}from'./chunk-PHA3YMFO.mjs';import {a as a$1,b as b$1}from'./chunk-ZJYULEER.mjs';import {useState,useEffect,useRef,useCallback}from'react';import {createPortal}from'react-dom';import {jsx,jsxs,Fragment}from'react/jsx-runtime';var mn=({options:c$1,onSave:gn,title:A,description:R,acceptAllButtonText:P,rejectAllButtonText:D,customizeButtonText:x,saveButtonText:E,position:vn="bottom",variant:s="bar",zIndex:J=9999,version:C="1.0",show:_,manageStorage:z=true,storageKey:w="ndpr_consent",className:W="",buttonClassName:L="",primaryButtonClassName:Cn="",secondaryButtonClassName:_n="",classNames:n,unstyled:o,onAnalytics:k,showCookieScan:$=false,declaredCookies:hn,cookieScanOptions:sn,cookieScanTitle:j})=>{var T,N,nn,en,tn,on,rn,dn,cn;let p=c(),kn=(T=A!=null?A:p.consent.title)!=null?T:"We Value Your Privacy",Sn=(N=R!=null?R:p.consent.description)!=null?N:"We use cookies and similar technologies to provide our services and enhance your experience. Your consent is collected in accordance with NDPA Sections 25-26.",yn=(nn=P!=null?P:p.consent.acceptAll)!=null?nn:"Accept All",Bn=(en=D!=null?D:p.consent.rejectAll)!=null?en:"Reject All",An=(tn=x!=null?x:p.consent.customize)!=null?tn:"Customize",Rn=(on=E!=null?E:p.consent.savePreferences)!=null?on:"Save Preferences",Pn=(rn=p.consent.selectAll)!=null?rn:"Select All",Dn=(dn=p.consent.deselectAll)!=null?dn:"Deselect All",xn=j!=null?j:"Cookies on this page",[b$2,l]=useState(false),[u,I]=useState(false),{result:h,rescan:Y}=b(hn,sn);useEffect(()=>{$&&u&&Y();},[$,u,Y]);let[S,y]=useState({}),[K,En]=useState(false),zn=a({active:b$2}),O=useRef(null),q=useRef(false);useEffect(()=>{En(true);},[]);let f=useCallback((e,t)=>{k==null||k(a$1({action:e,timestamp:Date.now(),version:C},t!==void 0?{categories:t}:{}));},[k,C]);useEffect(()=>{let e={};if(c$1.forEach(t=>{e[t.id]=t.defaultValue||false;}),y(e),_===void 0)if(!z)l(true);else {let t=localStorage.getItem(w);if(t)try{JSON.parse(t).version!==C?l(!0):l(!1);}catch(M){l(true);}else l(true);}else l(_);},[c$1,w,C,z]),useEffect(()=>{_!==void 0&&l(_);},[_]);let wn=()=>{let e={};c$1.forEach(t=>{e[t.id]=true;}),f("accepted_all",e),H(e);},Q=()=>{let e={};c$1.forEach(t=>{e[t.id]=t.required||false;}),f("rejected_all",e),H(e);},Ln=(e,t)=>{y(M=>b$1(a$1({},M),{[e]:t}));},$n=()=>{let e={};c$1.forEach(t=>{e[t.id]=true;}),y(e);},jn=()=>{let e={};c$1.forEach(t=>{e[t.id]=t.required||false;}),y(e);},U=c$1.length>0&&c$1.every(e=>S[e.id]),In=()=>{f("customized",S),H(S);},H=e=>{let t={consents:e,timestamp:Date.now(),version:C,method:u?"customize":"banner",hasInteracted:true};z&&localStorage.setItem(w,JSON.stringify(t)),gn(t),l(false),I(false);};if(useEffect(()=>{if(!b$2)return;let e=t=>{t.key==="Escape"&&(f("dismissed"),Q());};return document.addEventListener("keydown",e),()=>document.removeEventListener("keydown",e)},[b$2,f,c$1,u]),useEffect(()=>{b$2&&!q.current&&(q.current=true,f("shown")),b$2||(q.current=false);},[b$2,f]),useEffect(()=>{if(u&&O.current){let e=O.current;e.style.maxHeight="0px",e.style.opacity="0",e.offsetHeight,e.style.maxHeight=`${e.scrollHeight}px`,e.style.opacity="1";}},[u]),!b$2)return null;let g=s==="modal"?"center":vn,X=g==="inline",F=g==="center",v=["ndpr-consent-banner"];s!=="bar"&&v.push(`ndpr-consent-banner--${s}`),g==="top"?v.push("ndpr-consent-banner--top"):g==="bottom"?v.push("ndpr-consent-banner--bottom"):g==="inline"&&v.push("ndpr-consent-banner--inline"),W&&v.push(W);let On=v.join(" "),Z=`ndpr-consent-banner__button ndpr-consent-banner__button--primary ${L} ${Cn}`.trim(),qn=`ndpr-consent-banner__button ndpr-consent-banner__button--secondary ${L} ${_n}`.trim(),Hn=`ndpr-consent-banner__button ndpr-consent-banner__button--ghost ${L}`.trim(),Fn=(n==null?void 0:n.primaryButton)||(n==null?void 0:n.acceptButton)||Z,m=(n==null?void 0:n.secondaryButton)||(n==null?void 0:n.rejectButton)||qn,Gn=(n==null?void 0:n.customizeButton)||Hn,Mn=(n==null?void 0:n.saveButton)||Z,G=jsx("div",{ref:zn,tabIndex:-1,"data-ndpr-component":"consent-banner","data-ndpr-variant":s,"data-ndpr-position":g,className:a$2(On,n==null?void 0:n.root,o),style:!X&&!F?{zIndex:J}:void 0,role:"dialog","aria-modal":F||void 0,"aria-labelledby":"consent-banner-title","aria-describedby":"consent-banner-description",children:jsxs("div",{className:a$2("ndpr-consent-banner__container",n==null?void 0:n.container,o),children:[jsx("h2",{id:"consent-banner-title",className:a$2("ndpr-consent-banner__title",n==null?void 0:n.title,o),children:kn}),jsx("p",{id:"consent-banner-description",className:a$2("ndpr-consent-banner__description",n==null?void 0:n.description,o),children:Sn}),u&&jsxs("div",{ref:O,className:a$2("ndpr-consent-banner__customize-panel",n==null?void 0:n.customizePanel,o),children:[jsx("div",{className:o?"":"ndpr-consent-banner__select-all-row",children:jsx("button",{type:"button",onClick:U?jn:$n,className:a$2("ndpr-consent-banner__select-all-button",n==null?void 0:n.selectAllButton,o),children:U?Dn:Pn})}),jsx("div",{className:a$2("ndpr-consent-banner__options-list",n==null?void 0:n.optionsList,o),children:c$1.map(e=>jsxs("div",{className:a$2("ndpr-consent-banner__option",n==null?void 0:n.optionItem,o),children:[jsx("input",{id:`consent-${e.id}`,type:"checkbox",checked:S[e.id]||false,onChange:t=>Ln(e.id,t.target.checked),disabled:e.required,className:a$2("ndpr-consent-banner__option-checkbox",n==null?void 0:n.optionCheckbox,o)}),jsxs("div",{className:o?"":"ndpr-consent-banner__option-text",children:[jsxs("label",{htmlFor:`consent-${e.id}`,className:a$2("ndpr-consent-banner__option-label",n==null?void 0:n.optionLabel,o),children:[e.label,e.required&&jsx("span",{className:o?"":"ndpr-consent-banner__required-marker",children:" *"})]}),jsx("p",{className:a$2("ndpr-consent-banner__option-description",n==null?void 0:n.optionDescription,o),children:e.description})]})]},e.id))}),$&&h&&jsxs("div",{"data-ndpr-section":"cookie-scan",className:a$2("ndpr-alert ndpr-alert--info mb-2",n==null?void 0:n.cookieScanPanel,o),children:[jsxs("div",{className:"flex items-center justify-between mb-1",children:[jsx("h3",{className:"text-sm font-bold ndpr-text-info",children:xn}),jsx("span",{className:"text-sm font-semibold ndpr-text-info",children:h.total})]}),h.undeclared.length===0?jsx("p",{className:"text-sm ndpr-text-info",children:"All cookies on this page are declared."}):jsxs(Fragment,{children:[jsxs("p",{className:"text-sm ndpr-text-warning",children:[h.undeclared.length," undeclared cookie(s) detected \u2014 review your cookie notice (NDPA S.25-26)."]}),jsx("ul",{className:"mt-1 list-disc pl-5",children:h.undeclared.map(e=>jsxs("li",{className:"text-xs ndpr-text-muted",children:[jsx("code",{children:e.name})," ",jsx("span",{className:"opacity-70",children:e.provider?`\u2014 ${e.provider} (${e.category})`:"\u2014 unrecognized"})]},e.name))})]})]}),jsxs("div",{className:a$2("ndpr-consent-banner__buttons",n==null?void 0:n.buttonGroup,o),children:[jsx("button",{onClick:In,className:a$2(Mn,n==null?void 0:n.saveButton,o),children:Rn}),jsx("button",{onClick:()=>I(false),className:a$2(m,n==null?void 0:n.rejectButton,o),children:(cn=p.common.back)!=null?cn:"Back"})]})]}),!u&&jsxs("div",{className:a$2("ndpr-consent-banner__buttons",n==null?void 0:n.buttonGroup,o),children:[jsx("button",{onClick:wn,className:a$2(Fn,n==null?void 0:n.acceptButton,o),children:yn}),jsx("button",{onClick:Q,className:a$2(m,n==null?void 0:n.rejectButton,o),children:Bn}),jsx("button",{onClick:()=>I(true),className:a$2(Gn,n==null?void 0:n.customizeButton,o),children:An})]}),jsx("div",{className:a$2("ndpr-consent-banner__footer-text",void 0,o),children:'By clicking "Accept All", you agree to the use of ALL cookies. Visit our Cookie Policy to learn more.'})]})});if(X)return G;if(F){let e=jsx("div",{className:o?"":"ndpr-consent-banner__overlay",style:{zIndex:J},children:G});return K?createPortal(e,document.body):null}return K?createPortal(G,document.body):null};export{mn as a};