@cookiemunch/react 0.3.0 → 0.4.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/CHANGELOG.md +5 -0
- package/dist/ConsentGate.d.ts +9 -0
- package/dist/ConsentProvider.d.ts +20 -0
- package/dist/CookieDeclaration.d.ts +20 -0
- package/dist/CookieSettingsLink.d.ts +14 -0
- package/dist/RightsLink.d.ts +27 -0
- package/dist/client.d.ts +8 -145
- package/dist/consent-cookie.d.ts +48 -0
- package/dist/consent-parse.d.ts +29 -0
- package/dist/context.d.ts +16 -0
- package/dist/helpers.d.ts +4 -145
- package/dist/index.d.ts +2 -2
- package/dist/types.d.ts +69 -0
- package/dist/useBannerInset.d.ts +12 -0
- package/dist/useConsent.d.ts +4 -0
- package/dist/useConsentValue.d.ts +21 -0
- package/package.json +5 -18
- package/LICENSE +0 -21
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
Every published release, newest first. Dates are the day the version was cut.
|
|
4
4
|
|
|
5
|
+
## 0.4.0 (2026-11-02)
|
|
6
|
+
|
|
7
|
+
- Added: types for `CookieMunch.consentState(category)` and `CookieMunch.onConsent(category, cb)` on the embed global.
|
|
8
|
+
- Added: `CookieMunch.cbid`, the site the embed resolved (also with `data-cbid="auto"`).
|
|
9
|
+
|
|
5
10
|
## 0.3.0 (2026-10-24)
|
|
6
11
|
|
|
7
12
|
- Added: `<CookieSettingsLink>`, the "Cookie settings" link that reopens the visitor's choices. Plain markup the embed handles, so it renders the same on the server and needs no provider.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
import type { Category } from './helpers.js';
|
|
3
|
+
export interface ConsentGateProps {
|
|
4
|
+
category: Exclude<Category, 'necessary'>;
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
fallback?: ReactNode;
|
|
7
|
+
}
|
|
8
|
+
/** Renders children only when the given category is granted (else `fallback`). */
|
|
9
|
+
export declare function ConsentGate({ category, children, fallback }: ConsentGateProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
export interface ConsentProviderProps {
|
|
3
|
+
/** The Cookie Munch site id (required). Injected as `data-cbid` on the embed. */
|
|
4
|
+
cbid: string;
|
|
5
|
+
/** Embed URL. Defaults to the public CDN. */
|
|
6
|
+
src?: string;
|
|
7
|
+
/** Override the API base the embed beacons to (injected as `data-api`). */
|
|
8
|
+
apiBase?: string;
|
|
9
|
+
children: ReactNode;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Loads the Cookie Munch browser embed from the CDN at runtime and exposes
|
|
13
|
+
* reactive consent state to descendants — a standalone script-tag SDK wrapper
|
|
14
|
+
* that does NOT bundle the proprietary engine.
|
|
15
|
+
*
|
|
16
|
+
* SSR-safe: no `window`/`document` access during render; the script is injected
|
|
17
|
+
* and the global API resolved only inside an effect. Descendants see an
|
|
18
|
+
* all-denied, not-ready snapshot until the embed reports in.
|
|
19
|
+
*/
|
|
20
|
+
export declare function ConsentProvider({ cbid, src, apiBase, children }: ConsentProviderProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface CookieDeclarationProps {
|
|
2
|
+
/** Shown when the site has no cookies to list yet (the mount's `data-empty`). */
|
|
3
|
+
emptyText?: string;
|
|
4
|
+
className?: string;
|
|
5
|
+
id?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The live cookie declaration, for a privacy page in a React / Next.js app (CKM-S-10).
|
|
9
|
+
*
|
|
10
|
+
* Renders `<div data-cc="declaration">` and lets the embed draw the table into it. The
|
|
11
|
+
* embed fills the element whenever it appears — on first load, after a client-side
|
|
12
|
+
* navigation, or before hydration on a server-rendered page — so the element's children
|
|
13
|
+
* belong to the embed, not to React:
|
|
14
|
+
* - `dangerouslySetInnerHTML` with a constant empty string tells React it does not own
|
|
15
|
+
* the children, so it neither hydrates nor replaces them;
|
|
16
|
+
* - `suppressHydrationWarning` covers the one difference React would otherwise report:
|
|
17
|
+
* the server sent an empty element and the embed may already have filled it.
|
|
18
|
+
* After mount it asks the embed to draw this element, in case it was added after load.
|
|
19
|
+
*/
|
|
20
|
+
export declare function CookieDeclaration({ emptyText, className, id }: CookieDeclarationProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ReactNode } from 'react';
|
|
2
|
+
export interface CookieSettingsLinkProps {
|
|
3
|
+
children?: ReactNode;
|
|
4
|
+
className?: string;
|
|
5
|
+
/** Where the link points before the embed has loaded (or with scripts off). Default `#cookie-settings`. */
|
|
6
|
+
href?: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* "Cookie settings": reopens the visitor's consent choices. It is plain markup — an anchor with
|
|
10
|
+
* `data-cc="show-settings"`, which the embed's own click listener turns into
|
|
11
|
+
* `CookieMunch.showSettings()` — so it renders identically on the server and the client, needs no
|
|
12
|
+
* `<ConsentProvider>`, and opens the panel exactly once (no second handler of its own).
|
|
13
|
+
*/
|
|
14
|
+
export declare function CookieSettingsLink({ children, className, href }: CookieSettingsLinkProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type ReactNode } from 'react';
|
|
2
|
+
/** What `mint` may resolve to: the mint response from `POST /v1/sites/{cbid}/rights-links`, or just its `url`. */
|
|
3
|
+
export type MintedRightsLink = string | {
|
|
4
|
+
url: string;
|
|
5
|
+
customDomain?: string | null;
|
|
6
|
+
};
|
|
7
|
+
export interface RightsLinkProps {
|
|
8
|
+
/**
|
|
9
|
+
* Mints a single-use account link for the signed-in person — a server action (or an endpoint
|
|
10
|
+
* you call) that holds a `dsar:rights-links` key and calls `POST /v1/sites/{cbid}/rights-links`.
|
|
11
|
+
* Never put the key in the browser. Called on click, not on render: links are single-use and
|
|
12
|
+
* expire (15 minutes by default), so one minted per page view would mostly go to waste.
|
|
13
|
+
*/
|
|
14
|
+
mint: () => Promise<MintedRightsLink>;
|
|
15
|
+
/** Where the link goes before a mint, and when one fails: the plain hosted form (the email-code flow), e.g. `https://cookiemunch.net/r/<cbid>`. */
|
|
16
|
+
fallbackHref?: string;
|
|
17
|
+
children?: ReactNode;
|
|
18
|
+
className?: string;
|
|
19
|
+
/** Called when a mint fails or returns a URL that is not a rights link, before falling back. */
|
|
20
|
+
onError?: (error: Error) => void;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* "Your privacy rights" for a signed-in person: on click it mints an account link (their email is
|
|
24
|
+
* filled in and locked, the email-code step skipped) and opens it. Plain anchor markup, so it
|
|
25
|
+
* renders the same on the server and the client.
|
|
26
|
+
*/
|
|
27
|
+
export declare function RightsLink({ mint, fallbackHref, children, className, onError }: RightsLinkProps): import("react").JSX.Element;
|
package/dist/client.d.ts
CHANGED
|
@@ -1,145 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
src?: string;
|
|
10
|
-
/** Override the API base the embed beacons to (injected as `data-api`). */
|
|
11
|
-
apiBase?: string;
|
|
12
|
-
children: ReactNode;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Loads the Cookie Munch browser embed from the CDN at runtime and exposes
|
|
16
|
-
* reactive consent state to descendants — a standalone script-tag SDK wrapper
|
|
17
|
-
* that does NOT bundle the proprietary engine.
|
|
18
|
-
*
|
|
19
|
-
* SSR-safe: no `window`/`document` access during render; the script is injected
|
|
20
|
-
* and the global API resolved only inside an effect. Descendants see an
|
|
21
|
-
* all-denied, not-ready snapshot until the embed reports in.
|
|
22
|
-
*/
|
|
23
|
-
declare function ConsentProvider({ cbid, src, apiBase, children }: ConsentProviderProps): react.JSX.Element;
|
|
24
|
-
|
|
25
|
-
interface ConsentContextValue {
|
|
26
|
-
ready: boolean;
|
|
27
|
-
consent: PublicConsent;
|
|
28
|
-
hasResponse: boolean;
|
|
29
|
-
consented: boolean;
|
|
30
|
-
acceptAll(): void;
|
|
31
|
-
declineAll(): void;
|
|
32
|
-
submitCustomConsent(preferences: boolean, statistics: boolean, marketing: boolean): void;
|
|
33
|
-
withdraw(): void;
|
|
34
|
-
show(): void;
|
|
35
|
-
renew(): void;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** Access reactive consent state + actions. Must be used within a ConsentProvider. */
|
|
39
|
-
declare function useConsent(): ConsentContextValue;
|
|
40
|
-
|
|
41
|
-
interface ConsentGateProps {
|
|
42
|
-
category: Exclude<Category, 'necessary'>;
|
|
43
|
-
children: ReactNode;
|
|
44
|
-
fallback?: ReactNode;
|
|
45
|
-
}
|
|
46
|
-
/** Renders children only when the given category is granted (else `fallback`). */
|
|
47
|
-
declare function ConsentGate({ category, children, fallback }: ConsentGateProps): react.JSX.Element;
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Whether `category` is granted right now — `'preferences'`, `'statistics'`,
|
|
51
|
-
* `'marketing'`, `'necessary'` (always true) or one of your custom category ids — and
|
|
52
|
-
* re-renders when that changes, whatever changed it.
|
|
53
|
-
*
|
|
54
|
-
* `false` until the embed has reported (and during SSR), so gated content never renders
|
|
55
|
-
* ahead of consent. Works inside `<ConsentProvider>` and, when the embed is on the page
|
|
56
|
-
* some other way, without one.
|
|
57
|
-
*
|
|
58
|
-
* const analytics = useConsentValue('statistics');
|
|
59
|
-
*/
|
|
60
|
-
declare function useConsentValue(category: string): boolean;
|
|
61
|
-
/**
|
|
62
|
-
* {@link consentCookie} for a component's lifetime: the cookie is written while `category`
|
|
63
|
-
* is granted and removed when it is not. Pass a stable `value` (or a lazy function); the
|
|
64
|
-
* subscription is re-created when `name`, `category`, `value` or an option changes.
|
|
65
|
-
*
|
|
66
|
-
* useConsentCookie('affiliate_id', 'marketing', ref, { maxAgeDays: 30 });
|
|
67
|
-
*/
|
|
68
|
-
declare function useConsentCookie(name: string, category: string, value: string | (() => string), opts?: ConsentCookieOptions): void;
|
|
69
|
-
|
|
70
|
-
/** What `mint` may resolve to: the mint response from `POST /v1/sites/{cbid}/rights-links`, or just its `url`. */
|
|
71
|
-
type MintedRightsLink = string | {
|
|
72
|
-
url: string;
|
|
73
|
-
customDomain?: string | null;
|
|
74
|
-
};
|
|
75
|
-
interface RightsLinkProps {
|
|
76
|
-
/**
|
|
77
|
-
* Mints a single-use account link for the signed-in person — a server action (or an endpoint
|
|
78
|
-
* you call) that holds a `dsar:rights-links` key and calls `POST /v1/sites/{cbid}/rights-links`.
|
|
79
|
-
* Never put the key in the browser. Called on click, not on render: links are single-use and
|
|
80
|
-
* expire (15 minutes by default), so one minted per page view would mostly go to waste.
|
|
81
|
-
*/
|
|
82
|
-
mint: () => Promise<MintedRightsLink>;
|
|
83
|
-
/** Where the link goes before a mint, and when one fails: the plain hosted form (the email-code flow), e.g. `https://cookiemunch.net/r/<cbid>`. */
|
|
84
|
-
fallbackHref?: string;
|
|
85
|
-
children?: ReactNode;
|
|
86
|
-
className?: string;
|
|
87
|
-
/** Called when a mint fails or returns a URL that is not a rights link, before falling back. */
|
|
88
|
-
onError?: (error: Error) => void;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* "Your privacy rights" for a signed-in person: on click it mints an account link (their email is
|
|
92
|
-
* filled in and locked, the email-code step skipped) and opens it. Plain anchor markup, so it
|
|
93
|
-
* renders the same on the server and the client.
|
|
94
|
-
*/
|
|
95
|
-
declare function RightsLink({ mint, fallbackHref, children, className, onError }: RightsLinkProps): react.JSX.Element;
|
|
96
|
-
|
|
97
|
-
interface CookieDeclarationProps {
|
|
98
|
-
/** Shown when the site has no cookies to list yet (the mount's `data-empty`). */
|
|
99
|
-
emptyText?: string;
|
|
100
|
-
className?: string;
|
|
101
|
-
id?: string;
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* The live cookie declaration, for a privacy page in a React / Next.js app (CKM-S-10).
|
|
105
|
-
*
|
|
106
|
-
* Renders `<div data-cc="declaration">` and lets the embed draw the table into it. The
|
|
107
|
-
* embed fills the element whenever it appears — on first load, after a client-side
|
|
108
|
-
* navigation, or before hydration on a server-rendered page — so the element's children
|
|
109
|
-
* belong to the embed, not to React:
|
|
110
|
-
* - `dangerouslySetInnerHTML` with a constant empty string tells React it does not own
|
|
111
|
-
* the children, so it neither hydrates nor replaces them;
|
|
112
|
-
* - `suppressHydrationWarning` covers the one difference React would otherwise report:
|
|
113
|
-
* the server sent an empty element and the embed may already have filled it.
|
|
114
|
-
* After mount it asks the embed to draw this element, in case it was added after load.
|
|
115
|
-
*/
|
|
116
|
-
declare function CookieDeclaration({ emptyText, className, id }: CookieDeclarationProps): react.JSX.Element;
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* How many pixels of the viewport's bottom (or top) edge the consent bar covers right now — 0
|
|
120
|
-
* when no bar shows — re-rendering on every change. For floating UI the CSS variable
|
|
121
|
-
* `--cookiemunch-inset-bottom` (or the vendor-neutral `--fd-safe-bottom`) does the same job
|
|
122
|
-
* without a render; use this when the offset is computed in JavaScript.
|
|
123
|
-
*
|
|
124
|
-
* const inset = useBannerInset();
|
|
125
|
-
* <ChatButton style={{ bottom: 20 + inset }} />
|
|
126
|
-
*
|
|
127
|
-
* 0 during SSR. Works with or without `<ConsentProvider>`.
|
|
128
|
-
*/
|
|
129
|
-
declare function useBannerInset(edge?: 'bottom' | 'top'): number;
|
|
130
|
-
|
|
131
|
-
interface CookieSettingsLinkProps {
|
|
132
|
-
children?: ReactNode;
|
|
133
|
-
className?: string;
|
|
134
|
-
/** Where the link points before the embed has loaded (or with scripts off). Default `#cookie-settings`. */
|
|
135
|
-
href?: string;
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* "Cookie settings": reopens the visitor's consent choices. It is plain markup — an anchor with
|
|
139
|
-
* `data-cc="show-settings"`, which the embed's own click listener turns into
|
|
140
|
-
* `CookieMunch.showSettings()` — so it renders identically on the server and the client, needs no
|
|
141
|
-
* `<ConsentProvider>`, and opens the panel exactly once (no second handler of its own).
|
|
142
|
-
*/
|
|
143
|
-
declare function CookieSettingsLink({ children, className, href }: CookieSettingsLinkProps): react.JSX.Element;
|
|
144
|
-
|
|
145
|
-
export { type ConsentContextValue, ConsentGate, type ConsentGateProps, ConsentProvider, type ConsentProviderProps, CookieDeclaration, type CookieDeclarationProps, CookieSettingsLink, type CookieSettingsLinkProps, type MintedRightsLink, RightsLink, type RightsLinkProps, useBannerInset, useConsent, useConsentCookie, useConsentValue };
|
|
1
|
+
export { ConsentProvider, type ConsentProviderProps } from './ConsentProvider.js';
|
|
2
|
+
export { useConsent, type ConsentContextValue } from './useConsent.js';
|
|
3
|
+
export { ConsentGate, type ConsentGateProps } from './ConsentGate.js';
|
|
4
|
+
export { useConsentValue, useConsentCookie } from './useConsentValue.js';
|
|
5
|
+
export { RightsLink, type RightsLinkProps, type MintedRightsLink } from './RightsLink.js';
|
|
6
|
+
export { CookieDeclaration, type CookieDeclarationProps } from './CookieDeclaration.js';
|
|
7
|
+
export { useBannerInset } from './useBannerInset.js';
|
|
8
|
+
export { CookieSettingsLink, type CookieSettingsLinkProps } from './CookieSettingsLink.js';
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* "Store this only with consent" — the helper every integrator ends up writing.
|
|
3
|
+
*
|
|
4
|
+
* A first-party cookie of your own (an A/B bucket, an affiliate id, a chat session) is
|
|
5
|
+
* only lawful while the visitor consents to the category it belongs to. `consentCookie`
|
|
6
|
+
* writes it the moment that consent holds and removes it the moment it stops — a
|
|
7
|
+
* withdrawal from a footer link, a "Deny all", a selection that unticks the category —
|
|
8
|
+
* and does nothing at all until the embed has said what the visitor decided.
|
|
9
|
+
*
|
|
10
|
+
* Framework-free: it listens to the embed's `CookieMunchOnChange` window event, so it
|
|
11
|
+
* works with or without `<ConsentProvider>`, and with custom categories.
|
|
12
|
+
*/
|
|
13
|
+
/** Anything carrying a category decision: the public `consent` snapshot or a `ConsentState`. */
|
|
14
|
+
type ConsentLike = Record<string, unknown> & {
|
|
15
|
+
custom?: Record<string, unknown>;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Whether `category` is granted in a consent snapshot. `necessary` is always granted. A
|
|
19
|
+
* custom category is read from the top level (the public `CookieMunch.consent` snapshot)
|
|
20
|
+
* or from `custom` (the `ConsentState` that events carry). Anything else: not granted.
|
|
21
|
+
*/
|
|
22
|
+
export declare function isGranted(consent: ConsentLike | null | undefined, category: string): boolean;
|
|
23
|
+
export interface ConsentCookieOptions {
|
|
24
|
+
/** Lifetime in days. Default: a session cookie (no max-age). */
|
|
25
|
+
maxAgeDays?: number;
|
|
26
|
+
/** Default `/`. */
|
|
27
|
+
path?: string;
|
|
28
|
+
/** Domain attribute, e.g. `example.com` to share across subdomains. Default: host-only. */
|
|
29
|
+
domain?: string;
|
|
30
|
+
/** Default `Lax`. */
|
|
31
|
+
sameSite?: 'Lax' | 'Strict' | 'None';
|
|
32
|
+
/** Default: true on an https page. Forced on with `sameSite: 'None'`, as browsers require. */
|
|
33
|
+
secure?: boolean;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Keep the cookie `name` in step with consent for `category`: written (with `value`) while
|
|
37
|
+
* it is granted, removed when it is not. `value` may be a function, called only when the
|
|
38
|
+
* cookie is about to be written and is not already there — so an id is never even
|
|
39
|
+
* generated without consent, and a stored one survives page loads.
|
|
40
|
+
*
|
|
41
|
+
* Applies the current decision immediately when the embed has already loaded, then follows
|
|
42
|
+
* every change. Until the embed reports a decision, nothing is written or removed. Returns
|
|
43
|
+
* a function that stops following (the cookie is left as it is).
|
|
44
|
+
*
|
|
45
|
+
* const stop = consentCookie('ab_bucket', 'statistics', () => pickBucket(), { maxAgeDays: 30 });
|
|
46
|
+
*/
|
|
47
|
+
export declare function consentCookie(name: string, category: string, value: string | (() => string), opts?: ConsentCookieOptions): () => void;
|
|
48
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read the embed's consent cookie on the server, where `document` does not exist — a Server
|
|
3
|
+
* Component, a route handler, middleware:
|
|
4
|
+
*
|
|
5
|
+
* const consent = parseConsentCookie((await cookies()).get(CONSENT_COOKIE_NAME)?.value);
|
|
6
|
+
* if (isGranted(consent, 'statistics')) { ... }
|
|
7
|
+
*
|
|
8
|
+
* The wire format is the embed's: URL-encoded JSON of its consent state. A legacy Cookiebot
|
|
9
|
+
* `CookieConsent` value (a relaxed JS object literal) is read too. Standalone on purpose — this
|
|
10
|
+
* package never imports `@cookiemunch/core`.
|
|
11
|
+
*/
|
|
12
|
+
/** The first-party cookie the embed stores the visitor's decision in. */
|
|
13
|
+
export declare const CONSENT_COOKIE_NAME = "CookieMunch";
|
|
14
|
+
/** The category decisions a consent cookie carries. Pass it to `isGranted`. */
|
|
15
|
+
export interface ConsentCookieState {
|
|
16
|
+
necessary: true;
|
|
17
|
+
preferences: boolean;
|
|
18
|
+
statistics: boolean;
|
|
19
|
+
marketing: boolean;
|
|
20
|
+
/** Custom categories the site defines, by id. */
|
|
21
|
+
custom?: Record<string, boolean>;
|
|
22
|
+
[key: string]: unknown;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The decisions in a consent cookie value, or `null` when there is none the embed would honour
|
|
26
|
+
* (not its format, or a required field missing or mistyped). `necessary` is always
|
|
27
|
+
* true, whatever the cookie says. `null` passed to `isGranted` grants only `necessary`.
|
|
28
|
+
*/
|
|
29
|
+
export declare function parseConsentCookie(raw: string | null | undefined): ConsentCookieState | null;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { PublicConsent } from './helpers.js';
|
|
2
|
+
export interface ConsentContextValue {
|
|
3
|
+
ready: boolean;
|
|
4
|
+
consent: PublicConsent;
|
|
5
|
+
hasResponse: boolean;
|
|
6
|
+
consented: boolean;
|
|
7
|
+
acceptAll(): void;
|
|
8
|
+
declineAll(): void;
|
|
9
|
+
submitCustomConsent(preferences: boolean, statistics: boolean, marketing: boolean): void;
|
|
10
|
+
withdraw(): void;
|
|
11
|
+
show(): void;
|
|
12
|
+
renew(): void;
|
|
13
|
+
}
|
|
14
|
+
export declare const EMPTY_CONSENT: PublicConsent;
|
|
15
|
+
/** null sentinel => used to detect "called outside a ConsentProvider". */
|
|
16
|
+
export declare const ConsentContext: import("react").Context<ConsentContextValue | null>;
|
package/dist/helpers.d.ts
CHANGED
|
@@ -1,150 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* A first-party cookie of your own (an A/B bucket, an affiliate id, a chat session) is
|
|
5
|
-
* only lawful while the visitor consents to the category it belongs to. `consentCookie`
|
|
6
|
-
* writes it the moment that consent holds and removes it the moment it stops — a
|
|
7
|
-
* withdrawal from a footer link, a "Deny all", a selection that unticks the category —
|
|
8
|
-
* and does nothing at all until the embed has said what the visitor decided.
|
|
9
|
-
*
|
|
10
|
-
* Framework-free: it listens to the embed's `CookieMunchOnChange` window event, so it
|
|
11
|
-
* works with or without `<ConsentProvider>`, and with custom categories.
|
|
12
|
-
*/
|
|
13
|
-
/** Anything carrying a category decision: the public `consent` snapshot or a `ConsentState`. */
|
|
14
|
-
type ConsentLike = Record<string, unknown> & {
|
|
15
|
-
custom?: Record<string, unknown>;
|
|
16
|
-
};
|
|
17
|
-
/**
|
|
18
|
-
* Whether `category` is granted in a consent snapshot. `necessary` is always granted. A
|
|
19
|
-
* custom category is read from the top level (the public `CookieMunch.consent` snapshot)
|
|
20
|
-
* or from `custom` (the `ConsentState` that events carry). Anything else: not granted.
|
|
21
|
-
*/
|
|
22
|
-
declare function isGranted(consent: ConsentLike | null | undefined, category: string): boolean;
|
|
23
|
-
interface ConsentCookieOptions {
|
|
24
|
-
/** Lifetime in days. Default: a session cookie (no max-age). */
|
|
25
|
-
maxAgeDays?: number;
|
|
26
|
-
/** Default `/`. */
|
|
27
|
-
path?: string;
|
|
28
|
-
/** Domain attribute, e.g. `example.com` to share across subdomains. Default: host-only. */
|
|
29
|
-
domain?: string;
|
|
30
|
-
/** Default `Lax`. */
|
|
31
|
-
sameSite?: 'Lax' | 'Strict' | 'None';
|
|
32
|
-
/** Default: true on an https page. Forced on with `sameSite: 'None'`, as browsers require. */
|
|
33
|
-
secure?: boolean;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Keep the cookie `name` in step with consent for `category`: written (with `value`) while
|
|
37
|
-
* it is granted, removed when it is not. `value` may be a function, called only when the
|
|
38
|
-
* cookie is about to be written and is not already there — so an id is never even
|
|
39
|
-
* generated without consent, and a stored one survives page loads.
|
|
40
|
-
*
|
|
41
|
-
* Applies the current decision immediately when the embed has already loaded, then follows
|
|
42
|
-
* every change. Until the embed reports a decision, nothing is written or removed. Returns
|
|
43
|
-
* a function that stops following (the cookie is left as it is).
|
|
44
|
-
*
|
|
45
|
-
* const stop = consentCookie('ab_bucket', 'statistics', () => pickBucket(), { maxAgeDays: 30 });
|
|
46
|
-
*/
|
|
47
|
-
declare function consentCookie(name: string, category: string, value: string | (() => string), opts?: ConsentCookieOptions): () => void;
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Read the embed's consent cookie on the server, where `document` does not exist — a Server
|
|
51
|
-
* Component, a route handler, middleware:
|
|
52
|
-
*
|
|
53
|
-
* const consent = parseConsentCookie((await cookies()).get(CONSENT_COOKIE_NAME)?.value);
|
|
54
|
-
* if (isGranted(consent, 'statistics')) { ... }
|
|
55
|
-
*
|
|
56
|
-
* The wire format is the embed's: URL-encoded JSON of its consent state. A legacy Cookiebot
|
|
57
|
-
* `CookieConsent` value (a relaxed JS object literal) is read too. Standalone on purpose — this
|
|
58
|
-
* package never imports `@cookiemunch/core`.
|
|
59
|
-
*/
|
|
60
|
-
/** The first-party cookie the embed stores the visitor's decision in. */
|
|
61
|
-
declare const CONSENT_COOKIE_NAME = "CookieMunch";
|
|
62
|
-
/** The category decisions a consent cookie carries. Pass it to `isGranted`. */
|
|
63
|
-
interface ConsentCookieState {
|
|
64
|
-
necessary: true;
|
|
65
|
-
preferences: boolean;
|
|
66
|
-
statistics: boolean;
|
|
67
|
-
marketing: boolean;
|
|
68
|
-
/** Custom categories the site defines, by id. */
|
|
69
|
-
custom?: Record<string, boolean>;
|
|
70
|
-
[key: string]: unknown;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* The decisions in a consent cookie value, or `null` when there is none the embed would honour
|
|
74
|
-
* (not its format, or a required field missing or mistyped). `necessary` is always
|
|
75
|
-
* true, whatever the cookie says. `null` passed to `isGranted` grants only `necessary`.
|
|
76
|
-
*/
|
|
77
|
-
declare function parseConsentCookie(raw: string | null | undefined): ConsentCookieState | null;
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Local, standalone type definitions for the Cookie Munch browser embed's public
|
|
81
|
-
* surface. These are copied from the public shapes exposed on the global
|
|
82
|
-
* `window.CookieMunch` object so this package can be published WITHOUT depending
|
|
83
|
-
* on (or bundling) the proprietary `@cookiemunch/core` engine.
|
|
84
|
-
*
|
|
85
|
-
* Keep these in sync with the embed's public API by shape only — never import
|
|
86
|
-
* from `@cookiemunch/core`.
|
|
87
|
-
*/
|
|
88
|
-
/** The four base consent categories. */
|
|
89
|
-
type Category = 'necessary' | 'preferences' | 'statistics' | 'marketing';
|
|
90
|
-
/**
|
|
91
|
-
* The visitor's consent snapshot, mirroring what the embed exposes at
|
|
92
|
-
* `window.CookieMunch.consent`. Includes the four base categories, the granular
|
|
93
|
-
* advertising signals, and a receipt `stamp`. Custom category keys may also be
|
|
94
|
-
* present, hence the index signature.
|
|
95
|
-
*/
|
|
96
|
-
interface PublicConsent {
|
|
97
|
-
necessary: boolean;
|
|
98
|
-
preferences: boolean;
|
|
99
|
-
statistics: boolean;
|
|
100
|
-
marketing: boolean;
|
|
101
|
-
/** Granular advertising signals (IAB TCF purposes), else fall back to `marketing`. */
|
|
102
|
-
personalizedAds: boolean;
|
|
103
|
-
basicAds: boolean;
|
|
104
|
-
adProfiling: boolean;
|
|
105
|
-
adMeasurement: boolean;
|
|
106
|
-
/** The current consent-receipt stamp. */
|
|
107
|
-
stamp: string;
|
|
108
|
-
/** Custom category keys are included when the site defines them. */
|
|
109
|
-
[key: string]: boolean | string;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* The minimal shape of the global `window.CookieMunch` API this wrapper relies
|
|
113
|
-
* on. The real embed exposes a superset of this (Cookiebot-compatible facade);
|
|
114
|
-
* we only type the members we call.
|
|
115
|
-
*/
|
|
116
|
-
interface CookieMunchGlobal {
|
|
117
|
-
readonly consent: PublicConsent;
|
|
118
|
-
readonly consented: boolean;
|
|
119
|
-
readonly hasResponse: boolean;
|
|
120
|
-
show(): void;
|
|
121
|
-
renew(): void;
|
|
122
|
-
/** Open the preference center / second layer. */
|
|
123
|
-
showSettings(): void;
|
|
124
|
-
withdraw(): void;
|
|
125
|
-
submitCustomConsent(preferences: boolean, statistics: boolean, marketing: boolean): void;
|
|
126
|
-
/** Subscribe to consent changes (accept/decline). Returns an unsubscribe fn. */
|
|
127
|
-
onConsentChange(callback: (state: unknown) => void): () => void;
|
|
128
|
-
/**
|
|
129
|
-
* Whether `category` (a site category id or an alias such as `functional`) is consented.
|
|
130
|
-
* An unknown category answers false. Optional: older embeds lack it.
|
|
131
|
-
*/
|
|
132
|
-
hasConsent?(category: string): boolean;
|
|
133
|
-
/** Draw the cookie declaration into `el` (all mounts when omitted). Optional: older embeds lack it. */
|
|
134
|
-
renderDeclaration?(el?: Element | null): Promise<void>;
|
|
135
|
-
}
|
|
136
|
-
/** Augment the browser `Window` with the embed's globals. */
|
|
137
|
-
declare global {
|
|
138
|
-
interface Window {
|
|
139
|
-
CookieMunch?: CookieMunchGlobal;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
1
|
+
export { consentCookie, isGranted, type ConsentCookieOptions } from './consent-cookie.js';
|
|
2
|
+
export { CONSENT_COOKIE_NAME, parseConsentCookie, type ConsentCookieState } from './consent-parse.js';
|
|
3
|
+
export type { Category, PublicConsent, CookieMunchGlobal } from './types.js';
|
|
143
4
|
/**
|
|
144
5
|
* The URL shapes a minted link can have (docs: account-linked-rights): `https://<customDomain>/?t=…`
|
|
145
6
|
* when the site has an active custom rights domain, else `https://<app>/r/<cbid>?t=…`. Anything
|
|
146
7
|
* else — another scheme, no token — is refused rather than followed.
|
|
147
8
|
*/
|
|
148
|
-
declare function isRightsLinkUrl(url: string, customDomain?: string | null): boolean;
|
|
149
|
-
|
|
150
|
-
export { CONSENT_COOKIE_NAME, type Category, type ConsentCookieOptions, type ConsentCookieState, type CookieMunchGlobal, type PublicConsent, consentCookie, isGranted, isRightsLinkUrl, parseConsentCookie };
|
|
9
|
+
export declare function isRightsLinkUrl(url: string, customDomain?: string | null): boolean;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { ConsentContextValue, ConsentGate, ConsentGateProps,
|
|
2
|
-
export {
|
|
1
|
+
export { ConsentProvider, type ConsentProviderProps, useConsent, type ConsentContextValue, ConsentGate, type ConsentGateProps, useConsentValue, useConsentCookie, useBannerInset, RightsLink, type RightsLinkProps, type MintedRightsLink, CookieDeclaration, type CookieDeclarationProps, CookieSettingsLink, type CookieSettingsLinkProps, } from './client.js';
|
|
2
|
+
export { consentCookie, isGranted, type ConsentCookieOptions, CONSENT_COOKIE_NAME, parseConsentCookie, isRightsLinkUrl, type ConsentCookieState, type Category, type PublicConsent, type CookieMunchGlobal, } from './helpers.js';
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local, standalone type definitions for the Cookie Munch browser embed's public
|
|
3
|
+
* surface. These are copied from the public shapes exposed on the global
|
|
4
|
+
* `window.CookieMunch` object so this package can be published WITHOUT depending
|
|
5
|
+
* on (or bundling) the proprietary `@cookiemunch/core` engine.
|
|
6
|
+
*
|
|
7
|
+
* Keep these in sync with the embed's public API by shape only — never import
|
|
8
|
+
* from `@cookiemunch/core`.
|
|
9
|
+
*/
|
|
10
|
+
/** The four base consent categories. */
|
|
11
|
+
export type Category = 'necessary' | 'preferences' | 'statistics' | 'marketing';
|
|
12
|
+
/**
|
|
13
|
+
* The visitor's consent snapshot, mirroring what the embed exposes at
|
|
14
|
+
* `window.CookieMunch.consent`. Includes the four base categories, the granular
|
|
15
|
+
* advertising signals, and a receipt `stamp`. Custom category keys may also be
|
|
16
|
+
* present, hence the index signature.
|
|
17
|
+
*/
|
|
18
|
+
export interface PublicConsent {
|
|
19
|
+
necessary: boolean;
|
|
20
|
+
preferences: boolean;
|
|
21
|
+
statistics: boolean;
|
|
22
|
+
marketing: boolean;
|
|
23
|
+
/** Granular advertising signals (IAB TCF purposes), else fall back to `marketing`. */
|
|
24
|
+
personalizedAds: boolean;
|
|
25
|
+
basicAds: boolean;
|
|
26
|
+
adProfiling: boolean;
|
|
27
|
+
adMeasurement: boolean;
|
|
28
|
+
/** The current consent-receipt stamp. */
|
|
29
|
+
stamp: string;
|
|
30
|
+
/** Custom category keys are included when the site defines them. */
|
|
31
|
+
[key: string]: boolean | string;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The minimal shape of the global `window.CookieMunch` API this wrapper relies
|
|
35
|
+
* on. The real embed exposes a superset of this (Cookiebot-compatible facade);
|
|
36
|
+
* we only type the members we call.
|
|
37
|
+
*/
|
|
38
|
+
export interface CookieMunchGlobal {
|
|
39
|
+
readonly consent: PublicConsent;
|
|
40
|
+
readonly consented: boolean;
|
|
41
|
+
readonly hasResponse: boolean;
|
|
42
|
+
/** The site id, including one resolved from the page host (`data-cbid="auto"`); undefined until known. */
|
|
43
|
+
cbid?: string;
|
|
44
|
+
show(): void;
|
|
45
|
+
renew(): void;
|
|
46
|
+
/** Open the preference center / second layer. */
|
|
47
|
+
showSettings(): void;
|
|
48
|
+
withdraw(): void;
|
|
49
|
+
submitCustomConsent(preferences: boolean, statistics: boolean, marketing: boolean): void;
|
|
50
|
+
/** Subscribe to consent changes (accept/decline). Returns an unsubscribe fn. */
|
|
51
|
+
onConsentChange(callback: (state: unknown) => void): () => void;
|
|
52
|
+
/**
|
|
53
|
+
* Whether `category` (a site category id or an alias such as `functional`) is consented.
|
|
54
|
+
* An unknown category answers false. Optional: older embeds lack it.
|
|
55
|
+
*/
|
|
56
|
+
hasConsent?(category: string): boolean;
|
|
57
|
+
/** `true` granted, `false` refused (or unknown name), `null` not decided yet. Optional: older embeds lack it. */
|
|
58
|
+
consentState?(category: string): boolean | null;
|
|
59
|
+
/** Call `callback` once when `category` is granted (a microtask later if it already is). Returns an unsubscribe. Optional: older embeds lack it. */
|
|
60
|
+
onConsent?(category: string, callback: () => void): () => void;
|
|
61
|
+
/** Draw the cookie declaration into `el` (all mounts when omitted). Optional: older embeds lack it. */
|
|
62
|
+
renderDeclaration?(el?: Element | null): Promise<void>;
|
|
63
|
+
}
|
|
64
|
+
/** Augment the browser `Window` with the embed's globals. */
|
|
65
|
+
declare global {
|
|
66
|
+
interface Window {
|
|
67
|
+
CookieMunch?: CookieMunchGlobal;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How many pixels of the viewport's bottom (or top) edge the consent bar covers right now — 0
|
|
3
|
+
* when no bar shows — re-rendering on every change. For floating UI the CSS variable
|
|
4
|
+
* `--cookiemunch-inset-bottom` (or the vendor-neutral `--fd-safe-bottom`) does the same job
|
|
5
|
+
* without a render; use this when the offset is computed in JavaScript.
|
|
6
|
+
*
|
|
7
|
+
* const inset = useBannerInset();
|
|
8
|
+
* <ChatButton style={{ bottom: 20 + inset }} />
|
|
9
|
+
*
|
|
10
|
+
* 0 during SSR. Works with or without `<ConsentProvider>`.
|
|
11
|
+
*/
|
|
12
|
+
export declare function useBannerInset(edge?: 'bottom' | 'top'): number;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type ConsentCookieOptions } from './helpers.js';
|
|
2
|
+
/**
|
|
3
|
+
* Whether `category` is granted right now — `'preferences'`, `'statistics'`,
|
|
4
|
+
* `'marketing'`, `'necessary'` (always true) or one of your custom category ids — and
|
|
5
|
+
* re-renders when that changes, whatever changed it.
|
|
6
|
+
*
|
|
7
|
+
* `false` until the embed has reported (and during SSR), so gated content never renders
|
|
8
|
+
* ahead of consent. Works inside `<ConsentProvider>` and, when the embed is on the page
|
|
9
|
+
* some other way, without one.
|
|
10
|
+
*
|
|
11
|
+
* const analytics = useConsentValue('statistics');
|
|
12
|
+
*/
|
|
13
|
+
export declare function useConsentValue(category: string): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* {@link consentCookie} for a component's lifetime: the cookie is written while `category`
|
|
16
|
+
* is granted and removed when it is not. Pass a stable `value` (or a lazy function); the
|
|
17
|
+
* subscription is re-created when `name`, `category`, `value` or an option changes.
|
|
18
|
+
*
|
|
19
|
+
* useConsentCookie('affiliate_id', 'marketing', ref, { maxAgeDays: 30 });
|
|
20
|
+
*/
|
|
21
|
+
export declare function useConsentCookie(name: string, category: string, value: string | (() => string), opts?: ConsentCookieOptions): void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cookiemunch/react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "React/Next wrapper for Cookie Munch",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -17,10 +17,6 @@
|
|
|
17
17
|
"README.md",
|
|
18
18
|
"CHANGELOG.md"
|
|
19
19
|
],
|
|
20
|
-
"scripts": {
|
|
21
|
-
"build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsup",
|
|
22
|
-
"test": "vitest run"
|
|
23
|
-
},
|
|
24
20
|
"peerDependencies": {
|
|
25
21
|
"react": ">=18"
|
|
26
22
|
},
|
|
@@ -33,17 +29,8 @@
|
|
|
33
29
|
"url": "https://github.com/Cookie-Munch/react/issues"
|
|
34
30
|
},
|
|
35
31
|
"license": "MIT",
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"@types/node": "22.20.0",
|
|
40
|
-
"@types/react": "19.2.17",
|
|
41
|
-
"@types/react-dom": "19.2.3",
|
|
42
|
-
"happy-dom": "15.11.7",
|
|
43
|
-
"react": "19.2.7",
|
|
44
|
-
"react-dom": "19.2.7",
|
|
45
|
-
"tsup": "8.5.1",
|
|
46
|
-
"typescript": "5.9.3",
|
|
47
|
-
"vitest": "2.1.9"
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsup",
|
|
34
|
+
"test": "vitest run"
|
|
48
35
|
}
|
|
49
|
-
}
|
|
36
|
+
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Cookie Munch
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|