@c15t/nextjs 2.0.0-rc.1 → 2.0.0-rc.12
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 +10 -3
- package/client/components/consent-dialog-link.js +3 -0
- package/dist/headless.cjs +1 -1
- package/dist/iab/styles.css +12 -0
- package/dist/iab/styles.tw3.css +14 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/libs/browser-initial-data.cjs +1 -0
- package/dist/libs/browser-initial-data.js +1 -0
- package/dist/libs/initial-data.cjs +1 -1
- package/dist/libs/initial-data.js +1 -1
- package/dist/styles.css +10 -0
- package/dist/styles.tw3.css +13 -0
- package/dist/types.cjs +1 -1
- package/dist/version.cjs +1 -1
- package/dist/version.js +1 -1
- package/{dist → dist-types}/headless.d.ts +0 -1
- package/{dist → dist-types}/index.d.ts +3 -2
- package/dist-types/libs/browser-initial-data.d.ts +9 -0
- package/{dist → dist-types}/libs/initial-data.d.ts +7 -2
- package/dist-types/types.d.ts +38 -0
- package/dist-types/version.d.ts +1 -0
- package/docs/README.md +73 -0
- package/docs/building-headless-components.md +377 -0
- package/docs/callbacks.md +184 -0
- package/docs/components/consent-banner.md +269 -0
- package/docs/components/consent-dialog-link.md +59 -0
- package/docs/components/consent-dialog-trigger.md +103 -0
- package/docs/components/consent-dialog.md +177 -0
- package/docs/components/consent-manager-provider.md +425 -0
- package/docs/components/consent-widget.md +133 -0
- package/docs/components/dev-tools.md +63 -0
- package/docs/components/frame.md +73 -0
- package/docs/concepts/client-modes.md +175 -0
- package/docs/concepts/consent-categories.md +97 -0
- package/docs/concepts/consent-models.md +116 -0
- package/docs/concepts/cookie-management.md +122 -0
- package/docs/concepts/glossary.md +23 -0
- package/docs/concepts/initialization-flow.md +148 -0
- package/docs/concepts/policy-packs.md +229 -0
- package/docs/headless.md +190 -0
- package/docs/hooks/use-color-scheme.md +40 -0
- package/docs/hooks/use-consent-manager/checking-consent.md +94 -0
- package/docs/hooks/use-consent-manager/location-info.md +95 -0
- package/docs/hooks/use-consent-manager/overview.md +420 -0
- package/docs/hooks/use-consent-manager/setting-consent.md +92 -0
- package/docs/hooks/use-draggable.md +57 -0
- package/docs/hooks/use-focus-trap.md +41 -0
- package/docs/hooks/use-reduced-motion.md +35 -0
- package/docs/hooks/use-ssr-status.md +31 -0
- package/docs/hooks/use-text-direction.md +49 -0
- package/docs/hooks/use-translations.md +118 -0
- package/docs/iab/consent-banner.md +94 -0
- package/docs/iab/consent-dialog.md +134 -0
- package/docs/iab/overview.md +126 -0
- package/docs/iab/use-gvl-data.md +20 -0
- package/docs/iframe-blocking.md +107 -0
- package/docs/integrations/building-integrations.md +405 -0
- package/docs/integrations/databuddy.md +203 -0
- package/docs/integrations/google-tag-manager.md +153 -0
- package/docs/integrations/google-tag.md +122 -0
- package/docs/integrations/linkedin-insights.md +109 -0
- package/docs/integrations/meta-pixel.md +342 -0
- package/docs/integrations/microsoft-uet.md +112 -0
- package/docs/integrations/overview.md +105 -0
- package/docs/integrations/posthog.md +199 -0
- package/docs/integrations/tiktok-pixel.md +113 -0
- package/docs/integrations/x-pixel.md +143 -0
- package/docs/internationalization.md +197 -0
- package/docs/network-blocker.md +178 -0
- package/docs/optimization.md +234 -0
- package/docs/policy-packs.md +246 -0
- package/docs/quickstart.md +161 -0
- package/docs/script-loader.md +321 -0
- package/docs/server-side.md +176 -0
- package/docs/styling/classnames.md +92 -0
- package/docs/styling/color-scheme.md +82 -0
- package/docs/styling/css-variables.md +92 -0
- package/docs/styling/overview.md +456 -0
- package/docs/styling/slots.md +127 -0
- package/docs/styling/tailwind.md +113 -0
- package/docs/styling/tokens.md +216 -0
- package/docs/troubleshooting.md +146 -0
- package/iab/styles.css +1 -0
- package/package.json +36 -15
- package/readme.json +4 -0
- package/src/iab/styles.css +12 -0
- package/src/iab/styles.tw3.css +14 -0
- package/src/styles.css +10 -0
- package/src/styles.tw3.css +13 -0
- package/styles.css +1 -0
- package/dist/headless.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/libs/initial-data.d.ts.map +0 -1
- package/dist/types.d.ts +0 -16
- package/dist/types.d.ts.map +0 -1
- package/dist/version.d.ts +0 -2
- package/dist/version.d.ts.map +0 -1
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Initialization Flow
|
|
3
|
+
description: What happens from provider mount to first render — the full consent lifecycle.
|
|
4
|
+
---
|
|
5
|
+
When the consent provider mounts, it creates a cached consent runtime, reads any stored consent from the browser, fetches the resolved policy from the backend (or uses SSR/offline data), and decides whether to show the banner. This entire sequence completes before the first meaningful consent-aware render.
|
|
6
|
+
|
|
7
|
+
## Lifecycle Sequence
|
|
8
|
+
|
|
9
|
+
**Simplified**
|
|
10
|
+
|
|
11
|
+
1. **Provider mounts** — creates (or retrieves from cache) a consent runtime and store
|
|
12
|
+
2. **Check stored consent** — reads existing consent from cookies / localStorage; if found and the policy fingerprint hasn't changed, the banner stays hidden
|
|
13
|
+
3. **Fetch init data** — calls the backend `GET /init` (or uses SSR/offline data) for the resolved policy, location, and translations
|
|
14
|
+
4. **Apply resolved policy** — the backend resolves the policy from your [policy pack](/docs/frameworks/react/concepts/policy-packs) based on visitor geo (region → country → fallback → default). The response includes the consent model, categories, UI mode, and a material fingerprint. If no policy pack is configured, the legacy jurisdiction-to-model mapping is used instead.
|
|
15
|
+
5. **Decide banner visibility** — shows the banner only if no prior consent exists, the resolved policy requires it (`ui.mode` is `banner` or `dialog`), or the policy fingerprint changed since last consent
|
|
16
|
+
6. **Gating enforced** — scripts, iframes, and network requests tagged with a consent category are blocked until that category is granted
|
|
17
|
+
7. **User interacts** — choices are persisted to storage, synced to the backend (with `policySnapshotToken` if configured), and blocked scripts/iframes load immediately after consent is granted
|
|
18
|
+
|
|
19
|
+
**Sequence Diagram**
|
|
20
|
+
|
|
21
|
+
```mermaid
|
|
22
|
+
sequenceDiagram
|
|
23
|
+
participant Provider as ConsentManagerProvider
|
|
24
|
+
participant Store as Consent Store
|
|
25
|
+
participant Storage as localStorage / Cookie
|
|
26
|
+
participant API as c15t Backend
|
|
27
|
+
participant UI as Banner / Dialog
|
|
28
|
+
participant Scripts as Script Loader
|
|
29
|
+
|
|
30
|
+
Provider->>Store: getOrCreateConsentRuntime()
|
|
31
|
+
Store->>Storage: getStoredConsent()
|
|
32
|
+
alt Stored consent exists
|
|
33
|
+
Storage-->>Store: consentInfo + consents
|
|
34
|
+
Store->>Store: activeUI = 'none'
|
|
35
|
+
else No stored consent
|
|
36
|
+
Store->>Store: isLoadingConsentInfo = true
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
Store->>Store: initConsentManager()
|
|
40
|
+
Store->>Storage: Check pending consent sync
|
|
41
|
+
opt Pending sync from revocation reload
|
|
42
|
+
Store->>API: Deferred setConsent() (non-blocking)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
alt SSR data provided
|
|
46
|
+
Store->>Store: tryUseSSRData()
|
|
47
|
+
else No SSR data
|
|
48
|
+
Store->>API: GET /init
|
|
49
|
+
API-->>Store: policy, policyDecision, location, translations
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
Store->>Store: Apply resolved policy (model, categories, ui.mode)
|
|
53
|
+
Store->>Store: Check fingerprint change → re-prompt if needed
|
|
54
|
+
Store->>Store: Set activeUI, auto-grant if opt-out/none
|
|
55
|
+
|
|
56
|
+
alt User has no prior consent or policy changed
|
|
57
|
+
UI->>UI: Banner / Dialog appears (per policy ui.mode)
|
|
58
|
+
UI->>Store: saveConsents({ type })
|
|
59
|
+
Store->>Storage: Persist consent + subjectId + fingerprint
|
|
60
|
+
Store->>Scripts: updateScripts(), updateIframes(), updateNetwork()
|
|
61
|
+
Store->>API: POST /subjects + policySnapshotToken (non-blocking)
|
|
62
|
+
end
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## How It Works
|
|
66
|
+
|
|
67
|
+
**Mount** — When the provider renders, it creates (or retrieves from cache) a consent runtime and store. Any existing consent is read from localStorage/cookies immediately. If consent already exists and the policy fingerprint matches, the banner stays hidden and gating rules apply right away. See [Client Modes](/docs/frameworks/react/concepts/client-modes) for how the mode affects runtime creation.
|
|
68
|
+
|
|
69
|
+
**Init** — The store fetches the resolved policy, location, and translation data. In hosted mode this calls `GET /init` on your backend; in offline mode it resolves from `offlinePolicy.policyPacks` locally. If SSR data was passed to the provider, the network fetch is skipped entirely. See [Server-Side Utilities](/docs/frameworks/react/server-side) for SSR setup.
|
|
70
|
+
|
|
71
|
+
**Policy resolution** — When [policy packs](/docs/frameworks/react/concepts/policy-packs) are configured, the backend resolves the right policy for the visitor based on their geo-location (region → country → fallback → default). The resolved policy determines the consent model (`opt-in`, `opt-out`, `iab`, or `none`), which categories are in scope, and what UI to show. For `opt-out` and `none` models, all categories are auto-granted — unless the resolved policy has `consent.gpc: true` and the browser sends a Global Privacy Control signal, in which case `marketing` and `measurement` are denied. If no policy pack is configured, the legacy jurisdiction-to-model mapping is used instead. See [Consent Models](/docs/frameworks/react/concepts/consent-models) for details.
|
|
72
|
+
|
|
73
|
+
**Re-prompting** — If the resolved policy's material fingerprint differs from the fingerprint stored with the user's last consent, the banner is shown again. This happens automatically when you change consent-affecting fields (model, categories, scope mode, allowed actions). Presentation-only changes do not trigger re-prompts. See [Policy Packs — Re-Prompting](/docs/frameworks/react/concepts/policy-packs#re-prompting) for details.
|
|
74
|
+
|
|
75
|
+
**Save** — When the user interacts with the banner or dialog, their choices are persisted to localStorage/cookies and synced to the backend (along with the `policySnapshotToken` if snapshot signing is configured). Script, iframe, and network gating rules update immediately based on the new consent state. See the [Script Loader](/docs/frameworks/react/script-loader), [Iframe Blocking](/docs/frameworks/react/iframe-blocking), and [Network Blocker](/docs/frameworks/react/network-blocker) guides for gating details.
|
|
76
|
+
|
|
77
|
+
**Revocation** — If a user revokes a previously granted category, the page reloads by default to ensure a clean execution environment. The API sync is deferred to the fresh page load. See [Cookie Management](/docs/frameworks/react/concepts/cookie-management) for revocation and persistence details.
|
|
78
|
+
|
|
79
|
+
## When Does the Banner Show?
|
|
80
|
+
|
|
81
|
+
The banner appears when any of these conditions are true:
|
|
82
|
+
|
|
83
|
+
1. **No existing consent** — the user has never consented (or their consent was cleared), **and** the resolved policy requires a UI (`ui.mode` is `banner` or `dialog`, or the model is `opt-in` or `iab`)
|
|
84
|
+
2. **Policy changed** — the material policy fingerprint differs from the fingerprint stored with the user's last consent (re-prompting)
|
|
85
|
+
3. **Storage is accessible** — the browser allows localStorage (not blocked in private mode)
|
|
86
|
+
|
|
87
|
+
If the resolved model is `none` or `opt-out` (and `ui.mode` is `none`), consents are auto-granted and the banner never appears. See [Consent Models](/docs/frameworks/react/concepts/consent-models) and [Policy Packs](/docs/frameworks/react/concepts/policy-packs) for details.
|
|
88
|
+
|
|
89
|
+
## Debugging the Lifecycle
|
|
90
|
+
|
|
91
|
+
Use the DevTools panel and callbacks to inspect each step of the initialization flow. `onConsentSet` is the broad lifecycle signal; `onConsentChanged` and `subscribeToConsentChanges()` are the change-only signals for explicit post-init saves.
|
|
92
|
+
|
|
93
|
+
|Step|DevTools Panel|Callback|What to check|
|
|
94
|
+
|--|--|--|--|
|
|
95
|
+
|Init / SSR hydration|Location|`onBannerFetched`|jurisdiction, countryCode, regionCode populated?|
|
|
96
|
+
|Policy resolution|Policy|`onBannerFetched`|`policyId`, `matchedBy`, `fingerprint` in policyDecision|
|
|
97
|
+
|Model resolution|Location|`onBannerFetched`|`model` value matches the resolved policy|
|
|
98
|
+
|Banner visibility|Consents|—|`activeUI` in store state; does policy `ui.mode` require it?|
|
|
99
|
+
|Re-prompting|Policy|—|Fingerprint mismatch between stored and resolved policy?|
|
|
100
|
+
|Consent save|Consents + Events|`onConsentSet`|`preferences` object in callback payload|
|
|
101
|
+
|Change-only integrations|Events|`onConsentChanged` or `subscribeToConsentChanges()`|`allowedCategories`, `deniedCategories`, and previous values only when a real save changed preferences|
|
|
102
|
+
|Script loading|Scripts|`onConsentSet`|Script IDs and their load/blocked status|
|
|
103
|
+
|Reload on revocation|Events|`onBeforeConsentRevocationReload`|Fires before reload; check localStorage for `c15t:pending-consent-sync`|
|
|
104
|
+
|Deferred sync|Events|`onError` (if sync fails)|After reload, check Events panel for successful API call|
|
|
105
|
+
|
|
106
|
+
Configure callbacks in the provider to log lifecycle events, and add DevTools for a visual inspector:
|
|
107
|
+
|
|
108
|
+
```tsx
|
|
109
|
+
import { type ReactNode } from 'react';
|
|
110
|
+
import { ConsentManagerProvider, ConsentBanner, ConsentDialog } from '@c15t/nextjs';
|
|
111
|
+
import { DevTools } from '@c15t/dev-tools/react';
|
|
112
|
+
|
|
113
|
+
export default function ConsentManager({ children }: { children: ReactNode }) {
|
|
114
|
+
return (
|
|
115
|
+
<ConsentManagerProvider
|
|
116
|
+
options={{
|
|
117
|
+
mode: 'hosted',
|
|
118
|
+
backendURL: '/api/c15t',
|
|
119
|
+
callbacks: {
|
|
120
|
+
onBannerFetched: ({ jurisdiction, location }) => {
|
|
121
|
+
console.log('Init complete:', { jurisdiction, location });
|
|
122
|
+
},
|
|
123
|
+
onConsentSet: ({ preferences }) => {
|
|
124
|
+
console.log('Broad consent lifecycle event:', preferences);
|
|
125
|
+
},
|
|
126
|
+
onConsentChanged: ({ allowedCategories, deniedCategories }) => {
|
|
127
|
+
console.log('Explicit consent change:', {
|
|
128
|
+
allowedCategories,
|
|
129
|
+
deniedCategories,
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
onBeforeConsentRevocationReload: ({ preferences }) => {
|
|
133
|
+
console.log('Reloading due to revocation:', preferences);
|
|
134
|
+
},
|
|
135
|
+
onError: ({ error }) => {
|
|
136
|
+
console.error('Consent error:', error);
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
}}
|
|
140
|
+
>
|
|
141
|
+
<ConsentBanner />
|
|
142
|
+
<ConsentDialog />
|
|
143
|
+
{process.env.NODE_ENV !== 'production' && <DevTools />}
|
|
144
|
+
{children}
|
|
145
|
+
</ConsentManagerProvider>
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
```
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Policy Packs
|
|
3
|
+
description: How c15t resolves regional consent policies and what a policy pack controls.
|
|
4
|
+
---
|
|
5
|
+
Different countries need different consent experiences. Policy packs let you define those rules once — c15t picks the right one automatically based on where the visitor is.
|
|
6
|
+
|
|
7
|
+
A policy pack is an ordered array of policies. Each policy targets a region or country and controls the consent model, which categories are in scope, what UI is shown, and how consent is recorded.
|
|
8
|
+
|
|
9
|
+
There are three ways to configure policy packs:
|
|
10
|
+
|
|
11
|
+
1. **inth.com (recommended)** — use [inth.com](https://inth.com) as your hosted backend. Configure packs visually in the dashboard or via API — no code changes required. Works with any frontend, including static sites.
|
|
12
|
+
2. **Self-hosted backend** — define packs in code via `policyPacks` and resolve them from real request geo data. Full control over policy logic and storage.
|
|
13
|
+
3. **Offline fallback** — pass the same policy shapes to the frontend via `offlinePolicy.policyPacks`. Use this mainly for local development, demos, deterministic testing, or resilience when the backend is temporarily unreachable. If you omit `offlinePolicy.policyPacks`, c15t falls back to a synthetic worldwide opt-in banner instead of no-banner mode.
|
|
14
|
+
|
|
15
|
+
In both hosted and self-hosted modes, the **backend is always the source of truth**. Offline packs are a preview or fallback layer and never override a live backend decision.
|
|
16
|
+
|
|
17
|
+
## Quickstart
|
|
18
|
+
|
|
19
|
+
The fastest way to get started is with the built-in presets:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { policyPackPresets } from 'c15t';
|
|
23
|
+
|
|
24
|
+
const policies = [
|
|
25
|
+
policyPackPresets.europeOptIn(), // GDPR opt-in banner
|
|
26
|
+
policyPackPresets.californiaOptOut(), // CCPA opt-out banner
|
|
27
|
+
policyPackPresets.worldNoBanner(), // No banner elsewhere
|
|
28
|
+
];
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
|Preset|Model|UI|Matches|
|
|
32
|
+
|--|--|--|--|
|
|
33
|
+
|`europeOptIn()`|`opt-in`|banner|EEA + UK countries + geo fallback|
|
|
34
|
+
|`europeIab()`|`iab`|banner|EEA + UK countries + geo fallback (TCF 2.3)|
|
|
35
|
+
|`californiaOptOut()`|`opt-out`|none|US-CA region|
|
|
36
|
+
|`quebecOptIn()`|`opt-in`|banner|CA-QC region|
|
|
37
|
+
|`worldNoBanner()`|`none`|none|default fallback|
|
|
38
|
+
|
|
39
|
+
Most apps only need these presets — pick the ones that match your regions, pass them to your backend config or provider, and you're done. Customize individual fields or write fully custom policies when you need more control.
|
|
40
|
+
|
|
41
|
+
For banner/dialog actions, policy packs can also control grouped button arrangement:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
ui: {
|
|
45
|
+
mode: 'banner',
|
|
46
|
+
banner: {
|
|
47
|
+
allowedActions: ['reject', 'accept', 'customize'],
|
|
48
|
+
layout: [['reject', 'accept'], 'customize'],
|
|
49
|
+
direction: 'row',
|
|
50
|
+
primaryActions: ['accept', 'customize'],
|
|
51
|
+
},
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
That expresses arrangement only. Button appearance like `stroke`, `filled`, or `ghost` lives in the UI theme.
|
|
56
|
+
|
|
57
|
+
## What Users See
|
|
58
|
+
|
|
59
|
+
Each policy combination produces a different consent experience:
|
|
60
|
+
|
|
61
|
+
|Policy Config|User Experience|
|
|
62
|
+
|--|--|
|
|
63
|
+
|`model: 'opt-in'`, `ui.mode: 'banner'`|Banner appears, nothing loads until the user consents|
|
|
64
|
+
|`model: 'opt-out'`, `ui.mode: 'none'`|No banner, everything loads immediately — user opts out via a "Do Not Sell" link|
|
|
65
|
+
|`model: 'none'`, `ui.mode: 'none'`|No banner, all categories auto-granted silently|
|
|
66
|
+
|`model: 'opt-in'`, `ui.mode: 'dialog'`|Full-screen dialog, nothing loads until the user consents|
|
|
67
|
+
|`model: 'iab'`, `ui.mode: 'banner'`|IAB TCF 2.3 banner with vendor-level controls|
|
|
68
|
+
|
|
69
|
+
## How Policy Resolution Works
|
|
70
|
+
|
|
71
|
+
When a visitor arrives, c15t walks the policy pack in priority order:
|
|
72
|
+
|
|
73
|
+
1. **Match by region** — checks for a policy targeting the specific region (e.g., US-CA, CA-QC)
|
|
74
|
+
2. **Match by country** — if no region match, checks for a country-level policy (e.g., US, DE)
|
|
75
|
+
3. **Fallback (geo failure)** — if geo-location failed (no country detected), uses the policy marked with `match.fallback`
|
|
76
|
+
4. **Fall back to default** — if nothing matches, uses the policy marked as the default
|
|
77
|
+
5. **No match, no default** — resolves to no-banner mode (silent, no consent UI)
|
|
78
|
+
|
|
79
|
+
Within the same matcher type, the first policy in the array wins. Pack order matters when two policies target the same country or region.
|
|
80
|
+
|
|
81
|
+
The **fallback** step is distinct from **default**: `isDefault` is a catch-all for known locations that don't match any specific policy ("rest of world"), while `fallback` is a safety net for unknown locations when geo-headers are missing ("assume strictest"). The `europeOptIn()` and `europeIab()` presets include `fallback: true` by default so EU-level consent applies when geo fails.
|
|
82
|
+
|
|
83
|
+
> ⚠️ **Warning:**
|
|
84
|
+
> Only one default and one fallback policy are allowed. Use inspectPolicies() to surface overlapping matchers and other warnings before deployment.
|
|
85
|
+
|
|
86
|
+
Inspect the resolved policy from any client component:
|
|
87
|
+
|
|
88
|
+
```tsx
|
|
89
|
+
'use client';
|
|
90
|
+
|
|
91
|
+
import { useConsentManager } from '@c15t/nextjs';
|
|
92
|
+
|
|
93
|
+
export function PolicyDebug() {
|
|
94
|
+
const { locationInfo, model, policy, policyDecision } = useConsentManager();
|
|
95
|
+
|
|
96
|
+
return (
|
|
97
|
+
<pre>
|
|
98
|
+
{JSON.stringify(
|
|
99
|
+
{
|
|
100
|
+
country: locationInfo?.countryCode,
|
|
101
|
+
region: locationInfo?.regionCode,
|
|
102
|
+
model,
|
|
103
|
+
policyId: policy?.id,
|
|
104
|
+
matchedBy: policyDecision?.matchedBy,
|
|
105
|
+
fingerprint: policyDecision?.fingerprint,
|
|
106
|
+
},
|
|
107
|
+
null,
|
|
108
|
+
2
|
|
109
|
+
)}
|
|
110
|
+
</pre>
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Common Patterns
|
|
116
|
+
|
|
117
|
+
**The 80% case** — strict in Europe, light in California, silent everywhere else:
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
const policies = [
|
|
121
|
+
{
|
|
122
|
+
id: 'eu',
|
|
123
|
+
match: { countries: ['DE', 'FR', 'IT'] },
|
|
124
|
+
consent: { model: 'opt-in', categories: ['necessary', 'measurement', 'marketing'] },
|
|
125
|
+
ui: { mode: 'banner' },
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
id: 'ca',
|
|
129
|
+
match: { regions: [{ country: 'US', region: 'CA' }] },
|
|
130
|
+
consent: { model: 'opt-out', gpc: true },
|
|
131
|
+
ui: { mode: 'none' },
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
id: 'default',
|
|
135
|
+
match: { isDefault: true },
|
|
136
|
+
consent: { model: 'none' },
|
|
137
|
+
ui: { mode: 'none' },
|
|
138
|
+
},
|
|
139
|
+
];
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
**Region overrides country** — stricter rules for California than the rest of the US:
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
const policies = [
|
|
146
|
+
{
|
|
147
|
+
id: 'us_ca',
|
|
148
|
+
match: { regions: [{ country: 'US', region: 'CA' }] },
|
|
149
|
+
consent: { model: 'opt-in', scopeMode: 'strict' },
|
|
150
|
+
ui: { mode: 'banner' },
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
id: 'us',
|
|
154
|
+
match: { countries: ['US'] },
|
|
155
|
+
consent: { model: 'opt-out' },
|
|
156
|
+
ui: { mode: 'banner' },
|
|
157
|
+
},
|
|
158
|
+
];
|
|
159
|
+
// US-CA → us_ca (region match wins)
|
|
160
|
+
// US-NY → us (country match)
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
**Different wording per region** — use `i18n.messageProfile` to vary copy without changing the consent model:
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
const c15t = c15tInstance({
|
|
167
|
+
i18n: {
|
|
168
|
+
defaultProfile: 'default',
|
|
169
|
+
messages: {
|
|
170
|
+
default: {
|
|
171
|
+
translations: {
|
|
172
|
+
en: { cookieBanner: { title: 'Privacy choices' } },
|
|
173
|
+
es: { cookieBanner: { title: 'Tus opciones de privacidad' } },
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
eu: {
|
|
177
|
+
fallbackLanguage: 'en',
|
|
178
|
+
translations: {
|
|
179
|
+
en: { cookieBanner: { title: 'EU GDPR Consent' } },
|
|
180
|
+
fr: { cookieBanner: { title: 'Consentement RGPD' } },
|
|
181
|
+
de: { cookieBanner: { title: 'GDPR-Einwilligung' } },
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
policyPacks: [
|
|
187
|
+
{
|
|
188
|
+
id: 'eu',
|
|
189
|
+
match: { countries: ['DE', 'FR', 'IT'] },
|
|
190
|
+
i18n: { messageProfile: 'eu' },
|
|
191
|
+
consent: { model: 'opt-in' },
|
|
192
|
+
ui: { mode: 'banner' },
|
|
193
|
+
},
|
|
194
|
+
],
|
|
195
|
+
});
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
In that setup, the `eu` policy uses only the `eu` language set. So Europe can
|
|
199
|
+
resolve to `en`, `fr`, or `de`, but not to `es` or any other locale defined
|
|
200
|
+
only in `default`. If the browser asks for an unsupported locale, c15t falls
|
|
201
|
+
back to the `eu` profile's `fallbackLanguage`.
|
|
202
|
+
|
|
203
|
+
## Re-Prompting on Policy Change
|
|
204
|
+
|
|
205
|
+
When you change a policy in a way that affects consent semantics — like adding a category, changing the consent model, or modifying allowed actions — c15t automatically re-prompts returning users.
|
|
206
|
+
|
|
207
|
+
This works through the **material policy fingerprint**: a hash of only the consent-affecting fields (model, categories, scope, allowed actions, grouped action layout, direction, proof settings). Presentation-only changes like copy, button styling, or scroll lock do not trigger re-prompts.
|
|
208
|
+
|
|
209
|
+
|Change|Re-prompts?|
|
|
210
|
+
|--|--|
|
|
211
|
+
|Add a consent category|Yes|
|
|
212
|
+
|Change `model` from `opt-out` to `opt-in`|Yes|
|
|
213
|
+
|Remove an `allowedAction`|Yes|
|
|
214
|
+
|Change `uiProfile` or button styling|No|
|
|
215
|
+
|Update translation copy|No|
|
|
216
|
+
|Change `scrollLock`|No|
|
|
217
|
+
|
|
218
|
+
## Design Guidelines
|
|
219
|
+
|
|
220
|
+
* **Start from presets.** Use `policyPackPresets` to get running, then customize for your needs.
|
|
221
|
+
* **Keep packs small.** A handful of regional policies is better than dozens of tiny fragments.
|
|
222
|
+
* **Think risk, not geography.** Geography is just a matcher — the real question is what consent behavior each region needs.
|
|
223
|
+
* **Always include a default.** Unless "no banner for unmatched traffic" is intentional.
|
|
224
|
+
* **Set a fallback for geo failures.** Mark your strictest policy with `match.fallback=true` so users in unknown locations still see a consent banner. The `europeOptIn()` and `europeIab()` presets do this automatically.
|
|
225
|
+
* **Keep policy IDs stable.** They appear in debugging output, snapshots, and audit records.
|
|
226
|
+
* **Use `inspectPolicies()` before deploying.** It catches overlapping matchers, missing defaults, and IAB misconfigurations.
|
|
227
|
+
|
|
228
|
+
> ℹ️ **Info:**
|
|
229
|
+
> For provider setup, see the Next.js policy pack guide. For backend configuration, see the self-host guide.
|
package/docs/headless.md
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Headless Mode
|
|
3
|
+
description: Build fully custom consent UI using only hooks - no pre-built components required.
|
|
4
|
+
---
|
|
5
|
+
c15t's headless mode means using the hooks (`useConsentManager`, `useTranslations`, etc.) without any pre-built UI components. This gives you complete control over the consent experience.
|
|
6
|
+
|
|
7
|
+
Before you go headless, walk the customization ladder in order:
|
|
8
|
+
|
|
9
|
+
1. **Pre-built components** - Use provider options, component props, tokens, slots, and `theme.consentActions`
|
|
10
|
+
2. **Compound components** - Rearrange c15t primitives when the markup order must change
|
|
11
|
+
3. **`noStyle`** - Keep c15t structure but replace its styling
|
|
12
|
+
4. **Headless** - Use only hooks and build the entire UI yourself
|
|
13
|
+
|
|
14
|
+
## When to Go Headless
|
|
15
|
+
|
|
16
|
+
Go headless when:
|
|
17
|
+
|
|
18
|
+
* Your design system requires complete control over markup
|
|
19
|
+
* You need a consent flow that doesn't fit the banner/dialog pattern
|
|
20
|
+
* You want to embed consent choices inline rather than as overlays
|
|
21
|
+
|
|
22
|
+
Use a lower-power tool instead when:
|
|
23
|
+
|
|
24
|
+
* The component structure works but the styling doesn't -> use tokens, slots, or `noStyle`
|
|
25
|
+
* You only need to rearrange existing c15t parts -> use compound components
|
|
26
|
+
* You want to change copy -> use `ConsentManagerProvider.options.i18n`
|
|
27
|
+
* You only need to restyle stock actions -> use `theme.consentActions`
|
|
28
|
+
|
|
29
|
+
> ⚠️ **Warning:**
|
|
30
|
+
> Headless mode is not the first answer for pure theming. If you are still trying to debug why a banner footer color did not change, stay in the styling system and verify the token-to-component mapping before you rebuild the UI.
|
|
31
|
+
|
|
32
|
+
> ℹ️ **Info:**
|
|
33
|
+
> Need a policy-aware implementation guide? See Building Headless Components.
|
|
34
|
+
|
|
35
|
+
## Full Example: Custom Consent Banner
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
import { useConsentManager, useTranslations } from '@c15t/nextjs';
|
|
39
|
+
|
|
40
|
+
function CustomConsentBanner() {
|
|
41
|
+
const {
|
|
42
|
+
activeUI,
|
|
43
|
+
consents,
|
|
44
|
+
consentCategories,
|
|
45
|
+
consentTypes,
|
|
46
|
+
saveConsents,
|
|
47
|
+
setSelectedConsent,
|
|
48
|
+
selectedConsents,
|
|
49
|
+
} = useConsentManager();
|
|
50
|
+
const translations = useTranslations();
|
|
51
|
+
|
|
52
|
+
if (activeUI !== 'banner') return null;
|
|
53
|
+
|
|
54
|
+
const displayedTypes = consentTypes.filter(
|
|
55
|
+
(t) => consentCategories.includes(t.name) && t.display
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<div className="fixed bottom-0 inset-x-0 bg-white border-t p-6 shadow-lg z-50">
|
|
60
|
+
<h2 className="text-lg font-semibold">
|
|
61
|
+
{translations.cookieBanner.title}
|
|
62
|
+
</h2>
|
|
63
|
+
<p className="text-sm text-gray-600 mt-1">
|
|
64
|
+
{translations.cookieBanner.description}
|
|
65
|
+
</p>
|
|
66
|
+
|
|
67
|
+
<div className="mt-4 space-y-3">
|
|
68
|
+
{displayedTypes.map((type) => (
|
|
69
|
+
<label key={type.name} className="flex items-center gap-3">
|
|
70
|
+
<input
|
|
71
|
+
type="checkbox"
|
|
72
|
+
checked={selectedConsents[type.name] ?? consents[type.name] ?? false}
|
|
73
|
+
disabled={type.disabled}
|
|
74
|
+
onChange={(e) => setSelectedConsent(type.name, e.target.checked)}
|
|
75
|
+
/>
|
|
76
|
+
<div>
|
|
77
|
+
<span className="font-medium">
|
|
78
|
+
{translations.consentTypes[type.name]?.title ?? type.name}
|
|
79
|
+
</span>
|
|
80
|
+
<p className="text-xs text-gray-500">{type.description}</p>
|
|
81
|
+
</div>
|
|
82
|
+
</label>
|
|
83
|
+
))}
|
|
84
|
+
</div>
|
|
85
|
+
|
|
86
|
+
<div className="mt-4 flex gap-3">
|
|
87
|
+
<button
|
|
88
|
+
onClick={() => saveConsents('necessary')}
|
|
89
|
+
className="px-4 py-2 border rounded"
|
|
90
|
+
>
|
|
91
|
+
{translations.common.rejectAll}
|
|
92
|
+
</button>
|
|
93
|
+
<button
|
|
94
|
+
onClick={() => saveConsents('custom')}
|
|
95
|
+
className="px-4 py-2 border rounded"
|
|
96
|
+
>
|
|
97
|
+
{translations.common.save}
|
|
98
|
+
</button>
|
|
99
|
+
<button
|
|
100
|
+
onClick={() => saveConsents('all')}
|
|
101
|
+
className="px-4 py-2 bg-blue-600 text-white rounded"
|
|
102
|
+
>
|
|
103
|
+
{translations.common.acceptAll}
|
|
104
|
+
</button>
|
|
105
|
+
</div>
|
|
106
|
+
</div>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Usage with Provider
|
|
112
|
+
|
|
113
|
+
The headless UI still needs a `ConsentManagerProvider`:
|
|
114
|
+
|
|
115
|
+
```tsx
|
|
116
|
+
import { type ReactNode } from 'react';
|
|
117
|
+
import { ConsentManagerProvider } from '@c15t/nextjs';
|
|
118
|
+
|
|
119
|
+
export function ConsentManager({ children }: { children: ReactNode }) {
|
|
120
|
+
return (
|
|
121
|
+
<ConsentManagerProvider
|
|
122
|
+
options={{
|
|
123
|
+
mode: 'hosted',
|
|
124
|
+
backendURL: '/api/c15t',
|
|
125
|
+
consentCategories: ['necessary', 'measurement', 'marketing'],
|
|
126
|
+
}}
|
|
127
|
+
>
|
|
128
|
+
<CustomConsentBanner />
|
|
129
|
+
{children}
|
|
130
|
+
</ConsentManagerProvider>
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Custom Dialog
|
|
136
|
+
|
|
137
|
+
Build a custom consent dialog for detailed category management:
|
|
138
|
+
|
|
139
|
+
```tsx
|
|
140
|
+
import { useConsentManager, useTranslations } from '@c15t/nextjs';
|
|
141
|
+
|
|
142
|
+
function CustomConsentDialog() {
|
|
143
|
+
const {
|
|
144
|
+
activeUI,
|
|
145
|
+
setActiveUI,
|
|
146
|
+
consentTypes,
|
|
147
|
+
consentCategories,
|
|
148
|
+
selectedConsents,
|
|
149
|
+
consents,
|
|
150
|
+
setSelectedConsent,
|
|
151
|
+
saveConsents,
|
|
152
|
+
has,
|
|
153
|
+
} = useConsentManager();
|
|
154
|
+
const translations = useTranslations();
|
|
155
|
+
|
|
156
|
+
if (activeUI !== 'dialog') return null;
|
|
157
|
+
|
|
158
|
+
return (
|
|
159
|
+
<div className="fixed inset-0 z-50 flex items-center justify-center">
|
|
160
|
+
<div className="absolute inset-0 bg-black/50" onClick={() => setActiveUI('none')} />
|
|
161
|
+
<div className="relative bg-white rounded-xl p-6 max-w-md w-full">
|
|
162
|
+
<h2 className="text-lg font-semibold">{translations.consentManagerDialog.title}</h2>
|
|
163
|
+
|
|
164
|
+
{consentTypes
|
|
165
|
+
.filter((t) => consentCategories.includes(t.name))
|
|
166
|
+
.map((type) => (
|
|
167
|
+
<div key={type.name} className="flex items-center justify-between py-3 border-b">
|
|
168
|
+
<div>
|
|
169
|
+
<p className="font-medium">{translations.consentTypes[type.name]?.title}</p>
|
|
170
|
+
<p className="text-sm text-gray-500">{type.description}</p>
|
|
171
|
+
</div>
|
|
172
|
+
<input
|
|
173
|
+
type="checkbox"
|
|
174
|
+
checked={selectedConsents[type.name] ?? consents[type.name] ?? false}
|
|
175
|
+
disabled={type.disabled}
|
|
176
|
+
onChange={(e) => setSelectedConsent(type.name, e.target.checked)}
|
|
177
|
+
/>
|
|
178
|
+
</div>
|
|
179
|
+
))}
|
|
180
|
+
|
|
181
|
+
<div className="mt-4 flex justify-end gap-2">
|
|
182
|
+
<button onClick={() => saveConsents('necessary')}>Reject</button>
|
|
183
|
+
<button onClick={() => saveConsents('custom')}>Save</button>
|
|
184
|
+
<button onClick={() => saveConsents('all')}>Accept All</button>
|
|
185
|
+
</div>
|
|
186
|
+
</div>
|
|
187
|
+
</div>
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
```
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: useColorScheme
|
|
3
|
+
description: Manage light/dark mode preferences for consent components.
|
|
4
|
+
---
|
|
5
|
+
`useColorScheme()` manages the color scheme preference for c15t components. It sets up the appropriate CSS class and media query listeners.
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { useColorScheme } from '@c15t/nextjs';
|
|
9
|
+
|
|
10
|
+
function ThemeManager() {
|
|
11
|
+
useColorScheme('system'); // Follow system preference
|
|
12
|
+
}
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Parameters
|
|
16
|
+
|
|
17
|
+
|Value|Behavior|
|
|
18
|
+
|--|--|
|
|
19
|
+
|`'light'`|Force light mode|
|
|
20
|
+
|`'dark'`|Force dark mode|
|
|
21
|
+
|`'system'`|Follow `prefers-color-scheme` media query|
|
|
22
|
+
|`null`|Disable - c15t won't manage color scheme|
|
|
23
|
+
|`undefined`|No-op|
|
|
24
|
+
|
|
25
|
+
## Provider-Level Configuration
|
|
26
|
+
|
|
27
|
+
You can also set the color scheme on the provider without using this hook:
|
|
28
|
+
|
|
29
|
+
```tsx
|
|
30
|
+
<ConsentManagerProvider
|
|
31
|
+
options={{
|
|
32
|
+
colorScheme: 'system',
|
|
33
|
+
// ...
|
|
34
|
+
}}
|
|
35
|
+
>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## System Preference Detection
|
|
39
|
+
|
|
40
|
+
When set to `'system'`, the hook listens for changes to the `prefers-color-scheme` media query and updates automatically when the user changes their OS theme.
|