@lanfermann/ui 0.5.0 → 0.6.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.
Files changed (165) hide show
  1. package/dist/components/action-bar/action-bar.d.ts +59 -0
  2. package/dist/components/action-bar/action-bar.js +116 -0
  3. package/dist/components/alert/alert.d.ts +1 -1
  4. package/dist/components/alert/alert.js +3 -17
  5. package/dist/components/alert-dialog/alert-dialog.d.ts +1 -1
  6. package/dist/components/app-shell/app-shell.d.ts +33 -0
  7. package/dist/components/app-shell/app-shell.js +93 -0
  8. package/dist/components/attachment/attachment.d.ts +46 -0
  9. package/dist/components/attachment/attachment.js +222 -0
  10. package/dist/components/attachment/kinds.d.ts +7 -0
  11. package/dist/components/attachment/kinds.js +100 -0
  12. package/dist/components/avatar/avatar.d.ts +14 -4
  13. package/dist/components/avatar/avatar.js +57 -13
  14. package/dist/components/avatar-group/avatar-group.d.ts +33 -0
  15. package/dist/components/avatar-group/avatar-group.js +110 -0
  16. package/dist/components/avatar-group/people.d.ts +12 -0
  17. package/dist/components/avatar-group/people.js +24 -0
  18. package/dist/components/badge/badge.d.ts +9 -3
  19. package/dist/components/badge/badge.js +21 -6
  20. package/dist/components/banner/banner.d.ts +39 -0
  21. package/dist/components/banner/banner.js +149 -0
  22. package/dist/components/card/card.d.ts +11 -3
  23. package/dist/components/card/card.js +18 -4
  24. package/dist/components/checkbox-card/checkbox-card.d.ts +21 -0
  25. package/dist/components/checkbox-card/checkbox-card.js +49 -0
  26. package/dist/components/chip/chip-group.d.ts +15 -0
  27. package/dist/components/chip/chip-group.js +44 -0
  28. package/dist/components/chip/chip-variants.d.ts +6 -0
  29. package/dist/components/chip/chip-variants.js +28 -0
  30. package/dist/components/chip/chip.d.ts +39 -0
  31. package/dist/components/chip/chip.js +71 -0
  32. package/dist/components/code-block/code-copy.d.ts +3 -11
  33. package/dist/components/code-block/code-copy.js +15 -62
  34. package/dist/components/code-block/code-keys.js +10 -22
  35. package/dist/components/combobox/combobox.d.ts +50 -2
  36. package/dist/components/combobox/combobox.js +178 -9
  37. package/dist/components/command/command.d.ts +131 -0
  38. package/dist/components/command/command.js +335 -0
  39. package/dist/components/consent/consent-card.js +365 -0
  40. package/dist/components/consent/consent-embed.d.ts +21 -0
  41. package/dist/components/consent/consent-embed.js +111 -0
  42. package/dist/components/consent/consent-labels.d.ts +70 -0
  43. package/dist/components/consent/consent-labels.js +61 -0
  44. package/dist/components/consent/consent-record.d.ts +95 -0
  45. package/dist/components/consent/consent-record.js +112 -0
  46. package/dist/components/consent/consent-store.d.ts +26 -0
  47. package/dist/components/consent/consent-store.js +240 -0
  48. package/dist/components/consent/consent.d.ts +86 -0
  49. package/dist/components/consent/consent.js +306 -0
  50. package/dist/components/container/container.d.ts +1 -1
  51. package/dist/components/context-menu/context-menu.d.ts +23 -0
  52. package/dist/components/context-menu/context-menu.js +50 -0
  53. package/dist/components/conversation/conversation.d.ts +45 -0
  54. package/dist/components/conversation/conversation.js +295 -0
  55. package/dist/components/conversation/stick.js +23 -0
  56. package/dist/components/copy-button/copy-button.d.ts +50 -0
  57. package/dist/components/copy-button/copy-button.js +65 -0
  58. package/dist/components/copy-page/copy-page.js +7 -17
  59. package/dist/components/copy-pill/copy-pill.js +1 -1
  60. package/dist/components/description-list/description-list.d.ts +32 -0
  61. package/dist/components/description-list/description-list.js +51 -0
  62. package/dist/components/empty/empty.d.ts +38 -0
  63. package/dist/components/empty/empty.js +57 -0
  64. package/dist/components/field/field.js +1 -1
  65. package/dist/components/file-upload/file-upload.d.ts +96 -0
  66. package/dist/components/file-upload/file-upload.js +347 -0
  67. package/dist/components/file-upload/files.d.ts +39 -0
  68. package/dist/components/file-upload/files.js +74 -0
  69. package/dist/components/grow/grow.d.ts +15 -3
  70. package/dist/components/grow/grow.js +24 -9
  71. package/dist/components/heading/heading.d.ts +1 -1
  72. package/dist/components/icon/icon.d.ts +1 -1
  73. package/dist/components/input/input.js +1 -1
  74. package/dist/components/message/message.d.ts +73 -0
  75. package/dist/components/message/message.js +196 -0
  76. package/dist/components/otp-field/otp-field.d.ts +37 -0
  77. package/dist/components/otp-field/otp-field.js +126 -0
  78. package/dist/components/pagination/pagination.d.ts +50 -0
  79. package/dist/components/pagination/pagination.js +188 -0
  80. package/dist/components/prompt-input/keys.js +15 -0
  81. package/dist/components/prompt-input/prompt-input.d.ts +89 -0
  82. package/dist/components/prompt-input/prompt-input.js +331 -0
  83. package/dist/components/radio-card/radio-card.d.ts +23 -0
  84. package/dist/components/radio-card/radio-card.js +48 -0
  85. package/dist/components/rolling-number/rolling-number.d.ts +30 -0
  86. package/dist/components/rolling-number/rolling-number.js +149 -0
  87. package/dist/components/seam/seam.d.ts +1 -1
  88. package/dist/components/secret-field/secret-field.d.ts +53 -0
  89. package/dist/components/secret-field/secret-field.js +121 -0
  90. package/dist/components/section/section.d.ts +1 -1
  91. package/dist/components/select/select.d.ts +1 -1
  92. package/dist/components/select/select.js +1 -1
  93. package/dist/components/shortcut/shortcut.d.ts +46 -0
  94. package/dist/components/shortcut/shortcut.js +87 -0
  95. package/dist/components/sidebar/sidebar-context.js +16 -0
  96. package/dist/components/sidebar/sidebar.d.ts +161 -0
  97. package/dist/components/sidebar/sidebar.js +588 -0
  98. package/dist/components/site-header/site-header.js +1 -1
  99. package/dist/components/skeleton/loaded.d.ts +21 -0
  100. package/dist/components/skeleton/loaded.js +57 -0
  101. package/dist/components/skeleton/skeleton.d.ts +43 -0
  102. package/dist/components/skeleton/skeleton.js +48 -0
  103. package/dist/components/sources/source-list.d.ts +17 -0
  104. package/dist/components/sources/source-list.js +26 -0
  105. package/dist/components/sources/sources.d.ts +45 -0
  106. package/dist/components/sources/sources.js +167 -0
  107. package/dist/components/stat/stat.d.ts +57 -0
  108. package/dist/components/stat/stat.js +110 -0
  109. package/dist/components/steps/steps.d.ts +57 -0
  110. package/dist/components/steps/steps.js +131 -0
  111. package/dist/components/table/table.d.ts +18 -9
  112. package/dist/components/table/table.js +21 -13
  113. package/dist/components/text/text.d.ts +1 -1
  114. package/dist/components/theme-switch/theme-switch.js +1 -1
  115. package/dist/components/time-field/time-field.js +1 -1
  116. package/dist/components/timeline/timeline.d.ts +45 -0
  117. package/dist/components/timeline/timeline.js +86 -0
  118. package/dist/components/toast/toast.js +10 -26
  119. package/dist/components/toggle/toggle.d.ts +24 -0
  120. package/dist/components/toggle/toggle.js +53 -0
  121. package/dist/components/toggle-group/toggle-group.d.ts +27 -0
  122. package/dist/components/toggle-group/toggle-group.js +38 -0
  123. package/dist/components/toolbar/toolbar-context.js +12 -0
  124. package/dist/components/toolbar/toolbar.d.ts +45 -0
  125. package/dist/components/toolbar/toolbar.js +92 -0
  126. package/dist/components/trace/trace.d.ts +2 -1
  127. package/dist/components/trace/trace.js +14 -26
  128. package/dist/index.d.ts +46 -3
  129. package/dist/index.js +44 -3
  130. package/dist/internal/arriving-pieces.js +56 -0
  131. package/dist/internal/arriving.js +171 -0
  132. package/dist/internal/avatar-cut.d.ts +15 -0
  133. package/dist/internal/avatar-cut.js +83 -0
  134. package/dist/internal/choice-card.d.ts +15 -0
  135. package/dist/internal/choice-card.js +65 -0
  136. package/dist/internal/clipboard.js +37 -0
  137. package/dist/internal/copy.js +58 -0
  138. package/dist/internal/field.js +24 -4
  139. package/dist/internal/glyphs.js +26 -1
  140. package/dist/internal/live-list.js +41 -0
  141. package/dist/internal/number-places.js +156 -0
  142. package/dist/internal/picker-sheet.js +1 -1
  143. package/dist/internal/presence-dot.js +24 -0
  144. package/dist/internal/presence.js +104 -0
  145. package/dist/internal/rail-mark.d.ts +3 -0
  146. package/dist/internal/rail-mark.js +33 -0
  147. package/dist/internal/status-icon.js +22 -0
  148. package/dist/internal/travelling-mark.js +204 -0
  149. package/dist/lib/cn.js +4 -0
  150. package/dist/lib/shortcut.d.ts +11 -0
  151. package/dist/lib/shortcut.js +165 -0
  152. package/package.json +1 -1
  153. package/src/styles/action-bar.css +28 -0
  154. package/src/styles/assistant.css +83 -0
  155. package/src/styles/avatar.css +24 -0
  156. package/src/styles/consent.css +65 -0
  157. package/src/styles/copy.css +12 -11
  158. package/src/styles/depth.css +46 -2
  159. package/src/styles/marks.css +20 -2
  160. package/src/styles/rail.css +49 -0
  161. package/src/styles/rolling-number.css +94 -0
  162. package/src/styles/steps.css +98 -0
  163. package/src/styles/timeline.css +48 -0
  164. package/src/styles/travel.css +36 -0
  165. package/src/styles.css +9 -0
