@luwiostack/cookie-consent 0.0.0-stage → 0.1.0
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 +292 -2
- package/dist/errors-BqpghFQf.cjs +55 -0
- package/dist/errors-BqpghFQf.cjs.map +1 -0
- package/dist/errors-D2ZIcEYK.mjs +32 -0
- package/dist/errors-D2ZIcEYK.mjs.map +1 -0
- package/dist/index.cjs +571 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +163 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +163 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +558 -0
- package/dist/index.mjs.map +1 -0
- package/dist/react.cjs +124 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.d.cts +109 -0
- package/dist/react.d.cts.map +1 -0
- package/dist/react.d.mts +109 -0
- package/dist/react.d.mts.map +1 -0
- package/dist/react.mjs +120 -0
- package/dist/react.mjs.map +1 -0
- package/dist/types-BrxRJL4I.d.cts +198 -0
- package/dist/types-BrxRJL4I.d.cts.map +1 -0
- package/dist/types-BrxRJL4I.d.mts +198 -0
- package/dist/types-BrxRJL4I.d.mts.map +1 -0
- package/dist/used-vendors-C8uSNIkd.cjs +14 -0
- package/dist/used-vendors-C8uSNIkd.cjs.map +1 -0
- package/dist/used-vendors-Dxk4PZtY.mjs +9 -0
- package/dist/used-vendors-Dxk4PZtY.mjs.map +1 -0
- package/dist/vendors.cjs +449 -0
- package/dist/vendors.cjs.map +1 -0
- package/dist/vendors.d.cts +163 -0
- package/dist/vendors.d.cts.map +1 -0
- package/dist/vendors.d.mts +163 -0
- package/dist/vendors.d.mts.map +1 -0
- package/dist/vendors.mjs +436 -0
- package/dist/vendors.mjs.map +1 -0
- package/package.json +90 -3
package/dist/react.cjs
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_used_vendors = require("./used-vendors-C8uSNIkd.cjs");
|
|
3
|
+
let react = require("react");
|
|
4
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
5
|
+
//#region src/react/consent-context.ts
|
|
6
|
+
const ConsentContext = (0, react.createContext)(null);
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/react/use-consent.ts
|
|
9
|
+
/** @internal Reads the raw context; throws outside `<Consent>`. */
|
|
10
|
+
function useConsentContext() {
|
|
11
|
+
const ctx = (0, react.useContext)(ConsentContext);
|
|
12
|
+
if (!ctx) throw new Error("useConsent must be used within <Consent consent={…}>.");
|
|
13
|
+
return ctx;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The visitor's consent, reactively, plus the actions to change it.
|
|
17
|
+
*
|
|
18
|
+
* @throws {Error} outside `<Consent>`.
|
|
19
|
+
*/
|
|
20
|
+
function useConsent() {
|
|
21
|
+
const { consent, snapshot, isPreferencesOpen, setPreferencesOpen } = useConsentContext();
|
|
22
|
+
return (0, react.useMemo)(() => {
|
|
23
|
+
const m = consent;
|
|
24
|
+
const decision = snapshot.decision;
|
|
25
|
+
const closing = (fn) => (...args) => {
|
|
26
|
+
fn(...args);
|
|
27
|
+
setPreferencesOpen(false);
|
|
28
|
+
};
|
|
29
|
+
const usedIn = require_used_vendors.usedVendors(m.categories);
|
|
30
|
+
const isGranted = (id, vendorId) => (vendorId === void 0 || usedIn[id].has(vendorId)) && (m.isRequired(id) || (decision?.isGranted(id) ?? false));
|
|
31
|
+
return {
|
|
32
|
+
consent: m,
|
|
33
|
+
status: snapshot.status,
|
|
34
|
+
decision,
|
|
35
|
+
categories: m.categories,
|
|
36
|
+
needsDecision: snapshot.status === "pending",
|
|
37
|
+
isGranted,
|
|
38
|
+
isRequired: (id) => m.isRequired(id),
|
|
39
|
+
acceptAll: closing(() => m.acceptAll()),
|
|
40
|
+
rejectAll: closing(() => m.rejectAll()),
|
|
41
|
+
save: closing((granted) => m.save(granted)),
|
|
42
|
+
grant: (...ids) => void m.grant(...ids),
|
|
43
|
+
revoke: (...ids) => void m.revoke(...ids),
|
|
44
|
+
reset: () => m.reset(),
|
|
45
|
+
isPreferencesOpen,
|
|
46
|
+
openPreferences: () => setPreferencesOpen(true),
|
|
47
|
+
closePreferences: () => setPreferencesOpen(false)
|
|
48
|
+
};
|
|
49
|
+
}, [
|
|
50
|
+
consent,
|
|
51
|
+
snapshot,
|
|
52
|
+
isPreferencesOpen,
|
|
53
|
+
setPreferencesOpen
|
|
54
|
+
]);
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region src/react/consent-gate.tsx
|
|
58
|
+
/**
|
|
59
|
+
* Renders its children only once consent allows them — for embeds and widgets that set cookies or
|
|
60
|
+
* track (a YouTube player, a map, a chat widget). Shows `fallback` otherwise, including on the
|
|
61
|
+
* server and during hydration, so tracking content never renders before consent.
|
|
62
|
+
*
|
|
63
|
+
* ```tsx
|
|
64
|
+
* <ConsentGate
|
|
65
|
+
* category="preferences"
|
|
66
|
+
* vendor="youtube"
|
|
67
|
+
* fallback={(gate) => <button type="button" onClick={gate.grant}>Load video</button>}
|
|
68
|
+
* >
|
|
69
|
+
* <iframe src="https://www.youtube-nocookie.com/embed/…" />
|
|
70
|
+
* </ConsentGate>
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
function ConsentGate({ category, vendor, fallback = null, children }) {
|
|
74
|
+
const consent = useConsent();
|
|
75
|
+
const required = typeof category === "string" ? [category] : category;
|
|
76
|
+
const usedIn = (id) => consent.categories.find((c) => c.id === id)?.vendors.some((v) => v.id === vendor);
|
|
77
|
+
if (vendor !== void 0 && !required.every(usedIn)) return null;
|
|
78
|
+
const missing = required.filter((id) => !consent.isGranted(id));
|
|
79
|
+
if (missing.length === 0) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children });
|
|
80
|
+
if (typeof fallback !== "function") return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: fallback });
|
|
81
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: fallback({
|
|
82
|
+
missing,
|
|
83
|
+
grant: () => consent.grant(...missing),
|
|
84
|
+
openPreferences: consent.openPreferences
|
|
85
|
+
}) });
|
|
86
|
+
}
|
|
87
|
+
//#endregion
|
|
88
|
+
//#region src/react/consent-provider.tsx
|
|
89
|
+
const SERVER_SNAPSHOT = Object.freeze({
|
|
90
|
+
status: "unknown",
|
|
91
|
+
decision: null
|
|
92
|
+
});
|
|
93
|
+
const getServerSnapshot = () => SERVER_SNAPSHOT;
|
|
94
|
+
/**
|
|
95
|
+
* Supplies a consent instance to the tree and keeps it reactive: every `useConsent()` and
|
|
96
|
+
* `<ConsentGate>` below re-renders when the decision changes — in this tab or another. Also owns
|
|
97
|
+
* whether your app's preferences UI is open, so a "Cookie settings" link anywhere (a footer) can
|
|
98
|
+
* reopen it with `openPreferences()`.
|
|
99
|
+
*/
|
|
100
|
+
function Consent({ consent, children }) {
|
|
101
|
+
const snapshot = (0, react.useSyncExternalStore)(consent.subscribe, consent.getSnapshot, getServerSnapshot);
|
|
102
|
+
const [isPreferencesOpen, setPreferencesOpen] = (0, react.useState)(false);
|
|
103
|
+
const value = (0, react.useMemo)(() => ({
|
|
104
|
+
consent,
|
|
105
|
+
snapshot,
|
|
106
|
+
isPreferencesOpen,
|
|
107
|
+
setPreferencesOpen
|
|
108
|
+
}), [
|
|
109
|
+
consent,
|
|
110
|
+
snapshot,
|
|
111
|
+
isPreferencesOpen
|
|
112
|
+
]);
|
|
113
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ConsentContext.Provider, {
|
|
114
|
+
value,
|
|
115
|
+
children
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
//#endregion
|
|
119
|
+
exports.Consent = Consent;
|
|
120
|
+
exports.ConsentContext = ConsentContext;
|
|
121
|
+
exports.ConsentGate = ConsentGate;
|
|
122
|
+
exports.useConsent = useConsent;
|
|
123
|
+
|
|
124
|
+
//# sourceMappingURL=react.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.cjs","names":["createContext","useContext","useMemo","usedVendors","useSyncExternalStore","useState","useMemo"],"sources":["../src/react/consent-context.ts","../src/react/use-consent.ts","../src/react/consent-gate.tsx","../src/react/consent-provider.tsx"],"sourcesContent":["import { createContext } from 'react'\nimport type { ConsentManager, ConsentSnapshot } from '../types'\n\nexport interface ConsentContextValue {\n consent: ConsentManager\n snapshot: ConsentSnapshot\n isPreferencesOpen: boolean\n setPreferencesOpen: (open: boolean) => void\n}\n\nexport const ConsentContext = createContext<ConsentContextValue | null>(null)\n","import { useContext, useMemo } from 'react'\nimport type { ConsentCategoryId } from '../categories'\nimport type { ConsentDecision } from '../domain/consent-decision'\nimport type { ConsentCategory, ConsentManager, ConsentStatus } from '../types'\nimport { usedVendors } from '../used-vendors'\nimport { ConsentContext, type ConsentContextValue } from './consent-context'\n\nexport interface UseConsentReturn {\n /** The underlying instance. */\n consent: ConsentManager\n /** `unknown` during SSR/hydration, then `pending` or `decided`. */\n status: ConsentStatus\n /** The valid decision, or `null` while `pending` / `unknown`. */\n decision: ConsentDecision | null\n /** The four categories in order, each with its vendors and their cookies. */\n categories: readonly ConsentCategory[]\n /** Whether the visitor should be asked (status `pending`). `false` while `unknown`. */\n needsDecision: boolean\n /**\n * Reactive: necessary always, others only once decided. With a vendor, also whether it's used in\n * that category — `isGranted('preferences', 'youtube')`.\n */\n isGranted: (category: ConsentCategoryId, vendorId?: string) => boolean\n isRequired: (category: ConsentCategoryId) => boolean\n /** Grant everything (closes the preferences). */\n acceptAll: () => void\n /** Grant only the required categories (closes the preferences). */\n rejectAll: () => void\n /** Grant exactly these (closes the preferences). */\n save: (granted: Iterable<ConsentCategoryId>) => void\n /** Grant these on top of the current decision. */\n grant: (...ids: ConsentCategoryId[]) => void\n /** Withdraw these from the current decision. */\n revoke: (...ids: ConsentCategoryId[]) => void\n /** Forget the decision — the banner shows again. */\n reset: () => void\n /** Whether the preferences are open (shared across the tree). */\n isPreferencesOpen: boolean\n openPreferences: () => void\n closePreferences: () => void\n}\n\n/** @internal Reads the raw context; throws outside `<Consent>`. */\nexport function useConsentContext(): ConsentContextValue {\n const ctx = useContext(ConsentContext)\n if (!ctx) throw new Error('useConsent must be used within <Consent consent={…}>.')\n return ctx\n}\n\n/**\n * The visitor's consent, reactively, plus the actions to change it.\n *\n * @throws {Error} outside `<Consent>`.\n */\nexport function useConsent(): UseConsentReturn {\n const { consent, snapshot, isPreferencesOpen, setPreferencesOpen } = useConsentContext()\n return useMemo(() => {\n const m = consent\n const decision = snapshot.decision\n const closing =\n <A extends unknown[]>(fn: (...args: A) => unknown) =>\n (...args: A) => {\n fn(...args)\n setPreferencesOpen(false)\n }\n const usedIn = usedVendors(m.categories)\n const isGranted = (id: ConsentCategoryId, vendorId?: string) =>\n (vendorId === undefined || usedIn[id].has(vendorId)) &&\n (m.isRequired(id) || (decision?.isGranted(id) ?? false))\n return {\n consent: m,\n status: snapshot.status,\n decision,\n categories: m.categories,\n needsDecision: snapshot.status === 'pending',\n isGranted,\n isRequired: (id) => m.isRequired(id),\n acceptAll: closing(() => m.acceptAll()),\n rejectAll: closing(() => m.rejectAll()),\n save: closing((granted: Iterable<ConsentCategoryId>) => m.save(granted)),\n grant: (...ids) => void m.grant(...ids),\n revoke: (...ids) => void m.revoke(...ids),\n reset: () => m.reset(),\n isPreferencesOpen,\n openPreferences: () => setPreferencesOpen(true),\n closePreferences: () => setPreferencesOpen(false),\n }\n }, [consent, snapshot, isPreferencesOpen, setPreferencesOpen])\n}\n","import type { ReactNode } from 'react'\nimport type { ConsentCategoryId } from '../categories'\nimport { useConsent } from './use-consent'\n\ntype Id = ConsentCategoryId\n\nexport interface ConsentGateApi {\n /** The categories still missing. */\n missing: readonly Id[]\n /** Grant the missing categories right here (\"Load video\" on a placeholder). */\n grant: () => void\n /** Open the preferences instead. */\n openPreferences: () => void\n}\n\nexport interface ConsentGateProps {\n /** The category — or categories, all required — the content needs. */\n category: Id | readonly Id[]\n /**\n * The vendor the content belongs to (`\"youtube\"`). The gate then also needs that vendor to be used\n * in the category — and renders nothing, not even the fallback, when it isn't: that integration\n * is off for this app.\n */\n vendor?: string\n /** Rendered instead while consent is missing. A function gets the gate's api. */\n fallback?: ReactNode | ((api: ConsentGateApi) => ReactNode)\n children?: ReactNode\n}\n\n/**\n * Renders its children only once consent allows them — for embeds and widgets that set cookies or\n * track (a YouTube player, a map, a chat widget). Shows `fallback` otherwise, including on the\n * server and during hydration, so tracking content never renders before consent.\n *\n * ```tsx\n * <ConsentGate\n * category=\"preferences\"\n * vendor=\"youtube\"\n * fallback={(gate) => <button type=\"button\" onClick={gate.grant}>Load video</button>}\n * >\n * <iframe src=\"https://www.youtube-nocookie.com/embed/…\" />\n * </ConsentGate>\n * ```\n */\nexport function ConsentGate({ category, vendor, fallback = null, children }: ConsentGateProps) {\n const consent = useConsent()\n const required = (typeof category === 'string' ? [category] : category) as readonly Id[]\n // A vendor that isn't used in (every one of) these categories: the integration is off.\n const usedIn = (id: Id) =>\n consent.categories.find((c) => c.id === id)?.vendors.some((v) => v.id === vendor)\n if (vendor !== undefined && !required.every(usedIn)) return null\n const missing = required.filter((id) => !consent.isGranted(id))\n\n if (missing.length === 0) return <>{children}</>\n if (typeof fallback !== 'function') return <>{fallback}</>\n return (\n <>\n {fallback({\n missing,\n grant: () => consent.grant(...missing),\n openPreferences: consent.openPreferences,\n })}\n </>\n )\n}\n","import type { ReactNode } from 'react'\nimport { useMemo, useState, useSyncExternalStore } from 'react'\nimport type { ConsentManager, ConsentSnapshot } from '../types'\nimport { ConsentContext, type ConsentContextValue } from './consent-context'\n\n// Storage is out of reach on the server: render as `unknown` (gates closed, no banner) and let the\n// client snapshot take over after hydration — so a visitor who already decided never sees a flash.\nconst SERVER_SNAPSHOT: ConsentSnapshot = Object.freeze({ status: 'unknown', decision: null })\nconst getServerSnapshot = () => SERVER_SNAPSHOT\n\nexport interface ConsentProps {\n /** The instance from `createConsent(config)`. */\n consent: ConsentManager\n children?: ReactNode\n}\n\n/**\n * Supplies a consent instance to the tree and keeps it reactive: every `useConsent()` and\n * `<ConsentGate>` below re-renders when the decision changes — in this tab or another. Also owns\n * whether your app's preferences UI is open, so a \"Cookie settings\" link anywhere (a footer) can\n * reopen it with `openPreferences()`.\n */\nexport function Consent({ consent, children }: ConsentProps) {\n const snapshot = useSyncExternalStore(consent.subscribe, consent.getSnapshot, getServerSnapshot)\n const [isPreferencesOpen, setPreferencesOpen] = useState(false)\n\n const value = useMemo<ConsentContextValue>(\n () => ({ consent, snapshot, isPreferencesOpen, setPreferencesOpen }),\n [consent, snapshot, isPreferencesOpen],\n )\n return <ConsentContext.Provider value={value}>{children}</ConsentContext.Provider>\n}\n"],"mappings":"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"}
|
package/dist/react.d.cts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { c as ConsentSnapshot, f as ConsentDecision, h as ConsentCategoryId, l as ConsentStatus, n as ConsentCategory, s as ConsentManager } from "./types-BrxRJL4I.cjs";
|
|
2
|
+
import { ReactNode } from "react";
|
|
3
|
+
//#region src/react/consent-context.d.ts
|
|
4
|
+
interface ConsentContextValue {
|
|
5
|
+
consent: ConsentManager;
|
|
6
|
+
snapshot: ConsentSnapshot;
|
|
7
|
+
isPreferencesOpen: boolean;
|
|
8
|
+
setPreferencesOpen: (open: boolean) => void;
|
|
9
|
+
}
|
|
10
|
+
declare const ConsentContext: import("react").Context<ConsentContextValue | null>;
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/react/consent-gate.d.ts
|
|
13
|
+
type Id = ConsentCategoryId;
|
|
14
|
+
interface ConsentGateApi {
|
|
15
|
+
/** The categories still missing. */
|
|
16
|
+
missing: readonly Id[];
|
|
17
|
+
/** Grant the missing categories right here ("Load video" on a placeholder). */
|
|
18
|
+
grant: () => void;
|
|
19
|
+
/** Open the preferences instead. */
|
|
20
|
+
openPreferences: () => void;
|
|
21
|
+
}
|
|
22
|
+
interface ConsentGateProps {
|
|
23
|
+
/** The category — or categories, all required — the content needs. */
|
|
24
|
+
category: Id | readonly Id[];
|
|
25
|
+
/**
|
|
26
|
+
* The vendor the content belongs to (`"youtube"`). The gate then also needs that vendor to be used
|
|
27
|
+
* in the category — and renders nothing, not even the fallback, when it isn't: that integration
|
|
28
|
+
* is off for this app.
|
|
29
|
+
*/
|
|
30
|
+
vendor?: string;
|
|
31
|
+
/** Rendered instead while consent is missing. A function gets the gate's api. */
|
|
32
|
+
fallback?: ReactNode | ((api: ConsentGateApi) => ReactNode);
|
|
33
|
+
children?: ReactNode;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Renders its children only once consent allows them — for embeds and widgets that set cookies or
|
|
37
|
+
* track (a YouTube player, a map, a chat widget). Shows `fallback` otherwise, including on the
|
|
38
|
+
* server and during hydration, so tracking content never renders before consent.
|
|
39
|
+
*
|
|
40
|
+
* ```tsx
|
|
41
|
+
* <ConsentGate
|
|
42
|
+
* category="preferences"
|
|
43
|
+
* vendor="youtube"
|
|
44
|
+
* fallback={(gate) => <button type="button" onClick={gate.grant}>Load video</button>}
|
|
45
|
+
* >
|
|
46
|
+
* <iframe src="https://www.youtube-nocookie.com/embed/…" />
|
|
47
|
+
* </ConsentGate>
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
declare function ConsentGate({ category, vendor, fallback, children }: ConsentGateProps): import("react").JSX.Element | null;
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/react/consent-provider.d.ts
|
|
53
|
+
interface ConsentProps {
|
|
54
|
+
/** The instance from `createConsent(config)`. */
|
|
55
|
+
consent: ConsentManager;
|
|
56
|
+
children?: ReactNode;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Supplies a consent instance to the tree and keeps it reactive: every `useConsent()` and
|
|
60
|
+
* `<ConsentGate>` below re-renders when the decision changes — in this tab or another. Also owns
|
|
61
|
+
* whether your app's preferences UI is open, so a "Cookie settings" link anywhere (a footer) can
|
|
62
|
+
* reopen it with `openPreferences()`.
|
|
63
|
+
*/
|
|
64
|
+
declare function Consent({ consent, children }: ConsentProps): import("react").JSX.Element;
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/react/use-consent.d.ts
|
|
67
|
+
interface UseConsentReturn {
|
|
68
|
+
/** The underlying instance. */
|
|
69
|
+
consent: ConsentManager;
|
|
70
|
+
/** `unknown` during SSR/hydration, then `pending` or `decided`. */
|
|
71
|
+
status: ConsentStatus;
|
|
72
|
+
/** The valid decision, or `null` while `pending` / `unknown`. */
|
|
73
|
+
decision: ConsentDecision | null;
|
|
74
|
+
/** The four categories in order, each with its vendors and their cookies. */
|
|
75
|
+
categories: readonly ConsentCategory[];
|
|
76
|
+
/** Whether the visitor should be asked (status `pending`). `false` while `unknown`. */
|
|
77
|
+
needsDecision: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Reactive: necessary always, others only once decided. With a vendor, also whether it's used in
|
|
80
|
+
* that category — `isGranted('preferences', 'youtube')`.
|
|
81
|
+
*/
|
|
82
|
+
isGranted: (category: ConsentCategoryId, vendorId?: string) => boolean;
|
|
83
|
+
isRequired: (category: ConsentCategoryId) => boolean;
|
|
84
|
+
/** Grant everything (closes the preferences). */
|
|
85
|
+
acceptAll: () => void;
|
|
86
|
+
/** Grant only the required categories (closes the preferences). */
|
|
87
|
+
rejectAll: () => void;
|
|
88
|
+
/** Grant exactly these (closes the preferences). */
|
|
89
|
+
save: (granted: Iterable<ConsentCategoryId>) => void;
|
|
90
|
+
/** Grant these on top of the current decision. */
|
|
91
|
+
grant: (...ids: ConsentCategoryId[]) => void;
|
|
92
|
+
/** Withdraw these from the current decision. */
|
|
93
|
+
revoke: (...ids: ConsentCategoryId[]) => void;
|
|
94
|
+
/** Forget the decision — the banner shows again. */
|
|
95
|
+
reset: () => void;
|
|
96
|
+
/** Whether the preferences are open (shared across the tree). */
|
|
97
|
+
isPreferencesOpen: boolean;
|
|
98
|
+
openPreferences: () => void;
|
|
99
|
+
closePreferences: () => void;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The visitor's consent, reactively, plus the actions to change it.
|
|
103
|
+
*
|
|
104
|
+
* @throws {Error} outside `<Consent>`.
|
|
105
|
+
*/
|
|
106
|
+
declare function useConsent(): UseConsentReturn;
|
|
107
|
+
//#endregion
|
|
108
|
+
export { Consent, ConsentContext, type ConsentContextValue, ConsentGate, type ConsentGateApi, type ConsentGateProps, type ConsentProps, type UseConsentReturn, useConsent };
|
|
109
|
+
//# sourceMappingURL=react.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.d.cts","names":[],"sources":["../src/react/consent-context.ts","../src/react/consent-gate.tsx","../src/react/consent-provider.tsx","../src/react/use-consent.ts"],"mappings":";;;UAGiB;EACf,SAAS;EACT,UAAU;EACV;EACA,qBAAqB;;cAGV,gCAAc,QAAA;;;KCNtB,KAAK;UAEO;;EAEf,kBAAkB;;EAElB;;EAEA;;UAGe;;EAEf,UAAU,cAAc;;;;;;EAMxB;;EAEA,WAAW,cAAc,KAAK,mBAAmB;EACjD,WAAW;;;;;;;;;;;;;;;;;iBAkBG,cAAc,UAAU,QAAQ,UAAiB,YAAY,mCAAgB,IAAA;;;UClC5E;;EAEf,SAAS;EACT,WAAW;;;;;;;;iBASG,UAAU,SAAS,YAAY,+BAAY,IAAA;;;UCf1C;;EAEf,SAAS;;EAET,QAAQ;;EAER,UAAU;;EAEV,qBAAqB;;EAErB;;;;;EAKA,YAAY,UAAU,mBAAmB;EACzC,aAAa,UAAU;;EAEvB;;EAEA;;EAEA,OAAO,SAAS,SAAS;;EAEzB,WAAW,KAAK;;EAEhB,YAAY,KAAK;;EAEjB;;EAEA;EACA;EACA;;;;;;;iBAec,cAAc"}
|
package/dist/react.d.mts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { c as ConsentSnapshot, f as ConsentDecision, h as ConsentCategoryId, l as ConsentStatus, n as ConsentCategory, s as ConsentManager } from "./types-BrxRJL4I.mjs";
|
|
2
|
+
import { ReactNode } from "react";
|
|
3
|
+
//#region src/react/consent-context.d.ts
|
|
4
|
+
interface ConsentContextValue {
|
|
5
|
+
consent: ConsentManager;
|
|
6
|
+
snapshot: ConsentSnapshot;
|
|
7
|
+
isPreferencesOpen: boolean;
|
|
8
|
+
setPreferencesOpen: (open: boolean) => void;
|
|
9
|
+
}
|
|
10
|
+
declare const ConsentContext: import("react").Context<ConsentContextValue | null>;
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/react/consent-gate.d.ts
|
|
13
|
+
type Id = ConsentCategoryId;
|
|
14
|
+
interface ConsentGateApi {
|
|
15
|
+
/** The categories still missing. */
|
|
16
|
+
missing: readonly Id[];
|
|
17
|
+
/** Grant the missing categories right here ("Load video" on a placeholder). */
|
|
18
|
+
grant: () => void;
|
|
19
|
+
/** Open the preferences instead. */
|
|
20
|
+
openPreferences: () => void;
|
|
21
|
+
}
|
|
22
|
+
interface ConsentGateProps {
|
|
23
|
+
/** The category — or categories, all required — the content needs. */
|
|
24
|
+
category: Id | readonly Id[];
|
|
25
|
+
/**
|
|
26
|
+
* The vendor the content belongs to (`"youtube"`). The gate then also needs that vendor to be used
|
|
27
|
+
* in the category — and renders nothing, not even the fallback, when it isn't: that integration
|
|
28
|
+
* is off for this app.
|
|
29
|
+
*/
|
|
30
|
+
vendor?: string;
|
|
31
|
+
/** Rendered instead while consent is missing. A function gets the gate's api. */
|
|
32
|
+
fallback?: ReactNode | ((api: ConsentGateApi) => ReactNode);
|
|
33
|
+
children?: ReactNode;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Renders its children only once consent allows them — for embeds and widgets that set cookies or
|
|
37
|
+
* track (a YouTube player, a map, a chat widget). Shows `fallback` otherwise, including on the
|
|
38
|
+
* server and during hydration, so tracking content never renders before consent.
|
|
39
|
+
*
|
|
40
|
+
* ```tsx
|
|
41
|
+
* <ConsentGate
|
|
42
|
+
* category="preferences"
|
|
43
|
+
* vendor="youtube"
|
|
44
|
+
* fallback={(gate) => <button type="button" onClick={gate.grant}>Load video</button>}
|
|
45
|
+
* >
|
|
46
|
+
* <iframe src="https://www.youtube-nocookie.com/embed/…" />
|
|
47
|
+
* </ConsentGate>
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
declare function ConsentGate({ category, vendor, fallback, children }: ConsentGateProps): import("react").JSX.Element | null;
|
|
51
|
+
//#endregion
|
|
52
|
+
//#region src/react/consent-provider.d.ts
|
|
53
|
+
interface ConsentProps {
|
|
54
|
+
/** The instance from `createConsent(config)`. */
|
|
55
|
+
consent: ConsentManager;
|
|
56
|
+
children?: ReactNode;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Supplies a consent instance to the tree and keeps it reactive: every `useConsent()` and
|
|
60
|
+
* `<ConsentGate>` below re-renders when the decision changes — in this tab or another. Also owns
|
|
61
|
+
* whether your app's preferences UI is open, so a "Cookie settings" link anywhere (a footer) can
|
|
62
|
+
* reopen it with `openPreferences()`.
|
|
63
|
+
*/
|
|
64
|
+
declare function Consent({ consent, children }: ConsentProps): import("react").JSX.Element;
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/react/use-consent.d.ts
|
|
67
|
+
interface UseConsentReturn {
|
|
68
|
+
/** The underlying instance. */
|
|
69
|
+
consent: ConsentManager;
|
|
70
|
+
/** `unknown` during SSR/hydration, then `pending` or `decided`. */
|
|
71
|
+
status: ConsentStatus;
|
|
72
|
+
/** The valid decision, or `null` while `pending` / `unknown`. */
|
|
73
|
+
decision: ConsentDecision | null;
|
|
74
|
+
/** The four categories in order, each with its vendors and their cookies. */
|
|
75
|
+
categories: readonly ConsentCategory[];
|
|
76
|
+
/** Whether the visitor should be asked (status `pending`). `false` while `unknown`. */
|
|
77
|
+
needsDecision: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Reactive: necessary always, others only once decided. With a vendor, also whether it's used in
|
|
80
|
+
* that category — `isGranted('preferences', 'youtube')`.
|
|
81
|
+
*/
|
|
82
|
+
isGranted: (category: ConsentCategoryId, vendorId?: string) => boolean;
|
|
83
|
+
isRequired: (category: ConsentCategoryId) => boolean;
|
|
84
|
+
/** Grant everything (closes the preferences). */
|
|
85
|
+
acceptAll: () => void;
|
|
86
|
+
/** Grant only the required categories (closes the preferences). */
|
|
87
|
+
rejectAll: () => void;
|
|
88
|
+
/** Grant exactly these (closes the preferences). */
|
|
89
|
+
save: (granted: Iterable<ConsentCategoryId>) => void;
|
|
90
|
+
/** Grant these on top of the current decision. */
|
|
91
|
+
grant: (...ids: ConsentCategoryId[]) => void;
|
|
92
|
+
/** Withdraw these from the current decision. */
|
|
93
|
+
revoke: (...ids: ConsentCategoryId[]) => void;
|
|
94
|
+
/** Forget the decision — the banner shows again. */
|
|
95
|
+
reset: () => void;
|
|
96
|
+
/** Whether the preferences are open (shared across the tree). */
|
|
97
|
+
isPreferencesOpen: boolean;
|
|
98
|
+
openPreferences: () => void;
|
|
99
|
+
closePreferences: () => void;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The visitor's consent, reactively, plus the actions to change it.
|
|
103
|
+
*
|
|
104
|
+
* @throws {Error} outside `<Consent>`.
|
|
105
|
+
*/
|
|
106
|
+
declare function useConsent(): UseConsentReturn;
|
|
107
|
+
//#endregion
|
|
108
|
+
export { Consent, ConsentContext, type ConsentContextValue, ConsentGate, type ConsentGateApi, type ConsentGateProps, type ConsentProps, type UseConsentReturn, useConsent };
|
|
109
|
+
//# sourceMappingURL=react.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.d.mts","names":[],"sources":["../src/react/consent-context.ts","../src/react/consent-gate.tsx","../src/react/consent-provider.tsx","../src/react/use-consent.ts"],"mappings":";;;UAGiB;EACf,SAAS;EACT,UAAU;EACV;EACA,qBAAqB;;cAGV,gCAAc,QAAA;;;KCNtB,KAAK;UAEO;;EAEf,kBAAkB;;EAElB;;EAEA;;UAGe;;EAEf,UAAU,cAAc;;;;;;EAMxB;;EAEA,WAAW,cAAc,KAAK,mBAAmB;EACjD,WAAW;;;;;;;;;;;;;;;;;iBAkBG,cAAc,UAAU,QAAQ,UAAiB,YAAY,mCAAgB,IAAA;;;UClC5E;;EAEf,SAAS;EACT,WAAW;;;;;;;;iBASG,UAAU,SAAS,YAAY,+BAAY,IAAA;;;UCf1C;;EAEf,SAAS;;EAET,QAAQ;;EAER,UAAU;;EAEV,qBAAqB;;EAErB;;;;;EAKA,YAAY,UAAU,mBAAmB;EACzC,aAAa,UAAU;;EAEvB;;EAEA;;EAEA,OAAO,SAAS,SAAS;;EAEzB,WAAW,KAAK;;EAEhB,YAAY,KAAK;;EAEjB;;EAEA;EACA;EACA;;;;;;;iBAec,cAAc"}
|
package/dist/react.mjs
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { t as usedVendors } from "./used-vendors-Dxk4PZtY.mjs";
|
|
2
|
+
import { createContext, useContext, useMemo, useState, useSyncExternalStore } from "react";
|
|
3
|
+
import { Fragment, jsx } from "react/jsx-runtime";
|
|
4
|
+
//#region src/react/consent-context.ts
|
|
5
|
+
const ConsentContext = createContext(null);
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/react/use-consent.ts
|
|
8
|
+
/** @internal Reads the raw context; throws outside `<Consent>`. */
|
|
9
|
+
function useConsentContext() {
|
|
10
|
+
const ctx = useContext(ConsentContext);
|
|
11
|
+
if (!ctx) throw new Error("useConsent must be used within <Consent consent={…}>.");
|
|
12
|
+
return ctx;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The visitor's consent, reactively, plus the actions to change it.
|
|
16
|
+
*
|
|
17
|
+
* @throws {Error} outside `<Consent>`.
|
|
18
|
+
*/
|
|
19
|
+
function useConsent() {
|
|
20
|
+
const { consent, snapshot, isPreferencesOpen, setPreferencesOpen } = useConsentContext();
|
|
21
|
+
return useMemo(() => {
|
|
22
|
+
const m = consent;
|
|
23
|
+
const decision = snapshot.decision;
|
|
24
|
+
const closing = (fn) => (...args) => {
|
|
25
|
+
fn(...args);
|
|
26
|
+
setPreferencesOpen(false);
|
|
27
|
+
};
|
|
28
|
+
const usedIn = usedVendors(m.categories);
|
|
29
|
+
const isGranted = (id, vendorId) => (vendorId === void 0 || usedIn[id].has(vendorId)) && (m.isRequired(id) || (decision?.isGranted(id) ?? false));
|
|
30
|
+
return {
|
|
31
|
+
consent: m,
|
|
32
|
+
status: snapshot.status,
|
|
33
|
+
decision,
|
|
34
|
+
categories: m.categories,
|
|
35
|
+
needsDecision: snapshot.status === "pending",
|
|
36
|
+
isGranted,
|
|
37
|
+
isRequired: (id) => m.isRequired(id),
|
|
38
|
+
acceptAll: closing(() => m.acceptAll()),
|
|
39
|
+
rejectAll: closing(() => m.rejectAll()),
|
|
40
|
+
save: closing((granted) => m.save(granted)),
|
|
41
|
+
grant: (...ids) => void m.grant(...ids),
|
|
42
|
+
revoke: (...ids) => void m.revoke(...ids),
|
|
43
|
+
reset: () => m.reset(),
|
|
44
|
+
isPreferencesOpen,
|
|
45
|
+
openPreferences: () => setPreferencesOpen(true),
|
|
46
|
+
closePreferences: () => setPreferencesOpen(false)
|
|
47
|
+
};
|
|
48
|
+
}, [
|
|
49
|
+
consent,
|
|
50
|
+
snapshot,
|
|
51
|
+
isPreferencesOpen,
|
|
52
|
+
setPreferencesOpen
|
|
53
|
+
]);
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region src/react/consent-gate.tsx
|
|
57
|
+
/**
|
|
58
|
+
* Renders its children only once consent allows them — for embeds and widgets that set cookies or
|
|
59
|
+
* track (a YouTube player, a map, a chat widget). Shows `fallback` otherwise, including on the
|
|
60
|
+
* server and during hydration, so tracking content never renders before consent.
|
|
61
|
+
*
|
|
62
|
+
* ```tsx
|
|
63
|
+
* <ConsentGate
|
|
64
|
+
* category="preferences"
|
|
65
|
+
* vendor="youtube"
|
|
66
|
+
* fallback={(gate) => <button type="button" onClick={gate.grant}>Load video</button>}
|
|
67
|
+
* >
|
|
68
|
+
* <iframe src="https://www.youtube-nocookie.com/embed/…" />
|
|
69
|
+
* </ConsentGate>
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
function ConsentGate({ category, vendor, fallback = null, children }) {
|
|
73
|
+
const consent = useConsent();
|
|
74
|
+
const required = typeof category === "string" ? [category] : category;
|
|
75
|
+
const usedIn = (id) => consent.categories.find((c) => c.id === id)?.vendors.some((v) => v.id === vendor);
|
|
76
|
+
if (vendor !== void 0 && !required.every(usedIn)) return null;
|
|
77
|
+
const missing = required.filter((id) => !consent.isGranted(id));
|
|
78
|
+
if (missing.length === 0) return /* @__PURE__ */ jsx(Fragment, { children });
|
|
79
|
+
if (typeof fallback !== "function") return /* @__PURE__ */ jsx(Fragment, { children: fallback });
|
|
80
|
+
return /* @__PURE__ */ jsx(Fragment, { children: fallback({
|
|
81
|
+
missing,
|
|
82
|
+
grant: () => consent.grant(...missing),
|
|
83
|
+
openPreferences: consent.openPreferences
|
|
84
|
+
}) });
|
|
85
|
+
}
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/react/consent-provider.tsx
|
|
88
|
+
const SERVER_SNAPSHOT = Object.freeze({
|
|
89
|
+
status: "unknown",
|
|
90
|
+
decision: null
|
|
91
|
+
});
|
|
92
|
+
const getServerSnapshot = () => SERVER_SNAPSHOT;
|
|
93
|
+
/**
|
|
94
|
+
* Supplies a consent instance to the tree and keeps it reactive: every `useConsent()` and
|
|
95
|
+
* `<ConsentGate>` below re-renders when the decision changes — in this tab or another. Also owns
|
|
96
|
+
* whether your app's preferences UI is open, so a "Cookie settings" link anywhere (a footer) can
|
|
97
|
+
* reopen it with `openPreferences()`.
|
|
98
|
+
*/
|
|
99
|
+
function Consent({ consent, children }) {
|
|
100
|
+
const snapshot = useSyncExternalStore(consent.subscribe, consent.getSnapshot, getServerSnapshot);
|
|
101
|
+
const [isPreferencesOpen, setPreferencesOpen] = useState(false);
|
|
102
|
+
const value = useMemo(() => ({
|
|
103
|
+
consent,
|
|
104
|
+
snapshot,
|
|
105
|
+
isPreferencesOpen,
|
|
106
|
+
setPreferencesOpen
|
|
107
|
+
}), [
|
|
108
|
+
consent,
|
|
109
|
+
snapshot,
|
|
110
|
+
isPreferencesOpen
|
|
111
|
+
]);
|
|
112
|
+
return /* @__PURE__ */ jsx(ConsentContext.Provider, {
|
|
113
|
+
value,
|
|
114
|
+
children
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
//#endregion
|
|
118
|
+
export { Consent, ConsentContext, ConsentGate, useConsent };
|
|
119
|
+
|
|
120
|
+
//# sourceMappingURL=react.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"react.mjs","names":[],"sources":["../src/react/consent-context.ts","../src/react/use-consent.ts","../src/react/consent-gate.tsx","../src/react/consent-provider.tsx"],"sourcesContent":["import { createContext } from 'react'\nimport type { ConsentManager, ConsentSnapshot } from '../types'\n\nexport interface ConsentContextValue {\n consent: ConsentManager\n snapshot: ConsentSnapshot\n isPreferencesOpen: boolean\n setPreferencesOpen: (open: boolean) => void\n}\n\nexport const ConsentContext = createContext<ConsentContextValue | null>(null)\n","import { useContext, useMemo } from 'react'\nimport type { ConsentCategoryId } from '../categories'\nimport type { ConsentDecision } from '../domain/consent-decision'\nimport type { ConsentCategory, ConsentManager, ConsentStatus } from '../types'\nimport { usedVendors } from '../used-vendors'\nimport { ConsentContext, type ConsentContextValue } from './consent-context'\n\nexport interface UseConsentReturn {\n /** The underlying instance. */\n consent: ConsentManager\n /** `unknown` during SSR/hydration, then `pending` or `decided`. */\n status: ConsentStatus\n /** The valid decision, or `null` while `pending` / `unknown`. */\n decision: ConsentDecision | null\n /** The four categories in order, each with its vendors and their cookies. */\n categories: readonly ConsentCategory[]\n /** Whether the visitor should be asked (status `pending`). `false` while `unknown`. */\n needsDecision: boolean\n /**\n * Reactive: necessary always, others only once decided. With a vendor, also whether it's used in\n * that category — `isGranted('preferences', 'youtube')`.\n */\n isGranted: (category: ConsentCategoryId, vendorId?: string) => boolean\n isRequired: (category: ConsentCategoryId) => boolean\n /** Grant everything (closes the preferences). */\n acceptAll: () => void\n /** Grant only the required categories (closes the preferences). */\n rejectAll: () => void\n /** Grant exactly these (closes the preferences). */\n save: (granted: Iterable<ConsentCategoryId>) => void\n /** Grant these on top of the current decision. */\n grant: (...ids: ConsentCategoryId[]) => void\n /** Withdraw these from the current decision. */\n revoke: (...ids: ConsentCategoryId[]) => void\n /** Forget the decision — the banner shows again. */\n reset: () => void\n /** Whether the preferences are open (shared across the tree). */\n isPreferencesOpen: boolean\n openPreferences: () => void\n closePreferences: () => void\n}\n\n/** @internal Reads the raw context; throws outside `<Consent>`. */\nexport function useConsentContext(): ConsentContextValue {\n const ctx = useContext(ConsentContext)\n if (!ctx) throw new Error('useConsent must be used within <Consent consent={…}>.')\n return ctx\n}\n\n/**\n * The visitor's consent, reactively, plus the actions to change it.\n *\n * @throws {Error} outside `<Consent>`.\n */\nexport function useConsent(): UseConsentReturn {\n const { consent, snapshot, isPreferencesOpen, setPreferencesOpen } = useConsentContext()\n return useMemo(() => {\n const m = consent\n const decision = snapshot.decision\n const closing =\n <A extends unknown[]>(fn: (...args: A) => unknown) =>\n (...args: A) => {\n fn(...args)\n setPreferencesOpen(false)\n }\n const usedIn = usedVendors(m.categories)\n const isGranted = (id: ConsentCategoryId, vendorId?: string) =>\n (vendorId === undefined || usedIn[id].has(vendorId)) &&\n (m.isRequired(id) || (decision?.isGranted(id) ?? false))\n return {\n consent: m,\n status: snapshot.status,\n decision,\n categories: m.categories,\n needsDecision: snapshot.status === 'pending',\n isGranted,\n isRequired: (id) => m.isRequired(id),\n acceptAll: closing(() => m.acceptAll()),\n rejectAll: closing(() => m.rejectAll()),\n save: closing((granted: Iterable<ConsentCategoryId>) => m.save(granted)),\n grant: (...ids) => void m.grant(...ids),\n revoke: (...ids) => void m.revoke(...ids),\n reset: () => m.reset(),\n isPreferencesOpen,\n openPreferences: () => setPreferencesOpen(true),\n closePreferences: () => setPreferencesOpen(false),\n }\n }, [consent, snapshot, isPreferencesOpen, setPreferencesOpen])\n}\n","import type { ReactNode } from 'react'\nimport type { ConsentCategoryId } from '../categories'\nimport { useConsent } from './use-consent'\n\ntype Id = ConsentCategoryId\n\nexport interface ConsentGateApi {\n /** The categories still missing. */\n missing: readonly Id[]\n /** Grant the missing categories right here (\"Load video\" on a placeholder). */\n grant: () => void\n /** Open the preferences instead. */\n openPreferences: () => void\n}\n\nexport interface ConsentGateProps {\n /** The category — or categories, all required — the content needs. */\n category: Id | readonly Id[]\n /**\n * The vendor the content belongs to (`\"youtube\"`). The gate then also needs that vendor to be used\n * in the category — and renders nothing, not even the fallback, when it isn't: that integration\n * is off for this app.\n */\n vendor?: string\n /** Rendered instead while consent is missing. A function gets the gate's api. */\n fallback?: ReactNode | ((api: ConsentGateApi) => ReactNode)\n children?: ReactNode\n}\n\n/**\n * Renders its children only once consent allows them — for embeds and widgets that set cookies or\n * track (a YouTube player, a map, a chat widget). Shows `fallback` otherwise, including on the\n * server and during hydration, so tracking content never renders before consent.\n *\n * ```tsx\n * <ConsentGate\n * category=\"preferences\"\n * vendor=\"youtube\"\n * fallback={(gate) => <button type=\"button\" onClick={gate.grant}>Load video</button>}\n * >\n * <iframe src=\"https://www.youtube-nocookie.com/embed/…\" />\n * </ConsentGate>\n * ```\n */\nexport function ConsentGate({ category, vendor, fallback = null, children }: ConsentGateProps) {\n const consent = useConsent()\n const required = (typeof category === 'string' ? [category] : category) as readonly Id[]\n // A vendor that isn't used in (every one of) these categories: the integration is off.\n const usedIn = (id: Id) =>\n consent.categories.find((c) => c.id === id)?.vendors.some((v) => v.id === vendor)\n if (vendor !== undefined && !required.every(usedIn)) return null\n const missing = required.filter((id) => !consent.isGranted(id))\n\n if (missing.length === 0) return <>{children}</>\n if (typeof fallback !== 'function') return <>{fallback}</>\n return (\n <>\n {fallback({\n missing,\n grant: () => consent.grant(...missing),\n openPreferences: consent.openPreferences,\n })}\n </>\n )\n}\n","import type { ReactNode } from 'react'\nimport { useMemo, useState, useSyncExternalStore } from 'react'\nimport type { ConsentManager, ConsentSnapshot } from '../types'\nimport { ConsentContext, type ConsentContextValue } from './consent-context'\n\n// Storage is out of reach on the server: render as `unknown` (gates closed, no banner) and let the\n// client snapshot take over after hydration — so a visitor who already decided never sees a flash.\nconst SERVER_SNAPSHOT: ConsentSnapshot = Object.freeze({ status: 'unknown', decision: null })\nconst getServerSnapshot = () => SERVER_SNAPSHOT\n\nexport interface ConsentProps {\n /** The instance from `createConsent(config)`. */\n consent: ConsentManager\n children?: ReactNode\n}\n\n/**\n * Supplies a consent instance to the tree and keeps it reactive: every `useConsent()` and\n * `<ConsentGate>` below re-renders when the decision changes — in this tab or another. Also owns\n * whether your app's preferences UI is open, so a \"Cookie settings\" link anywhere (a footer) can\n * reopen it with `openPreferences()`.\n */\nexport function Consent({ consent, children }: ConsentProps) {\n const snapshot = useSyncExternalStore(consent.subscribe, consent.getSnapshot, getServerSnapshot)\n const [isPreferencesOpen, setPreferencesOpen] = useState(false)\n\n const value = useMemo<ConsentContextValue>(\n () => ({ consent, snapshot, isPreferencesOpen, setPreferencesOpen }),\n [consent, snapshot, isPreferencesOpen],\n )\n return <ConsentContext.Provider value={value}>{children}</ConsentContext.Provider>\n}\n"],"mappings":";;;;AAUA,MAAa,iBAAiB,cAA0C,IAAI;;;;ACiC5E,SAAgB,oBAAyC;CACvD,MAAM,MAAM,WAAW,cAAc;CACrC,IAAI,CAAC,KAAK,MAAM,IAAI,MAAM,uDAAuD;CACjF,OAAO;AACT;;;;;;AAOA,SAAgB,aAA+B;CAC7C,MAAM,EAAE,SAAS,UAAU,mBAAmB,uBAAuB,kBAAkB;CACvF,OAAO,cAAc;EACnB,MAAM,IAAI;EACV,MAAM,WAAW,SAAS;EAC1B,MAAM,WACkB,QACrB,GAAG,SAAY;GACd,GAAG,GAAG,IAAI;GACV,mBAAmB,KAAK;EAC1B;EACF,MAAM,SAAS,YAAY,EAAE,UAAU;EACvC,MAAM,aAAa,IAAuB,cACvC,aAAa,KAAA,KAAa,OAAO,GAAG,CAAC,IAAI,QAAQ,OACjD,EAAE,WAAW,EAAE,MAAM,UAAU,UAAU,EAAE,KAAK;EACnD,OAAO;GACL,SAAS;GACT,QAAQ,SAAS;GACjB;GACA,YAAY,EAAE;GACd,eAAe,SAAS,WAAW;GACnC;GACA,aAAa,OAAO,EAAE,WAAW,EAAE;GACnC,WAAW,cAAc,EAAE,UAAU,CAAC;GACtC,WAAW,cAAc,EAAE,UAAU,CAAC;GACtC,MAAM,SAAS,YAAyC,EAAE,KAAK,OAAO,CAAC;GACvE,QAAQ,GAAG,QAAQ,KAAK,EAAE,MAAM,GAAG,GAAG;GACtC,SAAS,GAAG,QAAQ,KAAK,EAAE,OAAO,GAAG,GAAG;GACxC,aAAa,EAAE,MAAM;GACrB;GACA,uBAAuB,mBAAmB,IAAI;GAC9C,wBAAwB,mBAAmB,KAAK;EAClD;CACF,GAAG;EAAC;EAAS;EAAU;EAAmB;CAAkB,CAAC;AAC/D;;;;;;;;;;;;;;;;;;AC5CA,SAAgB,YAAY,EAAE,UAAU,QAAQ,WAAW,MAAM,YAA8B;CAC7F,MAAM,UAAU,WAAW;CAC3B,MAAM,WAAY,OAAO,aAAa,WAAW,CAAC,QAAQ,IAAI;CAE9D,MAAM,UAAU,OACd,QAAQ,WAAW,MAAM,MAAM,EAAE,OAAO,EAAE,CAAC,EAAE,QAAQ,MAAM,MAAM,EAAE,OAAO,MAAM;CAClF,IAAI,WAAW,KAAA,KAAa,CAAC,SAAS,MAAM,MAAM,GAAG,OAAO;CAC5D,MAAM,UAAU,SAAS,QAAQ,OAAO,CAAC,QAAQ,UAAU,EAAE,CAAC;CAE9D,IAAI,QAAQ,WAAW,GAAG,OAAO,oBAAA,UAAA,EAAG,SAAW,CAAA;CAC/C,IAAI,OAAO,aAAa,YAAY,OAAO,oBAAA,UAAA,EAAA,UAAG,SAAW,CAAA;CACzD,OACE,oBAAA,UAAA,EAAA,UACG,SAAS;EACR;EACA,aAAa,QAAQ,MAAM,GAAG,OAAO;EACrC,iBAAiB,QAAQ;CAC3B,CAAC,EACD,CAAA;AAEN;;;ACzDA,MAAM,kBAAmC,OAAO,OAAO;CAAE,QAAQ;CAAW,UAAU;AAAK,CAAC;AAC5F,MAAM,0BAA0B;;;;;;;AAchC,SAAgB,QAAQ,EAAE,SAAS,YAA0B;CAC3D,MAAM,WAAW,qBAAqB,QAAQ,WAAW,QAAQ,aAAa,iBAAiB;CAC/F,MAAM,CAAC,mBAAmB,sBAAsB,SAAS,KAAK;CAE9D,MAAM,QAAQ,eACL;EAAE;EAAS;EAAU;EAAmB;CAAmB,IAClE;EAAC;EAAS;EAAU;CAAiB,CACvC;CACA,OAAO,oBAAC,eAAe,UAAhB;EAAgC;EAAQ;CAAkC,CAAA;AACnF"}
|