@c15t/nextjs 2.0.0-rc.1 → 2.0.0-rc.10
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,377 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Building Headless Components
|
|
3
|
+
description: Build policy-aware custom consent components in Next.js using the headless hooks and policy-pack tooling.
|
|
4
|
+
---
|
|
5
|
+
Building custom consent UI is easier now because c15t exposes multiple layers of policy-aware primitives instead of forcing you to reconstruct banner rules by hand.
|
|
6
|
+
|
|
7
|
+
Think of customization as a ladder:
|
|
8
|
+
|
|
9
|
+
* stock component props for the shortest path
|
|
10
|
+
* `ConsentBanner.PolicyActions` and `ConsentWidget.PolicyActions` when you want custom structure but still want c15t to resolve policy-aware actions
|
|
11
|
+
* `useHeadlessConsentUI()` when you need fully manual action rendering, custom controls, or non-standard flow
|
|
12
|
+
|
|
13
|
+
> ⚠️ **Warning:**
|
|
14
|
+
> Headless is the last step in the customization ladder. Use this guide only when pre-built components, tokens, slots, compound components, and noStyle are no longer sufficient.
|
|
15
|
+
|
|
16
|
+
The headless stack underneath that is:
|
|
17
|
+
|
|
18
|
+
* `useHeadlessConsentUI()` for policy-aware banner/dialog actions, ordering, layout, and primary actions hints
|
|
19
|
+
* `@c15t/ui/utils` for the pure policy-action helpers that framework packages build on
|
|
20
|
+
* `useConsentManager()` for runtime state, categories, selected consent state, and policy metadata
|
|
21
|
+
* `useTranslations()` for the resolved copy
|
|
22
|
+
* `offlinePolicy.policyPacks` for offline previews that behave like backend policy resolution
|
|
23
|
+
|
|
24
|
+
The split is intentional: `@c15t/ui` owns pure policy-action resolution, while the framework hooks own visibility, consent mutations, and reactive state.
|
|
25
|
+
|
|
26
|
+
> ℹ️ **Info:**
|
|
27
|
+
> This guide is about building your own components while still respecting resolved policy-pack behavior. For the general headless overview, see Headless Mode.
|
|
28
|
+
|
|
29
|
+
## Choose the Smallest Layer That Solves the Job
|
|
30
|
+
|
|
31
|
+
Start with the smallest API surface that still gives you the behavior you need:
|
|
32
|
+
|
|
33
|
+
* Stay with stock components when you only need theming, spacing, copy, or legal-link changes
|
|
34
|
+
* Use `ConsentBanner.PolicyActions` or `ConsentWidget.PolicyActions` when you want a custom compound-component layout but still want grouped actions, ordering, and primary emphasis to come from policy
|
|
35
|
+
* Add `renderAction` when the grouping is still correct but you want to remap actions to stock c15t button compounds
|
|
36
|
+
* Reach for `useHeadlessConsentUI()` only when you need custom button elements, need to map `actionGroups` yourself, wire non-button controls, or coordinate the consent UI with a more custom state machine
|
|
37
|
+
|
|
38
|
+
This order matters because every step down the ladder gives you more control, but also makes it easier for your UI to drift away from the resolved policy if you stop using the provided state.
|
|
39
|
+
|
|
40
|
+
## Before You Build Headless UI
|
|
41
|
+
|
|
42
|
+
Do not use headless mode for problems that are still inside the stock component model:
|
|
43
|
+
|
|
44
|
+
* Use `layout`, `direction`, `primaryButton`, and `legalLinks` before you rebuild banner markup
|
|
45
|
+
* Use `theme.consentActions` before you swap out stock actions
|
|
46
|
+
* Use tokens such as `colors.surface` and `colors.surfaceHover` before raw CSS overrides
|
|
47
|
+
* Use slots such as `consentBannerCard`, `consentBannerFooter`, and `consentDialogCard` before compound components
|
|
48
|
+
* Use `ConsentManagerProvider.options.i18n` before rebuilding UI just to change text
|
|
49
|
+
|
|
50
|
+
A good rule: if the stock banner or dialog structure is still correct, you probably do not need headless mode.
|
|
51
|
+
|
|
52
|
+
## What the Headless Tooling Gives You
|
|
53
|
+
|
|
54
|
+
The main win is that your custom UI can stay aligned with policy packs without duplicating policy logic in your components.
|
|
55
|
+
|
|
56
|
+
`useHeadlessConsentUI()` already resolves:
|
|
57
|
+
|
|
58
|
+
* which actions are allowed
|
|
59
|
+
* the order those actions should render in
|
|
60
|
+
* grouped actions from policy `layout`
|
|
61
|
+
* layout `direction` (`row` or `column`)
|
|
62
|
+
* the primary actions
|
|
63
|
+
* UI profile and scroll-lock hints
|
|
64
|
+
* whether the banner or dialog should currently be visible
|
|
65
|
+
|
|
66
|
+
The hook also gives you the policy-aware action helpers you are expected to call:
|
|
67
|
+
|
|
68
|
+
* `performBannerAction('accept' | 'reject')`
|
|
69
|
+
* `performDialogAction('accept' | 'reject')`
|
|
70
|
+
* `saveCustomPreferences()` for the dialog `customize` action
|
|
71
|
+
* `openDialog()`, `openBanner()`, and `closeUI()` for surface visibility
|
|
72
|
+
|
|
73
|
+
That means your component mostly focuses on markup and design-system concerns instead of re-implementing policy interpretation.
|
|
74
|
+
|
|
75
|
+
For most compound-component layouts, start with `ConsentBanner.PolicyActions` or `ConsentWidget.PolicyActions`. They render stock c15t buttons and translations by default, and `renderAction` is only needed when you want to override which stock compound renders for each action. Reach for manual `actionGroups` mapping when you need action rendering that no longer fits the stock button compounds.
|
|
76
|
+
|
|
77
|
+
## Policy-Aware Compound Components First
|
|
78
|
+
|
|
79
|
+
If your goal is "custom layout, same policy behavior", start here before dropping to manual `actionGroups` rendering:
|
|
80
|
+
|
|
81
|
+
```tsx title="components/consent-manager/banner-shell.tsx"
|
|
82
|
+
'use client';
|
|
83
|
+
|
|
84
|
+
import { ConsentBanner } from '@c15t/nextjs';
|
|
85
|
+
|
|
86
|
+
export function BannerShell() {
|
|
87
|
+
return (
|
|
88
|
+
<ConsentBanner.Root>
|
|
89
|
+
<ConsentBanner.Card>
|
|
90
|
+
<ConsentBanner.Header>
|
|
91
|
+
<ConsentBanner.Title />
|
|
92
|
+
<ConsentBanner.Description />
|
|
93
|
+
</ConsentBanner.Header>
|
|
94
|
+
<ConsentBanner.PolicyActions />
|
|
95
|
+
</ConsentBanner.Card>
|
|
96
|
+
</ConsentBanner.Root>
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Use `renderAction` only when you want to remap actions to stock button compounds while keeping the same policy-driven grouping and ordering:
|
|
102
|
+
|
|
103
|
+
```tsx title="components/consent-manager/banner-actions.tsx"
|
|
104
|
+
'use client';
|
|
105
|
+
|
|
106
|
+
import { ConsentBanner } from '@c15t/nextjs';
|
|
107
|
+
|
|
108
|
+
export function BannerActionsWithCustomMapping() {
|
|
109
|
+
return (
|
|
110
|
+
<ConsentBanner.PolicyActions
|
|
111
|
+
renderAction={(action, props) => {
|
|
112
|
+
const { key, ...buttonProps } = props;
|
|
113
|
+
|
|
114
|
+
switch (action) {
|
|
115
|
+
case 'accept':
|
|
116
|
+
return <ConsentBanner.AcceptButton key={key} {...buttonProps} />;
|
|
117
|
+
case 'reject':
|
|
118
|
+
return <ConsentBanner.RejectButton key={key} {...buttonProps} />;
|
|
119
|
+
case 'customize':
|
|
120
|
+
return <ConsentBanner.CustomizeButton key={key} {...buttonProps} />;
|
|
121
|
+
}
|
|
122
|
+
}}
|
|
123
|
+
/>
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
> ℹ️ **Info:**
|
|
129
|
+
> For custom layouts built from c15t compound components, prefer ConsentBanner.PolicyActions and ConsentWidget.PolicyActions. The examples below intentionally use manual actionGroups mapping to show the fully headless escape hatch.
|
|
130
|
+
|
|
131
|
+
## Provider Setup for Local Policy Testing
|
|
132
|
+
|
|
133
|
+
```tsx title="components/consent-manager/provider.tsx"
|
|
134
|
+
'use client';
|
|
135
|
+
|
|
136
|
+
import type { ReactNode } from 'react';
|
|
137
|
+
import {
|
|
138
|
+
ConsentManagerProvider,
|
|
139
|
+
policyPackPresets,
|
|
140
|
+
} from '@c15t/nextjs';
|
|
141
|
+
|
|
142
|
+
export function ConsentManager({ children }: { children: ReactNode }) {
|
|
143
|
+
return (
|
|
144
|
+
<ConsentManagerProvider
|
|
145
|
+
options={{
|
|
146
|
+
mode: 'offline',
|
|
147
|
+
offlinePolicy: {
|
|
148
|
+
policyPacks: [
|
|
149
|
+
policyPackPresets.californiaOptOut(),
|
|
150
|
+
policyPackPresets.europeOptIn(),
|
|
151
|
+
policyPackPresets.worldNoBanner(),
|
|
152
|
+
],
|
|
153
|
+
},
|
|
154
|
+
overrides: {
|
|
155
|
+
country: 'GB',
|
|
156
|
+
},
|
|
157
|
+
}}
|
|
158
|
+
>
|
|
159
|
+
{children}
|
|
160
|
+
</ConsentManagerProvider>
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Policy-Aware Banner Example
|
|
166
|
+
|
|
167
|
+
```tsx title="components/consent-manager/custom-banner.tsx"
|
|
168
|
+
'use client';
|
|
169
|
+
|
|
170
|
+
import { useHeadlessConsentUI, useTranslations } from '@c15t/nextjs/headless';
|
|
171
|
+
|
|
172
|
+
export function CustomConsentBanner() {
|
|
173
|
+
const { banner, openDialog, performBannerAction } = useHeadlessConsentUI();
|
|
174
|
+
const translations = useTranslations();
|
|
175
|
+
|
|
176
|
+
function getActionLabel(action: (typeof banner.allowedActions)[number]) {
|
|
177
|
+
switch (action) {
|
|
178
|
+
case 'accept':
|
|
179
|
+
return translations.common.acceptAll;
|
|
180
|
+
case 'reject':
|
|
181
|
+
return translations.common.rejectAll;
|
|
182
|
+
case 'customize':
|
|
183
|
+
return translations.common.customize;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (!banner.isVisible) return null;
|
|
188
|
+
|
|
189
|
+
return (
|
|
190
|
+
<aside className="rounded-xl border bg-white p-6 shadow-lg">
|
|
191
|
+
<h2 className="text-lg font-semibold">{translations.cookieBanner.title}</h2>
|
|
192
|
+
<p className="mt-2 text-sm text-gray-600">
|
|
193
|
+
{translations.cookieBanner.description}
|
|
194
|
+
</p>
|
|
195
|
+
|
|
196
|
+
<div className="mt-4 space-y-2">
|
|
197
|
+
{banner.actionGroups.map((group, index) => (
|
|
198
|
+
<div key={`${group.join('-')}-${index}`} className="flex gap-2">
|
|
199
|
+
{group.map((action) => (
|
|
200
|
+
<button
|
|
201
|
+
key={action}
|
|
202
|
+
type="button"
|
|
203
|
+
className={banner.primaryActions.includes(action) ? 'btn-primary' : 'btn-secondary'}
|
|
204
|
+
onClick={() => {
|
|
205
|
+
if (action === 'customize') {
|
|
206
|
+
openDialog();
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
void performBannerAction(action);
|
|
210
|
+
}}
|
|
211
|
+
>
|
|
212
|
+
{getActionLabel(action)}
|
|
213
|
+
</button>
|
|
214
|
+
))}
|
|
215
|
+
</div>
|
|
216
|
+
))}
|
|
217
|
+
</div>
|
|
218
|
+
</aside>
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## Category List That Respects the Resolved Policy
|
|
224
|
+
|
|
225
|
+
```tsx title="components/consent-manager/custom-dialog.tsx"
|
|
226
|
+
'use client';
|
|
227
|
+
|
|
228
|
+
import {
|
|
229
|
+
useConsentManager,
|
|
230
|
+
useHeadlessConsentUI,
|
|
231
|
+
useTranslations,
|
|
232
|
+
} from '@c15t/nextjs/headless';
|
|
233
|
+
|
|
234
|
+
export function CustomConsentDialog() {
|
|
235
|
+
const { dialog, performDialogAction, saveCustomPreferences } = useHeadlessConsentUI();
|
|
236
|
+
const {
|
|
237
|
+
consentTypes,
|
|
238
|
+
consentCategories,
|
|
239
|
+
consents,
|
|
240
|
+
selectedConsents,
|
|
241
|
+
setSelectedConsent,
|
|
242
|
+
} = useConsentManager();
|
|
243
|
+
const translations = useTranslations();
|
|
244
|
+
|
|
245
|
+
function getActionLabel(action: (typeof dialog.allowedActions)[number]) {
|
|
246
|
+
switch (action) {
|
|
247
|
+
case 'accept':
|
|
248
|
+
return translations.common.acceptAll;
|
|
249
|
+
case 'reject':
|
|
250
|
+
return translations.common.rejectAll;
|
|
251
|
+
case 'customize':
|
|
252
|
+
return translations.common.save;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (!dialog.isVisible) return null;
|
|
257
|
+
|
|
258
|
+
const displayedTypes = consentTypes.filter(
|
|
259
|
+
(type) => type.display && consentCategories.includes(type.name)
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
return (
|
|
263
|
+
<section className="rounded-xl border bg-white p-6 shadow-xl">
|
|
264
|
+
<h2 className="text-lg font-semibold">
|
|
265
|
+
{translations.consentManagerDialog.title}
|
|
266
|
+
</h2>
|
|
267
|
+
|
|
268
|
+
<div className="mt-4 space-y-3">
|
|
269
|
+
{displayedTypes.map((type) => (
|
|
270
|
+
<label key={type.name} className="flex items-start justify-between gap-4">
|
|
271
|
+
<div>
|
|
272
|
+
<p className="font-medium">
|
|
273
|
+
{translations.consentTypes[type.name]?.title ?? type.name}
|
|
274
|
+
</p>
|
|
275
|
+
<p className="text-sm text-gray-600">{type.description}</p>
|
|
276
|
+
</div>
|
|
277
|
+
<input
|
|
278
|
+
type="checkbox"
|
|
279
|
+
checked={selectedConsents[type.name] ?? consents[type.name] ?? false}
|
|
280
|
+
disabled={type.disabled}
|
|
281
|
+
onChange={(event) =>
|
|
282
|
+
setSelectedConsent(type.name, event.target.checked)
|
|
283
|
+
}
|
|
284
|
+
/>
|
|
285
|
+
</label>
|
|
286
|
+
))}
|
|
287
|
+
</div>
|
|
288
|
+
|
|
289
|
+
<div className="mt-4 space-y-2">
|
|
290
|
+
{dialog.actionGroups.map((group, index) => (
|
|
291
|
+
<div key={`${group.join('-')}-${index}`} className="flex gap-2">
|
|
292
|
+
{group.map((action) => (
|
|
293
|
+
<button
|
|
294
|
+
key={action}
|
|
295
|
+
type="button"
|
|
296
|
+
onClick={() => {
|
|
297
|
+
if (action === 'customize') {
|
|
298
|
+
void saveCustomPreferences();
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
void performDialogAction(action);
|
|
302
|
+
}}
|
|
303
|
+
>
|
|
304
|
+
{getActionLabel(action)}
|
|
305
|
+
</button>
|
|
306
|
+
))}
|
|
307
|
+
</div>
|
|
308
|
+
))}
|
|
309
|
+
</div>
|
|
310
|
+
</section>
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
## What Headless Is Not For
|
|
316
|
+
|
|
317
|
+
Headless mode is not the recommended path for:
|
|
318
|
+
|
|
319
|
+
* changing the banner footer background
|
|
320
|
+
* rounding the stock banner card
|
|
321
|
+
* restyling stock banner or dialog buttons
|
|
322
|
+
* changing consent copy
|
|
323
|
+
|
|
324
|
+
Those should stay in the pre-built stack with tokens, slots, `theme.consentActions`, and provider `i18n`.
|
|
325
|
+
|
|
326
|
+
## What a Policy-Aware Headless Component Should Respect
|
|
327
|
+
|
|
328
|
+
When you build custom banner or dialog components, make sure they use:
|
|
329
|
+
|
|
330
|
+
* `activeUI` or `banner.isVisible` / `dialog.isVisible` for visibility
|
|
331
|
+
* `allowedActions`, `actionGroups`, and `primaryActions` instead of hard-coding buttons
|
|
332
|
+
* `primaryActions` for visual emphasis
|
|
333
|
+
* `consentCategories` when deciding which category toggles to render
|
|
334
|
+
* `policyDecision` when you want to debug why a specific UI state was chosen
|
|
335
|
+
|
|
336
|
+
If you ignore those values, your custom UI can drift away from the resolved policy pack even though the underlying consent engine is configured correctly.
|
|
337
|
+
|
|
338
|
+
## Test Custom UI Against the Resolved Policy
|
|
339
|
+
|
|
340
|
+
```ts
|
|
341
|
+
import {
|
|
342
|
+
getEffectivePolicy,
|
|
343
|
+
type PolicyUIState,
|
|
344
|
+
validateUIAgainstPolicy,
|
|
345
|
+
} from 'c15t';
|
|
346
|
+
|
|
347
|
+
const policy = getEffectivePolicy(initData);
|
|
348
|
+
|
|
349
|
+
const dialogState: PolicyUIState = {
|
|
350
|
+
mode: 'dialog',
|
|
351
|
+
actions: ['accept', 'reject', 'customize'],
|
|
352
|
+
layout: 'split',
|
|
353
|
+
uiProfile: 'compact',
|
|
354
|
+
scrollLock: true,
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
const issues = validateUIAgainstPolicy({
|
|
358
|
+
policy,
|
|
359
|
+
state: dialogState,
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
expect(issues).toEqual([]);
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
## Validation and Testing
|
|
366
|
+
|
|
367
|
+
If you are building a reusable headless component library, validate your rendered UI against the resolved runtime policy in tests.
|
|
368
|
+
|
|
369
|
+
The core package exposes:
|
|
370
|
+
|
|
371
|
+
* `getEffectivePolicy(initData)` to read the resolved policy from `/init`
|
|
372
|
+
* `validateUIAgainstPolicy({ policy, state })` to detect mismatches such as wrong actions, layout, or mode
|
|
373
|
+
|
|
374
|
+
This is useful when your design system renders custom button arrangements and you want tests to catch policy drift early.
|
|
375
|
+
|
|
376
|
+
> ℹ️ **Info:**
|
|
377
|
+
> Pair this with Policy Packs when you want to exercise multiple regional UI states locally before wiring a live backend.
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Callbacks
|
|
3
|
+
description: React to consent lifecycle events - initialization, consent changes, errors, and revocation reloads.
|
|
4
|
+
---
|
|
5
|
+
Callbacks let you run custom code at key points in the consent lifecycle. Define them in the provider or runtime `callbacks` option, or register them dynamically after initialization.
|
|
6
|
+
|
|
7
|
+
For analytics SDKs and other change-only integrations, prefer `subscribeToConsentChanges()` or `onConsentChanged`. Use `onConsentSet` when you want the broader lifecycle signal, including initialization, automatic defaults, and replay-aware registration.
|
|
8
|
+
|
|
9
|
+
> ℹ️ **Info:**
|
|
10
|
+
> subscribeToConsentChanges() is the recommended API for analytics SDKs and consent-mode integrations. It only emits future saves that actually changed persisted preferences.
|
|
11
|
+
|
|
12
|
+
## Configuration
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { type ReactNode } from 'react';
|
|
16
|
+
import { ConsentManagerProvider } from '@c15t/nextjs';
|
|
17
|
+
|
|
18
|
+
export function ConsentManager({ children }: { children: ReactNode }) {
|
|
19
|
+
return (
|
|
20
|
+
<ConsentManagerProvider
|
|
21
|
+
options={{
|
|
22
|
+
mode: 'hosted',
|
|
23
|
+
backendURL: '/api/c15t',
|
|
24
|
+
callbacks: {
|
|
25
|
+
onBannerFetched: ({ jurisdiction, location, translations }) => {
|
|
26
|
+
console.log('Jurisdiction:', jurisdiction);
|
|
27
|
+
console.log('Country:', location.countryCode);
|
|
28
|
+
console.log('Language:', translations.language);
|
|
29
|
+
},
|
|
30
|
+
onConsentSet: ({ preferences }) => {
|
|
31
|
+
console.log('Consent lifecycle event:', preferences);
|
|
32
|
+
},
|
|
33
|
+
onConsentChanged: ({ allowedCategories, deniedCategories }) => {
|
|
34
|
+
analytics.syncConsent({ allowedCategories, deniedCategories });
|
|
35
|
+
},
|
|
36
|
+
onError: ({ error }) => {
|
|
37
|
+
errorReporter.captureMessage(error);
|
|
38
|
+
},
|
|
39
|
+
onBeforeConsentRevocationReload: ({ preferences }) => {
|
|
40
|
+
// Flush pending analytics before page reloads
|
|
41
|
+
analytics.flush();
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
}}
|
|
45
|
+
>
|
|
46
|
+
{children}
|
|
47
|
+
</ConsentManagerProvider>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Choose the Right Surface
|
|
53
|
+
|
|
54
|
+
|Surface|Replays when registered late?|Fires on init / hydration / auto-grants?|Best for|
|
|
55
|
+
|--|--|--|--|
|
|
56
|
+
|`onBannerFetched`|Yes, via `setCallback('onBannerFetched', ...)` after init|Yes|Logging resolved policy, location, and translations|
|
|
57
|
+
|`onConsentSet`|Yes, via `setCallback('onConsentSet', ...)`|Yes|Broad lifecycle hooks, debugging, and integrations that want the latest full state regardless of how it was reached|
|
|
58
|
+
|`onConsentChanged`|No|No|Declarative change-only integrations|
|
|
59
|
+
|`subscribeToConsentChanges()`|No|No|Canonical change-only subscriptions after mount|
|
|
60
|
+
|
|
61
|
+
> ℹ️ **Info:**
|
|
62
|
+
> Script.onConsentChange is a script-scoped lifecycle hook. It is not the global consent change API for analytics SDKs or other app-wide integrations.
|
|
63
|
+
|
|
64
|
+
## Available Callbacks
|
|
65
|
+
|
|
66
|
+
### `onBannerFetched`
|
|
67
|
+
|
|
68
|
+
Called when the consent banner data is fetched from the backend (or loaded from SSR data). The payload includes jurisdiction info, location data, and resolved translations.
|
|
69
|
+
|
|
70
|
+
```tsx
|
|
71
|
+
onBannerFetched: ({ jurisdiction, location, translations }) => {
|
|
72
|
+
// jurisdiction: 'GDPR' | 'CCPA' | { code: 'GDPR', message: '...' } | ...
|
|
73
|
+
// location: { countryCode: 'DE', regionCode: 'BY' }
|
|
74
|
+
// translations: { language: 'de', translations: {...} }
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### `onConsentSet`
|
|
79
|
+
|
|
80
|
+
Called whenever c15t broadly settles consent state: store initialization, automatic defaults during init, explicit saves, and replay via `setCallback('onConsentSet', ...)`.
|
|
81
|
+
|
|
82
|
+
```tsx
|
|
83
|
+
onConsentSet: ({ preferences }) => {
|
|
84
|
+
// preferences: { necessary: true, measurement: true, marketing: false, ... }
|
|
85
|
+
console.log('Latest consent state:', preferences);
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### `onConsentChanged`
|
|
90
|
+
|
|
91
|
+
Called only after an explicit `saveConsents()` or `setConsent()` that actually changes the saved consent state. It never fires on store creation, hydration, automatic grants, unchanged saves, or `setCallback('onConsentChanged', ...)`.
|
|
92
|
+
|
|
93
|
+
```tsx
|
|
94
|
+
onConsentChanged: ({
|
|
95
|
+
preferences,
|
|
96
|
+
previousPreferences,
|
|
97
|
+
allowedCategories,
|
|
98
|
+
deniedCategories,
|
|
99
|
+
previousAllowedCategories,
|
|
100
|
+
previousDeniedCategories,
|
|
101
|
+
}) => {
|
|
102
|
+
analytics.syncConsent({
|
|
103
|
+
allowedCategories,
|
|
104
|
+
deniedCategories,
|
|
105
|
+
previousAllowedCategories,
|
|
106
|
+
previousDeniedCategories,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### `onError`
|
|
112
|
+
|
|
113
|
+
Called when an error occurs during consent operations (e.g., API request failure). If no `onError` callback is provided, errors are logged to `console.error`.
|
|
114
|
+
|
|
115
|
+
```tsx
|
|
116
|
+
onError: ({ error }) => {
|
|
117
|
+
// error: string describing what went wrong
|
|
118
|
+
Sentry.captureMessage(`Consent error: ${error}`);
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### `onBeforeConsentRevocationReload`
|
|
123
|
+
|
|
124
|
+
Called synchronously before the page reloads due to consent revocation. This is your last chance to run cleanup before the reload. Keep this callback fast - avoid async operations.
|
|
125
|
+
|
|
126
|
+
```tsx
|
|
127
|
+
onBeforeConsentRevocationReload: ({ preferences }) => {
|
|
128
|
+
// Flush any pending data
|
|
129
|
+
navigator.sendBeacon('/api/flush', JSON.stringify({ session: sessionId }));
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Change-Only Subscriptions
|
|
134
|
+
|
|
135
|
+
Use `subscribeToConsentChanges()` when you want a stable listener for real preference changes after mount:
|
|
136
|
+
|
|
137
|
+
```tsx
|
|
138
|
+
import { useEffect } from 'react';
|
|
139
|
+
import { useConsentManager } from '@c15t/nextjs';
|
|
140
|
+
|
|
141
|
+
function ConsentAnalytics() {
|
|
142
|
+
const { subscribeToConsentChanges } = useConsentManager();
|
|
143
|
+
|
|
144
|
+
useEffect(() => {
|
|
145
|
+
return subscribeToConsentChanges(({ allowedCategories, deniedCategories }) => {
|
|
146
|
+
analytics.syncConsent({ allowedCategories, deniedCategories });
|
|
147
|
+
});
|
|
148
|
+
}, [subscribeToConsentChanges]);
|
|
149
|
+
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Runtime Callback Registration
|
|
155
|
+
|
|
156
|
+
Register or update callbacks at runtime using `setCallback()`:
|
|
157
|
+
|
|
158
|
+
```tsx
|
|
159
|
+
import { useEffect } from 'react';
|
|
160
|
+
import { useConsentManager } from '@c15t/nextjs';
|
|
161
|
+
|
|
162
|
+
function ConsentAnalytics() {
|
|
163
|
+
const { setCallback } = useConsentManager();
|
|
164
|
+
|
|
165
|
+
useEffect(() => {
|
|
166
|
+
setCallback('onBannerFetched', ({ jurisdiction, location }) => {
|
|
167
|
+
console.log('Resolved init data:', { jurisdiction, location });
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
setCallback('onConsentSet', ({ preferences }) => {
|
|
171
|
+
console.log('Broad consent lifecycle event:', preferences);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
return () => {
|
|
175
|
+
setCallback('onBannerFetched', undefined);
|
|
176
|
+
setCallback('onConsentSet', undefined);
|
|
177
|
+
};
|
|
178
|
+
}, [setCallback]);
|
|
179
|
+
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
`setCallback('onConsentSet', ...)` immediately replays the current consent state. For change-only logic, prefer `subscribeToConsentChanges()` or `onConsentChanged`.
|