UNPKG

@c15t/react

Version:

Developer-first CMP for React: cookie banner, consent manager, preferences centre. GDPR ready with minimal setup and rich customization

2 lines 2.74 kB
'use client'; import*as e from"react/jsx-runtime";import*as t from"c15t";import*as n from"react";import*as o from"../context/consent-manager-context.js";import*as r from"../context/theme-context.js";import*as a from"../hooks/use-color-scheme.js";import*as s from"../version.js";let i=new Map,c=new Map;function l(){i.clear(),c.clear()}function g({children:l,options:g}){var u,d,m;let{mode:f,backendURL:p,store:C={},translations:M,react:b={}}=g,{initialGdprTypes:v,initialComplianceSettings:h}=C,{theme:x,disableAnimation:w=!1,scrollLock:S=!1,trapFocus:T=!0,colorScheme:j,noStyle:k=!1}=b,y=(0,n.useMemo)(()=>"undefined"!=typeof window&&!!(("c15t"===f||"offline"===f)&&(p?.includes("c15t.dev")||p?.includes("c15t.cloud")||window.location.hostname.includes("c15t.dev")||window.location.hostname.includes("c15t.cloud"))),[f,p]),G=(u=f||"c15t",d=p||"/api/c15t",m="endpointHandlers"in g?g.endpointHandlers:void 0,`${u}:${d??"default"}:${m?"custom":"none"}`),H=(0,n.useMemo)(()=>{let e,n=c.get(G);return n||(e="offline"===f?(0,t.configureConsentManager)({mode:"offline",store:C}):"custom"===f&&"endpointHandlers"in g?(0,t.configureConsentManager)({mode:"custom",endpointHandlers:g.endpointHandlers,store:C}):(0,t.configureConsentManager)({mode:"c15t",backendURL:p||"/api/c15t",store:C}),c.set(G,e),e)},[G,f,p,C,g]),L=(0,n.useMemo)(()=>{let e=i.get(G);if(e)return e;let n=(0,t.createConsentManagerStore)(H,{unstable_googleTagManager:g.unstable_googleTagManager,config:{pkg:"@c15t/react",version:s.version,mode:f||"Unknown"},ignoreGeoLocation:g.ignoreGeoLocation,initialGdprTypes:g.consentCategories,callbacks:g.callbacks,...C,isConsentDomain:y,initialTranslationConfig:M});return i.set(G,n),n},[G,H,f,g.callbacks,g.unstable_googleTagManager,g.ignoreGeoLocation,g.consentCategories,C,y,M]),E=(0,n.useRef)({gdprTypes:v,complianceSettings:h,consentCategories:g.consentCategories}),[P,$]=(0,n.useState)(()=>L?L.getState():{});(0,n.useEffect)(()=>{if(L)return L.subscribe($)},[L]),(0,n.useEffect)(()=>{if(!L)return;let{setGdprTypes:e,setComplianceSetting:t}=L.getState(),n=E.current;if((n.gdprTypes||n.consentCategories)&&e(n.gdprTypes||n.consentCategories||[]),n.complianceSettings)for(let[e,o]of Object.entries(n.complianceSettings))t(e,o);$(L.getState())},[L]);let _=(0,n.useMemo)(()=>({theme:x,noStyle:k,disableAnimation:w,scrollLock:S,trapFocus:T,colorScheme:j}),[x,k,w,S,T,j]);(0,a.useColorScheme)(j);let R=(0,n.useMemo)(()=>{if(!L)throw Error("Consent store must be initialized before creating context value");return{state:P,store:L,manager:H}},[P,L,H]);return(0,e.jsx)(o.ConsentStateContext.Provider,{value:R,children:(0,e.jsx)(r.GlobalThemeContext.Provider,{value:_,children:l})})}export{g as ConsentManagerProvider,l as clearConsentManagerCache};