@@ -0,0 +1,95 @@
1
+ /** Where a service keeps something on the device. */
2
+ export type ConsentStorageKind = "cookie" | "local" | "session";
3
+ /** One thing a service stores in the browser: a cookie, or a key in local or session storage. */
4
+ export type ConsentStorage = {
5
+ /** Its name. A trailing `*` stands for any ending: `_ga_*`. */
6
+ name: string;
7
+ /** Where it is kept. A cookie unless said otherwise. */
8
+ kind?: ConsentStorageKind;
9
+ /** How long it is kept, as people read it: „2 Jahre“, „Sitzung“. */
10
+ duration: string;
11
+ /** What it is for, where the service's purpose does not already say it. */
12
+ purpose?: string;
13
+ };
14
+ /** A service that needs consent, or (under `essential`) one that runs without it. */
15
+ export type ConsentService = {
16
+ /** Unique across all services and categories. It is what the cookie stores. */
17
+ id: string;
18
+ /** The service as people know it: „YouTube“, „Plausible“. */
19
+ name: string;
20
+ /** Who runs it, with the seat: „Google Ireland Limited, Irland“. */
21
+ provider: string;
22
+ /** What it does on this site, concretely: „Zeigt eingebettete Videos.“ */
23
+ purpose: string;
24
+ /**
25
+ * Whether the provider joins what it learns here with data from other sites into profiles.
26
+ * The first layer has to say so (Rn. 119), so it is never left out.
27
+ */
28
+ profiling: boolean;
29
+ /** Where data is processed outside the EEA, as a country: „USA“. Absent when it stays inside. */
30
+ thirdCountry?: string;
31
+ /** What it stores in the browser. Revoking deletes the first-party entries named here. */
32
+ storage?: ConsentStorage[];
33
+ /** The provider's privacy policy. */
34
+ privacyUrl?: string;
35
+ };
36
+ /** A purpose: the services that serve it, switched on and off together or one by one. */
37
+ export type ConsentCategory = {
38
+ /** Unique across all services and categories. */
39
+ id: string;
40
+ /** The purpose as one or two words: „Statistik“, „Externe Medien“. */
41
+ name: string;
42
+ /** One sentence on what it does for the reader or the site. */
43
+ description: string;
44
+ services: ConsentService[];
45
+ };
46
+ /** Everything a site asks consent for. */
47
+ export type ConsentConfig = {
48
+ /**
49
+ * The version of what is shown, as a date („2026-11-18“). Change it when the wording changes in
50
+ * substance: everyone is asked again. Keep the config of each revision (git history does), so a
51
+ * stored answer can be matched to the text it was given to (Rn. 85).
52
+ */
53
+ revision: string;
54
+ /** The purposes that need consent. */
55
+ categories: ConsentCategory[];
56
+ /** Services that run without consent (strictly necessary). The consent cookie is added itself. */
57
+ essential?: ConsentService[];
58
+ /** The name of the consent cookie. „lf-consent“ unless set. */
59
+ cookieName?: string;
60
+ /** How long an answer counts before it is asked for again. 365 days unless set. */
61
+ maxAgeDays?: number;
62
+ /** A domain to share the answer across subdomains („example.com“). The host only, unless set. */
63
+ cookieDomain?: string;
64
+ };
65
+ /** The answer, as it is kept. */
66
+ export type ConsentRecord = {
67
+ /** The `revision` of the config it was given under. */
68
+ revision: string;
69
+ /** When it was given, in milliseconds since the epoch. */
70
+ decidedAt: number;
71
+ /** The ids of the services that were granted. */
72
+ granted: readonly string[];
73
+ };
74
+ /**
75
+ * Reads the cookie's value. Returns the record only if it still counts: written by this format,
76
+ * for what is asked now (the fingerprint), and not older than `maxAgeDays`. Ids of services that
77
+ * are no longer configured are dropped. Pass `now` from the request on a server.
78
+ */
79
+ export declare function parseConsent(raw: string | undefined | null, config: ConsentConfig, now?: number): ConsentRecord | undefined;
80
+ /**
81
+ * Whether an id is allowed: a service that was granted, or a category all of whose services
82
+ * were. Essential services are always allowed; anything unknown never is.
83
+ */
84
+ export declare function consentAllows(record: Pick<ConsentRecord, "granted"> | undefined, id: string, config: ConsentConfig): boolean;
85
+ /** What the first layer has to say about the services as a whole (Rn. 119). */
86
+ export type ConsentSummary = {
87
+ /** The purposes, by name. */
88
+ purposes: string[];
89
+ /** Every provider once, in the order they appear. */
90
+ providers: string[];
91
+ /** Every country outside the EEA once. */
92
+ thirdCountries: string[];
93
+ /** The providers that build profiles across sites. */
94
+ profilers: string[];
95
+ };
@@ -0,0 +1,112 @@
1
+ // src/components/consent/consent-record.ts
2
+ const defaultCookieName = "lf-consent";
3
+ /** FNV-1a over a string, in base 36: short, stable, and enough to tell two configs apart. */
4
+ function hash(text) {
5
+ let value = 2166136261;
6
+ for (let index = 0; index < text.length; index++) {
7
+ value ^= text.charCodeAt(index);
8
+ value = Math.imul(value, 16777619);
9
+ }
10
+ return (value >>> 0).toString(36);
11
+ }
12
+ /**
13
+ * What was asked, in a few characters: the revision, and every service with its purpose, its
14
+ * provider and where its data goes. A new service, a new recipient or a new country asks again
15
+ * (Rn. 63); a reworded description does not, until the revision changes.
16
+ */
17
+ function consentFingerprint(config) {
18
+ const services = config.categories.flatMap((category) => category.services.map((service) => `${category.id}/${service.id}/${service.provider}/${service.thirdCountry ?? ""}`)).sort();
19
+ return hash([config.revision, ...services].join("|"));
20
+ }
21
+ /** The record as the cookie's value. */
22
+ function encodeConsent(record, config) {
23
+ return encodeURIComponent(JSON.stringify({
24
+ v: 1,
25
+ r: record.revision,
26
+ f: consentFingerprint(config),
27
+ t: Math.round(record.decidedAt / 1e3),
28
+ s: record.granted
29
+ }));
30
+ }
31
+ /** Every service that needs consent, by id. */
32
+ function consentServices(config) {
33
+ return new Map(config.categories.flatMap((category) => category.services.map((service) => [service.id, service])));
34
+ }
35
+ /**
36
+ * Reads the cookie's value. Returns the record only if it still counts: written by this format,
37
+ * for what is asked now (the fingerprint), and not older than `maxAgeDays`. Ids of services that
38
+ * are no longer configured are dropped. Pass `now` from the request on a server.
39
+ */
40
+ function parseConsent(raw, config, now = Date.now()) {
41
+ if (!raw) return void 0;
42
+ let data;
43
+ for (let text = raw, layer = 0; layer < 2 && data === void 0; layer++) try {
44
+ text = decodeURIComponent(text);
45
+ data = JSON.parse(text);
46
+ } catch {}
47
+ if (typeof data !== "object" || data === null) return void 0;
48
+ const { v, r, f, t, s } = data;
49
+ if (v !== 1 || typeof r !== "string" || typeof f !== "string") return void 0;
50
+ if (typeof t !== "number" || !Number.isFinite(t)) return void 0;
51
+ if (!Array.isArray(s) || !s.every((id) => typeof id === "string")) return void 0;
52
+ if (f !== consentFingerprint(config)) return void 0;
53
+ const decidedAt = t * 1e3;
54
+ const maxAge = (config.maxAgeDays ?? 365) * 864e5;
55
+ if (now - decidedAt > maxAge || decidedAt - now > 864e5) return void 0;
56
+ const known = consentServices(config);
57
+ return {
58
+ revision: r,
59
+ decidedAt,
60
+ granted: s.filter((id) => known.has(id))
61
+ };
62
+ }
63
+ /**
64
+ * Whether an id is allowed: a service that was granted, or a category all of whose services
65
+ * were. Essential services are always allowed; anything unknown never is.
66
+ */
67
+ function consentAllows(record, id, config) {
68
+ if (config.essential?.some((service) => service.id === id)) return true;
69
+ const granted = new Set(record?.granted);
70
+ const category = config.categories.find((entry) => entry.id === id);
71
+ if (category) return category.services.length > 0 && category.services.every((service) => granted.has(service.id));
72
+ return granted.has(id) && consentServices(config).has(id);
73
+ }
74
+ /** Whether a stored name is the one a `ConsentStorage` names (a trailing `*` is a prefix). */
75
+ function storageMatches(pattern, name) {
76
+ return pattern.endsWith("*") ? name.startsWith(pattern.slice(0, -1)) : name === pattern;
77
+ }
78
+ /** The provider's name without its seat: „Google Ireland Limited, Irland“ → „Google Ireland Limited“. */
79
+ function providerName(provider) {
80
+ return provider.split(",")[0]?.trim() ?? provider;
81
+ }
82
+ function consentSummary(config) {
83
+ const unique = (values) => [...new Set(values)];
84
+ const services = config.categories.flatMap((category) => category.services);
85
+ return {
86
+ purposes: config.categories.filter((category) => category.services.length).map((c) => c.name),
87
+ providers: unique(services.map((service) => providerName(service.provider))),
88
+ thirdCountries: unique(services.flatMap((service) => service.thirdCountry ?? [])),
89
+ profilers: unique(services.filter((service) => service.profiling).map((s) => providerName(s.provider)))
90
+ };
91
+ }
92
+ /**
93
+ * What is wrong with a config, as sentences for the console: ids that repeat (`allows` takes a
94
+ * service or a category id, so they must not collide), and categories without services.
95
+ */
96
+ function consentConfigProblems(config) {
97
+ const problems = [];
98
+ const seen = /* @__PURE__ */ new Set();
99
+ const ids = [
100
+ ...config.categories.map((category) => category.id),
101
+ ...config.categories.flatMap((category) => category.services.map((service) => service.id)),
102
+ ...(config.essential ?? []).map((service) => service.id)
103
+ ];
104
+ for (const id of ids) {
105
+ if (seen.has(id)) problems.push(`The consent id "${id}" is used more than once.`);
106
+ seen.add(id);
107
+ }
108
+ for (const category of config.categories) if (!category.services.length) problems.push(`The consent category "${category.id}" has no services.`);
109
+ if (!config.revision) problems.push("The consent config has no revision.");
110
+ return problems;
111
+ }
112
+ export { consentAllows, consentConfigProblems, consentFingerprint, consentServices, consentSummary, defaultCookieName, encodeConsent, parseConsent, storageMatches };
@@ -0,0 +1,26 @@
1
+ /** Where the consent cookie lives. The page's cookies unless a test or a preview passes its own. */
2
+ export type ConsentJar = {
3
+ /** All cookies as `document.cookie` reads them: `a=1; b=2`. */
4
+ read(): string;
5
+ /** One cookie as `document.cookie` takes it: `a=1; Path=/; Max-Age=60`. */
6
+ write(cookie: string): void;
7
+ };
8
+ /**
9
+ * A jar that keeps cookies in memory, for tests and previews. It honours `Max-Age=0` and ignores
10
+ * paths and domains. `blocked` makes it refuse every write, as a browser with cookies off does.
11
+ */
12
+ export declare function createMemoryJar(initial?: Record<string, string>, { blocked }?: {
13
+ blocked?: boolean | undefined;
14
+ }): ConsentJar & {
15
+ writes: string[];
16
+ };
17
+ export type ConsentStatus = "unknown" | "undecided" | "decided";
18
+ /** What changed with a decision, for `onChange`. */
19
+ export type ConsentChange = {
20
+ /** Services granted now that were not before. */
21
+ granted: string[];
22
+ /** Services that were granted and are not anymore. */
23
+ revoked: string[];
24
+ /** The first read of a stored answer when the page loads, not a decision. */
25
+ initial: boolean;
26
+ };
@@ -0,0 +1,240 @@
1
+ import { consentServices, encodeConsent, parseConsent, storageMatches } from "./consent-record.js";
2
+ // src/components/consent/consent-store.ts
3
+ const documentJar = {
4
+ read: () => document.cookie,
5
+ write: (cookie) => {
6
+ document.cookie = cookie;
7
+ }
8
+ };
9
+ /**
10
+ * A jar that keeps cookies in memory, for tests and previews. It honours `Max-Age=0` and ignores
11
+ * paths and domains. `blocked` makes it refuse every write, as a browser with cookies off does.
12
+ */
13
+ function createMemoryJar(initial = {}, { blocked = false } = {}) {
14
+ const cookies = new Map(Object.entries(initial));
15
+ const writes = [];
16
+ return {
17
+ writes,
18
+ read: () => [...cookies].map(([name, value]) => `${name}=${value}`).join("; "),
19
+ write(cookie) {
20
+ writes.push(cookie);
21
+ if (blocked) return;
22
+ const [pair = "", ...attributes] = cookie.split(";").map((part) => part.trim());
23
+ const split = pair.indexOf("=");
24
+ const name = pair.slice(0, split);
25
+ const value = pair.slice(split + 1);
26
+ if (attributes.some((attribute) => /^max-age=(0|-\d+)$/i.test(attribute))) cookies.delete(name);
27
+ else cookies.set(name, value);
28
+ }
29
+ };
30
+ }
31
+ /** The value of one cookie in a `document.cookie` string. */
32
+ function readCookie(all, name) {
33
+ for (const part of all.split(";")) {
34
+ const pair = part.trim();
35
+ if (pair.startsWith(`${name}=`)) return pair.slice(name.length + 1);
36
+ }
37
+ }
38
+ function cookieNames(all) {
39
+ return all.split(";").map((part) => part.trim().split("=")[0] ?? "").filter(Boolean);
40
+ }
41
+ const unknown = {
42
+ status: "unknown",
43
+ session: /* @__PURE__ */ new Set()
44
+ };
45
+ /** The host and each domain above it, for deleting a cookie whose domain is not known. */
46
+ function domainVariants(hostname) {
47
+ if (/^[\d.]+$/.test(hostname) || hostname.includes(":") || !hostname.includes(".")) return [void 0];
48
+ const labels = hostname.split(".");
49
+ return [void 0, ...labels.slice(0, -1).map((_, index) => labels.slice(index).join("."))];
50
+ }
51
+ function createConsentStore(config, options = {}) {
52
+ const jar = options.jar ?? documentJar;
53
+ const now = options.now ?? Date.now;
54
+ const location = () => options.location ?? window.location;
55
+ const name = config.cookieName ?? "lf-consent";
56
+ const maxAgeSeconds = (config.maxAgeDays ?? 365) * 86400;
57
+ const services = consentServices(config);
58
+ let onChange = options.onChange;
59
+ let persist = options.persist;
60
+ const listeners = /* @__PURE__ */ new Set();
61
+ /** The answer, when the cookie could not be written. */
62
+ let memory;
63
+ let session = /* @__PURE__ */ new Set();
64
+ /** Services whose script a `ConsentGate` has rendered in this document. */
65
+ const rendered = /* @__PURE__ */ new Set();
66
+ /** Counts the changes the cookie does not show: the answer in memory, the session grants. */
67
+ let version = 0;
68
+ let cache;
69
+ /** The snapshot the listeners last heard of. */
70
+ let announced;
71
+ function read() {
72
+ try {
73
+ return readCookie(jar.read(), name);
74
+ } catch {
75
+ return;
76
+ }
77
+ }
78
+ function getSnapshot() {
79
+ const raw = read();
80
+ if (cache && cache.raw === raw && cache.version === version) return cache.snapshot;
81
+ const record = memory ?? parseConsent(raw, config, now());
82
+ const snapshot = {
83
+ status: record ? "decided" : "undecided",
84
+ record,
85
+ session
86
+ };
87
+ cache = {
88
+ raw,
89
+ version,
90
+ snapshot
91
+ };
92
+ return snapshot;
93
+ }
94
+ function notify() {
95
+ announced = getSnapshot();
96
+ for (const listener of listeners) listener();
97
+ }
98
+ function secure(cookieName) {
99
+ return location().protocol === "https:" || cookieName.startsWith("__Secure-") || cookieName.startsWith("__Host-") ? "; Secure" : "";
100
+ }
101
+ function write(value, maxAge) {
102
+ const domain = config.cookieDomain ? `; Domain=${config.cookieDomain}` : "";
103
+ try {
104
+ jar.write(`${name}=${value}; Path=/; Max-Age=${maxAge}; SameSite=Lax${domain}${secure(name)}`);
105
+ } catch {}
106
+ }
107
+ /** Deletes what a service stored on this site. Its own cookies on other domains are out of reach. */
108
+ function forget(serviceId) {
109
+ const service = services.get(serviceId);
110
+ for (const entry of service?.storage ?? []) {
111
+ const kind = entry.kind ?? "cookie";
112
+ if (kind === "cookie") {
113
+ let all = "";
114
+ try {
115
+ all = jar.read();
116
+ } catch {
117
+ continue;
118
+ }
119
+ const names = entry.name.endsWith("*") ? cookieNames(all).filter((cookie) => storageMatches(entry.name, cookie)) : [entry.name];
120
+ for (const cookie of names) {
121
+ const variants = cookie.startsWith("__Host-") ? [void 0] : domainVariants(location().hostname);
122
+ for (const domain of variants) try {
123
+ jar.write(`${cookie}=; Path=/; Max-Age=0${domain ? `; Domain=${domain}` : ""}${secure(cookie)}`);
124
+ } catch {}
125
+ }
126
+ } else try {
127
+ const storage = kind === "local" ? window.localStorage : window.sessionStorage;
128
+ const keys = Array.from({ length: storage.length }, (_, index) => storage.key(index));
129
+ for (const key of keys) if (key !== null && storageMatches(entry.name, key)) storage.removeItem(key);
130
+ } catch {}
131
+ }
132
+ }
133
+ function diff(before, after) {
134
+ const was = new Set(before?.record?.granted);
135
+ const is = new Set(after.record?.granted);
136
+ return {
137
+ granted: [...is].filter((id) => !was.has(id)),
138
+ revoked: [...was].filter((id) => !is.has(id)),
139
+ initial: false
140
+ };
141
+ }
142
+ /** Reads the cookie again: another tab may have answered, or the page came back from the cache. */
143
+ function refresh() {
144
+ const before = announced;
145
+ const after = getSnapshot();
146
+ if (after === before) return;
147
+ const change = diff(before, after);
148
+ const changed = change.granted.length > 0 || change.revoked.length > 0;
149
+ notify();
150
+ if (after.record && changed) onChange?.(after.record, change);
151
+ if (change.revoked.some((id) => rendered.has(id))) location().reload();
152
+ }
153
+ const onVisible = () => {
154
+ if (document.visibilityState === "visible") refresh();
155
+ };
156
+ return {
157
+ subscribe: (listener) => {
158
+ listeners.add(listener);
159
+ if (listeners.size === 1 && typeof window !== "undefined") {
160
+ document.addEventListener("visibilitychange", onVisible);
161
+ window.addEventListener("pageshow", refresh);
162
+ }
163
+ return () => {
164
+ listeners.delete(listener);
165
+ if (listeners.size === 0 && typeof window !== "undefined") {
166
+ document.removeEventListener("visibilitychange", onVisible);
167
+ window.removeEventListener("pageshow", refresh);
168
+ }
169
+ };
170
+ },
171
+ getSnapshot,
172
+ getServerSnapshot: () => unknown,
173
+ /** Tells `onChange` about a stored answer once the page has loaded. */
174
+ announce() {
175
+ const snapshot = getSnapshot();
176
+ announced = snapshot;
177
+ if (snapshot.record) onChange?.(snapshot.record, {
178
+ granted: [...snapshot.record.granted],
179
+ revoked: [],
180
+ initial: true
181
+ });
182
+ },
183
+ setOnChange(handler) {
184
+ onChange = handler;
185
+ },
186
+ setPersist(handler) {
187
+ persist = handler;
188
+ },
189
+ /** Stores an answer: exactly these services are granted, every other one is not. */
190
+ decide(grantedIds) {
191
+ const before = getSnapshot();
192
+ const granted = [...new Set(grantedIds)].filter((id) => services.has(id));
193
+ const record = {
194
+ revision: config.revision,
195
+ decidedAt: now(),
196
+ granted
197
+ };
198
+ const value = encodeConsent(record, config);
199
+ write(value, maxAgeSeconds);
200
+ memory = read() === value ? void 0 : record;
201
+ version++;
202
+ if (persist) {
203
+ const store = persist;
204
+ Promise.resolve().then(() => store(value, maxAgeSeconds)).catch(() => {});
205
+ }
206
+ const dropped = [...session].filter((id) => !granted.includes(id));
207
+ if (dropped.length) session = new Set([...session].filter((id) => granted.includes(id)));
208
+ const change = diff(before, getSnapshot());
209
+ for (const id of /* @__PURE__ */ new Set([...change.revoked, ...dropped])) forget(id);
210
+ const reload = [...change.revoked, ...dropped].some((id) => rendered.has(id));
211
+ notify();
212
+ onChange?.(record, change);
213
+ return {
214
+ record,
215
+ change,
216
+ reload
217
+ };
218
+ },
219
+ /** Loads a service once, for this document only. */
220
+ grantOnce(serviceId) {
221
+ if (!services.has(serviceId) || session.has(serviceId)) return;
222
+ session = /* @__PURE__ */ new Set([...session, serviceId]);
223
+ version++;
224
+ notify();
225
+ },
226
+ /** Notes that a service's content ran in this document, so revoking it reloads the page. */
227
+ markRendered(serviceIds) {
228
+ for (const id of serviceIds) rendered.add(id);
229
+ },
230
+ /** Removes the answer (for tests and previews): the reader is asked again. */
231
+ reset() {
232
+ memory = void 0;
233
+ session = /* @__PURE__ */ new Set();
234
+ version++;
235
+ write("", 0);
236
+ notify();
237
+ }
238
+ };
239
+ }
240
+ export { createConsentStore, createMemoryJar, documentJar, readCookie };
@@ -0,0 +1,86 @@
1
+ import { ConsentLabels } from "./consent-labels.js";
2
+ import { ConsentConfig, ConsentRecord } from "./consent-record.js";
3
+ import { ConsentChange, ConsentJar, ConsentStatus } from "./consent-store.js";
4
+ import { ReactNode } from "react";
5
+ import { Dialog } from "@base-ui/react/dialog";
6
+ export type ConsentProviderProps = {
7
+ /** The services the site asks about. See `ConsentConfig`. */
8
+ config: ConsentConfig;
9
+ /** The site's privacy policy. The banner links to it, and must never cover the way there. */
10
+ privacyHref: string;
11
+ /** The site's imprint. */
12
+ imprintHref: string;
13
+ /**
14
+ * What the services do, in the site's own words: concrete purposes, not „ein besseres
15
+ * Erlebnis“ (Rn. 53). `labels.intro` unless given.
16
+ */
17
+ intro?: ReactNode;
18
+ /** Any of the strings, in place of the German defaults (`consentLabels`). */
19
+ labels?: Partial<ConsentLabels>;
20
+ /**
21
+ * Called when the answer changes, and once on load with a stored answer (`initial`): the place
22
+ * to update Google Consent Mode or log the decision. A function cannot cross from a Server
23
+ * Component; pass it from a small client component that renders the provider.
24
+ */
25
+ onChange?: (record: ConsentRecord, change: ConsentChange) => void;
26
+ /**
27
+ * Also stores the cookie from the server, typically through a Server Action that sets the same
28
+ * cookie. Safari keeps cookies written by script for at most seven days.
29
+ */
30
+ persist?: (value: string, maxAgeSeconds: number) => void | Promise<void>;
31
+ /** Where the answer is kept: the page's cookies unless a test or a preview passes its own. */
32
+ jar?: ConsentJar;
33
+ children?: ReactNode;
34
+ };
35
+ /**
36
+ * Asks for consent before any service that stores or reads data in the browser is loaded, and
37
+ * keeps the answer. Put it once around the app, in the root layout; it renders the banner
38
+ * itself. On the first visit the banner stands at the foot of the window without covering the
39
+ * page: „Alle ablehnen“ and „Alle akzeptieren“ side by side, equal in every way, and the
40
+ * settings one step away in the same card. Gate what needs consent with `ConsentGate` or
41
+ * `ConsentEmbed`, and put a `ConsentSettingsTrigger` in the footer of every page so the answer
42
+ * can be changed as easily as it was given.
43
+ *
44
+ * It frames the legal requirements (DSK, OH Digitale Dienste; § 25 TDDDG); the site still states
45
+ * its services truthfully in `config` and in its privacy policy.
46
+ */
47
+ export declare function ConsentProvider({ config, privacyHref, imprintHref, intro, labels, onChange, persist, jar, children }: ConsentProviderProps): import("react").JSX.Element;
48
+ export type UseConsentResult = {
49
+ /** `unknown` until the browser has read the cookie, then `undecided` or `decided`. */
50
+ status: ConsentStatus;
51
+ /** The stored answer, once there is one. */
52
+ record?: ConsentRecord;
53
+ /** Whether a service or a category (all of its services) may run. */
54
+ allows: (id: string) => boolean;
55
+ acceptAll: () => void;
56
+ rejectAll: () => void;
57
+ /** Stores exactly these services as granted. */
58
+ save: (granted: string[]) => void;
59
+ /** Opens the banner on its settings. */
60
+ openSettings: () => void;
61
+ };
62
+ /** The answer, and the ways to change it, for code that loads a script itself. */
63
+ export declare function useConsent(): UseConsentResult;
64
+ export type ConsentGateProps = {
65
+ /** The service whose consent the content needs. */
66
+ service?: string;
67
+ /** Or a category, when the content needs all of its services. */
68
+ category?: string;
69
+ /** What stands in its place without consent. Nothing unless given. */
70
+ fallback?: ReactNode;
71
+ children?: ReactNode;
72
+ };
73
+ /**
74
+ * Renders what needs consent (a script, a pixel, an embed) only once it is given, and never on
75
+ * the server. Revoking a service whose content ran here loads the page again, since a script
76
+ * cannot be taken back.
77
+ */
78
+ export declare function ConsentGate({ service, category, fallback, children }: ConsentGateProps): ReactNode;
79
+ export type ConsentSettingsTriggerProps = Dialog.Trigger.Props;
80
+ /**
81
+ * Opens the banner on its settings, so an answer can be changed as easily as it was given
82
+ * (Art. 7 (3) GDPR; Rn. 62). Put it among the legal links in the footer of every page. It is
83
+ * dressed like those links; in a `SiteFooter`, pass `siteFooterLinkClass`, whose underline waits
84
+ * for the pointer.
85
+ */
86
+ export declare function ConsentSettingsTrigger({ id, className, children, ...props }: ConsentSettingsTriggerProps): import("react").JSX.Element;