@codefast/tracking 0.5.0-canary.4 → 0.5.0-canary.5
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 +6 -0
- package/README.md +47 -15
- package/dist/react/consent-banner.d.mts +123 -35
- package/dist/react/consent-banner.mjs +181 -86
- package/dist/react/index.d.mts +2 -2
- package/dist/react/index.mjs +2 -2
- package/package.json +3 -2
- package/src/css/consent.css +119 -0
- package/src/react/consent-banner.tsx +274 -103
- package/src/react/index.ts +26 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @codefast/tracking
|
|
2
2
|
|
|
3
|
+
## 0.5.0-canary.5
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [`08f10fb`](https://github.com/codefastlabs/codefast/commit/08f10fb6f4f16e175557fac1adeb5b480dc691db) Thanks [@thevuong](https://github.com/thevuong)! - Rebuild `ConsentBanner` as composable compound parts (`ConsentBannerTitle`/`Description`/`Actions`/`Accept`/`Reject`/`Customize`/`Preferences`/`Category`/`Save`) — the root owns visibility (`needsPrompt`, overridable via `open` for a "Cookie settings" reopen) and the preferences-layer state, action parts wire their own clicks and compose the consumer's `onClick`, so any markup including a design system's button styles slots in via `className`. The monolithic `message`/`acceptLabel`/`categories` props are gone. An optional plain-CSS default theme ships at `@codefast/tracking/css/consent.css` — data-slot selectors, `--consent-*` custom properties with `light-dark()` fallbacks, zero Tailwind dependency.
|
|
8
|
+
|
|
3
9
|
## 0.5.0-canary.4
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -75,15 +75,37 @@ GDPR requires purpose-level consent, and the ads signals (`ad_storage`/`ad_user_
|
|
|
75
75
|
|
|
76
76
|
Resolve the region/mode server-side (`resolveRegion` + `resolveConsentMode` from
|
|
77
77
|
`@codefast/tracking/server` and `@codefast/tracking`) and pass `mode` down to the client.
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
78
|
+
The banner ships as composable, headless parts — no `@codefast/ui` dependency, any
|
|
79
|
+
markup (including a design system's button styles via `className`) slots in. For a
|
|
80
|
+
default look without writing CSS, import the optional plain-CSS theme:
|
|
81
|
+
|
|
82
|
+
```css
|
|
83
|
+
@import "@codefast/tracking/css/consent.css";
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Or style the parts yourself via `className`/`data-slot` attributes (`consent-banner`,
|
|
87
|
+
`consent-title`, `consent-description`, `consent-actions`, `consent-action`,
|
|
88
|
+
`consent-preferences`, `consent-category`, `consent-toggle`), e.g. Tailwind's
|
|
89
|
+
`**:data-[slot=consent-action]:rounded-md`. `data-state` on the root flips between
|
|
90
|
+
`prompt` and `preferences`.
|
|
82
91
|
|
|
83
92
|
```tsx
|
|
84
93
|
import { createLocalStorageConsentStorage } from "@codefast/tracking/client";
|
|
85
94
|
import { updateGoogleConsent } from "@codefast/tracking/destinations";
|
|
86
|
-
import {
|
|
95
|
+
import {
|
|
96
|
+
ConsentBanner,
|
|
97
|
+
ConsentBannerAccept,
|
|
98
|
+
ConsentBannerActions,
|
|
99
|
+
ConsentBannerCategory,
|
|
100
|
+
ConsentBannerCustomize,
|
|
101
|
+
ConsentBannerDescription,
|
|
102
|
+
ConsentBannerPreferences,
|
|
103
|
+
ConsentBannerReject,
|
|
104
|
+
ConsentBannerSave,
|
|
105
|
+
ConsentBannerTitle,
|
|
106
|
+
ConsentToggle,
|
|
107
|
+
useConsent,
|
|
108
|
+
} from "@codefast/tracking/react";
|
|
87
109
|
|
|
88
110
|
// Module scope — useConsent subscribes to the storage, so it must be a stable reference.
|
|
89
111
|
const consentStorage = createLocalStorageConsentStorage("tracking-consent");
|
|
@@ -101,22 +123,32 @@ function ConsentGate({ mode }: { mode: "opt-in" | "opt-out" }) {
|
|
|
101
123
|
});
|
|
102
124
|
|
|
103
125
|
return mode === "opt-in" ? (
|
|
104
|
-
<ConsentBanner
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
126
|
+
<ConsentBanner consent={consent}>
|
|
127
|
+
<ConsentBannerTitle>Cookies & analytics</ConsentBannerTitle>
|
|
128
|
+
<ConsentBannerDescription>
|
|
129
|
+
We use cookies to understand how you use this site. <a href="/privacy">Privacy policy</a>
|
|
130
|
+
</ConsentBannerDescription>
|
|
131
|
+
{/* optional second layer: per-category checkboxes ("Customize" → "Save preferences") */}
|
|
132
|
+
<ConsentBannerPreferences>
|
|
133
|
+
<ConsentBannerCategory category="analytics">Analytics</ConsentBannerCategory>
|
|
134
|
+
<ConsentBannerSave>Save preferences</ConsentBannerSave>
|
|
135
|
+
</ConsentBannerPreferences>
|
|
136
|
+
<ConsentBannerActions>
|
|
137
|
+
<ConsentBannerAccept>Accept</ConsentBannerAccept>
|
|
138
|
+
<ConsentBannerReject>Reject</ConsentBannerReject>
|
|
139
|
+
<ConsentBannerCustomize>Customize</ConsentBannerCustomize>
|
|
140
|
+
</ConsentBannerActions>
|
|
141
|
+
</ConsentBanner>
|
|
114
142
|
) : (
|
|
115
143
|
<ConsentToggle consent={consent} />
|
|
116
144
|
);
|
|
117
145
|
}
|
|
118
146
|
```
|
|
119
147
|
|
|
148
|
+
The root gates itself on `consent.needsPrompt`; pass `open` to override — e.g. reopening
|
|
149
|
+
the banner as a "Cookie settings" panel after a decision (GDPR expects withdrawing
|
|
150
|
+
consent to be as easy as giving it).
|
|
151
|
+
|
|
120
152
|
The stored decision is the single source of truth (`useSyncExternalStore` under the
|
|
121
153
|
hood): hydration-safe on prerendered pages, and a decision made in one tab dismisses the
|
|
122
154
|
banner in every other tab via the `storage` event.
|
|
@@ -4,52 +4,140 @@ import { ComponentProps, ReactNode } from "react";
|
|
|
4
4
|
|
|
5
5
|
//#region src/react/consent-banner.d.ts
|
|
6
6
|
/**
|
|
7
|
-
* One row of the preferences layer — the visitor-facing wording for a consent category.
|
|
8
|
-
*
|
|
9
7
|
* @since 0.5.0-canary.4
|
|
10
8
|
*/
|
|
11
|
-
interface
|
|
12
|
-
category: ConsentCategory;
|
|
13
|
-
description?: string;
|
|
14
|
-
label: string;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* @since 0.5.0-canary.4
|
|
18
|
-
*/
|
|
19
|
-
interface ConsentBannerProps extends Omit<ComponentProps<"section">, "children"> {
|
|
20
|
-
acceptLabel?: string;
|
|
21
|
-
/** Enables the per-category preferences layer; omit to keep the two-button banner. */
|
|
22
|
-
categories?: ReadonlyArray<ConsentCategoryOption>;
|
|
9
|
+
interface ConsentBannerProps extends ComponentProps<"section"> {
|
|
23
10
|
consent: UseConsentResult;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
message?: ReactNode;
|
|
27
|
-
rejectLabel?: string;
|
|
28
|
-
saveLabel?: string;
|
|
11
|
+
/** Overrides the default `needsPrompt` gating — e.g. to reopen the banner as a settings panel. */
|
|
12
|
+
open?: boolean | undefined;
|
|
29
13
|
}
|
|
30
14
|
/**
|
|
31
|
-
* Non-blocking opt-in prompt for GDPR/NĐ 13 regions
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* region, not a `<dialog>`: an open non-modal dialog
|
|
37
|
-
* page, so the dialog semantics would over-promise.
|
|
38
|
-
*
|
|
39
|
-
*
|
|
15
|
+
* Non-blocking opt-in prompt for GDPR/NĐ 13 regions, as composable parts — the root owns
|
|
16
|
+
* the visibility gating and the preferences-layer state; the `Accept`/`Reject`/`Customize`/
|
|
17
|
+
* `Save` parts wire their own clicks to the consent hook, so any markup (including a
|
|
18
|
+
* design system's button styles via `className`) slots in. Renders nothing once a decision
|
|
19
|
+
* exists or the region defaults to opt-out (`consent.needsPrompt`), unless `open` says
|
|
20
|
+
* otherwise. Rendered as a labeled region, not a `<dialog>`: an open non-modal dialog
|
|
21
|
+
* neither traps focus nor blocks the page, so the dialog semantics would over-promise.
|
|
22
|
+
* Unstyled by default — style via `className`/`data-slot` selectors, or import the
|
|
23
|
+
* optional `@codefast/tracking/css/consent.css` theme. `data-state` on the root flips
|
|
24
|
+
* between `prompt` and `preferences` for state-dependent styling.
|
|
40
25
|
*
|
|
41
26
|
* @since 0.5.0-canary.4
|
|
42
27
|
*/
|
|
43
28
|
declare function ConsentBanner({
|
|
44
|
-
|
|
45
|
-
categories,
|
|
29
|
+
children,
|
|
46
30
|
consent,
|
|
47
|
-
|
|
48
|
-
message,
|
|
49
|
-
rejectLabel,
|
|
50
|
-
saveLabel,
|
|
31
|
+
open,
|
|
51
32
|
...props
|
|
52
33
|
}: ConsentBannerProps): ReactNode;
|
|
34
|
+
/**
|
|
35
|
+
* @since 0.5.0-canary.5
|
|
36
|
+
*/
|
|
37
|
+
interface ConsentBannerTitleProps extends ComponentProps<"h2"> {}
|
|
38
|
+
/**
|
|
39
|
+
* @since 0.5.0-canary.5
|
|
40
|
+
*/
|
|
41
|
+
declare function ConsentBannerTitle({
|
|
42
|
+
children,
|
|
43
|
+
...props
|
|
44
|
+
}: ConsentBannerTitleProps): ReactNode;
|
|
45
|
+
/**
|
|
46
|
+
* @since 0.5.0-canary.5
|
|
47
|
+
*/
|
|
48
|
+
interface ConsentBannerDescriptionProps extends ComponentProps<"p"> {}
|
|
49
|
+
/**
|
|
50
|
+
* Hosts the message and the privacy-policy link informed consent requires.
|
|
51
|
+
*
|
|
52
|
+
* @since 0.5.0-canary.5
|
|
53
|
+
*/
|
|
54
|
+
declare function ConsentBannerDescription(props: ConsentBannerDescriptionProps): ReactNode;
|
|
55
|
+
/**
|
|
56
|
+
* @since 0.5.0-canary.5
|
|
57
|
+
*/
|
|
58
|
+
interface ConsentBannerActionsProps extends ComponentProps<"div"> {}
|
|
59
|
+
/**
|
|
60
|
+
* @since 0.5.0-canary.5
|
|
61
|
+
*/
|
|
62
|
+
declare function ConsentBannerActions(props: ConsentBannerActionsProps): ReactNode;
|
|
63
|
+
/**
|
|
64
|
+
* @since 0.5.0-canary.5
|
|
65
|
+
*/
|
|
66
|
+
interface ConsentBannerAcceptProps extends Omit<ComponentProps<"button">, "type"> {}
|
|
67
|
+
/**
|
|
68
|
+
* Grants the categories the app requested (`useConsent`'s `categories`) — never more.
|
|
69
|
+
*
|
|
70
|
+
* @since 0.5.0-canary.5
|
|
71
|
+
*/
|
|
72
|
+
declare function ConsentBannerAccept({
|
|
73
|
+
onClick,
|
|
74
|
+
...props
|
|
75
|
+
}: ConsentBannerAcceptProps): ReactNode;
|
|
76
|
+
/**
|
|
77
|
+
* @since 0.5.0-canary.5
|
|
78
|
+
*/
|
|
79
|
+
interface ConsentBannerRejectProps extends Omit<ComponentProps<"button">, "type"> {}
|
|
80
|
+
/**
|
|
81
|
+
* @since 0.5.0-canary.5
|
|
82
|
+
*/
|
|
83
|
+
declare function ConsentBannerReject({
|
|
84
|
+
onClick,
|
|
85
|
+
...props
|
|
86
|
+
}: ConsentBannerRejectProps): ReactNode;
|
|
87
|
+
/**
|
|
88
|
+
* @since 0.5.0-canary.5
|
|
89
|
+
*/
|
|
90
|
+
interface ConsentBannerCustomizeProps extends Omit<ComponentProps<"button">, "type"> {}
|
|
91
|
+
/**
|
|
92
|
+
* Opens the per-category preferences layer, starting from the current effective state so
|
|
93
|
+
* nothing arrives pre-ticked in an opt-in region (pre-ticked consent is invalid under
|
|
94
|
+
* GDPR). Hidden while the layer is open — `ConsentBannerSave` takes over.
|
|
95
|
+
*
|
|
96
|
+
* @since 0.5.0-canary.5
|
|
97
|
+
*/
|
|
98
|
+
declare function ConsentBannerCustomize({
|
|
99
|
+
onClick,
|
|
100
|
+
...props
|
|
101
|
+
}: ConsentBannerCustomizeProps): ReactNode;
|
|
102
|
+
/**
|
|
103
|
+
* @since 0.5.0-canary.5
|
|
104
|
+
*/
|
|
105
|
+
interface ConsentBannerPreferencesProps extends ComponentProps<"div"> {}
|
|
106
|
+
/**
|
|
107
|
+
* Container for the per-category rows — renders only while the preferences layer is open.
|
|
108
|
+
*
|
|
109
|
+
* @since 0.5.0-canary.5
|
|
110
|
+
*/
|
|
111
|
+
declare function ConsentBannerPreferences(props: ConsentBannerPreferencesProps): ReactNode;
|
|
112
|
+
/**
|
|
113
|
+
* @since 0.5.0-canary.5
|
|
114
|
+
*/
|
|
115
|
+
interface ConsentBannerCategoryProps extends ComponentProps<"label"> {
|
|
116
|
+
category: ConsentCategory;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* One checkbox row of the preferences layer — `children` is the visitor-facing wording.
|
|
120
|
+
*
|
|
121
|
+
* @since 0.5.0-canary.5
|
|
122
|
+
*/
|
|
123
|
+
declare function ConsentBannerCategory({
|
|
124
|
+
category,
|
|
125
|
+
children,
|
|
126
|
+
...props
|
|
127
|
+
}: ConsentBannerCategoryProps): ReactNode;
|
|
128
|
+
/**
|
|
129
|
+
* @since 0.5.0-canary.5
|
|
130
|
+
*/
|
|
131
|
+
interface ConsentBannerSaveProps extends Omit<ComponentProps<"button">, "type"> {}
|
|
132
|
+
/**
|
|
133
|
+
* Persists the pending per-category selection — renders only while the preferences layer is open.
|
|
134
|
+
*
|
|
135
|
+
* @since 0.5.0-canary.5
|
|
136
|
+
*/
|
|
137
|
+
declare function ConsentBannerSave({
|
|
138
|
+
onClick,
|
|
139
|
+
...props
|
|
140
|
+
}: ConsentBannerSaveProps): ReactNode;
|
|
53
141
|
/**
|
|
54
142
|
* @since 0.5.0-canary.4
|
|
55
143
|
*/
|
|
@@ -72,4 +160,4 @@ declare function ConsentToggle({
|
|
|
72
160
|
...props
|
|
73
161
|
}: ConsentToggleProps): ReactNode;
|
|
74
162
|
//#endregion
|
|
75
|
-
export { ConsentBanner, ConsentBannerProps,
|
|
163
|
+
export { ConsentBanner, ConsentBannerAccept, ConsentBannerAcceptProps, ConsentBannerActions, ConsentBannerActionsProps, ConsentBannerCategory, ConsentBannerCategoryProps, ConsentBannerCustomize, ConsentBannerCustomizeProps, ConsentBannerDescription, ConsentBannerDescriptionProps, ConsentBannerPreferences, ConsentBannerPreferencesProps, ConsentBannerProps, ConsentBannerReject, ConsentBannerRejectProps, ConsentBannerSave, ConsentBannerSaveProps, ConsentBannerTitle, ConsentBannerTitleProps, ConsentToggle, ConsentToggleProps };
|
|
@@ -1,96 +1,191 @@
|
|
|
1
1
|
import { CONSENT_CATEGORIES } from "../core/consent.mjs";
|
|
2
|
-
import { useState } from "react";
|
|
3
|
-
import {
|
|
2
|
+
import { createContext, useContext, useEffect, useState } from "react";
|
|
3
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
4
|
//#region src/react/consent-banner.tsx
|
|
5
|
+
const ConsentBannerContext = createContext(null);
|
|
6
|
+
function useConsentBannerContext(part) {
|
|
7
|
+
const context = useContext(ConsentBannerContext);
|
|
8
|
+
if (context === null) throw new Error(`\`${part}\` must be rendered inside \`ConsentBanner\``);
|
|
9
|
+
return context;
|
|
10
|
+
}
|
|
5
11
|
/**
|
|
6
|
-
* Non-blocking opt-in prompt for GDPR/NĐ 13 regions
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* region, not a `<dialog>`: an open non-modal dialog
|
|
12
|
-
* page, so the dialog semantics would over-promise.
|
|
13
|
-
*
|
|
14
|
-
*
|
|
12
|
+
* Non-blocking opt-in prompt for GDPR/NĐ 13 regions, as composable parts — the root owns
|
|
13
|
+
* the visibility gating and the preferences-layer state; the `Accept`/`Reject`/`Customize`/
|
|
14
|
+
* `Save` parts wire their own clicks to the consent hook, so any markup (including a
|
|
15
|
+
* design system's button styles via `className`) slots in. Renders nothing once a decision
|
|
16
|
+
* exists or the region defaults to opt-out (`consent.needsPrompt`), unless `open` says
|
|
17
|
+
* otherwise. Rendered as a labeled region, not a `<dialog>`: an open non-modal dialog
|
|
18
|
+
* neither traps focus nor blocks the page, so the dialog semantics would over-promise.
|
|
19
|
+
* Unstyled by default — style via `className`/`data-slot` selectors, or import the
|
|
20
|
+
* optional `@codefast/tracking/css/consent.css` theme. `data-state` on the root flips
|
|
21
|
+
* between `prompt` and `preferences` for state-dependent styling.
|
|
15
22
|
*
|
|
16
23
|
* @since 0.5.0-canary.4
|
|
17
24
|
*/
|
|
18
|
-
function ConsentBanner({
|
|
25
|
+
function ConsentBanner({ children, consent, open, ...props }) {
|
|
19
26
|
const [pending, setPending] = useState(void 0);
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
27
|
+
const isVisible = open ?? consent.needsPrompt;
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (!isVisible) setPending(void 0);
|
|
30
|
+
}, [isVisible]);
|
|
31
|
+
if (!isVisible) return null;
|
|
32
|
+
return /* @__PURE__ */ jsx(ConsentBannerContext.Provider, {
|
|
33
|
+
value: {
|
|
34
|
+
consent,
|
|
35
|
+
pending,
|
|
36
|
+
setPending
|
|
37
|
+
},
|
|
38
|
+
children: /* @__PURE__ */ jsx("section", {
|
|
39
|
+
"aria-label": "Cookie consent",
|
|
40
|
+
"data-slot": "consent-banner",
|
|
41
|
+
"data-state": pending === void 0 ? "prompt" : "preferences",
|
|
42
|
+
...props,
|
|
43
|
+
children
|
|
44
|
+
})
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* @since 0.5.0-canary.5
|
|
49
|
+
*/
|
|
50
|
+
function ConsentBannerTitle({ children, ...props }) {
|
|
51
|
+
return /* @__PURE__ */ jsx("h2", {
|
|
52
|
+
"data-slot": "consent-title",
|
|
53
|
+
...props,
|
|
54
|
+
children
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Hosts the message and the privacy-policy link informed consent requires.
|
|
59
|
+
*
|
|
60
|
+
* @since 0.5.0-canary.5
|
|
61
|
+
*/
|
|
62
|
+
function ConsentBannerDescription(props) {
|
|
63
|
+
return /* @__PURE__ */ jsx("p", {
|
|
64
|
+
"data-slot": "consent-description",
|
|
65
|
+
...props
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* @since 0.5.0-canary.5
|
|
70
|
+
*/
|
|
71
|
+
function ConsentBannerActions(props) {
|
|
72
|
+
return /* @__PURE__ */ jsx("div", {
|
|
73
|
+
"data-slot": "consent-actions",
|
|
74
|
+
...props
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Grants the categories the app requested (`useConsent`'s `categories`) — never more.
|
|
79
|
+
*
|
|
80
|
+
* @since 0.5.0-canary.5
|
|
81
|
+
*/
|
|
82
|
+
function ConsentBannerAccept({ onClick, ...props }) {
|
|
83
|
+
const { consent, setPending } = useConsentBannerContext("ConsentBannerAccept");
|
|
84
|
+
return /* @__PURE__ */ jsx("button", {
|
|
85
|
+
"data-action": "accept",
|
|
86
|
+
"data-slot": "consent-action",
|
|
87
|
+
...props,
|
|
88
|
+
onClick: (event) => {
|
|
89
|
+
onClick?.(event);
|
|
90
|
+
consent.grantAll();
|
|
91
|
+
setPending(void 0);
|
|
92
|
+
},
|
|
93
|
+
type: "button"
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* @since 0.5.0-canary.5
|
|
98
|
+
*/
|
|
99
|
+
function ConsentBannerReject({ onClick, ...props }) {
|
|
100
|
+
const { consent, setPending } = useConsentBannerContext("ConsentBannerReject");
|
|
101
|
+
return /* @__PURE__ */ jsx("button", {
|
|
102
|
+
"data-action": "reject",
|
|
103
|
+
"data-slot": "consent-action",
|
|
104
|
+
...props,
|
|
105
|
+
onClick: (event) => {
|
|
106
|
+
onClick?.(event);
|
|
107
|
+
consent.denyAll();
|
|
108
|
+
setPending(void 0);
|
|
109
|
+
},
|
|
110
|
+
type: "button"
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Opens the per-category preferences layer, starting from the current effective state so
|
|
115
|
+
* nothing arrives pre-ticked in an opt-in region (pre-ticked consent is invalid under
|
|
116
|
+
* GDPR). Hidden while the layer is open — `ConsentBannerSave` takes over.
|
|
117
|
+
*
|
|
118
|
+
* @since 0.5.0-canary.5
|
|
119
|
+
*/
|
|
120
|
+
function ConsentBannerCustomize({ onClick, ...props }) {
|
|
121
|
+
const { consent, pending, setPending } = useConsentBannerContext("ConsentBannerCustomize");
|
|
122
|
+
if (pending !== void 0) return null;
|
|
123
|
+
return /* @__PURE__ */ jsx("button", {
|
|
124
|
+
"data-action": "customize",
|
|
125
|
+
"data-slot": "consent-action",
|
|
126
|
+
...props,
|
|
127
|
+
onClick: (event) => {
|
|
128
|
+
onClick?.(event);
|
|
129
|
+
setPending(consent.effectiveConsent);
|
|
130
|
+
},
|
|
131
|
+
type: "button"
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Container for the per-category rows — renders only while the preferences layer is open.
|
|
136
|
+
*
|
|
137
|
+
* @since 0.5.0-canary.5
|
|
138
|
+
*/
|
|
139
|
+
function ConsentBannerPreferences(props) {
|
|
140
|
+
const { pending } = useConsentBannerContext("ConsentBannerPreferences");
|
|
141
|
+
if (pending === void 0) return null;
|
|
142
|
+
return /* @__PURE__ */ jsx("div", {
|
|
143
|
+
"data-slot": "consent-preferences",
|
|
144
|
+
...props
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* One checkbox row of the preferences layer — `children` is the visitor-facing wording.
|
|
149
|
+
*
|
|
150
|
+
* @since 0.5.0-canary.5
|
|
151
|
+
*/
|
|
152
|
+
function ConsentBannerCategory({ category, children, ...props }) {
|
|
153
|
+
const { pending, setPending } = useConsentBannerContext("ConsentBannerCategory");
|
|
154
|
+
if (pending === void 0) return null;
|
|
155
|
+
return /* @__PURE__ */ jsxs("label", {
|
|
156
|
+
"data-slot": "consent-category",
|
|
157
|
+
...props,
|
|
158
|
+
children: [/* @__PURE__ */ jsx("input", {
|
|
159
|
+
checked: pending[category],
|
|
160
|
+
"data-slot": "consent-category-checkbox",
|
|
161
|
+
onChange: (event) => {
|
|
162
|
+
setPending({
|
|
163
|
+
...pending,
|
|
164
|
+
[category]: event.target.checked
|
|
165
|
+
});
|
|
166
|
+
},
|
|
167
|
+
type: "checkbox"
|
|
168
|
+
}), children]
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Persists the pending per-category selection — renders only while the preferences layer is open.
|
|
173
|
+
*
|
|
174
|
+
* @since 0.5.0-canary.5
|
|
175
|
+
*/
|
|
176
|
+
function ConsentBannerSave({ onClick, ...props }) {
|
|
177
|
+
const { consent, pending, setPending } = useConsentBannerContext("ConsentBannerSave");
|
|
178
|
+
if (pending === void 0) return null;
|
|
179
|
+
return /* @__PURE__ */ jsx("button", {
|
|
180
|
+
"data-action": "save",
|
|
181
|
+
"data-slot": "consent-action",
|
|
24
182
|
...props,
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
"data-slot": "consent-preferences",
|
|
32
|
-
children: categories.map((option) => /* @__PURE__ */ jsxs("label", {
|
|
33
|
-
"data-slot": "consent-category",
|
|
34
|
-
children: [
|
|
35
|
-
/* @__PURE__ */ jsx("input", {
|
|
36
|
-
checked: pending[option.category],
|
|
37
|
-
"data-slot": "consent-category-checkbox",
|
|
38
|
-
onChange: (event) => {
|
|
39
|
-
setPending({
|
|
40
|
-
...pending,
|
|
41
|
-
[option.category]: event.target.checked
|
|
42
|
-
});
|
|
43
|
-
},
|
|
44
|
-
type: "checkbox"
|
|
45
|
-
}),
|
|
46
|
-
/* @__PURE__ */ jsx("span", {
|
|
47
|
-
"data-slot": "consent-category-label",
|
|
48
|
-
children: option.label
|
|
49
|
-
}),
|
|
50
|
-
option.description === void 0 ? null : /* @__PURE__ */ jsx("span", {
|
|
51
|
-
"data-slot": "consent-category-description",
|
|
52
|
-
children: option.description
|
|
53
|
-
})
|
|
54
|
-
]
|
|
55
|
-
}, option.category))
|
|
56
|
-
}) : null,
|
|
57
|
-
/* @__PURE__ */ jsx("div", {
|
|
58
|
-
"data-slot": "consent-actions",
|
|
59
|
-
children: pending === void 0 ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
60
|
-
/* @__PURE__ */ jsx("button", {
|
|
61
|
-
"data-action": "accept",
|
|
62
|
-
"data-slot": "consent-action",
|
|
63
|
-
onClick: consent.grantAll,
|
|
64
|
-
type: "button",
|
|
65
|
-
children: acceptLabel
|
|
66
|
-
}),
|
|
67
|
-
/* @__PURE__ */ jsx("button", {
|
|
68
|
-
"data-action": "reject",
|
|
69
|
-
"data-slot": "consent-action",
|
|
70
|
-
onClick: consent.denyAll,
|
|
71
|
-
type: "button",
|
|
72
|
-
children: rejectLabel
|
|
73
|
-
}),
|
|
74
|
-
categories !== void 0 && categories.length > 0 ? /* @__PURE__ */ jsx("button", {
|
|
75
|
-
"data-action": "customize",
|
|
76
|
-
"data-slot": "consent-action",
|
|
77
|
-
onClick: () => {
|
|
78
|
-
setPending(consent.effectiveConsent);
|
|
79
|
-
},
|
|
80
|
-
type: "button",
|
|
81
|
-
children: customizeLabel
|
|
82
|
-
}) : null
|
|
83
|
-
] }) : /* @__PURE__ */ jsx("button", {
|
|
84
|
-
"data-action": "save",
|
|
85
|
-
"data-slot": "consent-action",
|
|
86
|
-
onClick: () => {
|
|
87
|
-
consent.save(pending);
|
|
88
|
-
},
|
|
89
|
-
type: "button",
|
|
90
|
-
children: saveLabel
|
|
91
|
-
})
|
|
92
|
-
})
|
|
93
|
-
]
|
|
183
|
+
onClick: (event) => {
|
|
184
|
+
onClick?.(event);
|
|
185
|
+
consent.save(pending);
|
|
186
|
+
setPending(void 0);
|
|
187
|
+
},
|
|
188
|
+
type: "button"
|
|
94
189
|
});
|
|
95
190
|
}
|
|
96
191
|
/**
|
|
@@ -111,4 +206,4 @@ function ConsentToggle({ allowLabel = "Allow tracking", consent, denyLabel = "Do
|
|
|
111
206
|
});
|
|
112
207
|
}
|
|
113
208
|
//#endregion
|
|
114
|
-
export { ConsentBanner, ConsentToggle };
|
|
209
|
+
export { ConsentBanner, ConsentBannerAccept, ConsentBannerActions, ConsentBannerCategory, ConsentBannerCustomize, ConsentBannerDescription, ConsentBannerPreferences, ConsentBannerReject, ConsentBannerSave, ConsentBannerTitle, ConsentToggle };
|
package/dist/react/index.d.mts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { UseConsentOptions, UseConsentResult, useConsent } from "./use-consent.mjs";
|
|
2
|
-
import { ConsentBanner, ConsentBannerProps,
|
|
3
|
-
export { ConsentBanner, type ConsentBannerProps, type
|
|
2
|
+
import { ConsentBanner, ConsentBannerAccept, ConsentBannerAcceptProps, ConsentBannerActions, ConsentBannerActionsProps, ConsentBannerCategory, ConsentBannerCategoryProps, ConsentBannerCustomize, ConsentBannerCustomizeProps, ConsentBannerDescription, ConsentBannerDescriptionProps, ConsentBannerPreferences, ConsentBannerPreferencesProps, ConsentBannerProps, ConsentBannerReject, ConsentBannerRejectProps, ConsentBannerSave, ConsentBannerSaveProps, ConsentBannerTitle, ConsentBannerTitleProps, ConsentToggle, ConsentToggleProps } from "./consent-banner.mjs";
|
|
3
|
+
export { ConsentBanner, ConsentBannerAccept, type ConsentBannerAcceptProps, ConsentBannerActions, type ConsentBannerActionsProps, ConsentBannerCategory, type ConsentBannerCategoryProps, ConsentBannerCustomize, type ConsentBannerCustomizeProps, ConsentBannerDescription, type ConsentBannerDescriptionProps, ConsentBannerPreferences, type ConsentBannerPreferencesProps, type ConsentBannerProps, ConsentBannerReject, type ConsentBannerRejectProps, ConsentBannerSave, type ConsentBannerSaveProps, ConsentBannerTitle, type ConsentBannerTitleProps, ConsentToggle, type ConsentToggleProps, type UseConsentOptions, type UseConsentResult, useConsent };
|
package/dist/react/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { ConsentBanner, ConsentToggle } from "./consent-banner.mjs";
|
|
1
|
+
import { ConsentBanner, ConsentBannerAccept, ConsentBannerActions, ConsentBannerCategory, ConsentBannerCustomize, ConsentBannerDescription, ConsentBannerPreferences, ConsentBannerReject, ConsentBannerSave, ConsentBannerTitle, ConsentToggle } from "./consent-banner.mjs";
|
|
2
2
|
import { useConsent } from "./use-consent.mjs";
|
|
3
|
-
export { ConsentBanner, ConsentToggle, useConsent };
|
|
3
|
+
export { ConsentBanner, ConsentBannerAccept, ConsentBannerActions, ConsentBannerCategory, ConsentBannerCustomize, ConsentBannerDescription, ConsentBannerPreferences, ConsentBannerReject, ConsentBannerSave, ConsentBannerTitle, ConsentToggle, useConsent };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codefast/tracking",
|
|
3
|
-
"version": "0.5.0-canary.
|
|
3
|
+
"version": "0.5.0-canary.5",
|
|
4
4
|
"description": "Fullstack, type-safe event tracking for TanStack Start — isomorphic client/server trackers over a per-app Zod event catalog",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"analytics",
|
|
@@ -181,6 +181,7 @@
|
|
|
181
181
|
"types": "./dist/server/region.d.mts",
|
|
182
182
|
"import": "./dist/server/region.mjs"
|
|
183
183
|
},
|
|
184
|
+
"./css/*": "./src/css/*",
|
|
184
185
|
"./package.json": "./package.json"
|
|
185
186
|
},
|
|
186
187
|
"publishConfig": {
|
|
@@ -208,7 +209,7 @@
|
|
|
208
209
|
"tsdown": "^0.22.3",
|
|
209
210
|
"typescript": "^6.0.3",
|
|
210
211
|
"vitest": "^4.1.9",
|
|
211
|
-
"@codefast/typescript-config": "0.5.0-canary.
|
|
212
|
+
"@codefast/typescript-config": "0.5.0-canary.5"
|
|
212
213
|
},
|
|
213
214
|
"peerDependencies": {
|
|
214
215
|
"@types/react": ">=19.0.0",
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/* -------------------------------------------------------------------------
|
|
2
|
+
* Optional consent UI theme
|
|
3
|
+
*
|
|
4
|
+
* Plain CSS over the components' data-slot attributes — no Tailwind, no
|
|
5
|
+
* design-system dependency. Import it for a sensible default look:
|
|
6
|
+
*
|
|
7
|
+
* @import "@codefast/tracking/css/consent.css";
|
|
8
|
+
*
|
|
9
|
+
* Every color routes through a `--consent-*` custom property with a
|
|
10
|
+
* `light-dark()` fallback, so a host app can retheme by defining those
|
|
11
|
+
* properties (or skip this file entirely and style the slots itself).
|
|
12
|
+
* ------------------------------------------------------------------------- */
|
|
13
|
+
|
|
14
|
+
[data-slot="consent-banner"] {
|
|
15
|
+
position: fixed;
|
|
16
|
+
inset-inline: 1rem;
|
|
17
|
+
bottom: 1rem;
|
|
18
|
+
z-index: 50;
|
|
19
|
+
margin-inline: auto;
|
|
20
|
+
max-width: 24rem;
|
|
21
|
+
padding: 1.25rem;
|
|
22
|
+
border: 1px solid var(--consent-border, light-dark(rgb(0 0 0 / 12%), rgb(255 255 255 / 14%)));
|
|
23
|
+
border-radius: 1rem;
|
|
24
|
+
background: var(--consent-surface, light-dark(#fff, #1c1c1e));
|
|
25
|
+
color: var(--consent-fg, light-dark(#1c1c1e, #f2f2f7));
|
|
26
|
+
box-shadow: 0 12px 32px rgb(0 0 0 / 18%);
|
|
27
|
+
font-size: 0.875rem;
|
|
28
|
+
line-height: 1.5;
|
|
29
|
+
animation: consent-banner-in 220ms ease-out;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
@media (min-width: 640px) {
|
|
33
|
+
[data-slot="consent-banner"] {
|
|
34
|
+
inset-inline: auto 1rem;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@keyframes consent-banner-in {
|
|
39
|
+
from {
|
|
40
|
+
opacity: 0;
|
|
41
|
+
transform: translateY(1rem);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
@media (prefers-reduced-motion: reduce) {
|
|
46
|
+
[data-slot="consent-banner"] {
|
|
47
|
+
animation: none;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
[data-slot="consent-title"] {
|
|
52
|
+
margin: 0 0 0.375rem;
|
|
53
|
+
font-size: 0.875rem;
|
|
54
|
+
font-weight: 600;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
[data-slot="consent-description"] {
|
|
58
|
+
margin: 0 0 1rem;
|
|
59
|
+
color: var(--consent-muted, light-dark(rgb(28 28 30 / 65%), rgb(242 242 247 / 65%)));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
[data-slot="consent-description"] a {
|
|
63
|
+
color: inherit;
|
|
64
|
+
text-decoration: underline;
|
|
65
|
+
text-underline-offset: 0.2em;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
[data-slot="consent-preferences"] {
|
|
69
|
+
display: flex;
|
|
70
|
+
flex-direction: column;
|
|
71
|
+
gap: 0.5rem;
|
|
72
|
+
margin-bottom: 1rem;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
[data-slot="consent-category"] {
|
|
76
|
+
display: flex;
|
|
77
|
+
gap: 0.5rem;
|
|
78
|
+
align-items: baseline;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
[data-slot="consent-actions"] {
|
|
82
|
+
display: flex;
|
|
83
|
+
gap: 0.5rem;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
[data-slot="consent-action"] {
|
|
87
|
+
flex: 1;
|
|
88
|
+
padding: 0.5rem 0.75rem;
|
|
89
|
+
border: 1px solid var(--consent-border, light-dark(rgb(0 0 0 / 12%), rgb(255 255 255 / 14%)));
|
|
90
|
+
border-radius: 0.5rem;
|
|
91
|
+
background: transparent;
|
|
92
|
+
color: inherit;
|
|
93
|
+
font: inherit;
|
|
94
|
+
font-weight: 500;
|
|
95
|
+
cursor: pointer;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/* The affirmative actions carry the accent; Reject stays equally prominent, just outlined. */
|
|
99
|
+
[data-slot="consent-action"][data-action="accept"],
|
|
100
|
+
[data-slot="consent-action"][data-action="save"] {
|
|
101
|
+
border-color: transparent;
|
|
102
|
+
background: var(--consent-accent, light-dark(#1c1c1e, #f2f2f7));
|
|
103
|
+
color: var(--consent-accent-fg, light-dark(#fff, #1c1c1e));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
[data-slot="consent-toggle"] {
|
|
107
|
+
position: fixed;
|
|
108
|
+
inset-inline-start: 1rem;
|
|
109
|
+
bottom: 1rem;
|
|
110
|
+
z-index: 50;
|
|
111
|
+
padding: 0.375rem 0.75rem;
|
|
112
|
+
border: 1px solid var(--consent-border, light-dark(rgb(0 0 0 / 12%), rgb(255 255 255 / 14%)));
|
|
113
|
+
border-radius: 9999px;
|
|
114
|
+
background: var(--consent-surface, light-dark(#fff, #1c1c1e));
|
|
115
|
+
color: var(--consent-muted, light-dark(rgb(28 28 30 / 65%), rgb(242 242 247 / 65%)));
|
|
116
|
+
font: inherit;
|
|
117
|
+
font-size: 0.75rem;
|
|
118
|
+
cursor: pointer;
|
|
119
|
+
}
|
|
@@ -1,127 +1,298 @@
|
|
|
1
|
-
import type { ComponentProps, ReactNode } from "react";
|
|
2
|
-
import { useState } from "react";
|
|
1
|
+
import type { ComponentProps, Dispatch, ReactNode, SetStateAction } from "react";
|
|
2
|
+
import { createContext, useContext, useEffect, useState } from "react";
|
|
3
3
|
|
|
4
4
|
import type { ConsentCategory, ConsentDecision } from "#/core/consent";
|
|
5
5
|
import { CONSENT_CATEGORIES } from "#/core/consent";
|
|
6
6
|
import type { UseConsentResult } from "#/react/use-consent";
|
|
7
7
|
|
|
8
|
+
interface ConsentBannerContextValue {
|
|
9
|
+
consent: UseConsentResult;
|
|
10
|
+
/** The in-progress per-category selection — `undefined` while the preferences layer is closed. */
|
|
11
|
+
pending: ConsentDecision | undefined;
|
|
12
|
+
setPending: Dispatch<SetStateAction<ConsentDecision | undefined>>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const ConsentBannerContext = createContext<ConsentBannerContextValue | null>(null);
|
|
16
|
+
|
|
17
|
+
function useConsentBannerContext(part: string): ConsentBannerContextValue {
|
|
18
|
+
const context = useContext(ConsentBannerContext);
|
|
19
|
+
|
|
20
|
+
if (context === null) {
|
|
21
|
+
throw new Error(`\`${part}\` must be rendered inside \`ConsentBanner\``);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return context;
|
|
25
|
+
}
|
|
26
|
+
|
|
8
27
|
/**
|
|
9
|
-
*
|
|
28
|
+
* @since 0.5.0-canary.4
|
|
29
|
+
*/
|
|
30
|
+
export interface ConsentBannerProps extends ComponentProps<"section"> {
|
|
31
|
+
consent: UseConsentResult;
|
|
32
|
+
/** Overrides the default `needsPrompt` gating — e.g. to reopen the banner as a settings panel. */
|
|
33
|
+
open?: boolean | undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Non-blocking opt-in prompt for GDPR/NĐ 13 regions, as composable parts — the root owns
|
|
38
|
+
* the visibility gating and the preferences-layer state; the `Accept`/`Reject`/`Customize`/
|
|
39
|
+
* `Save` parts wire their own clicks to the consent hook, so any markup (including a
|
|
40
|
+
* design system's button styles via `className`) slots in. Renders nothing once a decision
|
|
41
|
+
* exists or the region defaults to opt-out (`consent.needsPrompt`), unless `open` says
|
|
42
|
+
* otherwise. Rendered as a labeled region, not a `<dialog>`: an open non-modal dialog
|
|
43
|
+
* neither traps focus nor blocks the page, so the dialog semantics would over-promise.
|
|
44
|
+
* Unstyled by default — style via `className`/`data-slot` selectors, or import the
|
|
45
|
+
* optional `@codefast/tracking/css/consent.css` theme. `data-state` on the root flips
|
|
46
|
+
* between `prompt` and `preferences` for state-dependent styling.
|
|
10
47
|
*
|
|
11
48
|
* @since 0.5.0-canary.4
|
|
12
49
|
*/
|
|
13
|
-
export
|
|
50
|
+
export function ConsentBanner({ children, consent, open, ...props }: ConsentBannerProps): ReactNode {
|
|
51
|
+
const [pending, setPending] = useState<ConsentDecision | undefined>(undefined);
|
|
52
|
+
|
|
53
|
+
const isVisible = open ?? consent.needsPrompt;
|
|
54
|
+
|
|
55
|
+
// Closing must reset the layer — a reopened banner starts at the prompt, not mid-preferences.
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
if (!isVisible) {
|
|
58
|
+
setPending(undefined);
|
|
59
|
+
}
|
|
60
|
+
}, [isVisible]);
|
|
61
|
+
|
|
62
|
+
if (!isVisible) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<ConsentBannerContext.Provider value={{ consent, pending, setPending }}>
|
|
68
|
+
<section
|
|
69
|
+
aria-label="Cookie consent"
|
|
70
|
+
data-slot="consent-banner"
|
|
71
|
+
data-state={pending === undefined ? "prompt" : "preferences"}
|
|
72
|
+
{...props}
|
|
73
|
+
>
|
|
74
|
+
{children}
|
|
75
|
+
</section>
|
|
76
|
+
</ConsentBannerContext.Provider>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @since 0.5.0-canary.5
|
|
82
|
+
*/
|
|
83
|
+
export interface ConsentBannerTitleProps extends ComponentProps<"h2"> {}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @since 0.5.0-canary.5
|
|
87
|
+
*/
|
|
88
|
+
export function ConsentBannerTitle({ children, ...props }: ConsentBannerTitleProps): ReactNode {
|
|
89
|
+
return (
|
|
90
|
+
<h2 data-slot="consent-title" {...props}>
|
|
91
|
+
{children}
|
|
92
|
+
</h2>
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @since 0.5.0-canary.5
|
|
98
|
+
*/
|
|
99
|
+
export interface ConsentBannerDescriptionProps extends ComponentProps<"p"> {}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Hosts the message and the privacy-policy link informed consent requires.
|
|
103
|
+
*
|
|
104
|
+
* @since 0.5.0-canary.5
|
|
105
|
+
*/
|
|
106
|
+
export function ConsentBannerDescription(props: ConsentBannerDescriptionProps): ReactNode {
|
|
107
|
+
return <p data-slot="consent-description" {...props} />;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* @since 0.5.0-canary.5
|
|
112
|
+
*/
|
|
113
|
+
export interface ConsentBannerActionsProps extends ComponentProps<"div"> {}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* @since 0.5.0-canary.5
|
|
117
|
+
*/
|
|
118
|
+
export function ConsentBannerActions(props: ConsentBannerActionsProps): ReactNode {
|
|
119
|
+
return <div data-slot="consent-actions" {...props} />;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* @since 0.5.0-canary.5
|
|
124
|
+
*/
|
|
125
|
+
export interface ConsentBannerAcceptProps extends Omit<ComponentProps<"button">, "type"> {}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Grants the categories the app requested (`useConsent`'s `categories`) — never more.
|
|
129
|
+
*
|
|
130
|
+
* @since 0.5.0-canary.5
|
|
131
|
+
*/
|
|
132
|
+
export function ConsentBannerAccept({ onClick, ...props }: ConsentBannerAcceptProps): ReactNode {
|
|
133
|
+
const { consent, setPending } = useConsentBannerContext("ConsentBannerAccept");
|
|
134
|
+
|
|
135
|
+
return (
|
|
136
|
+
<button
|
|
137
|
+
data-action="accept"
|
|
138
|
+
data-slot="consent-action"
|
|
139
|
+
{...props}
|
|
140
|
+
// the part owns the decision — composed so a consumer onClick (e.g. closing a settings view) still runs
|
|
141
|
+
onClick={(event) => {
|
|
142
|
+
onClick?.(event);
|
|
143
|
+
consent.grantAll();
|
|
144
|
+
setPending(undefined);
|
|
145
|
+
}}
|
|
146
|
+
type="button"
|
|
147
|
+
/>
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* @since 0.5.0-canary.5
|
|
153
|
+
*/
|
|
154
|
+
export interface ConsentBannerRejectProps extends Omit<ComponentProps<"button">, "type"> {}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @since 0.5.0-canary.5
|
|
158
|
+
*/
|
|
159
|
+
export function ConsentBannerReject({ onClick, ...props }: ConsentBannerRejectProps): ReactNode {
|
|
160
|
+
const { consent, setPending } = useConsentBannerContext("ConsentBannerReject");
|
|
161
|
+
|
|
162
|
+
return (
|
|
163
|
+
<button
|
|
164
|
+
data-action="reject"
|
|
165
|
+
data-slot="consent-action"
|
|
166
|
+
{...props}
|
|
167
|
+
// the part owns the decision — composed so a consumer onClick (e.g. closing a settings view) still runs
|
|
168
|
+
onClick={(event) => {
|
|
169
|
+
onClick?.(event);
|
|
170
|
+
consent.denyAll();
|
|
171
|
+
setPending(undefined);
|
|
172
|
+
}}
|
|
173
|
+
type="button"
|
|
174
|
+
/>
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* @since 0.5.0-canary.5
|
|
180
|
+
*/
|
|
181
|
+
export interface ConsentBannerCustomizeProps extends Omit<ComponentProps<"button">, "type"> {}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Opens the per-category preferences layer, starting from the current effective state so
|
|
185
|
+
* nothing arrives pre-ticked in an opt-in region (pre-ticked consent is invalid under
|
|
186
|
+
* GDPR). Hidden while the layer is open — `ConsentBannerSave` takes over.
|
|
187
|
+
*
|
|
188
|
+
* @since 0.5.0-canary.5
|
|
189
|
+
*/
|
|
190
|
+
export function ConsentBannerCustomize({ onClick, ...props }: ConsentBannerCustomizeProps): ReactNode {
|
|
191
|
+
const { consent, pending, setPending } = useConsentBannerContext("ConsentBannerCustomize");
|
|
192
|
+
|
|
193
|
+
if (pending !== undefined) {
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return (
|
|
198
|
+
<button
|
|
199
|
+
data-action="customize"
|
|
200
|
+
data-slot="consent-action"
|
|
201
|
+
{...props}
|
|
202
|
+
// the part owns the layer toggle — composed so a consumer onClick still runs
|
|
203
|
+
onClick={(event) => {
|
|
204
|
+
onClick?.(event);
|
|
205
|
+
setPending(consent.effectiveConsent);
|
|
206
|
+
}}
|
|
207
|
+
type="button"
|
|
208
|
+
/>
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* @since 0.5.0-canary.5
|
|
214
|
+
*/
|
|
215
|
+
export interface ConsentBannerPreferencesProps extends ComponentProps<"div"> {}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Container for the per-category rows — renders only while the preferences layer is open.
|
|
219
|
+
*
|
|
220
|
+
* @since 0.5.0-canary.5
|
|
221
|
+
*/
|
|
222
|
+
export function ConsentBannerPreferences(props: ConsentBannerPreferencesProps): ReactNode {
|
|
223
|
+
const { pending } = useConsentBannerContext("ConsentBannerPreferences");
|
|
224
|
+
|
|
225
|
+
if (pending === undefined) {
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return <div data-slot="consent-preferences" {...props} />;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* @since 0.5.0-canary.5
|
|
234
|
+
*/
|
|
235
|
+
export interface ConsentBannerCategoryProps extends ComponentProps<"label"> {
|
|
14
236
|
category: ConsentCategory;
|
|
15
|
-
description?: string;
|
|
16
|
-
label: string;
|
|
17
237
|
}
|
|
18
238
|
|
|
19
239
|
/**
|
|
20
|
-
*
|
|
240
|
+
* One checkbox row of the preferences layer — `children` is the visitor-facing wording.
|
|
241
|
+
*
|
|
242
|
+
* @since 0.5.0-canary.5
|
|
21
243
|
*/
|
|
22
|
-
export
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
244
|
+
export function ConsentBannerCategory({ category, children, ...props }: ConsentBannerCategoryProps): ReactNode {
|
|
245
|
+
const { pending, setPending } = useConsentBannerContext("ConsentBannerCategory");
|
|
246
|
+
|
|
247
|
+
if (pending === undefined) {
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return (
|
|
252
|
+
<label data-slot="consent-category" {...props}>
|
|
253
|
+
<input
|
|
254
|
+
checked={pending[category]}
|
|
255
|
+
data-slot="consent-category-checkbox"
|
|
256
|
+
onChange={(event) => {
|
|
257
|
+
setPending({ ...pending, [category]: event.target.checked });
|
|
258
|
+
}}
|
|
259
|
+
type="checkbox"
|
|
260
|
+
/>
|
|
261
|
+
{children}
|
|
262
|
+
</label>
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* @since 0.5.0-canary.5
|
|
268
|
+
*/
|
|
269
|
+
export interface ConsentBannerSaveProps extends Omit<ComponentProps<"button">, "type"> {}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Persists the pending per-category selection — renders only while the preferences layer is open.
|
|
44
273
|
*
|
|
45
|
-
* @since 0.5.0-canary.
|
|
274
|
+
* @since 0.5.0-canary.5
|
|
46
275
|
*/
|
|
47
|
-
export function
|
|
48
|
-
|
|
49
|
-
categories,
|
|
50
|
-
consent,
|
|
51
|
-
customizeLabel = "Customize",
|
|
52
|
-
message = "We use cookies to understand how you use this site.",
|
|
53
|
-
rejectLabel = "Reject",
|
|
54
|
-
saveLabel = "Save preferences",
|
|
55
|
-
...props
|
|
56
|
-
}: ConsentBannerProps): ReactNode {
|
|
57
|
-
// The pending per-category selection — `undefined` while the preferences layer is closed.
|
|
58
|
-
const [pending, setPending] = useState<ConsentDecision | undefined>(undefined);
|
|
276
|
+
export function ConsentBannerSave({ onClick, ...props }: ConsentBannerSaveProps): ReactNode {
|
|
277
|
+
const { consent, pending, setPending } = useConsentBannerContext("ConsentBannerSave");
|
|
59
278
|
|
|
60
|
-
if (
|
|
279
|
+
if (pending === undefined) {
|
|
61
280
|
return null;
|
|
62
281
|
}
|
|
63
282
|
|
|
64
283
|
return (
|
|
65
|
-
<
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
type="checkbox"
|
|
78
|
-
/>
|
|
79
|
-
<span data-slot="consent-category-label">{option.label}</span>
|
|
80
|
-
{option.description === undefined ? null : (
|
|
81
|
-
<span data-slot="consent-category-description">{option.description}</span>
|
|
82
|
-
)}
|
|
83
|
-
</label>
|
|
84
|
-
))}
|
|
85
|
-
</div>
|
|
86
|
-
) : null}
|
|
87
|
-
<div data-slot="consent-actions">
|
|
88
|
-
{pending === undefined ? (
|
|
89
|
-
<>
|
|
90
|
-
<button data-action="accept" data-slot="consent-action" onClick={consent.grantAll} type="button">
|
|
91
|
-
{acceptLabel}
|
|
92
|
-
</button>
|
|
93
|
-
<button data-action="reject" data-slot="consent-action" onClick={consent.denyAll} type="button">
|
|
94
|
-
{rejectLabel}
|
|
95
|
-
</button>
|
|
96
|
-
{categories !== undefined && categories.length > 0 ? (
|
|
97
|
-
<button
|
|
98
|
-
data-action="customize"
|
|
99
|
-
data-slot="consent-action"
|
|
100
|
-
onClick={() => {
|
|
101
|
-
// Starts from the current effective state — all-denied in opt-in regions,
|
|
102
|
-
// so no box arrives pre-ticked (pre-ticked consent is invalid under GDPR).
|
|
103
|
-
setPending(consent.effectiveConsent);
|
|
104
|
-
}}
|
|
105
|
-
type="button"
|
|
106
|
-
>
|
|
107
|
-
{customizeLabel}
|
|
108
|
-
</button>
|
|
109
|
-
) : null}
|
|
110
|
-
</>
|
|
111
|
-
) : (
|
|
112
|
-
<button
|
|
113
|
-
data-action="save"
|
|
114
|
-
data-slot="consent-action"
|
|
115
|
-
onClick={() => {
|
|
116
|
-
consent.save(pending);
|
|
117
|
-
}}
|
|
118
|
-
type="button"
|
|
119
|
-
>
|
|
120
|
-
{saveLabel}
|
|
121
|
-
</button>
|
|
122
|
-
)}
|
|
123
|
-
</div>
|
|
124
|
-
</section>
|
|
284
|
+
<button
|
|
285
|
+
data-action="save"
|
|
286
|
+
data-slot="consent-action"
|
|
287
|
+
{...props}
|
|
288
|
+
// the part owns the decision — composed so a consumer onClick still runs
|
|
289
|
+
onClick={(event) => {
|
|
290
|
+
onClick?.(event);
|
|
291
|
+
consent.save(pending);
|
|
292
|
+
setPending(undefined);
|
|
293
|
+
}}
|
|
294
|
+
type="button"
|
|
295
|
+
/>
|
|
125
296
|
);
|
|
126
297
|
}
|
|
127
298
|
|
package/src/react/index.ts
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
|
-
export type {
|
|
2
|
-
|
|
1
|
+
export type {
|
|
2
|
+
ConsentBannerAcceptProps,
|
|
3
|
+
ConsentBannerActionsProps,
|
|
4
|
+
ConsentBannerCategoryProps,
|
|
5
|
+
ConsentBannerCustomizeProps,
|
|
6
|
+
ConsentBannerDescriptionProps,
|
|
7
|
+
ConsentBannerPreferencesProps,
|
|
8
|
+
ConsentBannerProps,
|
|
9
|
+
ConsentBannerRejectProps,
|
|
10
|
+
ConsentBannerSaveProps,
|
|
11
|
+
ConsentBannerTitleProps,
|
|
12
|
+
ConsentToggleProps,
|
|
13
|
+
} from "#/react/consent-banner";
|
|
14
|
+
export {
|
|
15
|
+
ConsentBanner,
|
|
16
|
+
ConsentBannerAccept,
|
|
17
|
+
ConsentBannerActions,
|
|
18
|
+
ConsentBannerCategory,
|
|
19
|
+
ConsentBannerCustomize,
|
|
20
|
+
ConsentBannerDescription,
|
|
21
|
+
ConsentBannerPreferences,
|
|
22
|
+
ConsentBannerReject,
|
|
23
|
+
ConsentBannerSave,
|
|
24
|
+
ConsentBannerTitle,
|
|
25
|
+
ConsentToggle,
|
|
26
|
+
} from "#/react/consent-banner";
|
|
3
27
|
|
|
4
28
|
export type { UseConsentOptions, UseConsentResult } from "#/react/use-consent";
|
|
5
29
|
export { useConsent } from "#/react/use-consent";
|